<?php

//doctor_action.php

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

$object = new Appointment;

if(isset($_POST["action"]))
{
	if($_POST["action"] == 'fetch')
	{
		$order_column = array('jenis_naskah','nama');

		$output = array();
	
		$main_query = "
		SELECT id_template,jenis_naskah.jenis_naskah,nama,file FROM `template_naskah` INNER JOIN jenis_naskah WHERE jenis_naskah.kode_naskah=template_naskah.jenis_naskah ";
		$search_query = '';

		if(isset($_POST["search"]["value"]))
		{
			$search_query .= 'AND (jenis_naskah.jenis_naskah LIKE "%'.$_POST["search"]["value"].'%" ';
			//$search_query .= 'OR perihal LIKE "'.$_POST["search"]["value"].'" '; 
			$search_query .= 'OR nama 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 jenis_naskah.jenis_naskah 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["jenis_naskah"];
			$sub_array[] = $row["nama"];
			$sub_array[] = '<a href="'.$row["file"].'"><i class="fas fa-file-word" style="font-size:36px;color:blue"></i></a>';
			$sub_array[] = '
			<div align="center">
			<button type="button" name="edit_button" class="btn btn-warning btn-circle btn-sm edit_button" data-id="'.$row["id_template"].'"><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 = '';
		//
		$nama_naskah = '';
		$allowed_file_format = array("doc", "docx");

		$file_extension = pathinfo($_FILES["nama_naskah"]["name"], PATHINFO_EXTENSION);

		if(!in_array($file_extension, $allowed_file_format))
	    {
	        $error = "<div class='alert alert-danger'>Upload valiid file. doc, docx</div>";
	    }
	    else if (($_FILES["nama_naskah"]["size"] > 2000000))
	    {
	       $error = "<div class='alert alert-danger'>File size exceeds 2MB</div>";
	    }
	    else
	    {
	    	//$new_name = rand() . '.' . $file_extension;
			$destination = '../images/' . $_FILES['nama_naskah']['name'];
			move_uploaded_file($_FILES['nama_naskah']['tmp_name'], $destination);
			$nama_naskah = $destination;
	    }
		//
		$data = array(
		':jenis_naskah'		=>	$object->clean_input($_POST["jenis_naskah"]),
		':nama'		=>	$_FILES['nama_naskah']['name'],
		':file'		=>	$nama_naskah,		
		);
		
		if($error){}
			else{
		//
		$object->query = "
		INSERT INTO template_naskah (jenis_naskah, nama, file) 
		VALUES (:jenis_naskah, :nama, :file)
		";

		$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 id_template,kode_naskah,jenis_naskah.jenis_naskah,nama,file FROM `template_naskah` INNER JOIN jenis_naskah WHERE jenis_naskah.kode_naskah=template_naskah.jenis_naskah AND id_template = '".$_POST["no_urut"]."'
		";

		$result = $object->get_result();

		$data = array();

		foreach($result as $row)
		{
			$data['jenis_naskah'] = $row['kode_naskah'];
			$data['nama'] = $row['nama'];
		}

		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);

	}
}

?>