<?php
session_start();

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

require 'functions.php';

$id = $_GET["id"];

$mhs = query("SELECT * FROM user WHERE id= $id")[0];
$nama = $mhs["gambar"];
$gambar = "img/$nama";

if (file_exists($gambar)) {
    unlink($gambar); // Delete now
}
$aa = file_exists($gambar);

$mhs3 = query("SELECT * FROM keluhan WHERE id_user= $id");
foreach ($mhs3 as $mhs2) {
    $nama1 = $mhs2["gambar1"];
    $nama2 = $mhs2["gambar2"];
    $gambar1 = "imgKeluhan/$nama1";
    $gambar2 = "imgKeluhan/$nama2";

    if (file_exists($gambar1)) {
        unlink($gambar1); // Delete now
    }
    $aaa = file_exists($gambar1);
    if (file_exists($gambar2)) {
        unlink($gambar2); // Delete now
    }
    $aaa2 = file_exists($gambar2);
}
if (hapus($id) > 0 && (!$aa) && (!$aaa) && (!$aaa2)) {
    echo "
            <script>
            alert('data berhasil dihapus!');
            document.location.href = 'Admin_KelolaAkun.php';
            </script>
        ";
} else {
    echo "
            <script>
            alert('data gagal dihapus!!');
            document.location.href = 'Admin_KelolaAkun.php';
            </script>
        ";
}
