<?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.*
				  FROM i_lazim A
				  WHERE A.id_lazim = ?";
$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 Soalan Lazim</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
					createTextArea('Soalan', 'soalan', 'Sila nyatakan soalan', $indicator_List['soalan'], 0, 3, 7, 12, '', 10);
					createTextArea('Jawapan', 'jawapan', 'Sila nyatakan jawapan', $indicator_List['jawapan'], 0, 3, 7, 12, '', 10);	
					createTextBox('Susunan', 'susunan', 'Susunan', $indicator_List['susunan'], 3, $readonly, 7, 12);			
					
					$flag_aktif = 1;
					if($myID > 0)
					{
						$flag_aktif = $indicator_List['aktif'];						
					}					
					$myArray = [];
					$myArray[] = array(1, 'YA');
					createDropDown_Array('Aktif', $myArray, 'aktif', $flag_aktif, '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(){  

	$('.select2').select2({ width: '100%' });
	
	tinymce.remove(); //need remove 1st before init again
	tinymce.init({
		selector: '#soalan, #jawapan',
		menubar: false,
		toolbar: ['undo redo | styleselect | bold italic underline strikethrough | bullist numlist | outdent indent |',
				'alignleft aligncenter alignright alignjustify | forecolor backcolor | preview | link'],
		plugins : 'advlist lists preview link'
	});

	$('.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>'
		}
	});
	
	$('#indicator_simpanBtn').click(function() {
		var indicator_list = [[]];
		indicator_list.push(['soalan', 'Soalan', 'tinymce']);
		indicator_list.push(['jawapan', 'Jawapan', 'tinymce']);
		indicator_list.push(['susunan', 'Susunan', 'tb']);
		indicator_list.push(['aktif', 'Aktif', 'dd']);
		
		if (checkInput(indicator_list))
		{
			$(this).prop("disabled",true);
			var soalan_content = tinymce.get('soalan').getContent();
			var jawapan_content = tinymce.get('jawapan').getContent();
			var param = $('#indicator_form').serialize();
			param += '&soalan_content=' + encodeURIComponent(soalan_content);
			param += '&jawapan_content=' + encodeURIComponent(jawapan_content);
			$.ajax({
				type: "POST", 
				url: "in_lazim-tambah_insert.php", 
				data: param+ '&id=' + "<?=$myID?>",

				success: function(res){
					if(res > 0)
					{
						swal.fire({
							text: "Berjaya Tambah/Kemaskini Soalan Lazim",
							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>
