<?php
// Centralized Configuration Array for the UI
$workflow_modules = [
    '1. Admission Engine' => [
        ['id' => 'adm_source', 'label' => 'Admission Sources', 'icon' => 'fa-arrow-right-to-city', 'title' => 'Admission Source'],
        ['id' => 'adm_priority', 'label' => 'Admission Priorities', 'icon' => 'fa-traffic-light', 'title' => 'Priority Level'],
        ['id' => 'adm_reason', 'label' => 'Admission Reasons', 'icon' => 'fa-stethoscope', 'title' => 'Admission Reason'],
        ['id' => 'adm_status', 'label' => 'Request Statuses', 'icon' => 'fa-list-check', 'title' => 'Request Status']
    ],
    '2. Ward Transfers' => [
        ['id' => 'trans_type', 'label' => 'Transfer Types', 'icon' => 'fa-shuffle', 'title' => 'Transfer Type'],
        ['id' => 'trans_reason', 'label' => 'Transfer Reasons', 'icon' => 'fa-comment-medical', 'title' => 'Transfer Reason']
    ],
    '3. Ward Rounds' => [
        ['id' => 'round_type', 'label' => 'Round Types', 'icon' => 'fa-user-doctor', 'title' => 'Round Type'],
        ['id' => 'round_outcome', 'label' => 'Round Outcomes', 'icon' => 'fa-clipboard-list', 'title' => 'Round Outcome']
    ],
    '4. Medication Admin' => [
        ['id' => 'med_status', 'label' => 'Admin Status', 'icon' => 'fa-pills', 'title' => 'Admin Status'],
        ['id' => 'med_missed', 'label' => 'Missed Reasons', 'icon' => 'fa-ban', 'title' => 'Missed Reason'],
        ['id' => 'med_alert', 'label' => 'High Alert Categories', 'icon' => 'fa-triangle-exclamation', 'title' => 'High Alert Category']
    ],
    '5. IV Therapy' => [
        ['id' => 'iv_cannula', 'label' => 'Cannula Types', 'icon' => 'fa-syringe', 'title' => 'Cannula Type'],
        ['id' => 'iv_site', 'label' => 'Insertion Sites', 'icon' => 'fa-hand-dots', 'title' => 'Insertion Site'],
        ['id' => 'iv_status', 'label' => 'IV Status', 'icon' => 'fa-bars-progress', 'title' => 'IV Status'],
        ['id' => 'iv_comp', 'label' => 'Complications', 'icon' => 'fa-circle-exclamation', 'title' => 'Complication']
    ],
    '6. Blood Bank Rules' => [
        ['id' => 'bb_prior', 'label' => 'Request Priorities', 'icon' => 'fa-truck-fast', 'title' => 'Request Priority'],
        ['id' => 'bb_compat', 'label' => 'Compatibility', 'icon' => 'fa-flask-vial', 'title' => 'Compatibility Result'],
        ['id' => 'bb_react', 'label' => 'Transfusion Reactions', 'icon' => 'fa-virus', 'title' => 'Transfusion Reaction'],
        ['id' => 'bb_sever', 'label' => 'Reaction Severity', 'icon' => 'fa-gauge-high', 'title' => 'Reaction Severity']
    ],
    '7. Discharge & Exit' => [
        ['id' => 'dis_type', 'label' => 'Discharge Types', 'icon' => 'fa-door-open', 'title' => 'Discharge Type'],
        ['id' => 'dis_dest', 'label' => 'Destinations', 'icon' => 'fa-house-chimney-medical', 'title' => 'Discharge Destination'],
        ['id' => 'dis_cond', 'label' => 'Patient Condition', 'icon' => 'fa-heart-circle-check', 'title' => 'Patient Condition'],
        ['id' => 'mort_cat', 'label' => 'Mortality Categories', 'icon' => 'fa-book-skull text-dark', 'title' => 'Mortality Category']
    ],
    '8. Consultations' => [
        ['id' => 'cons_type', 'label' => 'Consult Types', 'icon' => 'fa-users-viewfinder', 'title' => 'Consult Type'],
        ['id' => 'cons_out', 'label' => 'Consult Outcomes', 'icon' => 'fa-list-check', 'title' => 'Consult Outcome']
    ],
    '9. Alerts & Scores' => [
        ['id' => 'alert_type', 'label' => 'Clinical Alerts', 'icon' => 'fa-bell text-danger', 'title' => 'Clinical Alert'],
        ['id' => 'alert_sev', 'label' => 'Alert Severity', 'icon' => 'fa-temperature-arrow-up', 'title' => 'Alert Severity'],
        ['id' => 'score_sys', 'label' => 'Scoring Systems', 'icon' => 'fa-calculator', 'title' => 'Scoring System']
    ]
];
$isFirst = true;
?>

