<?php
if ($c['level'] == "superadmin") {

    if (isset($_GET['id'])) {
        $id = mysqli_real_escape_string($koneksi, $_GET['id']);

        // 1. Ambil data promosi
        $get = mysqli_query($koneksi, "SELECT gambar FROM tb_bonus WHERE id='$id'");
        $data = mysqli_fetch_assoc($get);

        if ($data) {

            // 2. Path file gambar
            $file = "../../uploads/fotopromosi/" . $data['gambar'];

            // 3. Hapus file jika ada
            if (!empty($data['gambar']) && file_exists($file)) {
                unlink($file);
            }

            // 4. Hapus data dari database
            $query = mysqli_query($koneksi, "DELETE FROM tb_bonus WHERE id='$id'");

            if ($query) {
                ?>
                <script>
                    alert('Data & gambar promosi berhasil dihapus');
                    window.location = "?page=promosi";
                </script>
                <?php
            } else {
                ?>
                <script>
                    alert('Gagal menghapus data');
                    window.location = "?page=promosi";
                </script>
                <?php
            }

        } else {
            ?>
            <script>
                alert('Data tidak ditemukan');
                window.location = "?page=promosi";
            </script>
            <?php
        }
    }

} else {
    ?>
    <script>
        window.location = "?page=dashboard";
    </script>
    <?php
}
?>