<?php
function nb_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}

$conference = $conference ?? [];
$roles = $roles ?? [];
$recent = $recent ?? [];

$success = $_SESSION['flash_success'] ?? '';
$error = $_SESSION['flash_error'] ?? '';
unset($_SESSION['flash_success'], $_SESSION['flash_error']);
?>

<div class="container-fluid py-3">

    <div class="card border-0 shadow-sm mb-4" style="border-radius:24px;background:linear-gradient(135deg,#0f172a,#1d4ed8);color:#fff;">
        <div class="card-body p-4">
            <h3 class="fw-bold mb-1">
                <i class="fa fa-bullhorn"></i>
                Notification Broadcast
            </h3>
            <div class="opacity-75">
                Send in-app notifications to conference users.
            </div>
            <div class="mt-2">
                <span class="badge bg-light text-dark">
                    <?= nb_h(($conference['short_name'] ?? '') . ' / #' . ($conference['id'] ?? '')) ?>
                </span>
                <span class="badge bg-warning text-dark">
                    <?= nb_h($conference['org_name'] ?? ('Org #' . ($conference['org_id'] ?? ''))) ?>
                </span>
            </div>
        </div>
    </div>

    <?php if ($success): ?>
        <div class="alert alert-success"><?= nb_h($success) ?></div>
    <?php endif; ?>

    <?php if ($error): ?>
        <div class="alert alert-danger"><?= nb_h($error) ?></div>
    <?php endif; ?>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body p-4">
            <h5 class="fw-bold mb-3">Send Broadcast Notification</h5>

            <form method="post" action="/easychair/admin/notification-broadcast/send" class="row g-3">

                <div class="col-md-3">
                    <label class="form-label fw-bold">Target</label>
                    <select name="target_mode" class="form-select" onchange="document.querySelectorAll('.target-extra').forEach(e=>e.style.display='none'); document.querySelector('.target-'+this.value)?.style.setProperty('display','block');">
                        <option value="role">Role</option>
                        <option value="email">Specific Email</option>
                        <option value="all">All Conference Users</option>
                    </select>
                </div>

                <div class="col-md-3 target-extra target-role">
                    <label class="form-label fw-bold">Role</label>
                    <select name="role_code" class="form-select">
                        <?php foreach ($roles as $r): ?>
                            <option value="<?= nb_h($r['code']) ?>">
                                <?= nb_h(($r['name'] ?? '') . ' / ' . ($r['code'] ?? '')) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-md-3 target-extra target-email" style="display:none;">
                    <label class="form-label fw-bold">User Email</label>
                    <input name="email" class="form-control" placeholder="user@example.com">
                </div>

                <div class="col-md-6">
                    <label class="form-label fw-bold">Optional Link</label>
                    <select class="form-select mb-1" onchange="document.getElementById('link_url').value=this.value;">
                        <option value="">No link</option>
                        <option value="/easychair/notifications">My Notifications</option>
                        <option value="/easychair/author/my-submissions">Author My Submissions</option>
                        <option value="/easychair/reviewer/dashboard">Reviewer Dashboard</option>
                        <option value="/easychair/admin/submission-search">Admin Submission Search</option>
                        <option value="/easychair/admin/conference-mail">Conference Mail Queue</option>
                    </select>
                    <input id="link_url" name="link_url" class="form-control" placeholder="/easychair/...">
                </div>

                <div class="col-md-12">
                    <label class="form-label fw-bold">Title</label>
                    <input name="title" class="form-control" required placeholder="Important conference update">
                </div>

                <div class="col-md-12">
                    <label class="form-label fw-bold">Body</label>
                    <textarea name="body" rows="6" class="form-control" required placeholder="Write the notification message..."></textarea>
                </div>

                <div class="col-md-12 text-end">
                    <button class="btn btn-success btn-lg rounded-pill px-5"
                            onclick="return confirm('Send this notification to selected recipients?')">
                        <i class="fa fa-paper-plane"></i>
                        Send Broadcast
                    </button>
                </div>
            </form>
        </div>
    </div>

    <div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body p-4">
            <h5 class="fw-bold mb-3">Recent Broadcasts</h5>

            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                    <tr>
                        <th>ID</th>
                        <th>User</th>
                        <th>Title</th>
                        <th>Read</th>
                        <th>Link</th>
                        <th>Date</th>
                    </tr>
                    </thead>

                    <tbody>
                    <?php foreach ($recent as $n): ?>
                        <tr>
                            <td><?= (int)$n['id'] ?></td>
                            <td>
                                <?= nb_h(trim($n['user_name'] ?? '') ?: ($n['email'] ?? '')) ?>
                                <div class="text-muted small"><?= nb_h($n['email'] ?? '') ?></div>
                            </td>
                            <td>
                                <strong><?= nb_h($n['title'] ?? '') ?></strong>
                                <div class="text-muted small"><?= nb_h(mb_substr((string)($n['body'] ?? ''), 0, 120)) ?></div>
                            </td>
                            <td>
                                <span class="badge <?= empty($n['is_read']) ? 'bg-danger' : 'bg-success' ?>">
                                    <?= empty($n['is_read']) ? 'Unread' : 'Read' ?>
                                </span>
                            </td>
                            <td><?= nb_h($n['link_url'] ?? '') ?></td>
                            <td><?= nb_h($n['created_at'] ?? '') ?></td>
                        </tr>
                    <?php endforeach; ?>

                    <?php if (!$recent): ?>
                        <tr><td colspan="6" class="text-center text-muted py-4">No broadcast notifications yet.</td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>

        </div>
    </div>

</div>
