<?php
session_start();

$dbPath = dirname(__DIR__, 3) . '/dripend/includes/db.php';
if (file_exists($dbPath)) {
    require_once $dbPath;
} else {
    die("<div class='alert alert-danger m-4'><strong>Critical Path Error:</strong> Cannot locate database connection at $dbPath</div>");
}

// Seed the main block if it does not exist to ensure floor routing doesn't break
$checkFloor = $conn->query("SELECT id FROM ipd_facility_floors LIMIT 1");
if($checkFloor && $checkFloor->num_rows == 0) {
    $conn->query("INSERT INTO ipd_facility_blocks (block_name) VALUES ('Main Block')");
    $block_id = $conn->insert_id;
    if ($block_id) {
        $conn->query("INSERT INTO ipd_facility_floors (block_id, floor_name) VALUES ($block_id, 'Ground Floor')");
    }
}

// Fetch facilities strictly for the Ward Modal
$facilities = [];
$res = $conn->query("SELECT id, hospital_name FROM facility_profile");
if ($res && $res->num_rows > 0) {
    while($row = $res->fetch_assoc()) {
        $facilities[] = $row;
    }
}
?>

<div class="container-fluid pb-5">
    <div class="row mb-3 align-items-center">
        <div class="col-md-8">
            <h4 class="text-primary fw-bold" style="letter-spacing: -0.5px;">
                <i class="fa-solid fa-hospital-user me-2"></i> Infrastructure & Ward Billing Master
            </h4>
            <p class="text-muted mb-0">Manage wards, rooms, physical locations, bed capacities, and dynamic billing rules.</p>
        </div>
    </div>

    <!-- KPI DASHBOARD -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card bg-primary text-white border-0 shadow-sm rounded-3">
                <div class="card-body py-3 d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="mb-0 text-white-50 fw-semibold text-uppercase" style="font-size:11px; letter-spacing:1px;">Active Wards</h6>
                        <h3 class="mb-0 fw-bold" id="kpi-wards">0</h3>
                    </div>
                    <i class="fa-solid fa-layer-group fa-2x text-white-50"></i>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-success text-white border-0 shadow-sm rounded-3">
                <div class="card-body py-3 d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="mb-0 text-white-50 fw-semibold text-uppercase" style="font-size:11px; letter-spacing:1px;">Active Rooms</h6>
                        <h3 class="mb-0 fw-bold" id="kpi-rooms">0</h3>
                    </div>
                    <i class="fa-solid fa-door-open fa-2x text-white-50"></i>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-info text-white border-0 shadow-sm rounded-3">
                <div class="card-body py-3 d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="mb-0 text-white-50 fw-semibold text-uppercase" style="font-size:11px; letter-spacing:1px;">Total Beds</h6>
                        <h3 class="mb-0 fw-bold" id="kpi-beds">0</h3>
                    </div>
                    <i class="fa-solid fa-bed fa-2x text-white-50"></i>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-warning text-dark border-0 shadow-sm rounded-3">
                <div class="card-body py-3 d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="mb-0 text-dark-50 fw-semibold text-uppercase" style="font-size:11px; letter-spacing:1px;">Billing Rules</h6>
                        <h3 class="mb-0 fw-bold" id="kpi-rules">0</h3>
                    </div>
                    <i class="fa-solid fa-file-invoice-dollar fa-2x text-dark-50" style="opacity: 0.5;"></i>
                </div>
            </div>
        </div>
    </div>

    <!-- MAIN INTERFACE - FULL WIDTH -->
    <div class="card shadow-sm border-0 rounded-3">
        <div class="card-header bg-white border-bottom pt-3 pb-0 px-4">
            <ul class="nav nav-tabs border-0" id="ipdMgmtTabs" role="tablist">
                <li class="nav-item"><button class="nav-link active fw-semibold text-secondary" data-bs-toggle="tab" data-bs-target="#tab-wards"><i class="fa-solid fa-layer-group"></i> Wards</button></li>
                <li class="nav-item"><button class="nav-link fw-semibold text-secondary" data-bs-toggle="tab" data-bs-target="#tab-rooms"><i class="fa-solid fa-door-open"></i> Rooms</button></li>
                <li class="nav-item"><button class="nav-link fw-semibold text-secondary" data-bs-toggle="tab" data-bs-target="#tab-beds"><i class="fa-solid fa-bed"></i> Beds</button></li>
                <li class="nav-item"><button class="nav-link fw-semibold text-primary" data-bs-toggle="tab" data-bs-target="#tab-ipd-services"><i class="fa-solid fa-tags"></i> IPD Services / Charges</button></li>
                <li class="nav-item"><button class="nav-link fw-semibold text-success" data-bs-toggle="tab" data-bs-target="#tab-billing-rules"><i class="fa-solid fa-link"></i> Auto-Billing Rules</button></li>
                <li class="nav-item"><button class="nav-link fw-semibold text-secondary" data-bs-toggle="tab" data-bs-target="#tab-categories"><i class="fa-solid fa-folder-tree"></i> Master Dictionaries</button></li>
            </ul>
        </div>
        
        <div class="card-body p-4 tab-content bg-light" style="min-height: 500px;">
            
            <div class="tab-pane fade show active" id="tab-wards">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h5 class="fw-bold mb-0 text-dark">Active Wards</h5>
                    <button class="btn btn-primary shadow-sm" onclick="openModal('Ward')"><i class="fa-solid fa-plus me-1"></i> Add Ward</button>
                </div>
                <div class="table-responsive bg-white border rounded">
                    <table class="table table-hover align-middle w-100 m-0" id="tblWards">
                        <thead style="background-color: #f1f5f9; color: #475569;"><tr><th width="5%">Sr.No</th><th>Ward Name</th><th>Clinic</th><th>Location (Floor)</th><th>Category</th><th>Beds</th><th>Status</th><th>Action</th></tr></thead>
                    </table>
                </div>
            </div>

            <div class="tab-pane fade" id="tab-rooms">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h5 class="fw-bold mb-0 text-dark">Active Rooms</h5>
                    <button class="btn btn-primary shadow-sm" onclick="openModal('Room')"><i class="fa-solid fa-plus me-1"></i> Add Room</button>
                </div>
                <div class="table-responsive bg-white border rounded">
                    <table class="table table-hover align-middle w-100 m-0" id="tblRooms">
                        <thead style="background-color: #f1f5f9; color: #475569;"><tr><th width="5%">Sr.No</th><th>Room Name</th><th>Ward</th><th>Category</th><th>Rate</th><th>Status</th><th>Action</th></tr></thead>
                    </table>
                </div>
            </div>

            <div class="tab-pane fade" id="tab-beds">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h5 class="fw-bold mb-0 text-dark">Bed Inventory</h5>
                    <button class="btn btn-primary shadow-sm" onclick="openModal('BedGen')"><i class="fa-solid fa-wand-magic-sparkles me-1"></i> Bulk Generator</button>
                </div>
                <div class="table-responsive bg-white border rounded">
                    <table class="table table-hover align-middle w-100 m-0" id="tblBeds">
                        <thead style="background-color: #f1f5f9; color: #475569;"><tr><th width="5%">Sr.No</th><th>Bed Label</th><th>Room</th><th>Ward</th><th>Occupancy Status</th><th>System Status</th><th>Action</th></tr></thead>
                    </table>
                </div>
            </div>

            <div class="tab-pane fade" id="tab-ipd-services">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h5 class="fw-bold mb-0 text-dark">IPD Specific Services / Charges</h5>
                    <button class="btn btn-primary shadow-sm" onclick="openModal('IpdService')"><i class="fa-solid fa-tag me-1"></i> Define Service</button>
                </div>
                <div class="table-responsive bg-white border rounded">
                    <table class="table table-hover align-middle w-100 m-0" id="tblIpdServices">
                        <thead style="background-color: #f1f5f9; color: #475569;"><tr><th width="5%">Sr.No</th><th>Service Name</th><th>Category</th><th>Cash (Ad/Ch)</th><th>NHIS Coverage</th><th>Status</th><th>Action</th></tr></thead>
                    </table>
                </div>
            </div>

            <div class="tab-pane fade" id="tab-billing-rules">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h5 class="fw-bold mb-0 text-dark">Ward / Room Auto-Billing Rules</h5>
                    <button class="btn btn-success shadow-sm" onclick="openModal('BillingRule')"><i class="fa-solid fa-link me-1"></i> Setup Rule</button>
                </div>
                <div class="table-responsive bg-white border rounded">
                    <table class="table table-hover align-middle w-100 m-0" id="tblWardCharges">
                        <thead style="background-color: #f1f5f9; color: #475569;"><tr><th width="5%">Sr.No</th><th>Ward</th><th>Room</th><th>IPD Service</th><th>Category</th><th>Frequency</th><th>Action</th></tr></thead>
                    </table>
                </div>
            </div>

            <div class="tab-pane fade" id="tab-categories">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <ul class="nav nav-pills" id="pills-tab" role="tablist">
                      <li class="nav-item"><button class="nav-link active px-4 rounded-pill" data-bs-toggle="pill" data-bs-target="#pills-ward-cats">Ward Cats</button></li>
                      <li class="nav-item"><button class="nav-link px-4 rounded-pill ms-2" data-bs-toggle="pill" data-bs-target="#pills-room-cats">Room Cats</button></li>
                      <li class="nav-item"><button class="nav-link px-4 rounded-pill ms-2" data-bs-toggle="pill" data-bs-target="#pills-svc-cats">Service Cats</button></li>
                      <li class="nav-item"><button class="nav-link px-4 rounded-pill ms-2" data-bs-toggle="pill" data-bs-target="#pills-freq-cats">Frequencies</button></li>
                      <li class="nav-item"><button class="nav-link px-4 rounded-pill ms-2 text-info" data-bs-toggle="pill" data-bs-target="#pills-floors">Locations (Floors)</button></li>
                    </ul>
                    <button class="btn btn-primary shadow-sm" onclick="openModal('Dictionary')"><i class="fa-solid fa-plus me-1"></i> Add Entry</button>
                </div>
                <div class="tab-content bg-white border rounded p-3" id="pills-tabContent">
                    <div class="tab-pane fade show active table-responsive" id="pills-ward-cats"><table class="table table-hover w-100 m-0" id="tblWardCategories"><thead style="background-color: #f1f5f9;"><tr><th width="10%">Sr.No</th><th>Category Name</th><th>Status</th><th width="30%">Action</th></tr></thead></table></div>
                    <div class="tab-pane fade table-responsive" id="pills-room-cats"><table class="table table-hover w-100 m-0" id="tblRoomCategories"><thead style="background-color: #f1f5f9;"><tr><th width="10%">Sr.No</th><th>Category Name</th><th>Status</th><th width="30%">Action</th></tr></thead></table></div>
                    <div class="tab-pane fade table-responsive" id="pills-svc-cats"><table class="table table-hover w-100 m-0" id="tblSvcCategories"><thead style="background-color: #f1f5f9;"><tr><th width="10%">Sr.No</th><th>Category Name</th><th>Status</th><th width="30%">Action</th></tr></thead></table></div>
                    <div class="tab-pane fade table-responsive" id="pills-freq-cats"><table class="table table-hover w-100 m-0" id="tblFreqCategories"><thead style="background-color: #f1f5f9;"><tr><th width="10%">Sr.No</th><th>Frequency Name</th><th>Status</th><th width="30%">Action</th></tr></thead></table></div>
                    <div class="tab-pane fade table-responsive" id="pills-floors"><table class="table table-hover w-100 m-0" id="tblFloors"><thead style="background-color: #f1f5f9;"><tr><th width="10%">Sr.No</th><th>Floor Name</th><th>Building / Block</th><th>Status</th><th width="30%">Action</th></tr></thead></table></div>
                </div>
            </div>

        </div>
    </div>
