<?php
// ✅ Start session early and check login
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header("Location: login.php");
    exit;
}

require_once 'db.php';


// Fetch programs from the database
$query = $pdo->query("SELECT id, title, slot_number, is_new FROM programs ORDER BY slot_number ASC");
$programs = $query->fetchAll(PDO::FETCH_ASSOC);

// ✅ Fetch branding settings
$stmt = $pdo->query("SELECT background_image, logo_image FROM branding WHERE id = 1");
$branding = $stmt->fetch();

// ✅ Define variables
$backgroundImage = $branding['background_image'] ?? '';
$logoImage = $branding['logo_image'] ?? '';



?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Panel</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
body {
    font-family: Arial, sans-serif;
    text-align: center;
    background: #f8f9fa; /* Default background */
    <?php if (!empty($backgroundImage)): ?>
        background-image: url('<?= $backgroundImage; ?>');
        background-repeat: no-repeat;
        background-position: center center;
        background-attachment: fixed;
        background-size: cover;
    <?php endif; ?>
}

.container {
    margin: 0;
    width: 100%;
    background: white;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0px 4px 12px rgba(0,0,0,0.15);
}

table { 
    width: 100%; 
    border-collapse: collapse; 
    margin-top: 20px; 
}

th, td { 
    padding: 10px; 
    border: 1px solid #ddd; 
    text-align: left; 
}

th { 
    background: #007bff; 
    color: white; 
}

.edit-btn { 
    background: #28a745; 
    color: white; 
    padding: 5px 10px; 
    border: none; 
    cursor: pointer; 
    border-radius: 5px; 
}

.edit-btn:hover { 
    background: #218838; 
}

.footer {
    text-align: center;
    padding: 20px;
    font-size: 14px;
    color: white;
    background: rgba(0, 0, 0, 0.6); /* Semi-transparent */
    border-radius: 8px;
    width: 80%;
    margin: 40px auto 10px; /* Ensures spacing from other elements */
    position: relative;
    bottom: 0;
    clear: both; /* Forces it to clear any floating elements */
}

/* Force admin sidebar links to look right on dark background */
.admin-sidebar a {
  color: #fff !important;
  text-decoration: none;
}
.admin-sidebar a:hover {
  text-decoration: underline;
}

    </style>
</head>
<body style="margin:0;">
<div style="display:flex; min-height:100vh;">

  <div class="admin-sidebar" style="width:220px; background:#343a40; padding:20px;">
  <?php include 'google-translate-widget-front-end.php'; ?>
  <?php include 'admin-links.php'; ?>
</div>

<!-- Main Content -->
<div style="flex-grow: 1; padding: 20px;">


<?php if (!empty($logoImage)): ?>
    <div style="text-align: center; padding: 20px;">
        <img src="<?= $logoImage; ?>" alt="Logo" style="max-width: 200px; height: auto;">
    </div>
<?php endif; ?>

<div class="container">

<div style="margin-top: 20px;">
    <a href="admin-branding.php" class="edit-btn">Manage Branding</a>
</div>



    <h2>Admin Panel - Manage Programs</h2>
    
    <a href="add_program.php" style="display: inline-block; padding: 10px 15px; background: #007bff; color: white; border-radius: 5px; text-decoration: none;">+ Add Program</a>


    <table>
        <tr>
<th>Program</th>
<th>Program Name</th>
<th>Actions</th>

        </tr>
        <?php foreach ($programs as $program): ?>
        <tr>
<td><?php echo htmlspecialchars($program['slot_number']); ?></td>
<td>
  <?php echo htmlspecialchars($program['title']); ?>
  <?php if (!empty($program['is_new'])): ?>
    <span style="
      display: inline-block;
      background: linear-gradient(45deg, #ff6b6b, #f06595);
      color: white;
      font-size: 11px;
      font-weight: bold;
      padding: 2px 6px;
      margin-left: 8px;
      border-radius: 6px;
      box-shadow: 0 0 6px rgba(255, 105, 135, 0.7);
      transform: rotate(-5deg);
    ">NEW</span>
  <?php endif; ?>
</td>


<td>
    <a href="edit_program.php?id=<?php echo $program['id']; ?>" class="edit-btn">Edit</a>
    <a href="delete_program.php?id=<?php echo $program['id']; ?>" 
       onclick="return confirm('Are you sure you want to delete this program?');"
       style="background: red; color: white; padding: 5px 10px; border-radius: 5px; text-decoration: none; margin-left: 5px;">
       Delete
    </a>
</td>



        </tr>
        <?php endforeach; ?>
    </table>
</div>


<?php include 'admin-footer.php'; ?>



</div> <!-- Close main-content -->
</body>
</html>
