<?php

//doctor_action.php

include('../class/Appointment.php');

$object = new Appointment;

if(isset($_POST["action"]))
{
	if($_POST["action"] == 'fetch')
	{
		$order_column = array('kode_klasifikasi','unit_kerja','uraian','kurun_waktu');

		$output = array();
		//SELECT * FROM arsip_dimanis where ruang is null
		$main_query = "
		SELECT * FROM tatausaha ";
		$search_query = '';

		if(isset($_POST["search"]["value"]))
		{
			$search_query .= 'Where (kode_klasifikasi LIKE "%'.$_POST["search"]["value"].'%" ';
			$search_query .= 'OR kurun_waktu = "'.$_POST["search"]["value"].'" '; 
			$search_query .= 'OR uraian LIKE "%'.$_POST["search"]["value"].'%") '; 
		}

		if(isset($_POST["order"]))
		{
			$order_query = 'ORDER BY '.$order_column[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';
		}
		else
		{
			$order_query = 'ORDER BY no_urut DESC ';
		}

		$limit_query = '';

		if($_POST["length"] != -1)
		{
			$limit_query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
		}

		$object->query = $main_query . $search_query . $order_query;

		$object->execute();

		$filtered_rows = $object->row_count();

		$object->query .= $limit_query;

		$result = $object->get_result();

		$object->query = $main_query;

		$object->execute();

		$total_rows = $object->row_count();

		$data = array();

		foreach($result as $row)
		{
			$sub_array = array();
			$sub_array[] = $row["kode_klasifikasi"];
			$sub_array[] = $row["unit_kerja"];
			$sub_array[] = $row["uraian"];
			$sub_array[] = $row["kurun_waktu"];
			$sub_array[] = '
			<div align="center">
			
			<button type="button" name="edit_button" class="btn btn-warning btn-circle btn-sm edit_button" data-id="'.$row["no_urut"].'"><i class="fas fa-edit"></i></button>
			
			</div>
			';
			$data[] = $sub_array;
		}

		$output = array(
			"draw"    			=> 	intval($_POST["draw"]),
			"recordsTotal"  	=>  $total_rows,
			"recordsFiltered" 	=> 	$filtered_rows,
			"data"    			=> 	$data
		);
			
		echo json_encode($output);

	}
	
	if($_POST["action"] == 'Add')
	{
		$error = '';

		$success = '';

		$data = array(
		':kode_klasifikasi'		=>	$object->clean_input($_POST["kode_klasifikasi"]),
		':unit_kerja'			=>	$object->clean_input($_POST["unit_kerja"]),
		':kurun_waktu'			=>	$object->clean_input($_POST["kurun_waktu"]),
		':no_arsip'				=>	$object->clean_input($_POST["no_arsip"]),
		':tingkat_perkembangan'			=>	$object->clean_input($_POST["tingkat_perkembangan"]),
		':media'				=>	$object->clean_input($_POST["media"]),
		':kondisi'				=>	$object->clean_input($_POST["kondisi"]),
		':box'					=>	$object->clean_input($_POST["box"]),
		':ruang'			=>	$object->clean_input($_POST["ruang"]),
		':rak'				=>	$object->clean_input($_POST["rak"]),
		':uraian'				=>	$object->clean_input($_POST["uraian"]),
		':keterangan'			=>	$object->clean_input($_POST["keterangan"]),
		':nip'			=>	$object->clean_input($_SESSION['arsiparis_id']),
		);
		
		if($data[':ruang'] == '') $data[':ruang'] = null;
		
		$object->query = "
		INSERT INTO arsip_dimanis 
		(kode_klasifikasi, unit_kerja, jenis, kurun_waktu, no_arsip, tingkat_perkembangan, media, kondisi, box, ruang, rak, uraian, keterangan, nip) 
		VALUES (:kode_klasifikasi, :unit_kerja, 'IN-AKTIF', :kurun_waktu, :no_arsip, :tingkat_perkembangan, :media, :kondisi, :box, :ruang, :rak, :uraian, :keterangan, :nip)
		";

		$object->execute($data);

		$success = '<div class="alert alert-success">Data Added</div>';
			
		$output = array(
			'error'		=>	$error,
			'success'	=>	$success
		);

		echo json_encode($output);

	}

	if($_POST["action"] == 'fetch_single')
	{
		$object->query = "
		SELECT * FROM arsip_dimanis 
		WHERE no_urut = '".$_POST["no_urut"]."'
		";

		$result = $object->get_result();

		$data = array();

		foreach($result as $row)
		{
			$data['kode_klasifikasi'] = $row['kode_klasifikasi'];
			$data['unit_kerja'] = $row['unit_kerja'];
			$data['kurun_waktu'] = $row['kurun_waktu'];
			$data['no_arsip'] = $row['no_arsip'];
			$data['tingkat_perkembangan'] = $row['tingkat_perkembangan'];
			$data['media'] = $row['media'];
			$data['kondisi'] = $row['kondisi'];
			$data['box'] = $row['box'];
			$data['ruang'] = $row['ruang'];
			$data['rak'] = $row['rak'];
			$data['uraian'] = $row['uraian'];
			$data['no_urut'] = $row['no_urut'];
			$data['keterangan'] = $row['keterangan'];
			if($row['nip'] != null) {
				//$data['nama_arsiparis'] = $row['arsiparis_nip'];
				$object->query = "
				SELECT arsiparis_name FROM arsiparis_table 
				WHERE arsiparis_nip = '".$row['nip']."'
				";
				$result2 = $object->get_result();
				foreach($result2 as $row2)
				{
					$data['nama_arsiparis'] =  $row2['arsiparis_name'];
				}
			}
			else $data['nama_arsiparis'] = '-';
		}

		echo json_encode($data);
	}
	
	if($_POST["action"] == 'Edit')
	{
		$error = '';

		$success = '';
		
		$data = array(
			//':unit_kerja'	=>	$_POST["unit_kerja"],
			':no_urut'			=>	$_POST['hidden_id']
		);
		
		$object->query = "
		SELECT * FROM arsip_dimanis 
		WHERE no_urut = :no_urut 
		";
		
		$object->execute($data);

		if($object->row_count() > 0)
		{
		//	$error = '<div class="alert alert-danger">NO urut Already Exists</div>';
		//}
		//else
		//{
			if($error == '')
			{
				$data = array(
					':kode_klasifikasi'		=>	$object->clean_input($_POST["kode_klasifikasi"]),
					':box'					=>	$_POST["box"],
					':unit_kerja'			=>	$_POST["unit_kerja"],
					':kurun_waktu'			=>	$_POST["kurun_waktu"],
					':no_arsip'				=>	$_POST["no_arsip"],
					':tingkat_perkembangan'	=>	$_POST["tingkat_perkembangan"],
					':media'				=>	$_POST["media"],
					':kondisi'				=>	$_POST["kondisi"],
					':ruang'				=>	$object->clean_input($_POST["ruang"]),
					':rak'					=>	$_POST["rak"],
					':uraian'				=>	$_POST["uraian"],
					':keterangan'			=>	$_POST["keterangan"],
				);
				
				if($_POST["ruang"] == '') {

					$object->query = "
					UPDATE arsip_dimanis SET 
					kode_klasifikasi = '".$data[':kode_klasifikasi']."',
					unit_kerja = '".$data[':unit_kerja']."',
					kurun_waktu = '".$data[':kurun_waktu']."',
					no_arsip = '".$data[':no_arsip']."',
					tingkat_perkembangan = '".$data[':tingkat_perkembangan']."',
					media = '".$data[':media']."',
					kondisi = '".$data[':kondisi']."',
					ruang = null,
					rak = '".$data[':rak']."',
					box = '".$data[':box']."',
					uraian = '".$data[':uraian']."',
					keterangan = '".$data[':keterangan']."'
					
					WHERE no_urut = '".$_POST['hidden_id']."'
					
					";
				} else {
					$object->query = "
					UPDATE arsip_dimanis SET 
					kode_klasifikasi = '".$data[':kode_klasifikasi']."',
					unit_kerja = '".$data[':unit_kerja']."',
					kurun_waktu = '".$data[':kurun_waktu']."',
					no_arsip = '".$data[':no_arsip']."',
					tingkat_perkembangan = '".$data[':tingkat_perkembangan']."',
					media = '".$data[':media']."',
					kondisi = '".$data[':kondisi']."',
					ruang = '".$data[':ruang']."',
					rak = '".$data[':rak']."',
					box = '".$data[':box']."',
					uraian = '".$data[':uraian']."',
					keterangan = '".$data[':keterangan']."'
					
					WHERE no_urut = '".$_POST['hidden_id']."'
					
					";
				}

				$object->execute($data);
				
				$success = '<div class="alert alert-success">Data Updated </div>';
			}	
		}
		
		$output = array(
			'error'		=>	$error,
			'success'	=>	$success
		);

		echo json_encode($output);

	}
}

?>