<?php

require_once __DIR__ . '/../library/auth.php'; 
$school_id = $_SESSION['school_id'];
allow_roles(['admin', 'accountant']);


// ======================================================
// FETCH VEHICLES
// ======================================================

$stmt = $pdo->prepare("
    SELECT *
    FROM vehicles
    WHERE school_id = ?
    ORDER BY id DESC
");

$stmt->execute([$school_id]);

$vehicles = $stmt->fetchAll(PDO::FETCH_ASSOC);

$total_vehicles = count($vehicles);

$page_title = 'Vehicle Management';

require_once __DIR__ . '/../include/header.php';
require_once __DIR__ . '/../include/sidebar.php';


?>

<div class="content-wrapper">

    <!-- ====================================================== -->
    <!-- PAGE HEADER -->
    <!-- ====================================================== -->

    <section class="content-header">

        <div class="container-fluid">

            <div class="row mb-2">

                <div class="col-sm-6">

                    <h1>

                        <i class="fas fa-truck text-info"></i>
                        Manage Vehicles

                    </h1>

                </div>

            </div>

        </div>

    </section>



    <!-- ====================================================== -->
    <!-- MAIN CONTENT -->
    <!-- ====================================================== -->

    <section class="content">

        <div class="container-fluid">

            <!-- SUCCESS MESSAGE -->

            <?php if (isset($_GET['msg'])): ?>

                <div class="alert alert-success alert-dismissible fade show">

                    <i class="fas fa-check-circle"></i>

                    <?= htmlspecialchars($_GET['msg']); ?>

                    <button
                        type="button"
                        class="close"
                        data-dismiss="alert"
                    >

                        &times;

                    </button>

                </div>

            <?php endif; ?>



            <!-- ====================================================== -->
            <!-- ADD VEHICLE -->
            <!-- ====================================================== -->

            <div class="row">

                <div class="col-12">

                    <div class="card card-primary">

                        <div class="card-header">

                            <h3 class="card-title">

                                <i class="fas fa-plus-circle"></i>
                                Add New Vehicle

                            </h3>

                        </div>



                        <form
                            method="post"
                            action="process.php?action=add_vehicle"
                            class="p-4"
                            autocomplete="off"
                        >

                            <div class="row">

                                <!-- VEHICLE NUMBER -->

                                <div class="col-lg-5 col-md-6">

                                    <div class="form-group">

                                        <label>

                                            <i class="fas fa-hashtag text-warning"></i>
                                            Vehicle Number

                                        </label>

                                        <input
                                            type="text"
                                            name="vehicle_no"
                                            class="form-control"
                                            placeholder="e.g. MP-07-AB-1234"
                                            required
                                        >

                                    </div>

                                </div>



                                <!-- VEHICLE MODEL -->

                                <div class="col-lg-5 col-md-6">

                                    <div class="form-group">

                                        <label>

                                            <i class="fas fa-car text-info"></i>
                                            Vehicle Model

                                        </label>

                                        <input
                                            type="text"
                                            name="vehicle_model"
                                            class="form-control"
                                            placeholder="e.g. Tata Marcopolo / Ashok Leyland"
                                            required
                                        >

                                    </div>

                                </div>



                                <!-- SUBMIT BUTTON -->

                                <div class="col-lg-2 col-md-12">

                                    <div class="form-group pt-3 pt-lg-0">

                                        <button
                                            type="submit"
                                            class="btn btn-primary btn-lg btn-block h-100 d-flex align-items-center justify-content-center"
                                        >

                                            <i class="fas fa-save mr-2"></i>
                                            Save Vehicle

                                        </button>

                                    </div>

                                </div>

                            </div>

                        </form>

                    </div>

                </div>

            </div>



            <!-- ====================================================== -->
            <!-- VEHICLES LIST -->
            <!-- ====================================================== -->

            <div class="row">

                <div class="col-12">

                    <div class="card card-info">

                        <div class="card-header">

                            <h3 class="card-title">

                                <i class="fas fa-list"></i>
                                Existing Vehicles

                                <span class="badge badge-light">

                                    <?= $total_vehicles ?>

                                </span>

                            </h3>

                        </div>



                        <?php if (empty($vehicles)): ?>

                            <div class="card-body text-center p-5">

                                <i class="fas fa-truck fa-4x text-muted mb-3"></i>

                                <h4 class="text-muted">

                                    No vehicles added yet

                                </h4>

                                <p class="text-muted">

                                    Add your first vehicle from above form

                                </p>

                            </div>

                        <?php else: ?>

                            <div class="card-body">

                                <div class="table-responsive">

                                    <table class="table table-hover">

                                        <thead class="thead-dark">

                                            <tr>

                                                <th>#</th>

                                                <th>Vehicle Number</th>

                                                <th>Model</th>

                                                <th>Status</th>

                                                <th>Added</th>

                                                <th>Actions</th>

                                            </tr>

                                        </thead>



                                        <tbody>

                                            <?php foreach ($vehicles as $index => $v): ?>

                                                <tr>

                                                    <th scope="row">

                                                        <?= $index + 1 ?>

                                                    </th>


                                                    <td>

                                                        <strong class="text-danger">

                                                            <?= htmlspecialchars($v['vehicle_no']) ?>

                                                        </strong>

                                                    </td>


                                                    <td>

                                                        <?= htmlspecialchars($v['vehicle_model']) ?>

                                                    </td>


                                                    <td>

                                                        <span class="badge badge-<?= ($v['status'] ?? 'active') == 'active' ? 'success' : 'secondary' ?>">

                                                            <?= ucfirst($v['status'] ?? 'active') ?>

                                                        </span>

                                                    </td>


                                                    <td>

                                                        <?= date('d-M-Y', strtotime($v['created_at'] ?? 'now')) ?>

                                                    </td>


                                                    <td>

                                                        <div class="btn-group btn-group-sm">

                                                            <a
                                                                href="edit_vehicle.php?id=<?= $v['id'] ?>"
                                                                class="btn btn-warning"
                                                                title="Edit"
                                                            >

                                                                <i class="fas fa-edit"></i>

                                                            </a>


                                                            <a
                                                                href="process.php?action=delete&table=vehicles&id=<?= $v['id'] ?>"
                                                                class="btn btn-danger"
                                                                onclick="return confirm('Delete <?= htmlspecialchars($v['vehicle_no']) ?>?')"
                                                                title="Delete"
                                                            >

                                                                <i class="fas fa-trash"></i>

                                                            </a>

                                                        </div>

                                                    </td>

                                                </tr>

                                            <?php endforeach; ?>

                                        </tbody>

                                    </table>

                                </div>

                            </div>

                        <?php endif; ?>

                    </div>

                </div>

            </div>

        </div>

    </section>

</div>

<?php require_once __DIR__ . '/../include/footer.php'; ?>