<?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_proto', ['id_jadwal' => $id]);
	
}
if ($pg == 'editjadwal') {
     $urut = $_POST['urut'];
     $guru = $_POST['guru'];
$data = [

'nourut' =>$urut,
'idguru' => $guru
];

   $idj = $_POST['idj'];
    update($koneksi, 'jadwal_proto',$data,['id_jadwal' => $idj]);
}
if ($pg == 'tambah') {
                $level = $_POST['level'];
				$fase = $_POST['fase'];
				$kelas = $_POST['kelas'];
				$kode = $_POST['kode'];
                $mapel = $_POST['mapel'];
                $urut = $_POST['urut'];
				$idguru = $_POST['guru'];
				
$where = [
'nourut'=>$urut,
'kelas' => $kelas,
'kode' => $kode,
'idguru' => $idguru
];
$data = [
'tingkat' => $level,
'kelas' => $kelas,
'kode' => $kode,
'mapel' => $mapel,
'idguru' => $idguru,
'fase' =>$fase,
'nourut' =>$urut
];
$cekmapel = rowcount($koneksi, 'jadwal_proto', $where);
    if ($cekmapel > 0) {
        echo "Jadwal sudah ada";
    } else {
    $exec = insert($koneksi, 'jadwal_proto', $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");
   
   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>";
    }
}