<?php
// C:\xampp\htdocs\swarnawahini_web\components\programme_list.php

include __DIR__ . '/db_connect.php';
include_once __DIR__ . '/functions.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; // Increased for fluid wide-screen layouts
$offset = ($page - 1) * $items_per_page;

try {
    // DUAL DB SUPPORT (PDO & MySQLi)
    if (isset($conn) && $conn instanceof PDO) {
        $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);
    } elseif (isset($conn) && $conn instanceof mysqli) {
        $result = $conn->query("SELECT COUNT(*) as total FROM programmes");
        $row = $result->fetch_assoc();
        $total_items = $row['total'] ?? 0;

        $stmt = $conn->prepare("SELECT * FROM programmes ORDER BY sort_order ASC LIMIT ? OFFSET ?");
        $stmt->bind_param("ii", $items_per_page, $offset);
        $stmt->execute();
        $result = $stmt->get_result();
        while ($row = $result->fetch_assoc()) {
            $programmes[] = $row;
        }
    }
    $total_pages = ceil($total_items / $items_per_page);
} catch (Exception $e) {
    error_log($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/">

    <style>
        /* Global Reset */
        * { box-sizing: border-box; margin: 0; padding: 0; }
        
        body {
            font-family: 'Poppins', sans-serif;
            background: rgb(25, 52, 134);
            margin: 0;
            padding: 90px 0 0;
            overflow-x: hidden;
        }

        /* --- FULLY FLUID CONTAINER --- */
        .container.ts {
            width: 95%;
            max-width: none !important; /* Matches teledrama fluid fix */
            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; 
        }

        /* Top Right Button */
        .top-right-btn {
            position: absolute;
            top: 20px;
            right: 20px;
            background-color: #FFC107;
            color: #1A237E;
            padding: 8px 20px;
            border-radius: 15px;
            font-weight: bold;
            text-decoration: none;
            transition: all 0.3s ease;
            z-index: 10;
        }
        .top-right-btn:hover {
            background-color: #ff0000;
            color: #fff;
            transform: scale(1.05);
        }

        .page-title {
            text-align: center;
            margin-bottom: 40px;
            font-size: 2.5rem;
            color: white;
        }

        /* --- DYNAMIC GRID (UNLIMITED COLUMNS) --- */
        .programme-grid {
display: grid;
    /* Default for Desktop / 1080p: Exactly 4 columns (2 rows of 4) */
    grid-template-columns: repeat(4, 1fr);
    gap: 25px;
    width: 100%;
        }

        /* Cards - Same Glassmorphism as Teledramas */
        .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: 200px;
            object-fit: cover; /* Prevents distortion, same as teledramas */
        }

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

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

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

        /* Essential Mobile Spacing */
        @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(250px, 1fr)); }
        }

        @media (max-width: 480px) {
            .programme-grid { grid-template-columns: 1fr; }
            .top-right-btn { font-size: 12px; top: 10px; right: 10px; }
        }
    </style>
</head>
<body>
    <div class="container ts">
        <a href="pages/programmes.php" class="top-right-btn">More</a>
        <div class="teledrama-container">
            <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">
                                
                                <a href="<?php echo htmlspecialchars($link); ?>" class="btn-watch">
                                    <?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>
        </div>
    </div>
</body>
</html>