<?php
session_start();
include "../config/conn.php";

$db = isset($koneksi) ? $koneksi : $conn;
$act = isset($_GET['act']) ? $_GET['act'] : '';

/* ==========================================================
   BAGIAN 1: DATA SEKOLAH
   ========================================================== */
if ($act == "input_sekolah") {
    $kode   = mysqli_real_escape_string($db, $_POST['kode']);
    $nama   = mysqli_real_escape_string($db, $_POST['nama']);
    $alamat = mysqli_real_escape_string($db, $_POST['alamat']);
    
    mysqli_query($db, "INSERT INTO sekolah (kode, nama, alamat) VALUES ('$kode', '$nama', '$alamat')");
    echo "<script>alert('Data Sekolah Berhasil Disimpan'); window.location='../media.php?module=sekolah';</script>";
}
elseif ($act == "edit_sekolah") {
    $id     = mysqli_real_escape_string($db, $_POST['id']);
    $kode   = mysqli_real_escape_string($db, $_POST['kode']);
    $nama   = mysqli_real_escape_string($db, $_POST['nama']);
    $alamat = mysqli_real_escape_string($db, $_POST['alamat']);

    mysqli_query($db, "UPDATE sekolah SET kode='$kode', nama='$nama', alamat='$alamat' WHERE id='$id'");
    echo "<script>alert('Data Sekolah Berhasil Diupdate'); window.location='../media.php?module=sekolah';</script>";
}
elseif ($act == "hapus_sekolah") {
    $id = mysqli_real_escape_string($db, $_GET['id']);
    mysqli_query($db, "DELETE FROM sekolah WHERE id='$id'");
    header("Location: ../media.php?module=sekolah");
}

/* ==========================================================
   BAGIAN 2: DATA SISWA
   ========================================================== */
elseif ($act == "input_siswa") {
    $nis     = mysqli_real_escape_string($db, $_POST['nis']);
    $nama    = mysqli_real_escape_string($db, $_POST['nama']);
    $jk      = mysqli_real_escape_string($db, $_POST['jk']);
    $alamat  = mysqli_real_escape_string($db, $_POST['alamat']);
    $idk     = mysqli_real_escape_string($db, $_POST['kelas']);
    $tlp     = mysqli_real_escape_string($db, $_POST['tlp']);
    $bapak   = mysqli_real_escape_string($db, $_POST['bapak']);
    $ibu     = mysqli_real_escape_string($db, $_POST['ibu']);
    $pass    = md5($_POST['password']); 

    $query = "INSERT INTO siswa (nis, nama, jk, alamat, idk, tlp, bapak, ibu, pass) 
              VALUES ('$nis', '$nama', '$jk', '$alamat', '$idk', '$tlp', '$bapak', '$ibu', '$pass')";
    
    if (mysqli_query($db, $query)) {
        echo "<script>alert('Data Siswa Berhasil Disimpan'); window.location='../media.php?module=siswa&kls=semua';</script>";
    } else {
        echo "<script>alert('Gagal Simpan: ".mysqli_error($db)."'); window.history.back();</script>";
    }
}
elseif ($act == "edit_siswa") {
    $ids     = mysqli_real_escape_string($db, $_POST['id']);
    $nis     = mysqli_real_escape_string($db, $_POST['nis']);
    $nama    = mysqli_real_escape_string($db, $_POST['nama']);
    $jk      = mysqli_real_escape_string($db, $_POST['jk']);
    $alamat  = mysqli_real_escape_string($db, $_POST['alamat']);
    $idk     = mysqli_real_escape_string($db, $_POST['kelas']);
    $tlp     = mysqli_real_escape_string($db, $_POST['tlp']);
    $bapak   = mysqli_real_escape_string($db, $_POST['bapak']);
    $ibu     = mysqli_real_escape_string($db, $_POST['ibu']);

    if (!empty($_POST['password'])) {
        $pass = md5($_POST['password']);
        $query_update = "UPDATE siswa SET nis='$nis', nama='$nama', jk='$jk', alamat='$alamat', idk='$idk', tlp='$tlp', bapak='$bapak', ibu='$ibu', pass='$pass' WHERE ids='$ids'";
    } else {
        $query_update = "UPDATE siswa SET nis='$nis', nama='$nama', jk='$jk', alamat='$alamat', idk='$idk', tlp='$tlp', bapak='$bapak', ibu='$ibu' WHERE ids='$ids'";
    }
    
    if(mysqli_query($db, $query_update)){
        echo "<script>alert('Data Siswa Berhasil Diupdate'); window.location='../media.php?module=siswa&kls=semua';</script>";
    } else {
        echo "<script>alert('Gagal Update: ".mysqli_error($db)."'); window.history.back();</script>";
    }
}
elseif ($act == "hapus_siswa") {
    $ids = mysqli_real_escape_string($db, $_GET['ids']); 
    mysqli_query($db, "DELETE FROM absen WHERE ids='$ids'");
    mysqli_query($db, "DELETE FROM siswa WHERE ids='$ids'");
    echo "<script>alert('Data Siswa Berhasil Dihapus'); window.location='../media.php?module=siswa&kls=semua';</script>";
}