</div>

<!-- MODAL: WARD -->
<div class="modal fade" id="modalWard" tabindex="-1">
    <div class="modal-dialog modal-lg modal-dialog-centered">
        <div class="modal-content border-0 shadow">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title fw-bold" id="wardModalTitle">Add New Ward</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form id="frmWard">
                <input type="hidden" name="action" value="save_ward">
                <input type="hidden" name="id" id="ward_id" value="">
                <div class="modal-body p-4 row g-4">
                    <div class="col-md-6">
                        <label class="form-label fw-semibold">Clinic / Branch <span class="text-danger">*</span></label>
                        <select name="facility_id" class="form-select" required>
                            <?php foreach($facilities as $f): ?><option value="<?= $f['id'] ?>"><?= htmlspecialchars($f['hospital_name']) ?></option><?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label fw-semibold">Ward Name <span class="text-danger">*</span></label>
                        <input type="text" name="ward_name" id="ward_name" class="form-control" placeholder="e.g. Male Surgical Ward" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label fw-semibold text-info">Physical Location (Floor) <span class="text-danger">*</span></label>
                        <select name="floor_id" id="ward_floor_id" class="form-select border-info" required>
                            <option value="">Loading Locations...</option>
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label fw-semibold">Ward Category <span class="text-danger">*</span></label>
                        <select name="ward_type_id" id="ward_type_id" class="form-select" required>
                            <option value="">Loading Categories...</option>
                        </select>
                    </div>
                </div>
                <div class="modal-footer bg-light">
                    <button type="button" class="btn btn-light border" data-bs-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary px-4 shadow-sm">Save Ward</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- MODAL: ROOM -->
