<?php

include_once ("includes/setup.inc.php");

//*****************************************************
//Get all POST and GET value
//*****************************************************
$sqlOnly = fetch_postget_val('sqlOnly',$_POST, $_GET);
$id_program = fetch_postget_val("id_program", $_POST, $_GET);
$kod_program = fetch_postget_val("kod_program", $_POST, $_GET);
$id_carabayar = fetch_postget_val("id_carabayar", $_POST, $_GET);
$id_sah = fetch_postget_val("id_sah", $_POST, $_GET);
$id_daerah = fetch_postget_val("id_daerah", $_POST, $_GET);
$tkh_mula = formatDate(fetch_postget_val("tkh_mula", $_POST, $_GET), 'Y-m-d');
$tkh_hingga = formatDate(fetch_postget_val("tkh_hingga", $_POST, $_GET), 'Y-m-d');
$tahun_bayar_year = fetch_postget_val("tahun_bayar_year", $_POST, $_GET);
$bayaranfasa_sc = fetch_postget_val("bayaranfasa_sc", $_POST, $_GET);
$dun_sc = fetch_postget_val("dun_sc", $_POST, $_GET);

$mbayar_table = "m_bayaran_".$kod_program;
$mbayar_table_id = "id_bayaran_".$kod_program;
//*****************************************************
// 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['main_name'] = main table name (optional), if no set, will use "main table name"
// sub['join_type'] = type of join. Default = 'LEFT'
// sub['aka'] = put alternative name for table
//*****************************************************
if($id_program == 5)
{
	$tableArray = array(
		'main' => $mbayar_table,
		'primary_key' => $mbayar_table_id,
		'sub' => array(
			array('name' => 'm_anakemas', 'sub_id' => 'id_anakemas', 'main_id' => 'id_anakemas'),
			array('name' => 'i_carabayar', 'sub_id' => 'id_carabayar', 'main_id' => 'id_carabayar'),
			array('name' => 'i_bayaranfasa', 'sub_id' => 'id_bayaranfasa', 'main_id' => 'id_bayaranfasa'),
			array('name' => 'i_bayarankat', 'sub_id' => 'id_bayarankat', 'main_id' => 'id_bayarankat'),
			array('name' => 'i_bank', 'sub_id' => 'id_bank', 'main_id' => 'id_bank'),
			array('name' => 'i_dun', 'sub_id' => 'id_dun', 'main_id' => 'id_dun'),
		),
	);

}
else
{
	$tableArray = array(
		'main' => $mbayar_table,
		'primary_key' => $mbayar_table_id,
		'sub' => array(
			array('name' => 'm_pemohon', 'sub_id' => 'id_pemohon', 'main_id' => 'id_pemohon'),
			array('name' => 'i_carabayar', 'sub_id' => 'id_carabayar', 'main_id' => 'id_carabayar'),
			array('name' => 'i_bayaranfasa', 'sub_id' => 'id_bayaranfasa', 'main_id' => 'id_bayaranfasa'),
			array('name' => 'i_bayarankat', 'sub_id' => 'id_bayarankat', 'main_id' => 'id_bayarankat'),
			array('name' => 'i_bank', 'sub_id' => 'id_bank', 'main_id' => 'id_bank'),
			array('name' => 'i_dun', 'sub_id' => 'id_dun', 'main_id' => 'id_dun'),
		),
	);
}

