<?php
require_once __DIR__ . '/../includes/config.php';
$me = require_login('internal');

$pdo = db();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    csrf_check_or_die();
    $action = $_POST['action'] ?? '';
    if ($action === 'mark_read') {
        $id = (int) input('id');
        $pdo->prepare('UPDATE notifications SET is_read=1, read_at=NOW() WHERE id=? AND user_id=?')->execute([$id, $me['id']]);
    } elseif ($action === 'mark_all_read') {
        $pdo->prepare('UPDATE notifications SET is_read=1, read_at=NOW() WHERE user_id=? AND is_read=0')->execute([$me['id']]);
        flash('success', 'All notifications marked read.');
    } elseif ($action === 'delete') {
        $id = (int) input('id');
        $pdo->prepare('DELETE FROM notifications WHERE id=? AND user_id=?')->execute([$id, $me['id']]);
    }
    redirect('/admin/notifications.php');
}

$rows = $pdo->prepare('SELECT * FROM notifications WHERE user_id=? ORDER BY created_at DESC LIMIT 200');
$rows->execute([$me['id']]);
$rows = $rows->fetchAll();

$pageTitle = 'Notifications';
require __DIR__ . '/../includes/header.php';
?>
<div class="page-header"><div class="container-xl">
    <div class="row align-items-center">
        <div class="col"><h2 class="page-title">Notifications</h2><div class="text-secondary">In-app notifications.</div></div>
        <div class="col-auto">
            <form method="POST" style="display:inline">
                <?= csrf_field() ?><input type="hidden" name="action" value="mark_all_read">
                <button class="btn"><i class="ti ti-checks me-1"></i>Mark all read</button>
            </form>
        </div>
    </div>
</div></div>
<div class="page-body"><div class="container-xl"><div class="card">
<table class="table card-table">
    <tbody>
    <?php if (!$rows): ?><tr><td class="text-center text-secondary py-4">No notifications.</td></tr>
    <?php else: foreach ($rows as $n): ?>
        <tr class="<?= $n['is_read']?'':'table-active' ?>">
            <td style="width:24px"><?php if (!$n['is_read']): ?><span class="status-dot status-dot-animated bg-blue"></span><?php endif; ?></td>
            <td>
                <strong><?= e($n['title']) ?></strong>
                <div class="text-secondary"><?= e($n['message']) ?></div>
                <div class="small text-secondary mt-1"><?= e($n['created_at']) ?> &middot; <span class="badge bg-secondary-lt"><?= e($n['type']) ?></span></div>
            </td>
            <td class="w-1">
                <?php if ($n['link']): $href = preg_match('~^https?://~i', $n['link']) ? $n['link'] : url($n['link']); ?><a href="<?= e($href) ?>" class="btn btn-sm btn-ghost-primary" title="Open"><i class="ti ti-arrow-right"></i></a><?php endif; ?>
                <?php if (!$n['is_read']): ?>
                    <form method="POST" style="display:inline"><?= csrf_field() ?><input type="hidden" name="action" value="mark_read"><input type="hidden" name="id" value="<?= $n['id'] ?>">
                        <button class="btn btn-sm btn-ghost-secondary" title="Mark read"><i class="ti ti-check"></i></button>
                    </form>
                <?php endif; ?>
                <form method="POST" style="display:inline" onsubmit="return confirm('Delete?');"><?= csrf_field() ?><input type="hidden" name="action" value="delete"><input type="hidden" name="id" value="<?= $n['id'] ?>">
                    <button class="btn btn-sm btn-ghost-danger"><i class="ti ti-trash"></i></button>
                </form>
            </td>
        </tr>
    <?php endforeach; endif; ?>
    </tbody>
</table>
</div></div></div>
<?php require __DIR__ . '/../includes/footer.php'; ?>
