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

$script_base = 'in_borangitem';
$myID = fetch_postget_val('id',$_POST, $_GET);
$readonly = fetch_postget_val('readonly',$_POST, $_GET);

$indicator_Query = "SELECT A.*
				  FROM u_borangitem A
				  WHERE A.id_borangitem = ?";
$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">Daftar / Kemaskini Maklumat Item</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
					$borangdiv_sql = "SELECT id_borangdiv AS id, title as name FROM u_borangdiv ORDER BY susunan";
					createDropDown_sql($PDO, 'Borang Div', $borangdiv_sql, 'id_borangdiv', $indicator_List['id_borangdiv'], 'Pilih Borang Div', 7, 3, 12, 1, '');
					$borangdiv_sql = "SELECT id_inputtype AS id, desk_inputtype as name FROM u_inputtype";
					createDropDown_sql($PDO, 'Input Type', $borangdiv_sql, 'id_inputtype', $indicator_List['id_inputtype'], 'Pilih Input Type', 7, 3, 12, 1, '');
					createTextBox('Label for Form', 'label', 'Label for Form', $indicator_List['label'], 3, $readonly, 7, 12, '', 'text');
					createTextBox('Label for Indicator', 'label_show', 'Label for Indicator', $indicator_List['label_show'], 3, $readonly, 7, 12, '', 'text');
					createTextArea('Help Text', 'help_text', 'Help Text', $indicator_List['help_text'], 0, 3, 7, 12, '', 10);		
					createTextBox('Input Name', 'input_name', 'Input Name', $indicator_List['input_name'], 3, 1, 7, 12, '', 'text');
					createTextBox('Placeholder', 'placeholder', 'Placeholder (Leave empty if follow label)', $indicator_List['placeholder'], 3, $readonly, 7, 12, '', 'text', 'Leave empty if follow label');
					createTextBox('Value', 'value', 'value (DB column name)', $indicator_List['value'], 3, 1, 7, 12, '', 'text', 'DB column name to pull value');
					
					$myArray = [];
					for($num = 1; $num <= 12; $num++){
						$myArray[] = array($num, $num);
					}
					createDropDown_Array('Div size', $myArray, 'div_size', $indicator_List['div_size'], 'Pilih Div Size', 7, 3, 12, $readonly);
					$myArray = [];
					for($num = 1; $num <= 6; $num++){
						$myArray[] = array($num, $num);
					}
					createDropDown_Array('Label size', $myArray, 'label_size', $indicator_List['label_size'], 'Pilih Label Size', 7, 3, 12, $readonly);
					$myArray = [];
					for($num = 1; $num <= 12; $num++){
						$myArray[] = array($num, $num);
					}
					createDropDown_Array('Input size', $myArray, 'input_size', $indicator_List['input_size'], 'Pilih Input Size', 7, 3, 12, $readonly);
					createTextBox('Extra Param', 'extra_param', 'Extra Param', $indicator_List['extra_param'], 3, $readonly, 7, 12, '', 'text', 'To use for dropdown (indicator table name), dropdown_sql (sql to run), custom_text (whatever html code to show)');
					
					createTextBox('Susunan', 'susunan', 'Susunan', $indicator_List['susunan'], 3, $readonly, 7, 12, '', 'number');
					
					if($myID > 0)
					{
						$myArray = [];
						$myArray[] = array(1, 'YA');
						createDropDown_Array('Aktif', $myArray, 'aktif', $indicator_List['aktif'], 'TIDAK', 7, 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="simpanBtn">Simpan</button>';
		}
	?>
</div>

<script>
$(document).ready(function(){  

	$('.select2').select2({ width: '100%' }); 
	
	$('#simpanBtn').click(function() {
		var indi_list = [[]];
		indi_list.push(['id_borangdiv', 'Borang Div', 'dd']);
		indi_list.push(['id_inputtype', 'Input Type', 'dd']);
		indi_list.push(['label', 'Label', 'tb']);
		
		if (checkInput(indi_list))
		{
			$(this).prop("disabled",true);
			var param = $('#indicator_form').serialize();
			$.ajax({
				type: "POST", 
				url: "<?=$script_base?>-tambah_insert.php", 
				data: param+ '&id=' + "<?=$myID?>",

				success: function(res){
					if(res > 0)
					{
						swal.fire({
							text: "Berjaya 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>