<?php

//doctor_action.php

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

$object = new Appointment;

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

		$output = array();

		$main_query = "
		SELECT * FROM arsip_dimanis WHERE unit_kerja='".$_POST['hidden_id']."' AND ruang is not null ";

		$search_query = '';
		
		if(isset($_POST["search"]["value"]))
		{
			$search_query .= 'AND (no_arsip LIKE "%'.$_POST["search"]["value"].'%" ';
			$search_query .= 'OR uraian LIKE "%'.$_POST["search"]["value"].'%" '; 
			$search_query .= 'OR kurun_waktu 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["no_arsip"];
			$sub_array[] = $row["uraian"];
			$sub_array[] = $row["kurun_waktu"];
			$sub_array[] = '
			<div align="center">
			
			<button type="button" name="edit_button" class="btn btn-info btn-circle btn-sm edit_button" data-id="'.$row["no_urut"].'"><i class="fas fa-fax"></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"]),
		':asli_copy'			=>	$object->clean_input($_POST["asli_copy"]),
		':media'				=>	$object->clean_input($_POST["media"]),
		':kondisi'				=>	$object->clean_input($_POST["kondisi"]),
		':box'					=>	$object->clean_input($_POST["box"]),
		':perihal'				=>	$object->clean_input($_POST["perihal"]),
		':uraian'				=>	$object->clean_input($_POST["uraian"]),
		);

		$object->query = "
		INSERT INTO tatausaha 
		(kode_klasifikasi, unit_kerja, kurun_waktu, asli_copy, media, kondisi, box, perihal, uraian) 
		VALUES (:kode_klasifikasi, :unit_kerja, :kurun_waktu, :asli_copy, :media, :kondisi, :box, :perihal, :uraian)
		";

		$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['no_arsip'] = $row['no_arsip'];
			$data['kurun_waktu'] = $row['kurun_waktu'];
			$data['tingkat_perkembangan'] = $row['tingkat_perkembangan'];
			$data['uraian'] = $row['uraian'];
			$data['media'] = $row['media'];
			$data['kondisi'] = $row['kondisi'];
			$data['ruang'] = $row['ruang'];
			$data['rak'] = $row['rak'];
			$data['box'] = $row['box'];
			$data['keterangan'] = $row['keterangan'];
			//$data['no_urut'] = $row['no_urut'];
			
		}

		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 tatausaha 
		WHERE no_urut = :no_urut 
		AND unit_kerja != :unit_kerja
		";
		
		$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"],
					':asli_copy'				=>	$_POST["asli_copy"],
					':media'				=>	$_POST["media"],
					':kondisi'				=>	$_POST["kondisi"],
					':perihal'				=>	$_POST["perihal"],
					':uraian'				=>	$_POST["uraian"],
				);

				$object->query = "
				UPDATE tatausaha SET 
				kode_klasifikasi = '".$data[':kode_klasifikasi']."',
				unit_kerja = '".$data[':unit_kerja']."',
				kurun_waktu = '".$data[':kurun_waktu']."',
				asli_copy = '".$data[':asli_copy']."',
				media = '".$data[':media']."',
				kondisi = '".$data[':kondisi']."',
				perihal = '".$data[':perihal']."',
				uraian = '".$data[':uraian']."',
				box = '".$data[':box']."'
				
				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);

	}

*/
}

?>