<?= $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-3">
        <div>
            <h1 class="h4 mb-1">Print Notification Recipients</h1>
            <div class="text-muted">Select which administrators should receive alerts whenever a print request changes.</div>
        </div>
        <a href="<?= site_url('administrator/notifications_alerts') ?>" class="btn btn-outline-secondary btn-sm">
            General Notification Subjects
        </a>
    </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 this feature.
        </div>
    <?php endif; ?>

    <form method="post" action="<?= site_url('administrator/print-notifications/save') ?>">
        <?= csrf_field() ?>
        <div class="card">
            <div class="card-header d-flex align-items-center justify-content-between">
                <span>Administrators</span>
                <button type="submit" class="btn btn-primary btn-sm" <?= empty($tableReady) ? 'disabled' : '' ?>>Save</button>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-striped align-middle mb-0">
                        <thead class="table-dark">
                            <tr>
                                <th style="width: 60px;">#</th>
                                <th>Name</th>
                                <th>Email</th>
                                <th class="text-center">Send Print Alerts</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($admins)): ?>
                                <tr>
                                    <td colspan="4" class="text-center text-muted py-4">No administrators found.</td>
                                </tr>
                            <?php else: ?>
                                <?php foreach ($admins as $index => $admin): ?>
                                    <?php
                                        $adminId = (int) ($admin['id'] ?? 0);
                                        $fullName = trim(($admin['firstname'] ?? '') . ' ' . ($admin['lastname'] ?? ''));
                                    ?>
                                    <tr>
                                        <td><?= $index + 1 ?></td>
                                        <td><?= esc($fullName !== '' ? $fullName : ('Admin #' . $adminId)) ?></td>
                                        <td><?= esc($admin['email'] ?? '') ?></td>
                                        <td class="text-center">
                                            <div class="form-check form-switch">
                                                <input
                                                    class="form-check-input"
                                                    type="checkbox"
                                                    name="notify[<?= $adminId ?>]"
                                                    value="1"
                                                    <?= !empty($assigned[$adminId]) ? 'checked' : '' ?>
                                                    <?= empty($tableReady) ? 'disabled' : '' ?>
                                                >
                                            </div>
                                        </td>
                                    </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() ?>
