<?php
// Fetch data for mechanic dashboard

// Active vehicles with no service info
$noServiceInfo = mysqli_query($db2, "
    SELECT f.fleet_id, f.lic_num, c.name as vehicle_name 
    FROM fleet f 
    LEFT JOIN jostj_vikrentcar_cars c ON c.id = f.veh_id
    WHERE f.active = 1 
    AND (f.fleet_last_service IS NULL OR f.fleet_last_service = '' 
         OR f.fleet_next_service IS NULL OR f.fleet_next_service = '')
");

// Vehicles due for service (within the next 30 days)
$dueService = mysqli_query($db2, "
    SELECT f.fleet_id, f.lic_num, c.name as vehicle_name, f.fleet_next_service 
    FROM fleet f 
    LEFT JOIN jostj_vikrentcar_cars c ON c.id = f.veh_id
    WHERE f.active = 1 
    AND f.fleet_next_service BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 30 DAY)
");

// Vehicles past service date
$pastService = mysqli_query($db2, "
    SELECT f.fleet_id, f.lic_num, c.name as vehicle_name, f.fleet_next_service 
    FROM fleet f 
    LEFT JOIN jostj_vikrentcar_cars c ON c.id = f.veh_id
    WHERE f.active = 1 
    AND f.fleet_next_service < NOW()
");
?>

<!-- Section Header -->
<div class="mb-4">
    <h2 class="text-center">Mechanics Dashboard - Service Status</h2>
    <hr>
</div>

<!-- Tab Navigation -->
<ul class="nav nav-tabs mb-3" id="mechTabs" role="tablist">
    <li class="nav-item">
        <a class="nav-link active" id="noservice-tab" data-toggle="tab" href="#noservice" role="tab">No Service Info</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" id="dueservice-tab" data-toggle="tab" href="#dueservice" role="tab">Due for Service</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" id="pastservice-tab" data-toggle="tab" href="#pastservice" role="tab">Past Service Date</a>
    </li>
</ul>

<!-- Tab Content -->
<div class="tab-content" id="mechTabContent">
    <!-- No Service Info Tab -->
    <div class="tab-pane fade show active p-3" id="noservice" role="tabpanel">
        <h3>Vehicles Without Service Information</h3>
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>License Plate</th>
                    <th>Vehicle Name</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php while ($row = mysqli_fetch_assoc($noServiceInfo)) { ?>
                <tr>
                    <td><?= $row['lic_num'] ?></td>
                    <td><?= $row['vehicle_name'] ?></td>
                    <td><a href="vehicle.php?id=<?= $row['fleet_id'] ?>" class="btn btn-sm btn-info"><i class="fas fa-eye"></i></a></td>
                </tr>
                <?php } ?>
            </tbody>
        </table>
    </div>

    <!-- Due for Service Tab -->
    <div class="tab-pane fade p-3" id="dueservice" role="tabpanel">
        <h3>Vehicles Due for Service (Next 30 Days)</h3>
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>License Plate</th>
                    <th>Vehicle Name</th>
                    <th>Next Service Date</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php while ($row = mysqli_fetch_assoc($dueService)) { ?>
                <tr>
                    <td><?= $row['lic_num'] ?></td>
                    <td><?= $row['vehicle_name'] ?></td>
                    <td class="text-warning"><?= date("d-m-Y", strtotime($row['fleet_next_service'])) ?></td>
                    <td><a href="vehicle.php?id=<?= $row['fleet_id'] ?>" class="btn btn-sm btn-info"><i class="fas fa-eye"></i></a></td>
                </tr>
                <?php } ?>
            </tbody>
        </table>
    </div>

    <!-- Past Service Date Tab -->
    <div class="tab-pane fade p-3" id="pastservice" role="tabpanel">
        <h3>Vehicles Past Service Date</h3>
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>License Plate</th>
                    <th>Vehicle Name</th>
                    <th>Last Service Date</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php while ($row = mysqli_fetch_assoc($pastService)) { ?>
                <tr>
                    <td><?= $row['lic_num'] ?></td>
                    <td><?= $row['vehicle_name'] ?></td>
                    <td class="text-danger"><?= date("d-m-Y", strtotime($row['fleet_next_service'])) ?></td>
                    <td><a href="vehicle.php?id=<?= $row['fleet_id'] ?>" class="btn btn-sm btn-info"><i class="fas fa-eye"></i></a></td>
                </tr>
                <?php } ?>
            </tbody>
        </table>
    </div>
</div>