<?php

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

//
$object = new Database;

if(isset($_POST["action"]))
{
	if($_POST["action"] == 'fetch')
	{
		$order_column = array('jenis_naskah','nomor','tujuan','klasifikasi','satker','tanggal','tanda_tangan','template');

		$output = array();
	
		$main_query = "
		SELECT id_surat,kode_naskah,jenis_naskah.jenis_naskah,nomor,perihal,tujuan,klasifikasi,satuan_kerja,tanggal,tanda_tangan,template,surat_keluar.status FROM surat_keluar INNER JOIN jenis_naskah,satuan_kerja,tanda_tangan WHERE surat_keluar.jenis_naskah=jenis_naskah.kode_naskah AND satuan_kerja.kode_satker=surat_keluar.satker AND ttd=tanda_tangan.kode_ttd ";
		$search_query = '';

		if(isset($_POST["search"]["value"]))
		{
			$search_query .= 'AND (nomor LIKE "%'.$_POST["search"]["value"].'%" ';
			$search_query .= 'OR perihal LIKE "'.$_POST["search"]["value"].'" '; 
			$search_query .= 'OR jenis_naskah.jenis_naskah LIKE "'.$_POST["search"]["value"].'" '; 
			$search_query .= 'OR tujuan 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 id_surat 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();

		$status = '';

		foreach($result as $row)
		{
			$sub_array = array();
			$sub_array[] = $row["jenis_naskah"];
			$sub_array[] = $row["nomor"];
			$sub_array[] = $row["perihal"];
			$sub_array[] = $row["tujuan"];
			$sub_array[] = $row["satuan_kerja"];
			$sub_array[] = $row["tanggal"];
			$sub_array[] = $row["tanda_tangan"];
			$file_extension = pathinfo($row["template"], PATHINFO_EXTENSION);
			if($file_extension=='docx')
			{
				$sub_array[] = '<center><a href="'.$row["template"].'"><i class="fas fa-file-word" style="font-size:36px;color:blue"></i></a></center>';
			}
				else {
				$sub_array[] = '<center><a href="'.$row["template"].'"><i class="fas fa-file-pdf" style="font-size:36px;color:red"></i></a></center>';
			}
			if($row["status"] == 0) {
				
				$status = '<button type="button" name="status_button" class="btn btn-primary btn-sm status_button" data-id="'.$row["id_surat"].'" data-status="'.$row["kode_naskah"].'"> Approve </button>';
				
			} else $status = '';
			
			$sub_array[] = $status;
			$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_surat"].'"><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 = '';
		
		$success = 'Data Added '.$_POST["jenis_naskah"];
			
		$output = array(
			'error'		=>	$error,
			'success'	=>	$success
		);

		echo json_encode($output);

	}
}