<?php 
include 'template/header.php'; 

$p_query = isset($_GET['p']) ? mysqli_real_escape_string($koneksi, $_GET['p']) : null;
?>

<?php if ($mobile): ?>
    <div style="margin-top: 50px;">
        <?php if (empty($p_query)): ?>
            <div style="padding: 20px 0; background-color:var(--secondary-color);">
                <div style="width: 90%; margin: 0 auto 10px auto; color: var(--primary-color); font-family: Teko-SemiBold; text-align: center; font-size: 24px;">PROMO</div>
                <table width="100%" style="margin: 20px auto;">
                    <tbody>
                        <?php
                        $promo = mysqli_query($koneksi, "SELECT * FROM promosi ORDER BY id_promosi DESC");
                        while ($d = mysqli_fetch_assoc($promo)): ?>
                            <tr style="height: 150px; background-color: var(--secondary-color); display: flex; margin-bottom: 6px; padding: 2% 5%; align-items: center;">
                                <td style="width: 50%; padding-right: 10px;">
                                    <div style="font-weight: 500; font-size: 13px;"><?= htmlspecialchars($d['judul_promosi']); ?></div>
                                    <div style="margin-top: 15px;">
                                        <a href="promo.php?p=<?= $d['id_promosi'] ?>" style="background-color: var(--primary-color); color: #001739; padding: 5px 16px; border-radius: 12px; text-decoration:none; font-size: 12px;">INFO DETAIL</a>
                                    </div>
                                </td>
                                <td style="width: 50%;">
                                    <img src="assets/img/<?= $d['gambar_promosi'] ?>" style="width: 100%; border-radius: 4px;">
                                </td>
                            </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        <?php else: 
            $p_get = mysqli_query($koneksi, "SELECT * FROM promosi WHERE id_promosi = '$p_query'");
            if ($p_get && mysqli_num_rows($p_get) > 0):
                $item = mysqli_fetch_assoc($p_get);
        ?>
            <div style="padding-bottom: 40px; background-color: var(--secondary-color); color: #fff;">
                <img src="assets/img/<?= $item['gambar_promosi'] ?>" style="width: 100%;">
                <div style="font-weight: bold; font-size: 18px; padding: 15px 20px; text-align: center; color: var(--primary-color);"><?= htmlspecialchars($item['judul_promosi']); ?></div>
                <div style="font-size: 14px; width: 90%; margin: 0 auto; line-height: 1.6; text-align: justify;">
                    <?= nl2br($item['deskripsi_promosi']); ?>
                </div>
                <div style="margin-top: 20px; text-align: center;">
                    <a href="promo.php" style="color: var(--primary-color); text-decoration: none;">&larr; Kembali</a>
                </div>
            </div>
        <?php endif; endif; ?>
    </div>
<?php else: ?>
    <div style="margin-top: 170px;">
        <div class="background-top" style="padding: 30px 0 50px 0;">
            <div style="width: 95%; margin: 0 auto; max-width: 1280px;">
                <?php if (empty($p_query)): ?>
                    <div style="margin-bottom: 20px; color: var(--primary-color); font-family: Teko-SemiBold; font-size: 24px; text-align: center;">PROMO</div>
                    <div style="background-color: var(--tertiary-color); padding: 20px;">
                        <ul style="list-style-type: none; padding: 0; display: flex; flex-wrap: wrap;">
                            <?php
                            $promo = mysqli_query($koneksi, "SELECT * FROM promosi ORDER BY id_promosi DESC");
                            while ($d = mysqli_fetch_assoc($promo)): ?>
                                <li style="background-color: var(--secondary-color); margin: 1%; width: 23%; border-radius: 8px; overflow: hidden; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
                                    <img src="assets/img/<?= $d['gambar_promosi'] ?>" style="width: 100%; height: 200px; object-fit: cover;">
                                    <div style="padding: 15px;">
                                        <div style="color: #fff; margin-bottom: 10px; font-weight:bold;"><?= htmlspecialchars($d['judul_promosi']); ?></div>
                                        <a href="promo.php?p=<?= $d['id_promosi'] ?>" style="background-color: var(--primary-color); color: #000; display:block; text-align:center; padding: 8px; border-radius: 20px; text-decoration: none; font-size: 14px;">Detail</a>
                                    </div>
                                </li>
                            <?php endwhile; ?>
                        </ul>
                    </div>
                <?php else: 
                    $p_get = mysqli_query($koneksi, "SELECT * FROM promosi WHERE id_promosi = '$p_query'");
                    if ($p_get && mysqli_num_rows($p_get) > 0):
                        $item = mysqli_fetch_assoc($p_get);
                ?>
                    <div style="margin-bottom: 20px;"><a href="promo.php" style="color: var(--primary-color); text-decoration: none;">&larr; Kembali</a></div>
                    <div style="background-color: var(--secondary-color); padding: 30px; color: #fff;">
                        <img src="assets/img/<?= $item['gambar_promosi'] ?>" style="max-width: 100%; height: auto; border-radius: 8px;">
                        <h1 style="color: var(--primary-color);"><?= htmlspecialchars($item['judul_promosi']); ?></h1>
                        <div style="line-height: 1.8; font-size: 16px; text-align: justify;">
                            <?= nl2br($item['deskripsi_promosi']); ?>
                        </div>
                    </div>
                <?php endif; endif; ?>
            </div>
        </div>
    </div>
<?php endif; ?>
<?php include 'template/footer.php'; ?>