<?php
require_once 'config/database.php';
require_once 'app/helpers/auth.php';
require_once 'app/helpers/rbac.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_underage_license.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 compliance-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">Underage License Receipt Report</h2>
            <p class="mb-0 text-white-50">Identify drivers whose first license was issued before reaching the minimum age of 18 years.</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">Compliant</div>
                            <div class="kpi-value text-success" id="kpiCompliant">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">Non-Compliant</div>
                            <div class="kpi-value text-danger" id="kpiNonCompliant">0</div>
                        </div>
                        <div class="kpi-icon text-danger">
                            <i class="bi bi-exclamation-triangle"></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">Total Drivers</div>
                            <div class="kpi-value text-primary" id="kpiTotalDrivers">0</div>
                        </div>
                        <div class="kpi-icon text-primary">
                            <i class="bi bi-people"></i>
                        </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">License Issue Register</h5>
                <p class="text-muted mb-0">List of drivers with their first license issue details and age 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>License Number</th>
                            <th>First Issue Date</th>
                            <th>Age at Issue</th>
                            <th>Compliance</th>
                            <th>Profile</th>
                        </tr>
                    </thead>
                    <tbody id="underageTable">
                        <tr><td colspan="8" class="text-center text-muted">Loading license data...</td></tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</main>

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