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

$myID = fetch_postget_val('id',$_POST, $_GET);
$readonly = fetch_postget_val('readonly',$_POST, $_GET);
$temp = ($my_details->usr_id).'_'.time();

$indicator_Query = "SELECT A.*, B.desk_program
				  FROM i_kawalan A
				  LEFT JOIN i_program B ON B.id_program = A.id_program
				  WHERE A.id_kawalan = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
$PDO_stmt->execute();
$indicator_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

?>
<div class="modal-header">
	<h5 class="modal-title">Kemaskini Kawalan</h5>
	<button type="button" class="close" data-dismiss="modal" aria-label="Close" id="closeBtn" name="closeBtn" ></button>
</div>
<div class="modal-body">
	<form action="javascript:;" class="form-horizontal" name="indicator_form" id="indicator_form">
		<div class="form-body">
			<div class="row">
				<?php
					createTextBox('Deskripsi', 'desk', 'Deskripsi', $indicator_List['desk_kawalan'], 3, 1, 7, 12, '', 'text');
					createTextBox('Program', 'program', 'Program', $indicator_List['desk_program'], 3, 1, 7, 12, '', 'text');
					createDateTB('Tarikh Tutup', 'tkh_tutup', 'Tarikh Tutup',(formatDate($indicator_List['tkh_tutup'], 'd M Y') == ''?date('d M Y'):formatDate($indicator_List['tkh_tutup'], 'd M Y')), 3, 4, 0, 12);					
					createDateTB('Tarikh Buka', 'tkh_buka', 'Tarikh Buka',(formatDate($indicator_List['tkh_buka'], 'd M Y') == ''?date('d M Y'):formatDate($indicator_List['tkh_buka'], 'd M Y')), 3, 4, 0, 12);					
					
					$flag_aktif = 1;
					if($myID > 0)
					{
						$flag_aktif = $indicator_List['aktif'];						
					}					
					$myArray = [];
					$myArray[] = array(1, 'YA');
					createDropDown_Array('Aktif Tempoh Tutup/Buka', $myArray, 'aktif', $flag_aktif, 'TIDAK', 4, 3, 12, $readonly);
					createTextBox('Tahun Bayaran Tunai', 'tahun_bayaran_tunai', 'Tahun Bayaran Tunai', $indicator_List['tahun_bayaran_tunai'], 3, $readonly, 7, 12, '', 'numonly', 'Cth : 2021', '', 4);
					createDropDown($PDO, 'Fasa Bayaran Tunai', 'i_bayaranfasa', 'id_bayaranfasa_tunai', $indicator_List['id_bayaranfasa_tunai'], 'Sila Pilih Fasa Bayaran Tunai', 7, 3, 12, $readonly);					
					createDateTB('Tarikh Cutoff Tunai', 'tkh_cutoff_tunai', 'Tarikh Cutoff Tunai',(formatDate($indicator_List['tkh_cutoff_tunai'], 'd M Y') == ''?date('d M Y'):formatDate($indicator_List['tkh_cutoff_tunai'], 'd M Y')), 3, 4, 0, 12);	
					createTextBox('Tahun Bayaran EFT', 'tahun_bayaran_EFT', 'Tahun Bayaran EFT', $indicator_List['tahun_bayaran_EFT'], 3, $readonly, 7, 12, '', 'numonly', 'Cth : 2021', '', 4);
					createDropDown($PDO, 'Fasa Bayaran EFT', 'i_bayaranfasa', 'id_bayaranfasa_EFT', $indicator_List['id_bayaranfasa_EFT'], 'Sila Pilih Fasa Bayaran Tunai', 7, 3, 12, $readonly);					
					createDateTB('Tarikh Cutoff EFT', 'tkh_cutoff_EFT', 'Tarikh Cutoff EFT',(formatDate($indicator_List['tkh_cutoff_EFT'], 'd M Y') == ''?date('d M Y'):formatDate($indicator_List['tkh_cutoff_EFT'], 'd M Y')), 3, 4, 0, 12);	
					createDateTB('Tarikh Cutoff Sah EFT', 'tkh_cutoff_sah_EFT', 'Tarikh Cutoff Sah EFT',(formatDate($indicator_List['tkh_cutoff_sah_EFT'], 'd M Y') == ''?date('d M Y'):formatDate($indicator_List['tkh_cutoff_sah_EFT'], 'd M Y')), 3, 4, 0, 12);	
					createDateTB('Tarikh Cutoff Aktif Mohon', 'tkh_cutoff_aktifmohon', 'Tarikh Cutoff Aktif Mohon',(formatDate($indicator_List['tkh_cutoff_aktifmohon'], 'd M Y') == ''?date('d M Y'):formatDate($indicator_List['tkh_cutoff_aktifmohon'], 'd M Y')), 3, 4, 0, 12);	
					
					$flag_khairat = 0;
					if($myID > 0)
					{
						$flag_khairat = $indicator_List['flag_khairat'];						
					}					
					$myKhairat = [];
					$myKhairat[] = array(1, 'YA');
					createDropDown_Array('Tutup Khairat Kematian', $myKhairat, 'flag_khairat', $flag_khairat, 'TIDAK', 4, 3, 12, $readonly);
					
					$flag_aktifmohon = 0;
					if($myID > 0)
					{
						$flag_aktifmohon = $indicator_List['flag_aktifmohon'];						
					}					
					$myAktifmohon = [];
					$myAktifmohon[] = array(1, 'YA');
					createDropDown_Array('Tutup Pertukaran Aktif Mohon', $myAktifmohon, 'flag_aktifmohon', $flag_aktifmohon, 'TIDAK', 4, 3, 12, $readonly);
				?>
				
			</div>
		</div>
	</form>
