<?php
session_start();
header('Content-Type: application/json');
require_once '../config/auth.php';
cek_role('admin_dpw');
include '../config/database.php';

$db = new Database();
$conn = $db->getConnection();

$id = $_POST['id'];

// Hapus file
$old = $conn->prepare("SELECT file_bagan FROM struktur_organisasi WHERE id =?");
$old->execute([$id]);
$file = $old->fetchColumn();
if($file && file_exists('../assets/img/struktur/'.$file)){
    unlink('../assets/img/struktur/'.$file);
}

$stmt = $conn->prepare("DELETE FROM struktur_organisasi WHERE id =?");
if($stmt->execute([$id])){
    echo json_encode(['status'=>'success','message'=>'Data struktur berhasil dihapus']);
} else {
    echo json_encode(['status'=>'error','message'=>'Gagal hapus data']);
}
?>