<?php

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




// ======================================================
// FETCH ROUTES
// ======================================================

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

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

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

$total_routes = count($routes);
$page_title = 'Route 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-route text-success"></i>
                        Manage Routes

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

            <div class="row">

                <div class="col-12">

                    <div class="card card-success">

                        <div class="card-header">

                            <h3 class="card-title">

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

                            </h3>

                        </div>



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

                            <div class="row">

                                <!-- ROUTE TITLE -->

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

                                    <div class="form-group">

                                        <label>

                                            <i class="fas fa-map-marked-alt text-info"></i>
                                            Route Title

                                        </label>

                                        <input
                                            type="text"
                                            name="route_title"
                                            class="form-control"
                                            placeholder="e.g. Gwalior - City Center / Bhopal - Bairagarh"
                                            required
                                        >

                                    </div>

                                </div>



                                <!-- FARE -->

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

                                    <div class="form-group">

                                        <label>

                                            <i class="fas fa-rupee-sign text-warning"></i>
                                            Monthly Fare

                                        </label>

                                        <div class="input-group">

                                            <div class="input-group-prepend">

                                                <span class="input-group-text">

                                                    ₹

                                                </span>

                                            </div>

                                            <input
                                                type="number"
                                                step="0.01"
                                                name="fare"
                                                class="form-control"
                                                placeholder="1500"
                                                required
                                            >

                                        </div>

                                    </div>

                                </div>



                                <!-- SUBMIT -->

                                <div class="col-lg-2 col-md-12 d-flex align-items-end">

                                    <button
                                        type="submit"
                                        class="btn btn-success btn-lg btn-block w-100"
                                    >

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

                                    </button>

                                </div>

                            </div>

                        </form>

                    </div>

                </div>

            </div>



            <!-- ====================================================== -->
            <!-- ROUTES 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 Routes

                                <span class="badge badge-light">

                                    <?= $total_routes ?>

                                </span>

                            </h3>

                        </div>



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

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

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

                                <h4 class="text-muted">

                                    No routes added yet

                                </h4>

                                <p class="text-muted">

                                    Add your first route 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>Route Title</th>

                                                <th>Fare</th>

                                                <th>Added</th>

                                                <th>Actions</th>

                                            </tr>

                                        </thead>



                                        <tbody>

                                            <?php foreach ($routes as $index => $r): ?>

                                                <tr>

                                                    <th scope="row">

                                                        <?= $index + 1 ?>

                                                    </th>


                                                    <td>

                                                        <strong>

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

                                                        </strong>

                                                    </td>


                                                    <td>

                                                        <span class="badge badge-warning p-2">

                                                            ₹ <?= number_format($r['fare'], 2) ?>

                                                        </span>

                                                    </td>


                                                    <td>

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

                                                    </td>


                                                    <td>

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

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

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

                                                            </a>


                                                            <a
                                                                href="process.php?action=delete&table=routes&id=<?= $r['id'] ?>"
                                                                class="btn btn-danger"
                                                                onclick="return confirm('Delete <?= htmlspecialchars($r['route_title']) ?>?')"
                                                                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'; ?>