<div class="modal fade" id="modalRoom" tabindex="-1">
    <div class="modal-dialog modal-lg modal-dialog-centered">
        <div class="modal-content border-0 shadow">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title fw-bold" id="roomModalTitle">Add New Room</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form id="frmRoom">
                <input type="hidden" name="action" value="save_room">
                <input type="hidden" name="id" id="room_id" value="">
                <div class="modal-body p-4 row g-4">
                    <div class="col-md-6">
                        <label class="form-label fw-semibold">Parent Ward <span class="text-danger">*</span></label>
                        <select name="ward_id" id="room_ward_id" class="form-select" required>
                            <option value="">Loading Wards...</option>
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label fw-semibold">Room Category <span class="text-danger">*</span></label>
                        <select name="room_type_id" id="room_type_id" class="form-select" required>
                            <option value="">Loading Categories...</option>
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label fw-semibold">Room Name <span class="text-danger">*</span></label>
                        <input type="text" name="room_name" id="room_name" class="form-control" placeholder="e.g. Room 101" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label fw-semibold">Nightly Rate (Override)</label>
                        <div class="input-group">
                            <span class="input-group-text"><?= htmlspecialchars($_SESSION['currency'] ?? 'GHS') ?></span>
                            <input type="number" step="0.01" name="room_nightly_rate" id="room_nightly_rate" class="form-control" value="0.00">
                        </div>
                    </div>
                </div>
                <div class="modal-footer bg-light">
                    <button type="button" class="btn btn-light border" data-bs-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary px-4 shadow-sm">Save Room</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- MODAL: BED GENERATOR -->
