<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>
<div class="container-fluid">
    <h2 class="text-center mt-4 mb-3">Incident Analysis</h2>
    <div class="d-flex gap-2 justify-content-end mb-3">
        <a class="btn btn-secondary" href="/flags/flags_management">Back to Pending Incidents</a>
        <a class="btn btn-secondary" href="/flags/processed_flags">View Processed 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>Student Name</th>
                <th>Grade</th>
                <th>Total Incidents</th>
                <th>Open</th>
                <th>Closed</th>
                <th>Canceled</th>
                <th>Last Incident</th>
                <th>Log &amp; Follow-up</th>
            </tr>
            </thead>
            <tbody>
            <?php if (!empty($students) && is_array($students)): ?>
                <?php foreach ($students as $student): ?>
                    <tr>
                        <td><?= esc($student['student_name'] ?? '') ?></td>
                        <td><?= esc($student['grade'] ?? '') ?></td>
                        <td><?= esc($student['total'] ?? 0) ?></td>
                        <td><?= esc($student['open'] ?? 0) ?></td>
                        <td><?= esc($student['closed'] ?? 0) ?></td>
                        <td><?= esc($student['canceled'] ?? 0) ?></td>
                        <td><?= esc(!empty($student['last_incident']) ? local_datetime($student['last_incident'], 'm-d-Y H:i') : '') ?></td>
                        <td>
                            <details>
                                <summary>View log</summary>
                                <div class="table-responsive mt-2">
                                    <table class="table table-sm table-bordered mb-0" data-no-mgmt-sticky>
                                        <thead>
                                        <tr>
                                            <th>Datetime</th>
                                            <th>Incident Type</th>
                                            <th>State</th>
                                            <th>Description</th>
                                            <th>Action Taken</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <?php if (!empty($student['logs'])): ?>
                                            <?php foreach ($student['logs'] as $log): ?>
                                                <?php
                                                $incidentValue = trim((string)($log['flag'] ?? ''));
                                                $incidentLabel = $incidentValue !== ''
                                                    ? ($incidentLabels[$incidentValue] ?? $incidentValue)
                                                    : 'N/A';

                                                $description = 'N/A';
                                                if (($log['flag_state'] ?? '') === 'Open' && !empty($log['open_description'])) {
                                                    $description = (string)$log['open_description'];
                                                } elseif (($log['flag_state'] ?? '') === 'Closed' && !empty($log['close_description'])) {
                                                    $description = (string)$log['close_description'];
                                                } elseif (($log['flag_state'] ?? '') === 'Canceled' && !empty($log['cancel_description'])) {
                                                    $description = (string)$log['cancel_description'];
                                                }
                                                ?>
                                                <tr>
                                                    <td><?= esc(!empty($log['flag_datetime']) ? local_datetime($log['flag_datetime'], 'm-d-Y H:i') : '') ?></td>
                                                    <td><?= esc($incidentLabel) ?></td>
                                                    <td><?= esc($log['flag_state'] ?? '') ?></td>
                                                    <td><?= esc($description) ?></td>
                                                    <td><?= esc($log['action_taken'] ?? 'N/A') ?></td>
                                                </tr>
                                            <?php endforeach; ?>
                                        <?php else: ?>
                                            <tr>
                                                <td colspan="5">No incident logs found.</td>
                                            </tr>
                                        <?php endif; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </details>
                        </td>
                    </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr>
                    <td colspan="8">No incidents found.</td>
                </tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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