<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once __DIR__ . '/auth.php';

// Handle adding a notification
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'add_notification') {
    $title = trim($_POST['title']);
    $message = trim($_POST['message']);
    
    if (!empty($title) && !empty($message)) {
        $stmt = $pdo->prepare("INSERT INTO notifications (title, message) VALUES (?, ?)");
        $stmt->execute([$title, $message]);
        header("Location: notifications.php?msg=Notification+Sent");
        exit;
    }
}

// Handle deleting a notification
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'delete_notification') {
    $id = intval($_POST['id']);
    $pdo->prepare("DELETE FROM notifications WHERE id = ?")->execute([$id]);
    header("Location: notifications.php?msg=Notification+Deleted");
    exit;
}

// Get recent notifications
$notif_stmt = $pdo->query("SELECT * FROM notifications ORDER BY id DESC LIMIT 50");
$notifications = $notif_stmt ? $notif_stmt->fetchAll(PDO::FETCH_ASSOC) : [];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Notifications â€” PakClub Admin</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
*{box-sizing:border-box;margin:0;padding:0}
body{background:#0b0e18;color:#e8eaf0;font-family:'Segoe UI',sans-serif;display:flex;min-height:100vh}
.main{flex:1;overflow-x:hidden}
.topbar{background:#111827;border-bottom:1px solid rgba(255,255,255,.07);padding:16px 28px;display:flex;align-items:center;justify-content:space-between}
.topbar h2{font-size:20px;font-weight:700;color:#fff}
.content{padding:24px 28px}
.card{background:#1e2130;border:1px solid rgba(255,255,255,.07);border-radius:14px;overflow:hidden;overflow-x:auto;margin-bottom:24px}
.card-header{padding:16px 20px;border-bottom:1px solid rgba(255,255,255,.07);background:#181b28;display:flex;justify-content:space-between;align-items:center}
.card-title{font-size:16px;font-weight:600;color:#fff}
.card-body{padding:20px}

.form-group{margin-bottom:15px}
.form-group label{display:block;margin-bottom:6px;font-size:13px;color:#9ca3af;font-weight:500}
.form-control{width:100%;background:#0f1117;border:1px solid rgba(255,255,255,.1);border-radius:8px;padding:10px 14px;color:#fff;font-size:14px;outline:none;font-family:inherit}
.form-control:focus{border-color:#f26d0f}
textarea.form-control{resize:vertical;min-height:80px}

.grid-2{display:grid;grid-template-columns:1fr 1fr;gap:15px}

.btn{padding:10px 18px;border-radius:10px;border:none;cursor:pointer;font-size:14px;font-weight:600;display:inline-flex;align-items:center;gap:7px;transition:opacity .2s}
.btn:hover{opacity:.85}
.btn-orange{background:#f26d0f;color:#fff}
.btn-red{background:#ef4444;color:#fff}
.btn-sm{padding:6px 12px;font-size:12px;border-radius:7px}

table{width:100%;border-collapse:collapse;font-size:13px;min-width:600px;}
th{padding:11px 16px;color:#4b5563;font-size:11px;text-transform:uppercase;letter-spacing:.7px;border-bottom:1px solid rgba(255,255,255,.06);text-align:left;font-weight:600}
td{padding:12px 16px;border-bottom:1px solid rgba(255,255,255,.04);color:#c9ccd6;vertical-align:middle}
tr:last-child td{border-bottom:none}
tr:hover td{background:rgba(255,255,255,.02)}

.alert{padding:15px;background:rgba(34,197,94,.15);color:#22c55e;border-radius:10px;margin-bottom:20px;border:1px solid rgba(34,197,94,.3)}
</style>
</head>
<body>
<?php include 'sidebar.php'; ?>
<div class="main">
    <div class="topbar">
        <h2><i class="fa-solid fa-bell" style="color:#f26d0f;margin-right:10px"></i>Manage Notifications</h2>
    </div>
    
    <div class="content">
        <?php if (isset($_GET['msg'])): ?>
            <div class="alert"><i class="fa-solid fa-check-circle"></i> <?php echo htmlspecialchars($_GET['msg']); ?></div>
        <?php endif; ?>

        <div class="card" style="max-width: 600px;">
            <div class="card-header">
                <div class="card-title">Send New Notification</div>
            </div>
            <div class="card-body">
                <form method="POST">
                    <input type="hidden" name="action" value="add_notification">
                    <div class="form-group">
                        <label>Title</label>
                        <input type="text" class="form-control" name="title" required placeholder="e.g. System Update">
                    </div>
                    <div class="form-group">
                        <label>Message</label>
                        <textarea class="form-control" name="message" rows="3" required placeholder="Type your message here..."></textarea>
                    </div>
                    <button type="submit" class="btn btn-orange"><i class="fa-solid fa-paper-plane"></i> Send Notification</button>
                </form>
            </div>
        </div>

        <div class="card">
            <div class="card-header">
                <div class="card-title">Recent Notifications</div>
            </div>
            <table>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Title</th>
                        <th>Message</th>
                        <th>Sent At</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($notifications as $n): ?>
                    <tr>
                        <td><?php echo $n['id']; ?></td>
                        <td style="font-weight:600;color:#fff"><?php echo htmlspecialchars($n['title']); ?></td>
                        <td style="max-width:300px;white-space:normal;color:#9ca3af"><?php echo htmlspecialchars($n['message']); ?></td>
                        <td><?php echo $n['created_at']; ?></td>
                        <td>
                            <form method="POST" style="margin:0;">
                                <input type="hidden" name="action" value="delete_notification">
                                <input type="hidden" name="id" value="<?php echo $n['id']; ?>">
                                <button type="submit" class="btn btn-red btn-sm" onclick="return confirm('Are you sure you want to delete this notification?');"><i class="fa-solid fa-trash"></i> Delete</button>
                            </form>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if(empty($notifications)): ?>
                    <tr><td colspan="5" style="text-align:center;padding:30px;color:#9ca3af">No notifications found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

    </div>
</div>
</body>
</html>