<?php

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




// ======================================================
// FETCH DRIVERS
// ======================================================

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

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

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

$total_drivers = count($drivers);
$page_title = 'Manage Drivers';

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-id-card text-primary"></i>
                        Manage Drivers

                    </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 DRIVER -->
            <!-- ====================================================== -->

            <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 Driver

                            </h3>

                        </div>



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

                            <div class="row">

                                <!-- DRIVER NAME -->

                                <div class="col-lg-4 col-md-6 col-sm-12">

                                    <div class="form-group">

                                        <label>

                                            <i class="fas fa-user text-primary"></i>
                                            Driver Name

                                        </label>

                                        <input
                                            type="text"
                                            name="name"
                                            class="form-control"
                                            placeholder="Enter full name"
                                            required
                                        >

                                    </div>

                                </div>



                                <!-- LICENSE -->

                                <div class="col-lg-4 col-md-6 col-sm-12">

                                    <div class="form-group">

                                        <label>

                                            <i class="fas fa-id-card text-warning"></i>
                                            License No

                                        </label>

                                        <input
                                            type="text"
                                            name="license_no"
                                            class="form-control"
                                            placeholder="DL-12-XXXX-XXXX"
                                            required
                                        >

                                    </div>

                                </div>



                                <!-- PHONE -->

                                <div class="col-lg-4 col-md-12 col-sm-12">

                                    <div class="form-group">

                                        <label>

                                            <i class="fas fa-phone text-success"></i>
                                            Phone

                                        </label>

                                        <input
                                            type="tel"
                                            name="phone"
                                            class="form-control"
                                            placeholder="98XXXXXXXX"
                                            required
                                        >

                                    </div>

                                </div>

                            </div>



                            <div class="text-right">

                                <button
                                    type="submit"
                                    class="btn btn-primary btn-lg"
                                >

                                    <i class="fas fa-save"></i>
                                    Add Driver

                                </button>

                            </div>

                        </form>

                    </div>

                </div>

            </div>



            <!-- ====================================================== -->
            <!-- DRIVERS 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>
                                Drivers List

                                <span class="badge badge-light">

                                    <?= $total_drivers ?>

                                </span>

                            </h3>

                        </div>



                        <div class="card-body">

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

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

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

                                    <h4 class="text-muted">

                                        No drivers added yet

                                    </h4>

                                </div>

                            <?php else: ?>

                                <div class="table-responsive">

                                    <table class="table table-hover">

                                        <thead class="thead-dark">

                                            <tr>

                                                <th>#</th>

                                                <th>Name</th>

                                                <th>License No</th>

                                                <th>Phone</th>

                                                <th>Added</th>

                                                <th>Actions</th>

                                            </tr>

                                        </thead>



                                        <tbody>

                                            <?php foreach ($drivers as $index => $d): ?>

                                                <tr>

                                                    <th scope="row">

                                                        <?= $index + 1 ?>

                                                    </th>


                                                    <td>

                                                        <strong>

                                                            <?= htmlspecialchars($d['name']) ?>

                                                        </strong>

                                                    </td>


                                                    <td>

                                                        <span class="badge badge-warning">

                                                            <?= htmlspecialchars($d['license_no']) ?>

                                                        </span>

                                                    </td>


                                                    <td>

                                                        <a href="tel:<?= htmlspecialchars($d['phone']) ?>">

                                                            <?= htmlspecialchars($d['phone']) ?>

                                                        </a>

                                                    </td>


                                                    <td>

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

                                                    </td>


                                                    <td>

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

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

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

                                                            </a>


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

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

                                                            </a>

                                                        </div>

                                                    </td>

                                                </tr>

                                            <?php endforeach; ?>

                                        </tbody>

                                    </table>

                                </div>

                            <?php endif; ?>

                        </div>

                    </div>

                </div>

            </div>

        </div>

    </section>

</div>

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