</div>
<div class="modal-footer">
	<?php
		echo '<button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>';
		if($readonly == 0)
		{
			echo '<button type="button" class="btn btn-primary" id="indicator_simpanBtn">Simpan</button>';
		}
	?>
</div>

<script>

$(document).ready(function(){  

	$('.numonly').keypress(function(evt){
		evt = (evt) ? evt : window.event;
		var charCode = (evt.which) ? evt.which : evt.keyCode;
		if ((charCode >= 48 && charCode <= 57)) {
			return true;
		}
		return false;
	});

	$('.myDatePicker').datepicker({
		autoclose: true,
		clearBtn: true,
		format: "dd M yyyy",
		todayHighlight: true,
		orientation: "bottom left",
		templates: {
			leftArrow: '<i class="la la-angle-left"></i>',
			rightArrow: '<i class="la la-angle-right"></i>'
		}
	});

	$('.select2').select2({ width: '100%' }); 
	
	$('#indicator_simpanBtn').click(function() {
		var indicator_list = [[]];
		indicator_list.push(['tkh_mula', 'Tarikh Mula', 'tb']);		
		indicator_list.push(['tkh_tamat', 'Tarikh Tamat', 'tb']);
		
		if (checkInput(indicator_list))
		{
			$(this).prop("disabled",true);
			var param = $('#indicator_form').serialize();
			$.ajax({
				type: "POST", 
				url: "in_kawalan-tambah_insert.php", 
				data: param+ '&id=' + "<?=$myID?>"+ '&temp=' + "<?=$temp?>",

				success: function(res){
					if(res > 0)
					{
						swal.fire({
							text: "Berjaya Tambah/Kemaskini Maklumat",
							icon: "success",
							buttonsStyling: false,
							confirmButtonText: "OK!",
							customClass: {
								confirmButton: "btn font-weight-bold btn-light-primary"
							}
						}).then(function() {
							window.parent.reloadTable();
							$("#closeBtn").click();
						});
					}
					else
					{
						swal.fire({
							text: "Error. " + res,
							icon: "error",
							buttonsStyling: false,
							confirmButtonText: "OK!",
							customClass: {
								confirmButton: "btn font-weight-bold btn-light-primary"
							}
						}).then(function() {
							$(this).prop("disabled",false);										
						});
					}
					
				} 
			});  //end ajax
		}
	});		
});		
</script>