/* ==========================================================
   BAGIAN 3: DATA GURU (SUDAH DIPERBAIKI)
   ========================================================== */
elseif ($act == "input_guru") {
    $nip    = mysqli_real_escape_string($db, $_POST['nip']);
    $nama   = mysqli_real_escape_string($db, $_POST['nama']);
    $jk     = mysqli_real_escape_string($db, $_POST['jk']);
    $alamat = mysqli_real_escape_string($db, $_POST['alamat']);
    $pass   = md5($_POST['password']);

    // Menggunakan kolom 'pass'
    $query = "INSERT INTO guru (nip, nama, jk, alamat, pass) VALUES ('$nip', '$nama', '$jk', '$alamat', '$pass')";
    
    if (mysqli_query($db, $query)) {
        echo "<script>alert('Data Guru Berhasil Disimpan'); window.location='../media.php?module=guru';</script>";
    } else {
        echo "<script>alert('Gagal Simpan Guru: ".mysqli_error($db)."'); window.history.back();</script>";
    }
}
elseif ($act == "edit_guru") {
    $idg    = mysqli_real_escape_string($db, $_POST['idg']);
    $nip    = mysqli_real_escape_string($db, $_POST['nip']);
    $nama   = mysqli_real_escape_string($db, $_POST['nama']);
    $jk     = mysqli_real_escape_string($db, $_POST['jk']);
    $alamat = mysqli_real_escape_string($db, $_POST['alamat']);

    if (!empty($_POST['password'])) {
        $pass = md5($_POST['password']);
        $query_update = "UPDATE guru SET nip='$nip', nama='$nama', jk='$jk', alamat='$alamat', pass='$pass' WHERE idg='$idg'";
    } else {
        $query_update = "UPDATE guru SET nip='$nip', nama='$nama', jk='$jk', alamat='$alamat' WHERE idg='$idg'";
    }

    if (mysqli_query($db, $query_update)) {
        echo "<script>alert('Data Guru Berhasil Diupdate'); window.location='../media.php?module=guru';</script>";
    } else {
        echo "<script>alert('Gagal Update Guru: ".mysqli_error($db)."'); window.history.back();</script>";
    }
}
elseif ($act == "hapus_guru") {
    $idg = $_GET['idg'];
    mysqli_query($db, "DELETE FROM jadwal WHERE idg='$idg'");
    mysqli_query($db, "DELETE FROM guru WHERE idg='$idg'");
    header("Location: ../media.php?module=guru");
}

/* ==========================================================
   BAGIAN 4: DATA KELAS
   ========================================================== */
elseif ($act == "input_kelas") {
    $nama = mysqli_real_escape_string($db, $_POST['nama']);
    mysqli_query($db, "INSERT INTO kelas (nama) VALUES ('$nama')");
    header("Location: ../media.php?module=kelas");
}
elseif ($act == "edit_kelas") {
    $idk  = mysqli_real_escape_string($db, $_POST['idk']);
    $nama = mysqli_real_escape_string($db, $_POST['nama']);
    mysqli_query($db, "UPDATE kelas SET nama='$nama' WHERE idk='$idk'");
    header("Location: ../media.php?module=kelas");
}
elseif ($act == "hapus_kelas") {
    $idk = mysqli_real_escape_string($db, $_GET['idk']);
    mysqli_query($db, "DELETE FROM kelas WHERE idk='$idk'");
    header("Location: ../media.php?module=kelas");
}