<div class="modal fade" id="modalBedGen" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title fw-bold">Bulk Bed Generator</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form id="frmBeds">
                <input type="hidden" name="action" value="generate_beds">
                <div class="modal-body p-4">
                    <div class="mb-3">
                        <label class="form-label fw-semibold">Assign Beds To: <span class="text-danger">*</span></label>
                        <select name="assignment_type" id="bed_assignment_type" class="form-select border-primary" required>
                            <option value="ward">Open Ward (General Beds)</option>
                            <option value="room">Specific Room (Private/Cubicle Beds)</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label fw-semibold">Target Ward <span class="text-danger">*</span></label>
                        <select name="ward_id" id="bed_ward_id" class="form-select" required>
                            <option value="">Loading Wards...</option>
                        </select>
                    </div>
                    <div class="mb-3" id="div_bed_room" style="display: none;">
                        <label class="form-label fw-semibold text-primary">Target Room <span class="text-danger">*</span></label>
                        <select name="room_id" id="bed_room_id" class="form-select border-primary">
                            <option value="">Select Ward First</option>
                        </select>
                    </div>
                    <div class="mb-2">
                        <label class="form-label fw-semibold">Number of Beds to Create <span class="text-danger">*</span></label>
                        <input type="number" name="num_beds" class="form-control" min="1" max="50" required placeholder="e.g. 5">
                    </div>
                </div>
                <div class="modal-footer bg-light">
                    <button type="button" class="btn btn-light border" data-bs-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-success px-4 shadow-sm"><i class="fa-solid fa-hammer me-2"></i> Generate</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- MODAL: IPD SERVICE DEFINITION -->
