<?php
include '../connect.php';
session_start();

$idSuratMasuk =$_GET['idSuratMasuk'];

		$query="SELECT id_surat_masuk FROM edaran_surat WHERE id_surat_masuk = '".$idSuratMasuk."'";
		$strQuery = mysqli_query($conn,$query);
		$row = mysqli_num_rows($strQuery);
		
		if($row>=1)
		{
			echo "<script>alert('Surat tidak boleh dipadam kerana telah diedar.');";
			echo "window.location.href = '../ubahsuaiSurat1.php';</script>";
			exit;
		}
		else 
		{
			
			$query1="SELECT id_surat_masuk FROM cek WHERE id_surat_masuk = '".$idSuratMasuk."'";
			$strQuery1 = mysqli_query($conn,$query1);	
			$row1 = mysqli_num_rows($strQuery1);
			
			if($row1>=1)
			{
				while($fetch=mysqli_fetch_array($strQuery1))
				{
					$query2="DELETE FROM cek WHERE id_surat_masuk = '".$fetch['id_surat_masuk']."'";
					$strQuery2 = mysqli_query($conn,$query2);
				}
				
				$query3="SELECT id_surat_masuk FROM files WHERE id_surat_masuk = '".$idSuratMasuk."'";
				$strQuery3 = mysqli_query($conn,$query3);
				$row3 = mysqli_num_rows($strQuery3);
				
				if($row3=1)
				{
					$query4="DELETE FROM files WHERE id_surat_masuk = '".$idSuratMasuk."'";
					$strQuery4 = mysqli_query($conn,$query4);
					
					$query5="SELECT id_surat_masuk FROM surat_masuk WHERE id_surat_masuk = '".$idSuratMasuk."'";
					$strQuery5 = mysqli_query($conn,$query5);
					$row5 = mysqli_num_rows($strQuery5);
					
					if($row5=1)
					{
						$query6="DELETE FROM surat_masuk WHERE id_surat_masuk = '".$idSuratMasuk."'";
						$strQuery6 = mysqli_query($conn,$query6);
						
						if($strQuery6)
						{
							date_default_timezone_set("Asia/Kuala_Lumpur");			
							$date=date('Y-m-d');
							$time=date('H:i:sa');
							
							$kadPengenalan= $_SESSION["kadPengenalan"];
							
							$q4="INSERT INTO audit_trail(kad_pengenalan,tarikh,masa,operasi,catatan)
									VALUES ('$kadPengenalan','$date','$time','Hapus','Hapus surat ID $idSuratMasuk')";
							$strQuery = mysqli_query($conn,$q4);
							if($strQuery)
							{
								
								echo "<script>alert('Maklumat surat berjaya dipadam.');";
								echo "window.location.href = '../ubahsuaiSurat1.php';</script>";
								exit;
							}
							else
							{
								echo 'MySQL Error: ' . mysqli_error($conn);
							exit;
							}
						}
						else
						{
							echo 'MySQL Error: ' . mysqli_error($conn);
							exit;
						}
					}
					else
					{
						echo 'MySQL Error: ' . mysqli_error($conn);
						exit;
					}
					
				}
					else
				{
					echo 'MySQL Error: ' . mysqli_error($conn);
					exit;
				}
			}
			else
			{
				
				$query3="SELECT id_surat_masuk FROM files WHERE id_surat_masuk = '".$idSuratMasuk."'";
				$strQuery3 = mysqli_query($conn,$query3);
				$row3 = mysqli_num_rows($strQuery3);
				
				if($row3=1)
				{
					$query4="DELETE FROM files WHERE id_surat_masuk = '".$idSuratMasuk."'";
					$strQuery4 = mysqli_query($conn,$query4);
					
					$query5="SELECT id_surat_masuk FROM surat_masuk WHERE id_surat_masuk = '".$idSuratMasuk."'";
					$strQuery5 = mysqli_query($conn,$query5);
					$row5 = mysqli_num_rows($strQuery5);
					
					if($row5=1)
					{
						$query6="DELETE FROM surat_masuk WHERE id_surat_masuk = '".$idSuratMasuk."'";
						$strQuery6 = mysqli_query($conn,$query6);
						
						if($strQuery6)
						{
							date_default_timezone_set("Asia/Kuala_Lumpur");			
							$date=date('Y-m-d');
							$time=date('H:i:sa');
							
							$kadPengenalan= $_SESSION["kadPengenalan"];
							
							$q4="INSERT INTO audit_trail(kad_pengenalan,tarikh,masa,operasi,catatan)
									VALUES ('$kadPengenalan','$date','$time','Hapus','Hapus surat ID $idSuratMasuk')";
							$strQuery = mysqli_query($conn,$q4);
							if($strQuery)
							{
								
								echo "<script>alert('Maklumat surat berjaya dipadam.');";
								echo "window.location.href = '../ubahsuaiSurat1.php';</script>";
								exit;
							}
							else
							{
								echo 'MySQL Error: ' . mysqli_error($conn);
							exit;
							}
						}
						else
						{
							echo 'MySQL Error: ' . mysqli_error($conn);
							exit;
						}
					}
					else
					{
						echo 'MySQL Error: ' . mysqli_error($conn);
						exit;
					}
				}
				else
				{
					$query5="SELECT id_surat_masuk FROM surat_masuk WHERE id_surat_masuk = '".$idSuratMasuk."'";
					$strQuery5 = mysqli_query($conn,$query5);
					$row5 = mysqli_num_rows($strQuery5);
					
					if($row5=1)
					{
						$query6="DELETE FROM surat_masuk WHERE id_surat_masuk = '".$idSuratMasuk."'";
						$strQuery6 = mysqli_query($conn,$query6);
						
						if($strQuery6)
						{
							date_default_timezone_set("Asia/Kuala_Lumpur");			
							$date=date('Y-m-d');
							$time=date('H:i:sa');
							
							$kadPengenalan= $_SESSION["kadPengenalan"];
							
							$q4="INSERT INTO audit_trail(kad_pengenalan,tarikh,masa,operasi,catatan)
									VALUES ('$kadPengenalan','$date','$time','Hapus','Hapus surat ID $idSuratMasuk')";
							$strQuery = mysqli_query($conn,$q4);
							if($strQuery)
							{
								echo "<script>alert('Maklumat surat berjaya dipadam.');";
								echo "window.location.href = '../ubahsuaiSurat1.php';</script>";
								exit;								
							}
							else
							{
								echo 'MySQL Error: ' . mysqli_error($conn);
							exit;
							}
						}
						else
						{
							echo 'MySQL Error: ' . mysqli_error($conn);
							exit;
						}
					}
					else
					{
						echo 'MySQL Error: ' . mysqli_error($conn);
						exit;
					}
				}
			}
		}
?>