<?php 
include 'config.php'; 
if(!isset($_SESSION['user_id']) || $_SESSION['user_role'] != 'SuperAdmin') { 
    header("Location: dashboard.php"); exit(); 
}

// Fetch all previous announcements
$announcements = $conn->query("SELECT * FROM announcements ORDER BY id DESC");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Broadcast Announcements | Super Admin</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root { --primary: #4e73df; --secondary: #1e293b; --bg-color: #f8f9fc; --sidebar-width: 260px; }
        * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Poppins', sans-serif; }
        body { background-color: var(--bg-color); display: flex; }
        
        .sidebar { width: var(--sidebar-width); background: var(--secondary); height: 100vh; position: fixed; color: #fff; padding: 20px 0; }
        .nav-link { display: flex; align-items: center; padding: 12px 15px; color: rgba(255,255,255,0.7); text-decoration: none; border-radius: 8px; margin: 5px 20px; }
        .active { background: rgba(255,255,255,0.1); color: #fff; }
        
        .main-content { margin-left: var(--sidebar-width); width: calc(100% - var(--sidebar-width)); padding: 40px; }
        .card { background: #fff; padding: 30px; border-radius: 15px; box-shadow: 0 4px 6px rgba(0,0,0,0.02); margin-bottom: 30px; }
        
        input, textarea { width: 100%; padding: 12px; margin: 10px 0; border: 1px solid #e2e8f0; border-radius: 10px; font-size: 14px; }
        label { font-size: 13px; font-weight: 600; color: #64748b; }
        
        .btn-send { background: var(--primary); color: #fff; padding: 12px 25px; border: none; border-radius: 10px; cursor: pointer; font-weight: 600; width: 100%; }
        
        .history-item { background: #f8f9fc; padding: 20px; border-radius: 12px; margin-bottom: 15px; border-left: 5px solid var(--primary); position: relative; }
        .history-date { font-size: 11px; color: #94a3b8; }
        .btn-delete { position: absolute; top: 20px; right: 20px; color: #e74a3b; cursor: pointer; text-decoration: none; }
        .sidebar-brand {
            padding: 0 25px 30px;
            font-size: 20px;
            font-weight: 600;
            display: flex;
            align-items: center;
            color: #fff;
            text-decoration: none;
        }
        .sidebar-brand i { margin-right: 10px; color: var(--primary); }
    </style>
</head>
<body>

    <div class="sidebar">
        <a href="#" class="sidebar-brand">
            <i class="fas fa-plane-departure"></i>
            <span>TRAVEL SAAS</span>
        </a>
        
        <ul class="nav-menu">
    <?php if($_SESSION['user_role'] == 'SuperAdmin'): ?>
        <!-- SUPER ADMIN MENU -->
         <li class="nav-item"><a href="dashboard.php" class="nav-link "><i class="fas fa-home"></i> Dashboard</a></li>
        <li class="nav-item"><a href="admin_dashboard.php" class="nav-link "><i class="fas fa-chart-line"></i> SaaS Overview</a></li>
        <li class="nav-item"><a href="manage_agencies.php" class="nav-link"><i class="fas fa-building"></i> Manage Agencies</a></li>
        <li class="nav-item"><a href="saas_revenue.php" class="nav-link"><i class="fas fa-file-invoice-dollar"></i> Revenue & Payments</a></li>
         <li class="nav-item"><a href="generate_invoice.php" class="nav-link" style="color: #1cc88a;"><i class="fas fa-file-invoice-dollar"></i> Generate New Bill</a></li>
         <li class="nav-item"><a href="admin_group_tickets.php" class="nav-link" style="color: #3498db;"><i class="fas fa-plane-arrival"></i> B2B Group Inventory</a></li>
    <li class="nav-item"><a href="broadcast.php" class="nav-link active"><i class="fas fa-bullhorn"></i> Send Announcements</a></li>
    <li class="nav-item"><a href="profile.php" class="nav-link"><i class="fas fa-user-shield"></i> My Profile</a></li>
    <?php else: ?>
        <!-- AGENCY MENU -->
        <li class="nav-item"><a href="dashboard.php" class="nav-link active"><i class="fas fa-home"></i> Dashboard</a></li>
        <li class="nav-item"><a href="clients.php" class="nav-link"><i class="fas fa-users"></i> My Clients</a></li>
        <li class="nav-item"><a href="queries.php" class="nav-link"><i class="fas fa-question-circle"></i> Enquiries</a></li>
        <?php if($_SESSION['user_role'] == 'AgencyOwner'): ?>
    <li class="nav-item"><a href="staff.php" class="nav-link"><i class="fas fa-user-tie"></i> Manage Staff</a></li>
<?php endif; ?>
        <li class="nav-item"><a href="marketplace.php" class="nav-link" style="background: rgba(28, 200, 138, 0.1); color: #1cc88a;"><i class="fas fa-shopping-cart"></i> B2B Marketplace</a></li>
        <li class="nav-item"><a href="umrah_vouchers.php" class="nav-link"><i class="fas fa-kaaba"></i> Umrah Vouchers</a></li>
        <li class="nav-item"><a href="ticket_vouchers.php" class="nav-link"><i class="fas fa-ticket-alt"></i> Ticket Vouchers</a></li>
        <li class="nav-item"><a href="posters.php" class="nav-link"><i class="fas fa-image"></i> Poster Maker</a></li>
        <li class="nav-item"><a href="ledger.php" class="nav-link"><i class="fas fa-wallet"></i> Clients Account</a></li>
        <li class="nav-item">
    <a href="agency_profile.php" class="nav-link" style="background: rgba(78, 115, 223, 0.1); color: #4e73df;">
        <i class="fas fa-cog"></i> Agency Settings
    </a>
</li>
    <?php endif; ?>
</ul>

        <?php if($_SESSION['user_role'] == 'SuperAdmin'): ?>

        <?php endif; ?>

        <div style="position: absolute; bottom: 20px; width: 100%;">
            <li class="nav-item" style="list-style: none;"><a href="logout.php" class="nav-link" style="color: #ff7675;"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
        </div>
    </div>

    <div class="main-content">
        <h1>System Broadcast</h1>
        <p style="color: #64748b; margin-bottom: 30px;">Send a message to all travel agencies. It will appear on their main dashboard.</p>

        <div style="display: grid; grid-template-columns: 1fr 1.5fr; gap: 30px;">
            <!-- Send Form -->
            <div class="card">
                <h3><i class="fas fa-paper-plane"></i> New Announcement</h3>
                <form action="handle_broadcast.php" method="POST">
                    <label>Title / Subject</label>
                    <input type="text" name="title" placeholder="e.g. New Feature Added!" required>
                    
                    <label>Message Content</label>
                    <textarea name="message" rows="5" placeholder="Write your message here..." required></textarea>
                    
                    <button type="submit" class="btn-send">Broadcast to All Agencies</button>
                </form>
            </div>

            <!-- History -->
            <div class="card">
                <h3><i class="fas fa-history"></i> Previous Broadcasts</h3>
                <div style="margin-top: 20px;">
                    <?php while($row = $announcements->fetch_assoc()): ?>
                    <div class="history-item">
                        <a href="handle_broadcast.php?delete=<?php echo $row['id']; ?>" class="btn-delete" onclick="return confirm('Remove this announcement?')"><i class="fas fa-trash"></i></a>
                        <div class="history-date"><?php echo date('d M Y, h:i A', strtotime($row['created_at'])); ?></div>
                        <h4 style="margin: 5px 0;"><?php echo $row['title']; ?></h4>
                        <p style="font-size: 13px; color: #64748b;"><?php echo $row['message']; ?></p>
                    </div>
                    <?php endwhile; ?>
                    <?php if($announcements->num_rows == 0) echo "<p style='color:#94a3b8;'>No messages sent yet.</p>"; ?>
                </div>
            </div>
        </div>
    </div>

</body>
</html>