<?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_user = '0'; //$_GET['userID'];
$rujukanSurat=strtoupper($_POST['rujukanSurat']);
$tarikhSurat=strtoupper($_POST['tarikhSurat']);
$tarikhTerima=strtoupper($_POST['tarikhTerima']);
$perkara1=strtoupper($_POST['perkara1']);
$perkara2=strtoupper($_POST['perkara2']);
$perkara3=strtoupper($_POST['perkara3']);
$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));

$AddNamaPengirim =strtoupper($_POST['AddNamaPengirim']);
$alamat1 =strtoupper($_POST['alamat1']);
$alamat2 =strtoupper($_POST['alamat2']);
$alamat3 =strtoupper($_POST['alamat3']);
$noTel = $_POST['noTel'];
$email = $_POST['email'];

$AddNamaPengirimNew = mysqli_real_escape_string($conn,$AddNamaPengirim);
$alamat1New = mysqli_real_escape_string($conn,$alamat1);
$alamat2New = mysqli_real_escape_string($conn,$alamat2);
$alamat3New = mysqli_real_escape_string($conn,$alamat3);
	
	if(!empty($_POST['bahagian'])){
      foreach($_POST['bahagian'] as $checked){
			
			if($checked=='BAHAGIAN PENTADBIRAN DAN SUMBER MANUSIA')
			{
				$tindakan='KB (PSM)';
			}
			else if($checked=='UNIT PENGURUSAN DANA WAKAF')
			{
				$tindakan=  'KU (DW)';
			}
			else if($checked=='UNIT PENGURUSAN DAN PENYELENGGARAAN PREMIS')
			{
				$tindakan=  'KB (PPP)';
			}
			else if($checked=='BAHAGIAN UNDANG-UNDANG')
			{
				$tindakan=  'KB(UU)';
			}
			else if($checked=='BAHAGIAN KEWANGAN DAN AKAUN')
			{
				$tindakan=  'KB (KA)';
			}
			else if($checked=='BAHAGIAN AUDIT DALAM')
			{
				$tindakan=  'KB (AD)';
			}
			else if($checked=='UNIT PENTADBIRAN TANAH')
			{
				$tindakan=  'KU (PT)';
			}
			else if($checked=='UNIT PEMBANGUNAN')
			{
				$tindakan=  'KU (P)';
			}
			else if($checked=='SETIAUSAHA')
			{
				$tindakan=  'SU';
			}
			else if($checked=='PENGURUS BAITULMAL')
			{
				$tindakan=  'PBM';
			}
			else if($checked=='YANG DI PERTUA')
			{
				$tindakan=  'YDP';
			}
			else if($checked=='TIMBALAN YANG DI PERTUA')
			{
				$tindakan=  'TYDP';
			}
			else if($checked=='PEJABAT SETIAUSAHA')
			{
				$tindakan=  'SUP';
			}
			else
			{
				$tindakan= "";
			}
			
			if($namapengirim=="")
			{
					$query2="INSERT INTO alamat (nama_pengirim,alamat1,alamat2,alamat3,no_tel,email)
					VALUES ('".$AddNamaPengirimNew."','".$alamat1New."','".$alamat2New."','".$alamat3New."','".$noTel."','".$email."')";
					$strQuery2 = mysqli_query($conn,$query2);
					
					if($strQuery2) 
					{
						date_default_timezone_set("Asia/Kuala_Lumpur");			
						$date=date('Y-m-d');
						$time=date('H:i:sa');
						$q4="INSERT INTO audit_trail(kad_pengenalan,tarikh,masa,operasi,catatan)
								VALUES ('$ic','$date','$time','Daftar','Daftar alamat')";
						$strQuery3 = mysqli_query($conn,$q4);
						
						if($strQuery3)
						{
							$query="INSERT INTO surat_masuk (no_rujukan_surat,id_user,tarikh_surat,tarikh_terima_surat,tajuk1,tajuk2,tajuk3,
							bahagian,tindakan,nama_pengirim,sesi)
							VALUES ('".$rujukanSuratNew."','".$id_user."','".$tarikhSuratNew."','".$tarikhTerimaNew."','".$perkara1New."','".$perkara2New."',
							'".$perkara3New."','".$checked."','".$tindakan."','".$AddNamaPengirimNew."','".$sesi."')";
							$strQuery = mysqli_query($conn,$query);
						}
						else
						{
							echo 'MySQL Error: ' . mysqli_error($conn);
							exit;
						}
					}
					else
					{
						echo 'MySQL Error: ' . mysqli_error($conn);
						exit;
					}	
			}
			else
			{
				$query="INSERT INTO surat_masuk (no_rujukan_surat,id_user,tarikh_surat,tarikh_terima_surat,tajuk1,tajuk2,tajuk3,
				bahagian,tindakan,nama_pengirim,sesi)
				VALUES ('".$rujukanSuratNew."','".$id_user."','".$tarikhSuratNew."','".$tarikhTerimaNew."','".$perkara1New."','".$perkara2New."',
				'".$perkara3New."','".$checked."','".$tindakan."','".$namapengirimNew."','".$sesi."')";
				$strQuery = mysqli_query($conn,$query);
			}

		}
		
			if($strQuery) 
			{
				
				$sql = "select * from surat_masuk where no_rujukan_surat='".$rujukanSuratNew."' and tarikh_terima_surat='".$tarikhTerimaNew."'";
				$result = mysqli_query($conn,$sql);
				
				$querys = "select id_surat_masuk from surat_masuk where no_rujukan_surat='".$rujukanSuratNew."' and tarikh_terima_surat='".$tarikhTerimaNew."' and tajuk1='".$perkara1New."'";
				$fetchs=mysqli_query($conn,$querys);
				$rows = mysqli_num_rows($fetchs);			
				
				
				while ($row = mysqli_fetch_array($result))
				{
					$idSuratMasuk= $row["id_surat_masuk"];
					
					$sql1 = "SELECT * FROM files";
					$result1 = mysqli_query($conn, $sql1);
					$files = mysqli_fetch_array($result1, MYSQLI_ASSOC);
					
					$getName = $_FILES['myfile']['name'];
					$filename = str_replace(',','_', $getName);
					if ($filename == "")
					{
						if($rows==1)
						{
							$q4="INSERT INTO audit_trail(kad_pengenalan,tarikh,masa,operasi,catatan)
							VALUES ('$ic','$date','$time','Tambah','Tambah 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 == false){window.location.href = '../daftarSurat.php'}";
								echo "else {window.location.href = '../daftarCek.php?idSuratMasuk=$idSuratMasuk'}";
								echo "</script>";
								exit;
								
							}
							else
							{
								echo 'MySQL Error: ' . mysqli_error($conn);;
								exit;							
							}
							
						}
						else{
							
							echo ("<script LANGUAGE='JavaScript'>
							window.alert('Maklumat surat masuk berjaya disimpan');
							window.location.href='../daftarSurat.php';
							</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 
						{
						// move the uploaded (temporary) file to the specified destination
							if (move_uploaded_file($file, $destination)) {
								
								$sql2 = "INSERT INTO files (id_surat_masuk,name, size, downloads) VALUES ('$idSuratMasuk','$filename', '$size', 0)";
								$result2=mysqli_query($conn, $sql2);
								
							} else {
								$sql2 = "INSERT INTO files (id_surat_masuk,name, size, downloads) VALUES ('$idSuratMasuk','$filename', '$size', 0)";
								$result2=mysqli_query($conn, $sql2);
								
									
							}
							
						}	
					}
			  			
				}
				
				if($rows==1)
				{
					$q4="INSERT INTO audit_trail(kad_pengenalan,tarikh,masa,operasi,catatan)
					VALUES ('$ic','$date','$time','Tambah','Tambah 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 == false){window.location.href = '../daftarSurat.php'}";
						echo "else {window.location.href = '../daftarCek.php?idSuratMasuk=$idSuratMasuk'}";
						echo "</script>";
						exit;
						
					}
					else
					{
						echo 'MySQL Error: ' . mysqli_error($conn);;
						exit;	
					}
					
				}
				else
				{
					echo ("<script LANGUAGE='JavaScript'>
					window.alert('Maklumat surat masuk berjaya disimpan');
					window.location.href='../daftarSurat.php';
					</script>");
					exit;
				}
						     

		
			}		
			else
			{
				echo 'MySQL Error: ' . mysqli_error($conn);;
				exit;	
			}
				
	}	
	
?>