<?php
 
/*
 * DataTables example server-side processing script.
 *
 * Please note that this script is intentionally extremely simply to show how
 * server-side processing can be implemented, and probably shouldn't be used as
 * the basis for a large complex system. It is suitable for simple use cases as
 * for learning.
 *
 * See http://datatables.net/usage/server-side for full details on the server-
 * side processing requirements of DataTables.
 *
 * @license MIT - http://datatables.net/license_mit
 */
 
include_once ("includes/setup.inc.php");	
//*****************************************************
//Get all POST and GET value
//*****************************************************
$sqlOnly = fetch_postget_val('sqlOnly',$_POST, $_GET);
$klinik_vat = fetch_postget_val('klinik_vat',$_POST, $_GET);
$tkh_from = fetch_postget_val('tkh_from',$_POST, $_GET);
$tkh_to = fetch_postget_val('tkh_to',$_POST, $_GET);

$from = explode('-', $tkh_from);
$to = explode('-', $tkh_to);

$tkh_from = $from[2].'-'.$from[1].'-'.$from[0];
$tkh_to = $to[2].'-'.$to[1].'-'.$to[0];

//*****************************************************
// MAIN TABLE AND JOIN TABLE
// main = main table name in the database,
// sub['name'] = join table name
// sub['sub_id'] = join table identifier
// sub['main_id'] = main table identifier
// sub['join_type'] = type of join. Default = 'LEFT'
// sub['aka'] = put alternative name for table
//*****************************************************
$script_base = 'pht_listjanji';
$tableArray = array(
	'main' => 'm_vatklinik_appointment',
	'primary_key' => 'id_vatklinik_appointment',
	'sub' => array(
		array('name' => 'm_pemilik', 'sub_id' => 'id_pemilik', 'main_id' => 'id_pemilik'),
		array('name' => 'm_pemilik_pet', 'sub_id' => 'id_pemilik_pet', 'main_id' => 'id_pemilik_pet'),
		array('name' => 'i_haiwan_peliharaan', 'sub_id' => 'id_haiwan_peliharaan', 'main_id' => 'id_haiwan_peliharaan', 'main_name' => 'm_pemilik_pet'),
		array('name' => 'i_status_appointment', 'sub_id' => 'id_status_appointment', 'main_id' => 'id_status_appointment'),
	),
);

//*****************************************************
// DATATABLE COLUMN
// db = column name in the database,
// dt = DataTables column identifier
// table = database table name
// akaFrom = use if and only if there is aka name for table
//*****************************************************
$columns = array(
    array( 'user_defined' => '', 'dt' => 0),
    array( 'db' => 'appointment_date', 'dt' => 1, 'table' => 'm_vatklinik_appointment' ),
    array( 'db' => 'appointment_time', 'dt' => 2, 'table' => 'm_vatklinik_appointment' ),
    array( 'db' => 'nama_pemilik', 'dt' => 3, 'table' => 'm_pemilik' ),
    array( 'db' => 'kpbaru_pemilik', 'dt' => 4, 'table' => 'm_pemilik' ),
    array( 'db' => 'nama_pet', 'dt' => 5, 'table' => 'm_pemilik_pet' ),
    array( 'db' => 'remarks', 'dt' => 6, 'table' => 'm_vatklinik_appointment' ),
    array( 'db' => 'resit_number', 'dt' => 7, 'table' => 'm_vatklinik_appointment' ),
    array( 'db' => 'id_status_appointment', 'dt' => 8, 'table' => 'i_status_appointment' ),
	array( 'dt' => 9, 
	'user_defined' => 
	'<a href="javascript:;" style="width:150px" class="btn btn-sm btn-light-primary mr-3" onclick="loadLink(%i_status_appointment.is_popup%, \''.$script_base.'-%i_status_appointment.next_step_subscript%?appointment=%m_vatklinik_appointment.id_vatklinik_appointment%\')" >%i_status_appointment.next_step_label%</a>
	'
	),
	/*array( 'dt' => 9, 
	'user_defined' => 
	'<a class="btn btn-sm btn-clean btn-icon mr-2" onclick="loadMyModal(\''.$script_base.'-tambah.php?id=%m_vatklinik_appointment.id_vatklinik_appointment%\', \'#ajax-modal\')" title="Edit">
		<span class="svg-icon svg-icon-primary svg-icon-2x">'.$Edit_svg.'</span>
	</a>
	'
	),
	*/
    array( 'db' => 'desk_status_appointment', 'dt' => 10, 'table' => 'i_status_appointment' ),
    array( 'db' => 'label_style', 'dt' => 11, 'table' => 'i_status_appointment' ),
    array( 'db' => 'desk_haiwan_peliharaan', 'dt' => 12, 'table' => 'i_haiwan_peliharaan' ),
);

//*****************************************************
// WHERE SQL
// Array ($table_name, $column_name, $value, $symbol(default : '='))
//*****************************************************
$usr_id_sys = $my_details->usr_id_sys;
$usr_id = $my_details->usr_id;

$whereArray = array(

	array('m_vatklinik_appointment', 'id_vatklinik', $klinik_vat, '='),
	array('m_vatklinik_appointment', 'appointment_date', "'".$tkh_from."'", '>='),
	array('m_vatklinik_appointment', 'appointment_date', "'".$tkh_to."'", '<='),
	
);	

//echo SSP::myComplex($_POST, $sql_details, buildSelectQuery($columns), buildFromQuery($tableArray), $tableArray['main'].'.'.$tableArray['primary_key'], $columns, 1, buildWhereQuery($whereArray));
//echo "whereArray = ".count($whereArray);
//exit(0);
//*****************************************************
// SSP GET JSON
// No need to modify below
//*****************************************************
if($sqlOnly == 1)
{
	//LEAVE EMPTY FOR FUTURE USAGE 
}
else
{	
	$temp_result = SSP::myComplex($_POST, $sql_details, buildSelectQuery($columns), buildFromQuery($tableArray), $tableArray['main'].'.'.$tableArray['primary_key'], $columns, 0, buildWhereQuery($whereArray));
	
	
	for($num = 0; $num < count($temp_result['data']); $num++)
	{
		$label_style = $temp_result['data'][$num][11];	
		
		$temp_result['data'][$num][8] = '<span class="label label-xl label-warning label-pill label-inline mr-2" style="'.$label_style.'">'.$temp_result['data'][$num][10].'</span>';
	}
	
	echo json_encode(
		$temp_result
	);
}