<div class="modal fade" id="modalIpdService" tabindex="-1">
    <div class="modal-dialog modal-xl modal-dialog-centered">
        <div class="modal-content border-0 shadow">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title fw-bold">Define IPD Service</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form id="frmIpdService">
                <input type="hidden" name="action" value="save_ipd_service">
                <input type="hidden" name="id" id="ipd_service_id" value="">
                
                <div class="modal-body p-4 bg-light">
                    <div class="row bg-white p-3 rounded shadow-sm border mb-4">
                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-semibold">Service Name <span class="text-danger">*</span></label>
                            <input type="text" name="service_name" id="ipd_svc_name" class="form-control" required placeholder="e.g., Ward Admission Fee">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-semibold">Category <span class="text-danger">*</span></label>
                            <select name="category_id" id="ipd_svc_cat" class="form-select" required>
                                <option value="">Loading...</option>
                            </select>
                        </div>
                        <div class="col-12">
                            <label class="form-label fw-semibold text-primary"><i class="fa-solid fa-money-bill-wave"></i> Uninsured (Cash) Pricing</label>
                            <div class="row">
                                <div class="col-6">
                                    <label class="small text-muted">Adult Rate (>=12 yrs)</label>
                                    <input type="number" step="0.01" name="cash_price_adult" id="ipd_svc_cash_a" class="form-control" value="0.00" required>
                                </div>
                                <div class="col-6">
                                    <label class="small text-muted">Child Rate (<12 yrs)</label>
                                    <input type="number" step="0.01" name="cash_price_child" id="ipd_svc_cash_c" class="form-control" value="0.00" required>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row bg-white p-3 rounded shadow-sm border mb-4">
                        <div class="col-12 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" name="is_nhis_covered" id="ipd_svc_nhis" value="1">
                                <label class="form-check-label fw-bold text-success" for="ipd_svc_nhis">Covered by NHIS</label>
                            </div>
                        </div>
                        <div class="col-12" id="div_nhis_pricing" style="display:none;">
                            <div class="row mb-3">
                                <div class="col-6">
                                    <label class="small text-success fw-bold">NHIS Claim Tariff (Adult)</label>
                                    <input type="number" step="0.01" name="nhis_tariff_adult" id="ipd_svc_nhis_t_a" class="form-control border-success" value="0.00">
                                </div>
                                <div class="col-6">
                                    <label class="small text-success fw-bold">NHIS Claim Tariff (Child)</label>
                                    <input type="number" step="0.01" name="nhis_tariff_child" id="ipd_svc_nhis_t_c" class="form-control border-success" value="0.00">
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-6">
                                    <label class="small text-warning fw-bold">Patient Copay (Adult)</label>
                                    <input type="number" step="0.01" name="nhis_copay_adult" id="ipd_svc_nhis_cp_a" class="form-control" value="0.00">
                                </div>
                                <div class="col-6">
                                    <label class="small text-warning fw-bold">Patient Copay (Child)</label>
                                    <input type="number" step="0.01" name="nhis_copay_child" id="ipd_svc_nhis_cp_c" class="form-control" value="0.00">
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row bg-white p-3 rounded shadow-sm border">
                        <div class="col-12 d-flex justify-content-between align-items-center mb-3">
                            <label class="form-label fw-bold text-info mb-0"><i class="fa-solid fa-building"></i> Corporate Insurances</label>
                            <button type="button" class="btn btn-sm btn-outline-info" onclick="addCorporateRow()"><i class="fa-solid fa-plus"></i> Add Policy Mapping</button>
                        </div>
                        <div class="col-12" id="corporate_container">
                            <!-- JS will inject rows here -->
                        </div>
                    </div>
                </div>
                <div class="modal-footer bg-light">
                    <button type="button" class="btn btn-light border" data-bs-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary px-4 shadow-sm">Save Service</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- MODAL: AUTO-BILLING RULE -->
