<?php
include __DIR__ . '/../components/db_connect.php';
include_once __DIR__ . '/../components/functions.php';
require '../components/header.php';

// Initialize variables
$teledramas = [];
$total_items = 0;
$total_pages = 0;

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if ($page < 1) $page = 1;

$items_per_page = 16; // Increased for fluid wide-screen layouts
$offset = ($page - 1) * $items_per_page;

try {
    if ($conn instanceof PDO) {
        $total_stmt = $conn->query("SELECT COUNT(*) FROM teledramas");
        $total_items = $total_stmt->fetchColumn();

        $stmt = $conn->prepare("SELECT * FROM teledramas 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();
        $teledramas = $stmt->fetchAll(PDO::FETCH_ASSOC);
    } elseif ($conn instanceof mysqli) {
        $result = $conn->query("SELECT COUNT(*) as total FROM teledramas");
        $row = $result->fetch_assoc();
        $total_items = $row['total'] ?? 0;

        $stmt = $conn->prepare("SELECT * FROM teledramas 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()) { $teledramas[] = $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>Teledramas | Swarnawahini</title>
    <base href="https://www.swarnavahini.lk/">
    <style>
        * { 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; 
            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 {
            text-align: center;
            margin-bottom: 40px;
            font-size: 2.5rem;
            text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
        }

        /* --- DYNAMIC GRID --- */
        .teledrama-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            gap: 25px;
            width: 100%;
        }

        .card {
            background: rgba(255, 255, 255, 0.08);
            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;
        }

        .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;
            display: block;
        }

        .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; list-style: none; }
        .pagination .page-link {
            color: #1A237E;
            background-color: rgba(255, 255, 255, 0.8);
            padding: 10px 18px;
            border-radius: 50px;
            text-decoration: none;
            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; cursor: not-allowed; }

        @media (max-width: 768px) {
            .container.ts { padding: 15px; width: 98%; }
            .teledrama-grid { grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
        }
        @media (max-width: 480px) {
            .teledrama-grid { grid-template-columns: 1fr; }
        }
    </style>
</head>
<body>
    <div class="container ts">
        <div class="teledrama-container">
            <h1 class="page-title"><?= isset($page_title) ? $page_title : 'Teledramas'; ?></h1>
            
            <div class="teledrama-grid">
                <?php if (!empty($teledramas)): ?>
                    <?php foreach ($teledramas as $row): ?>
                        <?php
                            $link = !empty($row['slug']) ? "teledramas/" . $row['slug'] : "teledrama_detail.php?id=" . $row['id'];
                        ?>
                        <div class="card">
                            <img src="<?= htmlspecialchars($row['thumbnail']); ?>" class="card-img-top" alt="<?= htmlspecialchars($row['title']); ?>">
                            <div class="card-body">
                                <!--<p class="card-text"><?= htmlspecialchars($row['description']); ?></p>-->
                                <a href="<?= htmlspecialchars($link) ?>" class="btn-primary">
                                    <?= htmlspecialchars($row['title']) ?>
                                </a>
                            </div>
                        </div>
                    <?php endforeach; ?>
                <?php else: ?>
                    <p style="text-align: center; grid-column: 1/-1;">No teledramas found.</p>
                <?php endif; ?>
            </div>

            <?php if ($total_pages > 1): ?>
                <ul class="pagination">
                    <li class="page-item <?= $page <= 1 ? 'disabled' : ''; ?>">
                        <a class="page-link" href="?page=<?= $page - 1; ?>">«</a>
                    </li>
                    <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                        <li class="page-item <?= $page === $i ? 'active' : ''; ?>">
                            <a class="page-link" href="?page=<?= $i; ?>"><?= $i; ?></a>
                        </li>
                    <?php endfor; ?>
                    <li class="page-item <?= $page >= $total_pages ? 'disabled' : ''; ?>">
                        <a class="page-link" href="?page=<?= $page + 1; ?>">»</a>
                    </li>
                </ul>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>