<div class="container-fluid pb-5">
    <div class="row mb-4 align-items-center">
        <div class="col-md-8">
            <h4 class="text-primary fw-bold" style="letter-spacing: -0.5px;">
                <i class="fa-solid fa-project-diagram me-2"></i> Clinical Workflow Engine
            </h4>
            <p class="text-muted mb-0">Master operational configurations for routing, transfers, rounds, and alerts.</p>
        </div>
    </div>

    <div class="row g-4">
        <!-- VERTICAL SIDEBAR -->
        <div class="col-md-3">
            <div class="card shadow-sm border-0 rounded-3 h-100">
                <div class="card-body p-0" style="max-height: 800px; overflow-y: auto;">
                    <div class="nav flex-column nav-pills custom-v-pills" id="v-pills-tab" role="tablist" aria-orientation="vertical">
                        <?php foreach($workflow_modules as $groupName => $items): ?>
                            <div class="px-3 py-2 text-muted fw-bold small text-uppercase mt-3 border-top pt-3"><?= $groupName ?></div>
                            <?php foreach($items as $item): ?>
                                <button class="nav-link text-start rounded-0 border-0 border-bottom <?= $isFirst ? 'active' : '' ?>" 
                                        data-bs-toggle="pill" 
                                        data-bs-target="#tab-<?= $item['id'] ?>"
                                        data-type="<?= $item['id'] ?>"
                                        data-title="<?= $item['title'] ?>">
                                    <i class="fa-solid <?= $item['icon'] ?> me-2 w-20px"></i> <?= $item['label'] ?>
                                </button>
                                <?php $isFirst = false; ?>
                            <?php endforeach; ?>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
        </div>

        <!-- CONTENT AREA -->
        <div class="col-md-9">
            <div class="card shadow-sm border-0 rounded-3">
                <div class="card-body p-4 tab-content bg-white min-vh-100" id="v-pills-tabContent">
                    <?php 
                    $isFirstPane = true;
                    foreach($workflow_modules as $groupName => $items): 
                        foreach($items as $item): 
                    ?>
                        <div class="tab-pane fade <?= $isFirstPane ? 'show active' : '' ?>" id="tab-<?= $item['id'] ?>">
                            <div class="d-flex justify-content-between align-items-center mb-3">
                                <h5 class="fw-bold mb-0 text-dark"><?= $item['label'] ?></h5>
                                <button class="btn btn-primary btn-sm shadow-sm" onclick="openWorkflowModal('<?= $item['id'] ?>', '<?= $item['title'] ?>')">
                                    <i class="fa-solid fa-plus"></i> Add <?= $item['title'] ?>
                                </button>
                            </div>
                            <table class="table table-hover align-middle w-100 m-0" id="tbl_<?= $item['id'] ?>">
                                <thead class="table-light">
                                    <tr>
                                        <th>Sr.No</th>
                                        <th><?= $item['title'] ?> Name</th>
                                        <?php if($item['id'] === 'adm_priority') echo '<th>Color Code</th>'; ?>
                                        <?php if($item['id'] === 'round_outcome') echo '<th>System Trigger</th>'; ?>
                                        <th>Status</th>
                                        <th width="15%">Action</th>
                                    </tr>
                                </thead>
                            </table>
                        </div>
                        <?php $isFirstPane = false; ?>
                    <?php 
                        endforeach; 
                    endforeach; 
                    ?>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- MODAL: UNIVERSAL DYNAMIC -->
<div class="modal fade" id="modalWorkflowDict" 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" id="wfModalTitle">Add Entry</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form id="frmWorkflowDict">
                <input type="hidden" name="action" value="save_dictionary">
                <input type="hidden" name="id" id="wf_id" value="">
                <input type="hidden" name="dict_type" id="wf_dict_type" value="">
                <div class="modal-body p-4" id="wf_body_content">
                    <!-- Dynamic Inputs injected by JS -->
                </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 Configuration</button>
                </div>
            </form>
        </div>
    </div>
</div>