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

$type = $_GET['type'] ?? 'Student';
$page_title = 'Transport Allocation';

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

?>

<div class="content-wrapper">

    <section class="content-header">

        <div class="container-fluid">

            <div class="row mb-2">

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

                    <h1>

                        <i class="fas fa-truck-loading text-warning"></i>
                        Allocate Transport

                    </h1>

                </div>

            </div>

        </div>

    </section>



    <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; ?>



            <!-- ====================================================== -->
            <!-- ALLOCATION FORM -->
            <!-- ====================================================== -->

            <div class="row">

                <div class="col-12">

                    <div class="card card-warning">

                        <div class="card-header">

                            <h3 class="card-title">

                                <i class="fas fa-plus-circle"></i>
                                Assign Transport

                            </h3>

                        </div>



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

                            <div class="row">

                                <!-- USER TYPE -->

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

                                    <div class="form-group">

                                        <label>

                                            <i class="fas fa-users text-primary"></i>
                                            User Type

                                        </label>

                                        <select
                                            name="user_type"
                                            class="form-control"
                                            onchange="window.location.href='assign_transport.php?type='+this.value"
                                            required
                                        >

                                            <option
                                                value="Student"
                                                <?= $type == 'Student' ? 'selected' : '' ?>
                                            >

                                                Student

                                            </option>

                                            <option
                                                value="Staff"
                                                <?= $type == 'Staff' ? 'selected' : '' ?>
                                            >

                                                Staff

                                            </option>

                                        </select>

                                    </div>

                                </div>



                                <!-- PERSON -->

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

                                    <div class="form-group">

                                        <label>

                                            <i class="fas fa-user-graduate text-info"></i>
                                            Select Person

                                        </label>

                                        <select
                                            name="reference_id"
                                            class="form-control"
                                            required
                                        >

                                            <option value="">

                                                Choose person...

                                            </option>

                                            <?php

                                           if ($type == 'Student') {

													$stmt = $pdo->prepare("

														SELECT DISTINCT

															s.id,
															s.name

														FROM student_sessions ss

														INNER JOIN students s
															ON ss.student_id = s.id

														WHERE ss.school_id = ?
														AND ss.status = 'Active'

														ORDER BY s.name ASC

													");

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

												}
												else {

													$stmt = $pdo->prepare("

														SELECT

															id,
															name

														FROM teachers

														WHERE school_id = ?
														AND status = 'Active'

														ORDER BY name ASC

													");

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

												}

                                            while($r = $stmt->fetch(PDO::FETCH_ASSOC)):

                                            ?>

                                                <option value="<?= $r['id'] ?>">

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

                                                </option>

                                            <?php endwhile; ?>

                                        </select>

                                    </div>

                                </div>



                                <!-- ROUTE -->

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

                                    <div class="form-group">

                                        <label>

                                            <i class="fas fa-route text-success"></i>
                                            Route

                                        </label>

                                        <select
                                            name="route_id"
                                            class="form-control"
                                            required
                                        >

                                            <option value="">

                                                Choose route...

                                            </option>

                                            <?php

                                            $stmt = $pdo->prepare("
                                                SELECT *
                                                FROM routes
                                                WHERE school_id = ?
                                                ORDER BY route_title
                                            ");

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

                                            while($r = $stmt->fetch(PDO::FETCH_ASSOC)):

                                            ?>

                                                <option value="<?= $r['id'] ?>">

                                                    <?= htmlspecialchars($r['route_title']) ?>

                                                </option>

                                            <?php endwhile; ?>

                                        </select>

                                    </div>

                                </div>



                                <!-- VEHICLE -->

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

                                    <div class="form-group">

                                        <label>

                                            <i class="fas fa-truck text-danger"></i>
                                            Vehicle

                                        </label>

                                        <select
                                            name="vehicle_id"
                                            class="form-control"
                                            required
                                        >

                                            <option value="">

                                                Choose vehicle...

                                            </option>

                                            <?php

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

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

                                            while($r = $stmt->fetch(PDO::FETCH_ASSOC)):

                                            ?>

                                                <option value="<?= $r['id'] ?>">

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

                                                </option>

                                            <?php endwhile; ?>

                                        </select>

                                    </div>

                                </div>

                            </div>



                            <div class="text-right">

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

                                    <i class="fas fa-truck-loading mr-2"></i>
                                    Allocate Transport

                                </button>

                            </div>

                        </form>

                    </div>

                </div>

            </div>



            <!-- ====================================================== -->
            <!-- ALLOCATIONS LIST -->
            <!-- ====================================================== -->

            <div class="row">

                <div class="col-12">

                    <?php

                    $query = "

                        SELECT

                            ta.*,

                            r.route_title,

                            v.vehicle_no,

                            s.name AS s_name,

                            t.name AS t_name

                        FROM transport_allocation ta

                        INNER JOIN routes r
                            ON ta.route_id = r.id

                        INNER JOIN vehicles v
                            ON ta.vehicle_id = v.id

                        LEFT JOIN students s
                            ON ta.reference_id = s.id
                            AND ta.user_type = 'Student'

                        LEFT JOIN teachers t
                            ON ta.reference_id = t.id
                            AND ta.user_type = 'Staff'

                        WHERE ta.school_id = ?

                        ORDER BY ta.id DESC

                    ";

                    $stmt = $pdo->prepare($query);

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

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

                    $total_allocations = count($allocations);

                    ?>



                    <div class="card card-info">

                        <div class="card-header">

                            <h3 class="card-title">

                                <i class="fas fa-list"></i>
                                All Transport Allocations

                                <span class="badge badge-light">

                                    <?= $total_allocations ?>

                                </span>

                            </h3>

                        </div>



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

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

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

                                <h4 class="text-muted">

                                    No transport allocated yet

                                </h4>

                                <p class="text-muted">

                                    Assign transport 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>Name</th>

                                                <th>Type</th>

                                                <th>Route</th>

                                                <th>Vehicle</th>

                                                <th>Allocated</th>

                                                <th>Action</th>

                                            </tr>

                                        </thead>


                                        <tbody>

                                            <?php foreach ($allocations as $index => $a): ?>

                                                <?php

                                                $name = $a['s_name'] ?? $a['t_name'];

                                                ?>

                                                <tr>

                                                    <th scope="row">

                                                        <?= $index + 1 ?>

                                                    </th>


                                                    <td>

                                                        <strong>

                                                            <?= htmlspecialchars($name) ?>

                                                        </strong>

                                                    </td>


                                                    <td>

                                                        <span class="badge badge-<?= $a['user_type'] == 'Student' ? 'primary' : 'info' ?>">

                                                            <?= htmlspecialchars($a['user_type']) ?>

                                                        </span>

                                                    </td>


                                                    <td>

                                                        <?= htmlspecialchars($a['route_title']) ?>

                                                    </td>


                                                    <td>

                                                        <strong>

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

                                                        </strong>

                                                    </td>


                                                    <td>

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

                                                    </td>


                                                    <td>

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

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

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

                                                            </a>


                                                            <a
                                                                href="process.php?action=delete&table=transport_allocation&id=<?= $a['id'] ?>"
                                                                class="btn btn-danger btn-sm"
                                                                onclick="return confirm('Remove <?= htmlspecialchars($name) ?> allocation?')"
                                                                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'; ?>