<?php 
include '../connect.php';
session_start();
$ic=$_SESSION["kadPengenalan"];
date_default_timezone_set("Asia/Kuala_Lumpur");			
$date=date('Y-m-d');
$time=date('H:i:sa');

	$id_cek =$_GET['id_cek'];
	$idSuratMasuk = $_GET['idSuratMasuk'];
	$noCek=strtoupper($_POST['noCek']);
	$noResit=strtoupper($_POST['noResit']);
	$jumlah=strtoupper($_POST['jumlah']);
	$tarikh_resit=$_POST['tarikh_resit'];
	$perihal1=strtoupper($_POST['perihal1']);
	$perihal2=strtoupper($_POST['perihal2']);
	$lainlainhal=strtoupper($_POST['lainlainhal']);
	
	$noCekNew = mysqli_real_escape_string($conn,$noCek);
	$noResitNew = mysqli_real_escape_string($conn,$noResit);
	$jumlahNew = mysqli_real_escape_string($conn,$jumlah);
	$perihal1New = mysqli_real_escape_string($conn,$perihal1);
	$perihal2New = mysqli_real_escape_string($conn,$perihal2);
	$lainlainhalNew = mysqli_real_escape_string($conn,$lainlainhal);
	
	$tarikhResitNew = date("Y-m-d", strtotime($tarikh_resit));
	
	$sql4="select * from`cek` where id_cek = '".$id_cek."'";
	$result4= mysqli_query($conn,$sql4);
	$row4 = mysqli_num_rows($result4);	
	
	if($row4 == 1)
	{
		$query="UPDATE `cek` SET no_cek='".$noCekNew."',no_resit='".$noResitNew."',
				jumlah='".$jumlahNew."',tarikh_resit='".$tarikhResitNew."',perihal1='".$perihal1New."',perihal2='".$perihal2New."',
				lain_lain_hal='".$lainlainhalNew."' WHERE id_cek='".$id_cek."'";				
		$strQuery = mysqli_query($conn,$query);
		
		if($strQuery) 
			{
				$sql = "select * from cek where id_cek='".$id_cek."'";
				$result = mysqli_query($conn,$sql);
				$row = mysqli_fetch_array($result);
				
				$id_cek = $row['id_cek'];
				
				$q4="INSERT INTO audit_trail(kad_pengenalan,tarikh,masa,operasi,catatan)
				VALUES ('$ic','$date','$time','Ubahsuai','Ubahsuai cek')";
				$strQuery1 = mysqli_query($conn,$q4);
				
				if($strQuery1)
				{
					echo "<script>alert('Maklumat cek berjaya dikemaskini');";
					echo "window.location.href = '../ubahsuaiCek.php?idSuratMasuk=$idSuratMasuk';</script>";
					exit;
				}
				else
				{
					echo 'MySQL Error: ' . mysqli_error($conn);
					exit;
				}
			}
			else
			{
				echo 'MySQL Error: ' . mysqli_error($conn);
				exit;
			}
		
	}
	else
	{
		$query="INSERT INTO cek (no_cek,id_surat_masuk,no_resit, jumlah, tarikh_resit,perihal1,perihal2, lain_lain_hal)
		VALUES ('".$noCekNew."','".$idSuratMasuk."','".$noResitNew."','".$jumlahNew."','".$tarikhResitNew."','".$perihal1New."',
		'".$perihal2New."','".$lainlainhalNew."')";
		$strQuery = mysqli_query($conn,$query);
		
		if($strQuery) 
		{
			$sql = "select * from cek where no_cek='".$noCekNew."'";
			$result = mysqli_query($conn,$sql);
			$row = mysqli_fetch_array($result);
			
			$noCekNew = $row['no_cek'];
			
			$q4="INSERT INTO audit_trail(kad_pengenalan,tarikh,masa,operasi,catatan)
			VALUES ('$ic','$date','$time','Ubahsuai','Ubahsuai cek')";
			$strQuery1 = mysqli_query($conn,$q4);
			
			if($strQuery1)
			{
				echo "<script>";
				echo "var r = confirm('Maklumat cek berjaya disimpan.Adakah anda ingin menambah cek? ');";
				echo "if (r == true){window.location.href = '../ubahsuaiCek.php?idSuratMasuk=$idSuratMasuk'}";
				echo "else {window.location.href = '../daftarSurat.php'}";
				echo "</script>";
				exit;
			}
			else
			{
				echo 'MySQL Error: ' . mysqli_error($conn);
				exit;
			}
		}
		else
		{
			echo 'MySQL Error: ' . mysqli_error($conn);
			exit;
		}
	}
?>


		