<?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 = 'pht_pemilik';
$tableArray = array(
	'main' => 'm_pemilik',
	'primary_key' => 'id_pemilik',
	'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_pemilik', 'dt' => 1, 'table' => 'm_pemilik' ),
    array( 'db' => 'kpbaru_pemilik', 'dt' => 2, 'table' => 'm_pemilik' ),
    array( 'db' => 'tel_pemilik', 'dt' => 3, 'table' => 'm_pemilik' ),
    array( 'db' => 'id_pemilik', 'dt' => 4, 'table' => 'm_pemilik' ),
	array( 'dt' => 5, 
	'user_defined' => 
	'<a class="btn btn-sm btn-clean btn-icon mr-2" onclick="loadMyModal(\''.$script_base.'-tambah.php?id=%m_pemilik.id_pemilik%\', \'#ajax-modal\')" title="Kemaskini Pemilik">
		<span class="svg-icon svg-icon-primary svg-icon-2x">'.$Edit_svg.'</span>
	</a>
	<a class="btn btn-sm btn-clean btn-icon mr-2" onclick="loadMyModal(\''.$script_base.'-pet_tambah.php?id=%m_pemilik.id_pemilik%\', \'#ajax-modal\')" title="Tambah Haiwan Ternakan">
		<span class="svg-icon svg-icon-primary svg-icon-2x">'.$Plus_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', 'id_jenispemilik', 1)
);	

//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));
	
	$indicator_Query = "SELECT * FROM i_haiwan_peliharaan";
	$PDO_stmt = $PDO->prepare($indicator_Query);
	$PDO_stmt->execute();
	$pet_List = [];
	while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
	{
		$pet_List[$result['id_haiwan_peliharaan']] = $result['desk_haiwan_peliharaan'];
	}
	
	for($num = 0; $num < count($temp_result['data']); $num++)
	{
		$id_pemilik = $temp_result['data'][$num][4];	
		
		$indicator_Query = "SELECT A.*
						  FROM m_pemilik_pet A
						  WHERE A.id_pemilik = ?";
		$PDO_stmt = $PDO->prepare($indicator_Query);
		$PDO_stmt->bindValue(1, $id_pemilik, PDO::PARAM_INT);
		$PDO_stmt->execute();
		$indicator_List = [];
		$counter = 1;
		while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
		{
			$indicator_List[] = ($counter++).') '.$pet_List[$result['id_haiwan_peliharaan']].' - '.$result['nama_pet'];
		}
		
		$temp_result['data'][$num][4] = join($indicator_List, '<br>');
		
	}
	
	echo json_encode(
		$temp_result
	);
}