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

$userName = $_SESSION['user_name'] ?? 'User';
$page_js = 'compliance_setup.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 mb-4">
        <div class="card-body p-4 p-lg-5">
            <div class="row g-4 align-items-center">
                <div class="col-lg-8">
                    <span class="badge bg-primary bg-opacity-10 text-primary mb-3 px-3 py-2">Compliance Setup</span>
                    <h2 class="mb-2">Manage Renewable Document Setup</h2>
                    <p class="mb-0 text-muted">Centralize states, authorities, offices, vendors, and registry entries so compliance workflows are fast, accurate, and consistent.</p>
                </div>
                <div class="col-lg-4 text-lg-end">
                </div>
            </div>
        </div>
    </div>

    <div class="row g-4 mb-4">
        <div class="col-12 col-md-6 col-xl-3">
            <div class="card kpi-card shadow-sm border-0 rounded-4 h-100">
                <div class="card-body py-3 px-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="kpi-label">Document Types</div>
                            <div class="kpi-value" id="kpiDocumentTypes">0</div>
                        </div>
                        <div class="kpi-icon text-primary fs-3">
                            <i class="bi bi-journal-bookmark"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row g-4">
        <div class="col-12 col-xl-6">
            <div class="card shadow-sm border-0 rounded-4 h-100">
                <div class="card-header bg-white d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-3">
                    <div>
                        <h5 class="mb-1">Document Registry</h5>
                        <p class="text-muted mb-0">Browse, add, and edit configured document types with renewal cycles.</p>
                    </div>
                    <button type="button" class="btn btn-primary btn-sm" id="btnAddDocumentType">+ Add Document Type</button>
                </div>
                <div class="card-body">
                    <div class="row g-3 mb-3">
                        <div class="col-12 col-md-8">
                            <input type="text" id="registrySearch" class="form-control form-control-sm" placeholder="Search registry by name, authority, or code">
                        </div>
                        <div class="col-12 col-md-4 text-md-end">
                            <span class="text-muted small">Showing <strong id="registryCount">0</strong> types</span>
                        </div>
                    </div>
                    <div class="row g-2" id="documentTypeGrid">
                        <div class="col-12"><div class="text-center text-muted py-4">Loading document registry...</div></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-12 col-xl-6">
            <div class="card shadow-sm border-0 rounded-4 h-100">
                <div class="card-header bg-white">
                    <h5 class="mb-1">Setup Catalogue</h5>
                    <p class="text-muted mb-0 small">All setup types are now managed from their dedicated pages in <strong>Settings</strong>.</p>
                </div>
                <div class="card-body">
                    <div class="d-flex flex-wrap gap-2">
                        <a href="regions.php" class="btn btn-sm btn-outline-info"><i class="bi bi-box-arrow-up-right"></i> Regions</a>
                        <a href="cities.php" class="btn btn-sm btn-outline-info"><i class="bi bi-box-arrow-up-right"></i> Cities</a>
                        <a href="locations.php" class="btn btn-sm btn-outline-info"><i class="bi bi-box-arrow-up-right"></i> Locations</a>
                        <a href="issuing_authorities.php" class="btn btn-sm btn-outline-info"><i class="bi bi-box-arrow-up-right"></i> Issuing Authorities</a>
                        <a href="issuing_offices.php" class="btn btn-sm btn-outline-info"><i class="bi bi-box-arrow-up-right"></i> Issuing Offices</a>
                        <a href="compliance_vendors.php" class="btn btn-sm btn-outline-info"><i class="bi bi-box-arrow-up-right"></i> Compliance Vendors</a>
                        <a href="projects.php" class="btn btn-sm btn-outline-info"><i class="bi bi-box-arrow-up-right"></i> Projects</a>
                        <a href="contract_types.php" class="btn btn-sm btn-outline-info"><i class="bi bi-box-arrow-up-right"></i> Contract Types</a>
                        <a href="accounts.php" class="btn btn-sm btn-outline-info"><i class="bi bi-box-arrow-up-right"></i> Accounts</a>
                        <a href="owners.php" class="btn btn-sm btn-outline-info"><i class="bi bi-box-arrow-up-right"></i> Owners</a>
                        <a href="suppliers.php" class="btn btn-sm btn-outline-info"><i class="bi bi-box-arrow-up-right"></i> Suppliers</a>
                        <a href="states.php" class="btn btn-sm btn-outline-info"><i class="bi bi-box-arrow-up-right"></i> States</a>
                        <a href="lgas.php" class="btn btn-sm btn-outline-info"><i class="bi bi-box-arrow-up-right"></i> LGAs</a>
                        <a href="branches.php" class="btn btn-sm btn-outline-info"><i class="bi bi-box-arrow-up-right"></i> Branches</a>
                        <a href="departments.php" class="btn btn-sm btn-outline-info"><i class="bi bi-box-arrow-up-right"></i> Departments</a>
                        <a href="vehicle_types.php" class="btn btn-sm btn-outline-info"><i class="bi bi-box-arrow-up-right"></i> Vehicle Types</a>
                        <a href="vehicle_models.php" class="btn btn-sm btn-outline-info"><i class="bi bi-box-arrow-up-right"></i> Vehicle Models</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</main>


<div class="modal fade" id="documentTypeModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="docTypeModalTitle">Add Document Type</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form id="documentTypeForm">
                    <input type="hidden" id="docTypeId">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label for="docTypeName" class="form-label">Document Type Name</label>
                            <input type="text" id="docTypeName" class="form-control" required>
                        </div>

                        <div class="col-md-6">
                            <label for="docTypeCode" class="form-label">Code</label>
                            <input type="text" id="docTypeCode" class="form-control">
                        </div>

                        <div class="col-md-6">
                            <label for="docTypeIssuer" class="form-label">Issuing Authority</label>
                            <input type="text" id="docTypeIssuer" class="form-control">
                        </div>

                        <div class="col-md-6">
                            <label for="docTypeRenewalMonths" class="form-label">Renewal Cycle (months)</label>
                            <input type="number" id="docTypeRenewalMonths" class="form-control" min="0" step="1">
                        </div>

                        <div class="col-12">
                            <label class="form-label">Applicability</label>
                            <div class="form-check">
                                <input type="checkbox" id="docTypePrivate" class="form-check-input">
                                <label class="form-check-label" for="docTypePrivate">Required for Private vehicles</label>
                            </div>
                            <div class="form-check">
                                <input type="checkbox" id="docTypeCommercial" class="form-check-input">
                                <label class="form-check-label" for="docTypeCommercial">Required for Commercial vehicles</label>
                            </div>
                            <div class="form-check">
                                <input type="checkbox" id="docTypeHeavyDuty" class="form-check-input">
                                <label class="form-check-label" for="docTypeHeavyDuty">Required for Heavy Duty vehicles</label>
                            </div>
                            <div class="form-check">
                                <input type="checkbox" id="docTypeInterstate" class="form-check-input">
                                <label class="form-check-label" for="docTypeInterstate">Required for Interstate vehicles</label>
                            </div>
                        </div>

                        <div class="col-md-6">
                            <label for="docTypeStorageTable" class="form-label">Storage Table (if applicable)</label>
                            <input type="text" id="docTypeStorageTable" class="form-control">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary" id="saveDocumentType">Save Document Type</button>
            </div>
        </div>
    </div>
</div>

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