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


if ($pg == 'sarana') {
	
	$data = [
			'jenis' => $_POST['jenis'],
			'kode' => $_POST['kode'],
			  'nama'=>$_POST['nama'],
			  'noreg'=>$_POST['noreg'],
			  'lokasi'=>$_POST['lokasi'],
			  'kondisi'=>$_POST['kondisi'],
			   'keperluan'=>$_POST['keperluan']
            ];	
	
            $exec = insert($koneksi, 'sarana', $data);
            echo "OK";
}

if ($pg == 'editsarana') {
	$id= $_POST['ids'];
 
	$data = [
			'jenis' => $_POST['jenis'],
			'kode' => $_POST['kode'],
			  'nama'=>$_POST['nama'],
			  'noreg'=>$_POST['noreg'],
			  'lokasi'=>$_POST['lokasi'],
			  'kondisi'=>$_POST['kondisi'],
			   'keperluan'=>$_POST['keperluan']
            ];	
	
            $exec = update($koneksi, 'sarana', $data,['id_sarana'=>$id]);
            echo "OK";
        	
}
if ($pg == 'hapus') {
	$id= $_POST['id'];
 $query = "SELECT * FROM sarana WHERE id_sarana='".$id."'";
		$sql = mysqli_query($koneksi, $query); 
		$data = mysqli_fetch_array($sql);

		if(is_file("../../".$data['foto'])) 
			unlink("../../".$data['foto']); 
            $exec = delete($koneksi, 'sarana',['id_sarana'=>$id]);
            echo "OK";
        	
}
if ($pg == 'foto') {
	$id= $_POST['ids'];
  $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/sapras/foto' . rand(1, 10000) . '.' . $ext;
                $upload = move_uploaded_file($temp, '../../' . $dest);
                if ($upload) {
                    $exec = update($koneksi, 'sarana', ['foto' => $dest], ['id_sarana' => $id]);
					echo "OK";
                } else {
                    echo "gagal";
                }
            }
        }
}
if ($pg == 'kondisi') {
	$id= $_POST['ids'];
 
	$data = [
			'atap' => $_POST['atap'],
			'plafon' => $_POST['plafon'],
			  'dinding'=>$_POST['dinding'],
			  'kusen'=>$_POST['kusen'],
			  'pondasi'=>$_POST['pondasi'],
			  'utilitas'=>$_POST['utilitas'],
			   'lante'=>$_POST['lantai']
            ];	
	
            $exec = update($koneksi, 'sapras', $data,['id_sapras'=>$id]);
            echo "OK";
        	
}