<?php
require_once 'app/helpers/rbac.php';
require_once 'config/database.php';
require_once 'app/helpers/auth.php';
requireLogin();

if (!isAdmin() && !can('driver.view')) {
    echo "<div class='alert alert-danger m-4'>Access denied. You do not have permission to view driver reports.</div>";
    exit;
}

$userName = $_SESSION['user_name'] ?? 'User';
$page_js = 'driver_overage_report.js';

include 'views/layouts/header.php';
include 'views/layouts/sidebar.php';
?>

<main class="main-content">

<?php include 'views/layouts/topbar.php'; ?>

<div class="container-fluid p-4">
    <div class="card shadow-sm border-0 maintenance-hero mb-4">
        <div class="card-body p-4 p-lg-5">
            <span class="section-chip bg-white text-primary mb-3">Driver Reporting</span>
            <h2 class="mb-2">Driver Age Limit Analysis</h2>
            <p class="mb-0 text-white-50">Monitor drivers whose current age exceeds the contractual maximum threshold.</p>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <div class="col-12 col-md-6 col-xl-4">
            <div class="card kpi-card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="kpi-label">Within Age Limit</div>
                            <div class="kpi-value text-success" id="kpiWithinLimit">0</div>
                        </div>
                        <div class="kpi-icon text-success">
                            <i class="bi bi-check-circle"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-12 col-md-6 col-xl-4">
            <div class="card kpi-card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="kpi-label">Above Limit</div>
                            <div class="kpi-value text-danger" id="kpiOverLimit">0</div>
                        </div>
                        <div class="kpi-icon text-danger">
                            <i class="bi bi-exclamation-octagon"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-12 col-md-6 col-xl-4">
            <div class="card kpi-card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="kpi-label">Contractual Limit</div>
                            <div class="input-group input-group-sm mt-1" style="max-width: 120px;">
                                <input type="number" id="maxAgeLimit" class="form-control" value="60" min="18" max="120">
                                <span class="input-group-text">yrs</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="card shadow-sm border-0">
        <div class="card-header bg-white d-flex flex-column flex-lg-row justify-content-between align-items-lg-center gap-3">
            <div>
                <h5 class="mb-1">Age Limit Register</h5>
                <p class="text-muted mb-0">List drivers evaluated against the contractual age threshold, including current age and compliance status.</p>
            </div>
            <div class="d-flex flex-column flex-sm-row gap-2 w-100 w-lg-auto">
                <input type="text" id="searchDriver" class="form-control" placeholder="Search name or code">
                <button class="btn btn-outline-secondary" id="btnRefresh">Refresh</button>
                <button class="btn btn-primary" id="btnExport">Export CSV</button>
            </div>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table align-middle table-hover">
                    <thead class="table-light">
                        <tr>
                            <th>Driver Code</th>
                            <th>Name</th>
                            <th>Date of Birth</th>
                            <th>Current Age</th>
                            <th>Status</th>
                            <th>Profile</th>
                        </tr>
                    </thead>
                    <tbody id="overageTable">
                        <tr><td colspan="6" class="text-center text-muted">Loading driver data...</td></tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</main>

<?php include 'views/layouts/footer.php'; ?>