<?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');

	$idSuratMasuk =$_GET['idSuratMasuk'];
	$rujukanSurat=strtoupper($_POST['rujukanSurat']);
	$tarikhSurat=strtoupper($_POST['tarikhSurat']);
	$tarikhTerima=strtoupper($_POST['tarikhTerima']);
	$perkara1=strtoupper($_POST['perkara1']);
	$perkara2=strtoupper($_POST['perkara2']);
	$perkara3=strtoupper($_POST['perkara3']);
	$bahagian=strtoupper($_POST['bahagian']);
	$namapengirim=strtoupper($_POST['namapengirim']);
	$sesi=strtoupper($_POST['sesi']);
	 
	$rujukanSuratNew = mysqli_real_escape_string($conn,$rujukanSurat);
	$perkara1New = mysqli_real_escape_string ($conn,$perkara1);
	$perkara2New = mysqli_real_escape_string ($conn,$perkara2);
	$perkara3New = mysqli_real_escape_string ($conn,$perkara3);
	$namapengirimNew = mysqli_real_escape_string ($conn,$namapengirim); 
    $tarikhSuratNew = date("Y-m-d", strtotime($tarikhSurat));
	$tarikhTerimaNew = date("Y-m-d", strtotime($tarikhTerima));

	
	if($rujukanSuratNew =="" || $tarikhSurat == "" || $tarikhTerima =="" || $perkara1 =="")
	{
		mysqli_commit($conn);
		echo "<script>alert('Sila lengkapkan maklumat penting. ');";
		echo "window.location.href = '../ubahsuaiSurat.php?idSuratMasuk=$idSuratMasuk';</script>";
		exit;
		
	}
	else
	{
		if($bahagian=='BAHAGIAN PENTADBIRAN DAN SUMBER MANUSIA')
		{
			$tindakan='KB (PSM)';
		}
		else if($bahagian=='UNIT PENGURUSAN DANA WAKAF')
		{
			$tindakan=  'KU (DW)';
		}
		else if($bahagian=='UNIT PENGURUSAN DAN PENYELENGGARAAN PREMIS')
		{
			$tindakan=  'KB (PPP)';
		}
		else if($bahagian=='BAHAGIAN UNDANG-UNDANG')
		{
			$tindakan=  'KB(UU)';
		}
		else if($bahagian=='BAHAGIAN KEWANGAN DAN AKAUN')
		{
			$tindakan=  'KB (KA)';
		}
		else if($bahagian=='UNIT AUDIT DALAM')
		{
			$tindakan=  'KU (AD)';
		}
		else if($bahagian=='UNIT PEMBANGUNAN HARTANAH DAN HARTA WAKAF')
		{
			$tindakan=  'KU (PHHW)';
		}
		else if($bahagian=='SETIAUSAHA')
		{
			$tindakan=  'SU';
		}
		else if($bahagian=='PENGURUS BAITULMAL')
		{
			$tindakan=  'PBM';
		}
		else if($bahagian=='YANG DI PERTUA')
		{
			$tindakan=  'YDP';
		}
		else if($bahagian=='TIMBALAN YANG DI PERTUA')
		{
			$tindakan=  'TYDP';
		}
		else if($bahagian=='PEJABAT SETIAUSAHA')
		{
			$tindakan=  'SUP';
		}
		else
		{
			$tindakan= "";
		}
		
		$query="UPDATE `surat_masuk` SET no_rujukan_surat='".$rujukanSuratNew."',tarikh_surat='".$tarikhSuratNew."',
				tarikh_terima_surat='".$tarikhTerimaNew."',tajuk1='".$perkara1New."',tajuk2='".$perkara2New."',tajuk3='".$perkara3New."',
				bahagian='".$bahagian."',tindakan='".$tindakan."',nama_pengirim='".$namapengirimNew."',sesi='".$sesi."' WHERE id_surat_masuk='".$idSuratMasuk."'";				
		$strQuery = mysqli_query($conn,$query);
		
		if($strQuery) 
		{
				$sql = "select * from surat_masuk where no_rujukan_surat='".$rujukanSuratNew."'";
				$result = mysqli_query($conn,$sql);
				$row = mysqli_fetch_array($result);
				
				if ($idSuratMasuk = $row['id_surat_masuk'])
				{

					$getName = ($_FILES['myfile']['name']);
					$filename = str_replace(',','_', $getName);

					if ($filename == "")
					{
						$q4="INSERT INTO audit_trail(kad_pengenalan,tarikh,masa,operasi,catatan)
						VALUES ('$ic','$date','$time','Ubahsuai','Ubahsuai surat')";
						$strQuery1 = mysqli_query($conn,$q4);
						
						if($strQuery1)
						{
							echo "<script>";
							echo "var r = confirm('Maklumat surat masuk berjaya dikemaskini.Adakah anda ingin ubahsuai cek?  ');";
							echo "if (r == true){window.location.href = '../daftarCek.php?idSuratMasuk=$idSuratMasuk'}";
							echo "else {window.location.href = '../ubahsuaiSurat1.php'}";
							echo "</script>";
							exit;
						}
					}
					else
					{
						
						// destination of the file on the server
						$destination = '../uploads/' . $filename;

						// get the file extension
						$extension = pathinfo($filename, PATHINFO_EXTENSION);

						// the physical file on a temporary uploads directory on the server
						$file = $_FILES['myfile']['tmp_name'];
						$size = $_FILES['myfile']['size'];

							if (!in_array($extension, ['zip', 'pdf', 'docx', 'jpg'])) 
							{
								echo "You file extension must be .zip, .pdf or .docx";
							} elseif ($_FILES['myfile']['size'] > 100000000) 
							{ // file shouldn't be larger than 100Megabyte
								echo "File too large!";
							}
						else 
						{
							$sql1 = "SELECT * FROM files WHERE id_surat_masuk='".$idSuratMasuk."' ";
							$result1 = mysqli_query($conn, $sql1);
							
							
							if (mysqli_num_rows($result1) == 0)
							{
								$sql = "INSERT INTO files (id_surat_masuk,name, size, downloads) VALUES ('$idSuratMasuk','$filename', '$size', 0)";
								if (mysqli_query($conn, $sql)) 
								{
									$q4="INSERT INTO audit_trail(kad_pengenalan,tarikh,masa,operasi,catatan)
									VALUES ('$ic','$date','$time','Ubahsuai','Ubahsuai surat')";
									$strQuery1 = mysqli_query($conn,$q4);
									
									if($strQuery1)
									{
										echo "<script>";
										echo "var r = confirm('Maklumat surat masuk berjaya disimpan.Adakah anda ingin menambah cek? ');";
										echo "if (r == true){window.location.href = '../daftarCek.php?idSuratMasuk=$idSuratMasuk'}";
										echo "else {window.location.href = '../ubahsuaiSurat1.php'}";
										echo "</script>";
										exit;
									}									
								}
							}
							else
							{
								if (move_uploaded_file($file, $destination)) { 
									
									$sql = "UPDATE files SET name='$filename', size='$size' WHERE id_surat_masuk='$idSuratMasuk'";
									if (mysqli_query($conn, $sql)) 
									{
										$q4="INSERT INTO audit_trail(kad_pengenalan,tarikh,masa,operasi,catatan)
										VALUES ('$ic','$date','$time','Ubahsuai','Ubahsuai surat')";
										$strQuery1 = mysqli_query($conn,$q4);
										
										if($strQuery1)
										{
											echo "<script>";
											echo "var r = confirm('Maklumat surat masuk berjaya dikemaskini.Adakah anda ingin ubahsuai cek?  ');";
											echo "if (r == true){window.location.href = '../daftarCek.php?idSuratMasuk=$idSuratMasuk'}";
											echo "else {window.location.href = '../ubahsuaiSurat1.php'}";
											echo "</script>";
											exit;
										}
										
									}
								} else 
								{
									// move the uploaded (temporary) file to the specified destination
									$sql = "INSERT INTO files (id_surat_masuk,name, size, downloads) VALUES ('$idSuratMasuk','', '', 0)";
									if (mysqli_query($conn, $sql)) 
									{
										$q4="INSERT INTO audit_trail(kad_pengenalan,tarikh,masa,operasi,catatan)
										VALUES ('$ic','$date','$time','Ubahsuai','Ubahsuai surat')";
										$strQuery1 = mysqli_query($conn,$q4);
										
										if($strQuery1)
										{
											echo "<script>";
											echo "var r = confirm('Maklumat surat masuk berjaya dikemaskini.Adakah anda ingin ubahsuai cek? ');";
											echo "if (r == true){window.location.href = '../daftarCek.php?idSuratMasuk=$idSuratMasuk'}";
											echo "else {window.location.href = '../daftarSurat.php'}";
											echo "</script>";
											exit;
										}
										
									}
								}
							}

						}
				  
					}								
			
				
				}
		}
	}	
?>