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

//*****************************************************
// 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 = 'sen_pet';
$tableArray = array(
	'main' => 'm_pemilik_pet',
	'primary_key' => 'id_pemilik_pet',
	'sub' => array(
		//array('name' => 'm_pemilik', 'sub_id' => 'id_pemilik', 'main_id' => 'id_pemilik'),
		array('name' => 'i_haiwan_peliharaan', 'sub_id' => 'id_haiwan_peliharaan', 'main_id' => 'id_haiwan_peliharaan'),
	),
);

//*****************************************************
// 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' => 'nama_pet', 'dt' => 1, 'table' => 'm_pemilik_pet' ),
    array( 'db' => 'desk_haiwan_peliharaan', 'dt' => 2, 'table' => 'i_haiwan_peliharaan' ),
	array( 'dt' => 3, 
	'user_defined' => 
	'<a class="btn btn-sm btn-clean btn-icon mr-2" onclick="loadMyModal(\''.$script_base.'-tambah.php?id=%m_pemilik_pet.id_pemilik_pet%\', \'#ajax-modal\')" title="Edit">
		<span class="svg-icon svg-icon-primary svg-icon-2x">'.$Edit_svg.'</span>
	</a>
	'
	),
);

//*****************************************************
// 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_pemilik_pet', 'id_pemilik', $my_details->usr_id)
	
);	

//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++)
	{
		$id_pemilik_pet = $temp_result['data'][$num][5];	
		
		$temp_result['data'][$num][2] = '<a href="javascript:;" onclick="loadMyModal(\'sen_penternak-maklumat.php?id='.$id_pemilik_pet.'\', \'#ajax-modal\')" title="Maklumat Penternak">'.$temp_result['data'][$num][2].'</a>';
		
		$ladang_list = [];
		if($temp_result['data'][$num][7] > 0){ $ladang_list[] = 'Ruminan'; }
		if($temp_result['data'][$num][8] > 0){ $ladang_list[] = 'Tenusu'; }
		if($temp_result['data'][$num][9] > 0){ $ladang_list[] = 'Babi'; }
		if($temp_result['data'][$num][10] > 0){ $ladang_list[] = 'Aneka Haiwan'; }
		$temp_result['data'][$num][5] = join($ladang_list, ',<br>');
		
	}
	*/
	echo json_encode(
		$temp_result
	);
}