<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>
<div class="container-fluid">
    <h2 class="text-center mt-4 mb-3">Processed Incidents</h2>
    <div class="d-flex justify-content-end mb-3">
        <a class="btn btn-secondary" href="/flags/flags_management">Back to Pending Incidents</a>
    </div>
    <?= $this->include('partials/academic_filter') ?>

    <?php
    $incidentLabels = [
        'payment' => 'Payment',
        'attendance' => 'Attendance',
        'grade' => 'Grade',
        'behavior' => 'Behavior (General)',
        'talking_off_task' => 'Talking/off-task',
        'calling_out' => 'Calling out',
        'minor_disruption' => 'Minor disruption',
        'minor_dress_code' => 'Minor dress code',
        'forgetting_materials' => 'Forgetting materials',
        'repeated_defiance' => 'Repeated defiance',
        'disrespectful_tone' => 'Disrespectful tone',
        'minor_profanity' => 'Minor profanity',
        'repeated_tardiness' => 'Repeated tardiness',
        'device_misuse' => 'Device misuse',
        'minor_conflict' => 'Minor conflict/horseplay',
        'bullying_harassment' => 'Bullying/harassment',
        'fighting_aggression' => 'Fighting/aggression',
        'threats' => 'Threats',
        'theft' => 'Theft',
        'vandalism' => 'Vandalism',
        'sexual_harassment' => 'Sexual harassment',
        'serious_cyber_incident' => 'Serious cyber incident',
        'contraband' => 'Contraband',
        'weapons_drugs' => 'Weapons/drugs',
        'other' => 'Other',
    ];
    ?>

    <div class="table-responsive">
        <table class="table table-bordered" data-no-mgmt-sticky>
            <thead>
            <tr>
                <th>Last Updater</th>
                <th>Student Name</th>
                <th>Grade</th>
                <th>Incident Type</th>
                <th>Description</th>
                <th>Datetime</th>
                <th>Incident State</th>
                <th>Actions</th>
            </tr>
            </thead>
            <tbody>
            <?php if (!empty($flags) && is_array($flags)): ?>
                <?php foreach ($flags as $flag): ?>
                    <tr>
                        <td>
                            <?php
                            if ($flag['flag_state'] === 'Open') {
                                $name = $flag['updated_by_open_name'] ?? null;
                                $id = $flag['updated_by_open'] ?? null;
                                echo esc($name ?: ($id ?? 'N/A'));
                            } elseif ($flag['flag_state'] === 'Closed') {
                                $name = $flag['updated_by_closed_name'] ?? null;
                                $id = $flag['updated_by_closed'] ?? null;
                                echo esc($name ?: ($id ?? 'N/A'));
                            } elseif ($flag['flag_state'] === 'Canceled') {
                                $name = $flag['updated_by_canceled_name'] ?? null;
                                $id = $flag['updated_by_canceled'] ?? null;
                                echo esc($name ?: ($id ?? 'N/A'));
                            } else {
                                echo 'N/A';
                            }
                            ?>
                        </td>
                        <td><?= esc($flag['student_name'] ?? '') ?></td>
                        <td><?= esc($flag['grade'] ?? '') ?></td>
                        <?php
                        $incidentValue = trim((string)($flag['flag'] ?? ''));
                        $incidentLabel = $incidentValue !== ''
                            ? ($incidentLabels[$incidentValue] ?? $incidentValue)
                            : 'N/A';
                        ?>
                        <td><?= esc($incidentLabel) ?></td>
                        <td>
                            <?php
                            if ($flag['flag_state'] === 'Open') {
                                echo isset($flag['open_description']) ? esc($flag['open_description']) : 'N/A';
                            } elseif ($flag['flag_state'] === 'Closed') {
                                echo isset($flag['close_description']) ? esc($flag['close_description']) : 'N/A';
                            } elseif ($flag['flag_state'] === 'Canceled') {
                                echo isset($flag['cancel_description']) ? esc($flag['cancel_description']) : 'N/A';
                            } else {
                                echo 'N/A';
                            }
                            ?>
                        </td>
                        <td><?= esc(!empty($flag['flag_datetime']) ? local_datetime($flag['flag_datetime'], 'm-d-Y H:i') : '') ?></td>
                        <td><?= esc($flag['flag_state'] ?? '') ?></td>
                        <td><?= esc($flag['action_taken'] ?? 'N/A') ?></td>
                    </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr>
                    <td colspan="8">No processed incidents found.</td>
                </tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?= $this->endSection() ?>
