<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once '../config.php';
include __DIR__ . '/../components/db_connect.php';
include_once __DIR__ . '/../components/functions.php';
require '../components/header.php';

// 1. Initialize variables to prevent "Undefined Variable" notices
$programmes = [];
$total_items = 0;
$total_pages = 0;

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if ($page < 1) {
    $page = 1; 
}
$items_per_page = 12; // Adjusted for fluid wide-screen layouts
$offset = ($page - 1) * $items_per_page;

// Fetch total items and programmes
try {
    $total_stmt = $conn->query("SELECT COUNT(*) FROM programmes");
    $total_items = $total_stmt->fetchColumn();

    $stmt = $conn->prepare("SELECT * FROM programmes ORDER BY sort_order ASC LIMIT :limit OFFSET :offset");
    $stmt->bindValue(':limit', $items_per_page, PDO::PARAM_INT);
    $stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
    $stmt->execute();
    $programmes = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    $total_pages = ceil($total_items / $items_per_page);
} catch (PDOException $e) {
    error_log("Database error in programme_list.php: " . $e->getMessage());
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Programmes | Swarnawahini</title>
    
    <base href="https://www.swarnavahini.lk/">

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        /* Global Reset and Base Styles */
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
        body {
            font-family: 'Poppins', sans-serif;
            background: rgb(25, 52, 134);
            padding: 90px 0 0;
            overflow-x: hidden;
        }

        /* --- FULLY FLUID CONTAINER --- */
        .container.ts {
            width: 95%;
            max-width: none !important; /* Removes 1200px cap for ultra-wide screens */
            margin: 40px auto;
            padding: 30px;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            -webkit-backdrop-filter: blur(10px);
            border-radius: 15px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
            color: white;
            position: relative;
        }

        /* Page Title */
        .page-title {
            text-align: center;
            margin-bottom: 40px;
            font-size: 2.5rem;
            text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
            color: white;
        }

        /* --- DYNAMIC GRID (UNLIMITED COLUMNS) --- */
        .programme-grid {
            display: grid;
            /* Browser fits as many cards as possible based on width (min 280px) */
            grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            gap: 25px;
            width: 100%;
        }

        /* Cards - Unified Glassmorphism Style */
        .card {
            background: rgba(255, 255, 255, 0.08);
            backdrop-filter: blur(20px) saturate(180%);
            -webkit-backdrop-filter: blur(20px) saturate(180%);
            border-radius: 20px;
            border: 1px solid rgba(255, 255, 255, 0.15);
            overflow: hidden;
            transition: all 0.3s ease;
            display: flex;
            flex-direction: column;
            height: 100%; 
        }

        .card:hover {
            transform: translateY(-8px);
            box-shadow: 0 12px 30px rgba(0, 0, 0, 0.4);
            background: rgba(255, 255, 255, 0.12);
        }

        .card-img-top {
            width: 100%;
            height: 220px;
            object-fit: cover; /* Unified poster look */
        }

        .card-body {
            padding: 20px;
            flex-grow: 1; 
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }

        .card-text {
            font-size: 14px;
            color: #ecf0f1;
            margin-bottom: 20px;
            line-height: 1.5;
        }

        .btn-primary {
            text-align: center;
            background-color: #FFC107;
            color: #1A237E;
            padding: 12px;
            border-radius: 12px;
            font-weight: bold;
            text-decoration: none;
            transition: 0.3s;
            border: none;
        }

        .btn-primary:hover {
            background-color: #fff;
            color: #1A237E;
            transform: scale(1.02);
        }

        /* Pagination Styles */
        .pagination { margin-top: 3rem; display: flex; justify-content: center; gap: 0.5rem; }
        .pagination .page-link {
            color: #1A237E;
            background-color: rgba(255, 255, 255, 0.8);
            border: none;
            padding: 10px 18px;
            border-radius: 50px;
            font-weight: bold;
            transition: 0.3s;
        }
        .pagination .page-item.active .page-link { background-color: #FFC107; transform: scale(1.1); }
        .pagination .page-item.disabled .page-link { opacity: 0.5; }

        /* Responsive Breakpoints */
        @media (max-width: 768px) {
            .container.ts { padding: 15px; width: 98%; }
            .page-title { font-size: 1.8rem; }
            .programme-grid { grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
        }

        @media (max-width: 480px) {
            .programme-grid { grid-template-columns: 1fr; } 
        }
    </style>
</head>
<body>
    <div class="container ts">
        <h1 class="page-title">Programmes</h1>
        
        <div class="programme-grid">
            <?php if (!empty($programmes)): ?>
                <?php foreach ($programmes as $row): ?>
                    <?php
                        $thumbnail = !empty($row['cover_image'])
                            ? $row['cover_image']
                            : 'assets/images/default-placeholder.jpg';

                        $link = !empty($row['slug']) ? "programmes/" . $row['slug'] : "programme_detail.php?id=" . $row['id'];
                    ?>
                    <div class="card">
                        <img src="<?php echo htmlspecialchars($thumbnail); ?>"
                             class="card-img-top"
                             alt="<?php echo htmlspecialchars($row['title']); ?>">

                        <div class="card-body">
                            <!--<p class="card-text"><?php echo htmlspecialchars($row['description']); ?></p>-->
                            <a href="<?= htmlspecialchars($link) ?>" class="btn-primary">
                                <?php echo htmlspecialchars($row['title']); ?>
                            </a>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php else: ?>
                <p style="grid-column: 1/-1; text-align: center;">No programmes available at the moment.</p>
            <?php endif; ?>
        </div>

        <?php if ($total_pages > 1): ?>
            <nav aria-label="Programme pagination" class="mt-4">
                <ul class="pagination justify-content-center">
                    <li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
                        <a class="page-link" href="programmes.php?page=<?php echo $page - 1; ?>">«</a>
                    </li>
                    <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                        <li class="page-item <?php echo $page === $i ? 'active' : ''; ?>">
                            <a class="page-link" href="programmes.php?page=<?php echo $i; ?>"><?php echo $i; ?></a>
                        </li>
                    <?php endfor; ?>
                    <li class="page-item <?php echo $page >= $total_pages ? 'disabled' : ''; ?>">
                        <a class="page-link" href="programmes.php?page=<?php echo $page + 1; ?>">»</a>
                    </li>
                </ul>
            </nav>
        <?php endif; ?>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>