<?php
require("../../config/koneksi.php");
require("../../config/function.php");
require("../../config/crud.php");
(isset($_GET['pg'])) ? $pg = $_GET['pg'] : $pg = '';

if ($pg == 'karpel') {
	$ektensi = ['jpg', 'png', 'jpeg'];
   $file = $_FILES['file']['name'];
   $temp = $_FILES['file']['tmp_name'];
   $ext = explode('.', $file);
   $ext = end($ext);
   if (in_array($ext, $ektensi)) {
      $dest = '../../dist/img/karpel/';
      $path = $dest . $file;
      $upload = move_uploaded_file($temp, $path);
      if ($upload) {
         $data = array(
            'level' => $_POST['level'],
            'gambar' => $file
         );
		 
		 $where = [
			'level' => $_POST['level']				 
            ];
			 $cek = rowcount($koneksi, 'karpel', $where);
            if ($cek == 0) {
         insert($koneksi, 'karpel', $data);
         echo "OK";
			}
   }
 }
}
if ($pg == 'hapus') {
    $kode = $_POST['id'];
$query = "SELECT * FROM karpel WHERE id='".$kode."'";
		$sql = mysqli_query($koneksi, $query); 
		$data = mysqli_fetch_array($sql);

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