<?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 == 'seting') {
   
    $data = [
        'vote' => $_POST['vote'],
	'realtime' => $_POST['real']   
    ];
	
  
	$exec = update($koneksi, 'menu', $data, ['id_menu' => 1]);
   
}
if ($pg == 'ambil_kelas') {
    $level = $_POST['level'];
    $sql = mysqli_query($koneksi, "SELECT * FROM siswa WHERE level='" . $level . "' GROUP BY id_kelas");
    echo "<option value=''>Pilih Kelas</option>";
   while ($data = mysqli_fetch_array($sql)) {
        echo "<option value='$data[id_kelas]'>$data[id_kelas]</option>";
    }
}
if ($pg == 'ambil_siswa') {
    $kelas = $_POST['kelas'];
    $sql = mysqli_query($koneksi, "SELECT * FROM siswa WHERE id_kelas='" . $kelas . "' ");
    echo "<option value=''>Pilih Siswa</option>";
   while ($data = mysqli_fetch_array($sql)) {
        echo "<option value='$data[id_siswa]'>$data[nama]</option>";
    }
}
if ($pg == 'kandidat') {
	$ids = $_POST['kandidat'];
	$data = [
        'idsiswa' => $_POST['kandidat'],
        'nomor' => $_POST['nomor'],
        'visi' => $_POST['visi'],
        'misi' => $_POST['misi'],
		];
	 $exec = insert($koneksi, 'kandidat', $data);
	
    if ($exec) {
        $ektensi = ['jpg', 'png','jpeg'];
        if ($_FILES['gambar']['name'] <> '') {
            $gambar = $_FILES['gambar']['name'];
            $temp = $_FILES['gambar']['tmp_name'];
            $ext = explode('.', $gambar);
            $ext = end($ext);
            if (in_array($ext, $ektensi)) {
                $dest = 'foto/kandidat/gambar' . rand(1, 100) . '.' . $ext;
                $upload = move_uploaded_file($temp, '../../' . $dest);
                if ($upload) {
                    $exec = update($koneksi, 'kandidat', ['gambar' => $dest], ['idsiswa' => $ids]);
                } else {
                    echo "gagal";
                }
            }
        }
     }
	 
}
if ($pg == 'hapus') {
    $id = $_POST['id'];
	$query = "SELECT * FROM kandidat WHERE id='".$id."'";
		$sql = mysqli_query($koneksi, $query); 
		$data = mysqli_fetch_array($sql);

		if(is_file("../../".$data['gambar'])) 
			unlink("../../".$data['gambar']); 
    delete($koneksi, 'kandidat', ['id' => $id]);
}
