<?php
require_once('../session.php'); // Sesuaikan path jika file session ada di folder admin

if (isset($_GET['cuid'])) {
    $cuid = $_GET['cuid'];

    // 1. Ambil nama file gambar
    // Asumsi: tabel tb_banner memiliki kolom 'gambar'
    $stmt = $conn->prepare("SELECT gambar FROM tb_banner WHERE cuid = ?");
    $stmt->bind_param("s", $cuid);
    $stmt->execute();
    $result = $stmt->get_result();
    $data = $result->fetch_assoc();

    if ($data) {
        $nama_file = $data['gambar'];
        
        // 2. Tentukan path file
        // Karena posisi di /admin/function/, maka naik 2 tingkat ke root (../..) baru masuk /upload/
        $path = '../../upload/' . $nama_file;
        
        // 3. Hapus file jika ada
        if (!empty($nama_file) && file_exists($path)) {
            unlink($path);
        }

        // 4. Hapus data dari database
        $delete_stmt = $conn->prepare("DELETE FROM tb_banner WHERE cuid = ?");
        $delete_stmt->bind_param("s", $cuid);
        $delete_stmt->execute();
    }
}

// 5. Redirect kembali ke halaman daftar banner
header('location:../banner/');
exit;
?>