<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>
<div class="container-fluid py-3">
    <div class="d-flex align-items-center justify-content-between mb-2">
        <div>
            <h1 class="h4 mb-1">Admin Notification Subjects</h1>
            <div class="text-muted">Choose the subject each admin should receive notifications for.</div>
        </div>
    </div>

    <?= view('partials/flash_messages') ?>

    <?php if (empty($tableReady)): ?>
        <div class="alert alert-warning" role="alert">
            Notification subject storage is not available. Please run migrations to enable saving.
        </div>
    <?php endif; ?>

    <form method="post" action="<?= site_url('administrator/notifications_alerts/save') ?>">
        <?= csrf_field() ?>
        <div class="card">
            <div class="card-header d-flex align-items-center justify-content-between">
                <span>Admins</span>
                <button type="submit" class="btn btn-primary btn-sm" <?= empty($tableReady) ? 'disabled' : '' ?>>Save</button>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table id="adminNotificationsTable" class="table table-striped align-middle no-mgmt-sticky no-dt-fixedheader">
                        <thead class="table-dark">
                            <tr>
                                <th style="width: 60px;">#</th>
                                <th>Name</th>
                                <th>Email</th>
                                <?php foreach (($subjects ?? []) as $label): ?>
                                    <th class="text-center" style="width: 140px;"><?= esc($label) ?></th>
                                <?php endforeach; ?>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($admins)): ?>
                                <tr>
                                    <td colspan="<?= 3 + count($subjects ?? []) ?>" class="text-center text-muted">No admins found.</td>
                                </tr>
                            <?php else: ?>
                                <?php foreach ($admins as $index => $admin): ?>
                                    <?php
                                        $adminId = (int) ($admin['id'] ?? 0);
                                        $selected = $assignedSubjects[$adminId] ?? [];
                                        $fullName = trim(($admin['firstname'] ?? '') . ' ' . ($admin['lastname'] ?? ''));
                                    ?>
                                    <tr>
                                        <td><?= (int) $index + 1 ?></td>
                                        <td><?= esc($fullName !== '' ? $fullName : ('Admin #' . $adminId)) ?></td>
                                        <td><?= esc($admin['email'] ?? '') ?></td>
                                        <?php foreach (($subjects ?? []) as $key => $label): ?>
                                            <td class="text-center">
                                                <input
                                                    type="checkbox"
                                                    class="form-check-input"
                                                    name="subjects[<?= $adminId ?>][]"
                                                    value="<?= esc($key) ?>"
                                                    <?= !empty($selected[$key]) ? 'checked' : '' ?>
                                                    <?= empty($tableReady) ? 'disabled' : '' ?>
                                                >
                                            </td>
                                        <?php endforeach; ?>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="card-footer text-end">
                <button type="submit" class="btn btn-primary" <?= empty($tableReady) ? 'disabled' : '' ?>>Save Changes</button>
            </div>
        </div>
    </form>
</div>
<?= $this->endSection() ?>

<?= $this->section('scripts') ?>
<script>
    document.addEventListener('DOMContentLoaded', function () {
        if (!window.jQuery || !window.jQuery.fn || !window.jQuery.fn.DataTable) {
            return;
        }
        const selector = '#adminNotificationsTable';
        if (window.jQuery.fn.DataTable.isDataTable(selector)) {
            return;
        }
        window.jQuery(selector).DataTable({
            pageLength: 25,
            order: [[1, 'asc']]
        });
    });
</script>
<?= $this->endSection() ?>