<div class="modal fade" id="modalBillingRule" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow">
            <div class="modal-header bg-success text-white">
                <h5 class="modal-title fw-bold">Setup Automation Rule</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form id="frmWardCharge">
                <input type="hidden" name="action" value="save_billing_rule">
                <div class="modal-body p-4">
                    <div class="mb-3">
                        <label class="form-label fw-semibold">Select Ward <span class="text-danger">*</span></label>
                        <select name="ward_id" id="charge_ward_id" class="form-select" required>
                            <option value="">Loading Wards...</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label fw-semibold text-primary">Select Room <span class="text-muted small">(Optional)</span></label>
                        <select name="room_id" id="charge_room_id" class="form-select border-primary">
                            <option value="">-- Apply to Entire Ward --</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label fw-semibold">IPD Service <span class="text-danger">*</span></label>
                        <select name="ipd_service_id" id="charge_service_id" class="form-select" required>
                            <option value="">Loading Services...</option>
                        </select>
                    </div>
                    <div class="mb-2">
                        <label class="form-label fw-semibold">Frequency <span class="text-danger">*</span></label>
                        <select name="frequency_id" id="charge_freq_id" class="form-select" required>
                            <option value="">Loading Frequencies...</option>
                        </select>
                    </div>
                </div>
                <div class="modal-footer bg-light">
                    <button type="button" class="btn btn-light border" data-bs-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-success px-4 shadow-sm">Save Rule</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- MODAL: DICTIONARIES -->
<div class="modal fade" id="modalDictionary" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow">
            <div class="modal-header bg-secondary text-white">
                <h5 class="modal-title fw-bold" id="dictModalTitle">Add Dictionary Entry</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form id="frmCategory">
                <input type="hidden" name="action" value="save_dictionary">
                <input type="hidden" name="id" id="dict_id" value="">
                <div class="modal-body p-4">
                    <div class="mb-3">
                        <label class="form-label fw-semibold">Dictionary Type <span class="text-danger">*</span></label>
                        <select name="dict_type" id="dict_type" class="form-select" required>
                            <option value="ward_cat">Ward Category</option>
                            <option value="room_cat">Room Category</option>
                            <option value="svc_cat">IPD Service Category</option>
                            <option value="freq_cat">Billing Frequency</option>
                            <option value="floor" class="text-info">Physical Location (Floor)</option>
                        </select>
                    </div>
                    
                    <div class="mb-3" id="div_block_id" style="display:none;">
                        <label class="form-label fw-semibold text-info">Select Building / Block <span class="text-danger">*</span></label>
                        <select name="block_id" id="dict_block" class="form-select border-info">
                            <option value="">Loading Blocks...</option>
                        </select>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label fw-semibold">Entry Name <span class="text-danger">*</span></label>
                        <input type="text" name="dict_name" id="dict_name" class="form-control" required placeholder="e.g. VIP Suites">
                    </div>

                    <div class="mb-2" id="div_interval_days" style="display:none;">
                        <label class="form-label fw-semibold text-primary">Billing Interval (in Days) <span class="text-danger">*</span></label>
                        <input type="number" name="interval_days" id="dict_interval" class="form-control border-primary" value="1" min="0">
                        <small class="text-muted d-block mt-1">Enter 0 for One-Time charges, 1 for Daily, 7 for Weekly.</small>
                    </div>
                </div>
                <div class="modal-footer bg-light">
                    <button type="button" class="btn btn-light border" data-bs-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary px-4 shadow-sm">Save Entry</button>
                </div>
            </form>
        </div>
    </div>
</div>