/* ==========================================================
   BAGIAN 5: DATA MATA PELAJARAN
   ========================================================== */
elseif ($act == "input_mapel") {
    $nama_mp = mysqli_real_escape_string($db, $_POST['nama_mp']);
    mysqli_query($db, "INSERT INTO mata_pelajaran (nama_mp) VALUES ('$nama_mp')");
    header("Location: ../media.php?module=mata_pelajaran");
}
elseif ($act == "edit_mapel") {
    $idm     = mysqli_real_escape_string($db, $_POST['idm']);
    $nama_mp = mysqli_real_escape_string($db, $_POST['nama_mp']);
    mysqli_query($db, "UPDATE mata_pelajaran SET nama_mp='$nama_mp' WHERE idm='$idm'");
    header("Location: ../media.php?module=mata_pelajaran");
}
elseif ($act == "hapus_mapel") {
    $idm = mysqli_real_escape_string($db, $_GET['idm']);
    mysqli_query($db, "DELETE FROM mata_pelajaran WHERE idm='$idm'");
    header("Location: ../media.php?module=mata_pelajaran");
}

/* ==========================================================
   BAGIAN 6: JADWAL PELAJARAN
   ========================================================== */
elseif ($act == "input_jadwal") {
    if (empty($_POST['guru']) || empty($_POST['kelas']) || empty($_POST['pelajaran']) || empty($_POST['hari'])) {
        echo "<script>alert('Gagal: Data tidak boleh kosong!'); window.history.back();</script>";
        exit();
    }
    $hari    = $_POST['hari'];
    $mulai   = $_POST['jam_mulai'];
    $selesai = $_POST['jam_selesai'];
    $id_guru = $_POST['guru'];
    $id_kelas= $_POST['kelas'];
    $id_mapel= $_POST['pelajaran'];

    $query = "INSERT INTO jadwal (idh, jam_mulai, jam_selesai, idg, idk, idm) 
              VALUES ('$hari', '$mulai', '$selesai', '$id_guru', '$id_kelas', '$id_mapel')";
    mysqli_query($db, $query);
    header("Location: ../media.php?module=senin&status=sukses");
}
elseif ($act == "hapus_jadwal") {
    $idj = $_GET['idj'];
    mysqli_query($db, "DELETE FROM jadwal WHERE idj='$idj'");
    echo "<script>alert('Jadwal Berhasil Dihapus'); window.history.back();</script>";
}

/* ==========================================================
   BAGIAN 7: ABSENSI
   ========================================================== */
elseif ($act == "input_absen") {
    $idj = isset($_POST['idj']) ? $_POST['idj'] : $_GET['idj'];
    $tgl = isset($_POST['tanggal']) ? $_POST['tanggal'] : $_GET['tanggal'];
    
    if (isset($_POST['ket']) && is_array($_POST['ket'])) {
        foreach ($_POST['ket'] as $ids => $status) {
            $ids = mysqli_real_escape_string($db, $ids);
            $status = mysqli_real_escape_string($db, $status);
            $cek = mysqli_query($db, "SELECT * FROM absen WHERE ids='$ids' AND idj='$idj' AND tgl='$tgl'");
            if (mysqli_num_rows($cek) > 0) {
                mysqli_query($db, "UPDATE absen SET ket='$status' WHERE ids='$ids' AND idj='$idj' AND tgl='$tgl'");
            } else {
                mysqli_query($db, "INSERT INTO absen (idj, ids, tgl, ket) VALUES ('$idj', '$ids', '$tgl', '$status')");
            }
        }
        echo "<script>alert('Absensi Berhasil Disimpan!'); window.location='../media.php?module=jadwal_mengajar';</script>";
    } else {
        echo "<script>alert('Gagal: Data Absen Kosong!'); window.history.back();</script>";
    }
}
?>