<?php 
include_once ("includes/setup_pemohon.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.*, C.desk_jenisaduan, D.desk_statusaduan, E.nama, F.desk_peranan, G.desk_dun, H.desk_daerah
				  FROM m_aduan A
				  LEFT JOIN i_jenisaduan C ON C.id_jenisaduan = A.id_jenisaduan
				  LEFT JOIN i_statusaduan D ON D.id_statusaduan = A.id_statusaduan
				  LEFT JOIN t_pengguna E ON E.id_pengguna = A.id_assign
				  LEFT JOIN t_peranan F ON F.id_peranan= E.id_peranan
				  LEFT JOIN i_dun G ON G.id_dun= A.id_dun
				  LEFT JOIN i_daerah H ON H.id_daerah= G.id_daerah
				  WHERE A.id_aduan = ?";
$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">Maklumat Aduan</h5>
	<button type="button" class="close" data-dismiss="modal" aria-label="Close" id="statusCloseBtn" name="statusCloseBtn" ></button>
</div>
<div class="modal-body">
	<div class="row">
		<div class="col-xl-12">
		<?php
			createLabels('Nama Pengadu :', 'nama_pengadu', $indicator_List['nama_pengadu'], '', 2, 7, 0, 12);
			createLabels('No KP Pengadu :', 'kpbaru_pengadu', $indicator_List['kpbaru_pengadu'], '', 2, 7, 0, 12);
			//createLabels('No KP Pemohon :', 'kpbaru_pemohon', $indicator_List['kpbaru_pemohon'], '', 2, 7, 0, 12);
			createLabels('Email :', 'emel_pengadu', $indicator_List['emel_pengadu'], '', 2, 7, 0, 12);
			createLabels('No. Telefon :', 'tel_pengadu', $indicator_List['tel_pengadu'], '', 2, 7, 0, 12);
			//createLabels('Program :', 'desk_program', $indicator_List['desk_program'], '', 2, 7, 0, 12);
			createLabels('Dun :', 'desk_dun', $indicator_List['desk_dun'], '', 2, 7, 0, 12);
			createLabels('Daerah :', 'desk_daerah', $indicator_List['desk_daerah'], '', 2, 7, 0, 12);
			createLabels('Jenis Aduan :', 'desk_jenisaduan', $indicator_List['desk_jenisaduan'], '', 2, 7, 0, 12);
			createLabels('Tajuk Aduan :', 'tajuk', $indicator_List['tajuk'], '', 2, 7, 0, 12);
			createLabels('Keterangan :', 'laporan', $indicator_List['laporan'], '', 2, 7, 0, 12);			
	
			if($my_details->user_level < 4 && $indicator_List['id_assign'] > 0)
			{
				createLabels('Pegawai Tugasan :', 'peg', $indicator_List['nama'], '', 2, 7, 0, 12);
				createLabels('Peranan :', 'per', $indicator_List['desk_peranan'], '', 2, 7, 0, 12);
			}
		?>
		</div>
	</div>
	<div class="separator separator-dashed my-5"></div>
	<div class="row">
		<div class="col-xl-12">
			<h6>Maklumbalas</h6>
			<div id="replyDiv"></div>
		</div>
	</div>	
	<?php
	if($indicator_List['id_statusaduan'] == 1)
	{
	?>
		<div class="separator separator-dashed my-5"></div>
		<div class="row">
			<div class="col-xl-12">
				<form action="javascript:;" class="form-horizontal" name="indicator_form" id="indicator_form">
				<?php
					createTextArea('', 'maklumbalas', 'Sila nyatakan maklumbalas', '', 0, 0, 12, 12, '', 10);
				?>
				</form>
				<button type="button" class="btn btn-primary" id="indicator_simpanBtn">Hantar Maklumbalas</button>
				<?php
				if($my_details->user_level != 10)
				{
				?>
					<a class="btn btn-warning" id="deleteBtn" title="Selesai" onclick="deleteFunction('<?=$indicator_List['id_aduan']?>', '<?=$indicator_List['tajuk']?>')"><span class="svg-icon svg-icon-danger svg-icon-2x">Selesai</span></a>
				<?php
				}
				?>
			</div>
		</div>		
		<?php
	
		if($my_details->user_level < 4 && $indicator_List['id_assign'] == 0)
		{
		?>
			<div class="separator separator-dashed my-5"></div>		
			<div class="row">
				<div class="col-xl-12">
					<form action="javascript:;" class="form-horizontal" name="assign_form" id="assign_form">
					<?php
					$my_sql = 'SELECT A.id_pengguna AS id, CONCAT(A.nama, " - ", B.desk_peranan) as name 
							   FROM t_pengguna A
							   LEFT JOIN t_peranan B ON B.id_peranan = A.id_peranan
							   WHERE A.id_peranan NOT IN (1,2,3,10)
							   ORDER BY A.id_peranan,A.nama';
					createDropDown_sql($PDO, 'Tugasan Pegawai', $my_sql, 'id_assign', $indicator_List['id_assign'], 'Sila Pilih Pegawai', 7, 2, 12);
					?>
					</form>
					<button type="button" class="btn btn-primary" id="assign_simpanBtn">Hantar Tugasan Aduan</button>
				</div>
			</div>
		<?php
		}
	}
	?>
</div>
<div class="modal-footer">
	<button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
</div>

<script>
function getComment()
{
	$.ajax({
		type: "POST", 
		url: "in_ad-status_ajax.php", 
		data: 'id=' + "<?=$myID?>",

		success: function(res){
			$('#replyDiv').html(res);
		} 
	});  //end ajax
}

$(document).ready(function(){
	
	getComment();  

	$('.select2').select2({ width: '100%' });
	
	$('.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(['maklumbalas', 'keterangan maklumbalas', 'tb']);
		
		if (checkInput(indicator_list))
		{
			$(this).prop("disabled",true);
			var param = $('#indicator_form').serialize();
			$.ajax({
				type: "POST", 
				url: "in_ad-status_insert.php", 
				data: param+ '&id=' + "<?=$myID?>"+ '&temp=' + "<?=$temp?>",

				success: function(res){
					if(res > 0)
					{
						swal.fire({
							text: "Maklumbalas berjaya dihantar",
							icon: "success",
							buttonsStyling: false,
							confirmButtonText: "OK!",
							customClass: {
								confirmButton: "btn font-weight-bold btn-light-primary"
							}
						}).then(function() {
							window.parent.reloadTable();
							$("#statusCloseBtn").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
		}
	});		
	
	$('#assign_simpanBtn').click(function() {
		var indicator_list = [[]];
		indicator_list.push(['id_assign', 'Tugasan Pegawai', 'dd']);
		
		if (checkInput(indicator_list))
		{
			$(this).prop("disabled",true);
			var param = $('#assign_form').serialize();
			$.ajax({
				type: "POST", 
				url: "in_ad-status_tugasan.php", 
				data: param+ '&id=' + "<?=$myID?>",

				success: function(res){
					if(res > 0)
					{
						swal.fire({
							text: "Berjaya Tambah/Kemaskini Pegawai Tugasan",
							icon: "success",
							buttonsStyling: false,
							confirmButtonText: "OK!",
							customClass: {
								confirmButton: "btn font-weight-bold btn-light-primary"
							}
						}).then(function() {
							window.parent.reloadTable();
							$("#statusCloseBtn").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
		}
	});	

	function deleteFunction(id, desk)
	{
		Swal.fire({
			title: "Pastikah Anda?",
			text: "Pastikah untuk selesaikan '" + desk + "' ?",
			icon: "warning",
			showCancelButton: true,
			confirmButtonText: "Ya",
			cancelButtonText: "Tidak",
		}).then(function(result) {
			if (result.value) {
				$.ajax({
				   type: "POST",
				   url: "in_ad-aktif.php",
				   data: 'id=' + id,
				   
				   success: function(res){
						
						swal.fire({
							text: "Berjaya selesaikan " + desk + ".",
							icon: "success",
							buttonsStyling: false,
							confirmButtonText: "OK!",
							customClass: {
								confirmButton: "btn font-weight-bold btn-light-primary"
							}
						}).then(function() {
							window.parent.reloadTable();
							//$("#statusCloseBtn").click();
							window.location = "in_ad.php";
						});
				   
				   }
			   });  //end ajax
			}
		});
	}	
});		
</script>
