<?php
require("../../config/koneksi.php");
require("../../config/function.php");
require("../../config/crud.php");
cek_session_admin();
(isset($_GET['pg'])) ? $pg = $_GET['pg'] : $pg = '';
if ($pg == 'hapus_jadwal') {
    $id = $_POST['id'];
	
    delete($koneksi, 'jadwal_mapel', ['id_jadwal' => $id]);
	
}
if ($pg == 'editjadwal') {
	            $idj = $_POST['idj'];
               
				$kelas = $_POST['kelas'];
				$kode = $_POST['kode'];
                $mapel = $_POST['mapel'];
                $hari = $_POST['hari'];
                $dari = $_POST['dari'];
                $sampai = $_POST['sampai'];
				$ke = serialize($_POST['ke']);
				$guru = $_POST['guru'];
$data = [

'kelas' => $kelas,
'kode' => $kode,
'mapel' => $mapel,
'guru' => $guru,
'ke' =>$ke,
'hari' => $hari,
'dari'=> $dari,
'sampai' => $sampai
];
$where = [
'kelas' => $kelas,
'kode' => $kode,
'hari' => $hari,
'ke' => $ke,
'guru' => $guru
];
  $cekmapel = rowcount($koneksi, 'jadwal_mapel', $where);
    if ($cekmapel > 0) {
        echo "Jadwal sudah ada";
    } else {
    update($koneksi, 'jadwal_mapel',$data,['id_jadwal' => $idj]);
	 echo $exec;
	}
}
if ($pg == 'tambah') {
                $level = $_POST['level'];
				$kelas = $_POST['kelas'];
				$kode = $_POST['kode'];
                $mapel = $_POST['mapel'];
                $hari = $_POST['hari'];
                $dari = $_POST['dari'];
                $sampai = $_POST['sampai'];
				$ke = serialize($_POST['ke']);
				$guru = $_POST['guru'];
				
$where = [
'kelas' => $kelas,
'kode' => $kode,
'hari' => $hari,
'ke' => $ke,
'guru' => $guru
];
$data = [
'tingkat' => $level,
'kelas' => $kelas,
'kode' => $kode,
'mapel' => $mapel,
'guru' => $guru,
'ke' =>$ke,
'hari' => $hari,
'dari'=> $dari,
'sampai' => $sampai

];
$cekmapel = rowcount($koneksi, 'jadwal_mapel', $where);
    if ($cekmapel > 0) {
        echo "Jadwal sudah ada";
    } else {
    $exec = insert($koneksi, 'jadwal_mapel', $data);
	  echo $exec;
	   }
}
if ($pg == 'ambil_kelas') {
    $id_level = $_POST['level'];
    $sql = mysqli_query($koneksi, "SELECT * FROM siswa WHERE level='" . $id_level . "' GROUP BY id_kelas");
    echo "<option value=''></option>";
   while ($data = mysqli_fetch_array($sql)) {
        echo "<option value='$data[id_kelas]'>$data[id_kelas]</option>";
    }
}
if ($pg == 'ambil_mapel') {
    $id_kode = $_POST['kode'];
    $sql = mysqli_query($koneksi, "SELECT * FROM mata_pelajaran WHERE kode_mapel='" . $id_kode . "' ");
   
   while ($data = mysqli_fetch_array($sql)) {
        echo "<option value='$data[nama_mapel]'>$data[nama_mapel]</option>";
    }
}