<?php
session_start();
include '../koneksi.php';

if(!isset($_SESSION['admin'])){
    header("Location: login.php");
    exit();
}

if(isset($_GET['id'])){
    $id = mysqli_real_escape_string($koneksi, $_GET['id']);
    
    // Ambil nama file video
    $query = mysqli_query($koneksi, "SELECT video FROM arsip_video WHERE id = '$id'");
    $data = mysqli_fetch_array($query);
    
    if($data){
        $file_video = "../img/video/" . $data['video'];
        
        // Hapus file fisik
        if(file_exists($file_video)){
            unlink($file_video);
        }
        
        // Hapus data dari database
        $hapus = mysqli_query($koneksi, "DELETE FROM arsip_video WHERE id = '$id'");
        
        if($hapus){
            echo "<script>
                alert('✅ Video berhasil dihapus!');
                window.location='tambah_video.php';
            </script>";
        } else {
            echo "<script>
                alert('❌ Gagal menghapus video!');
                window.location='tambah_video.php';
            </script>";
        }
    } else {
        echo "<script>
            alert('❌ Video tidak ditemukan!');
            window.location='tambah_video.php';
        </script>";
    }
} else {
    header("Location: tambah_video.php");
    exit();
}
?>