//*****************************************************
// 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
//*****************************************************
if($id_program == 5)
{	
	$columns = array(
		array( 'bil_col' => '', 'dt' => 0),
		array( 'db' => 'nama_anakemas', 'dt' => 1, 'table' => 'm_anakemas' , 'print'=>1 ),
		array( 'db' => 'kpbaru_anakemas', 'dt' => 2, 'table' => 'm_anakemas' , 'print'=>1 ),
		array( 'db' => 'desk_dun', 'dt' => 3, 'table' => 'i_dun' , 'print'=>1 ),
		array( 'db' => 'tkh_bayar', 'dt' => 4, 'table' => $mbayar_table , 'print'=>1 ),
		array( 'db' => 'desk_bayaranfasa', 'dt' => 5, 'table' => 'i_bayaranfasa' , 'print'=>1 ),
		array( 'db' => 'desk_bayarankat', 'dt' => 6, 'table' => 'i_bayarankat' , 'print'=>1 ),
		array( 'db' => 'tahun_bayar', 'dt' => 7, 'table' => $mbayar_table , 'print'=>1 ),
		array( 'db' => 'jum_bayaran', 'dt' => 8, 'table' => $mbayar_table , 'print'=>1 ),
		array( 'db' => 'desk_carabayar', 'dt' => 9, 'table' => 'i_carabayar' , 'print'=>1 ),
		array( 'db' => 'desk_bank', 'dt' => 10, 'table' => 'i_bank' , 'print'=>1 ),
		array( 'db' => 'no_akaun', 'dt' => 11, 'table' => $mbayar_table , 'print'=>1 ),
		array( 'dt' => 12, 'user_defined' => ''),
		array( 'db' => $mbayar_table_id, 'dt' => 13, 'table' => $mbayar_table),
		array( 'db' => 'id_carabayar', 'dt' => 14, 'table' => $mbayar_table),
		array( 'db' => 'id_anakemas', 'dt' => 15, 'table' => $mbayar_table),
	);
}
else
{
	$columns = array(
		array( 'bil_col' => '', 'dt' => 0),
		array( 'db' => 'nama_pemohon', 'dt' => 1, 'table' => 'm_pemohon' , 'print'=>1 ),
		array( 'db' => 'kpbaru_pemohon', 'dt' => 2, 'table' => 'm_pemohon' , 'print'=>1 ),
		array( 'db' => 'desk_dun', 'dt' => 3, 'table' => 'i_dun' , 'print'=>1 ),
		array( 'db' => 'tkh_bayar', 'dt' => 4, 'table' => $mbayar_table , 'print'=>1 ),
		array( 'db' => 'desk_bayaranfasa', 'dt' => 5, 'table' => 'i_bayaranfasa' , 'print'=>1 ),
		array( 'db' => 'desk_bayarankat', 'dt' => 6, 'table' => 'i_bayarankat' , 'print'=>1 ),
		array( 'db' => 'tahun_bayar', 'dt' => 7, 'table' => $mbayar_table , 'print'=>1 ),
		array( 'db' => 'jum_bayaran', 'dt' => 8, 'table' => $mbayar_table , 'print'=>1 ),
		array( 'db' => 'desk_carabayar', 'dt' => 9, 'table' => 'i_carabayar' , 'print'=>1 ),
		array( 'db' => 'desk_bank', 'dt' => 10, 'table' => 'i_bank' , 'print'=>1 ),
		array( 'db' => 'no_akaun', 'dt' => 11, 'table' => $mbayar_table , 'print'=>1 ),
		array( 'dt' => 12, 'user_defined' => ''),
		array( 'db' => $mbayar_table_id, 'dt' => 13, 'table' => $mbayar_table),
		array( 'db' => 'id_carabayar', 'dt' => 14, 'table' => $mbayar_table),
		array( 'db' => 'id_pemohon', 'dt' => 15, 'table' => $mbayar_table),
	);
}


//*****************************************************
// WHERE SQL
// Array ($table_name, $column_name, $value, $symbol(default : '='))
//*****************************************************
$whereArray = array(
	//array($mbayar_table, 'id_daerah', $id_daerah),
	array($mbayar_table, 'tkh_bayar', "'".$tkh_mula." 00:00:00' AND '".$tkh_hingga." 23:59:59'", 'BETWEEN' ),
);

if($id_daerah > 0)
{
	$whereArray[] = array($mbayar_table, 'id_daerah', $id_daerah);
}

if($dun_sc > 0)
{
	$whereArray[] = array($mbayar_table, 'id_dun', $dun_sc);
}

if($id_carabayar > 0)
{
	$whereArray[] = array($mbayar_table, 'id_carabayar', $id_carabayar);
}

if($id_sah > 0)
{
	$whereArray[] = array($mbayar_table, 'id_sah', $id_sah);
}

if($tahun_bayar_year > 0)
{
	$whereArray[] = array($mbayar_table, 'tahun_bayar', $tahun_bayar_year);
}

if($bayaranfasa_sc > 0)
{
	$whereArray[] = array($mbayar_table, 'id_bayaranfasa', $bayaranfasa_sc);
}
		
//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++)
	{
			$start_index = 12;
			$id_bayaran = $temp_result['data'][$num][$start_index + 1];
			$id_carabayar = $temp_result['data'][$num][$start_index + 2];
			$id_pemohon = $temp_result['data'][$num][$start_index + 3];
			
			$edit_bayaran = '-';
			if($my_details->user_level <= 4)
			{
				if($id_program == 5)
				{
					$edit_bayaran = '<a class="btn btn-sm btn-default btn-text-primary btn-hover-primary btn-icon mr-2" onclick="loadMyModal(\'general-byr_ae.php?id='.$id_pemohon.'\', \'#ajax-modal\')" title="Kemaskini Pembayaran"><span class="svg-icon svg-icon-primary svg-icon-2x">'.$Check_svg.'</span></a>';
				}
				else if($id_carabayar == 1)
				{
					$edit_bayaran = '<a class="btn btn-sm btn-default btn-text-primary btn-hover-primary btn-icon mr-2" onclick="loadMyModal(\'general-byr.php?id_bayaran='.$id_bayaran.'&id_pemohon='.$id_pemohon.'&mbayar_table='.$mbayar_table.'&mbayar_table_id='.$mbayar_table_id.'&id_program='.$id_program.'\', \'#ajax-modal\')" title="Kemaskini Pembayaran"><span class="svg-icon svg-icon-primary svg-icon-2x">'.$Check_svg.'</span></a>';					
				}
			}
			
			$temp_result['data'][$num][$start_index] = $edit_bayaran;	
	}
	echo json_encode(
		$temp_result
	);
}
?>