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

if (!isAdmin() && !can('driver.view') && !can('vehicle.view') && !can('report.view')) {
    echo "<div class='alert alert-danger m-4'>Access denied.</div>";
    exit;
}

$userName = $_SESSION['user_name'] ?? 'User';
$page_js = 'driver_assignment_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">Reports</span>
            <h2 class="mb-2">Driver Assignment History</h2>
            <p class="mb-0 text-white-50">Search assignment records by driver or vehicle, and export to CSV or PDF.</p>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <div class="col-6 col-md-3">
            <div class="card kpi-card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="kpi-label">Total Assignments</div>
                    <div class="kpi-value" id="kpiTotal">0</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="card kpi-card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="kpi-label">Active</div>
                    <div class="kpi-value text-success" id="kpiActive">0</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="card kpi-card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="kpi-label">Inactive / Expired</div>
                    <div class="kpi-value text-danger" id="kpiInactive">0</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="card kpi-card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="kpi-label">Unique Drivers / Vehicles</div>
                    <div class="kpi-value" id="kpiUnique">- / -</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">Assignment Records</h5>
                <p class="text-muted mb-0">Search by driver name, code, vehicle plate, or model.</p>
            </div>
            <div class="d-flex flex-column flex-sm-row gap-2 w-100 w-lg-auto">
                <input type="text" id="searchAssignment" class="form-control" placeholder="Search driver or vehicle...">
                <button class="btn btn-outline-secondary" id="btnRefresh">Refresh</button>
                <button class="btn btn-success" id="btnExportCsv">Excel</button>
                <button class="btn btn-danger" id="btnExportPdf">PDF</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</th>
                            <th>Vehicle</th>
                            <th>Company</th>
                            <th>Start Date</th>
                            <th>End Date</th>
                            <th>Duration</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody id="assignmentReportTable">
                        <tr><td colspan="7" class="text-center text-muted">Loading assignment data...</td></tr>
                    </tbody>
                </table>
            </div>
            <div class="d-flex justify-content-between align-items-center mt-3">
                <small class="text-muted" id="tableInfo">Loading...</small>
                <button class="btn btn-sm btn-outline-primary d-none" id="btnLoadMore">Load More</button>
            </div>
        </div>
    </div>
</div>
</main>

<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js"></script>
<?php include 'views/layouts/footer.php'; ?>
