<?php
declare(strict_types=1);
require_once 'config/database.php';
require_once 'app/helpers/auth.php';
requireLogin();

require_once 'app/helpers/rbac.php';
if (!isAdmin() && !can('maintenance.view')) {
    echo "<div class='alert alert-danger m-4'>Access denied.</div>";
    exit;
}

$userName = $_SESSION['user_name'] ?? 'User';
$page_js = 'maintenance_requisition_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">Financial Reporting</span>
            <h2 class="mb-2">Maintenance Requisition Analysis</h2>
            <p class="mb-0 text-white-50">Track the financial flow of maintenance jobs from approval to final payment.</p>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <div class="col-12 col-md-6 col-xl-3">
            <div class="card kpi-card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="kpi-label">Total Requisitions</div>
                    <div class="kpi-value" id="kpiTotalCount">0</div>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-6 col-xl-3">
            <div class="card kpi-card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="kpi-label">Total Value</div>
                    <div class="kpi-value" id="kpiTotalValue">₦0.00</div>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-6 col-xl-3">
            <div class="card kpi-card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="kpi-label">Pending Payment</div>
                    <div class="kpi-value text-warning" id="kpiPendingValue">₦0.00</div>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-6 col-xl-3">
            <div class="card kpi-card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="kpi-label">Paid To Date</div>
                    <div class="kpi-value text-success" id="kpiPaidValue">₦0.00</div>
                </div>
            </div>
        </div>
    </div>

    <div class="card shadow-sm border-0 mb-4">
        <div class="card-header bg-white border-0 d-flex flex-column flex-lg-row justify-content-between align-items-lg-center gap-3">
            <h5 class="mb-0">Requisition Register</h5>
            <div class="d-flex flex-wrap gap-2">
                <input type="date" id="filterStartDate" class="form-control form-control-sm w-auto">
                <input type="date" id="filterEndDate" class="form-control form-control-sm w-auto">
                <select id="filterStatus" class="form-select form-select-sm w-auto">
                    <option value="">All Statuses</option>
                    <option value="Raised">Raised</option>
                    <option value="Approved">Approved</option>
                    <option value="Paid">Paid</option>
                </select>
                <button class="btn btn-sm btn-primary" id="btnRefresh">Apply Filters</button>
                <button class="btn btn-sm btn-outline-success" id="btnExport">Export CSV</button>
            </div>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table align-middle table-hover" id="requisitionTable">
                    <thead class="table-light">
                        <tr>
                            <th>Work Order</th>
                            <th>Date</th>
                            <th>Vehicle</th>
                            <th>Workshop</th>
                            <th>Approved Amount</th>
                            <th>Status</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody id="requisitionTableBody">
                        <tr>
                            <td colspan="7" class="text-center text-muted py-4">Loading requisition data...</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</main>

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