<?php 
include 'connect.php';
session_start();
	
	$sql1 = "select * from `user` where kad_pengenalan = '".$_SESSION['kadPengenalan']."' ";
	$result1 = mysqli_query($conn,$sql1);
	$row1 = mysqli_fetch_array($result1);
	
	$sql2 = "SELECT * FROM `bahagian/unit`";
	$result2 = mysqli_query($conn,$sql2);

	if(isset($_POST['pdf'])) {
		pdf();
	}
	else {
	}

	$sql1 = "select * from `user` where kad_pengenalan = '".$_SESSION['kadPengenalan']."' ";
	$result1 = mysqli_query($conn,$sql1);
	$row1 = mysqli_fetch_array($result1);
	
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">

  <title>Sistem Pendaftaran Surat</title>
  <meta content="" name="description">
  <meta content="" name="keywords">

  <!-- Favicons -->
  <link href="assets/img/logo_tab.png" rel="icon">

  <!-- Google Fonts -->
  <link href="https://fonts.gstatic.com" rel="preconnect">
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Nunito:300,300i,400,400i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">

  <!-- Vendor CSS Files -->
  <link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <link href="assets/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">
  <link href="assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
  <link href="assets/vendor/quill/quill.snow.css" rel="stylesheet">
  <link href="assets/vendor/quill/quill.bubble.css" rel="stylesheet">
  <link href="assets/vendor/remixicon/remixicon.css" rel="stylesheet">
  <link href="assets/vendor/simple-datatables/style.css" rel="stylesheet">
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>  
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>   
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.0/js/bootstrap-datepicker.js"></script> 
  
  <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.0/css/bootstrap-datepicker.css" rel="stylesheet">  
   

  <!-- Template Main CSS File -->
  <link href="assets/css/style.css" rel="stylesheet">
  
  <!-- Script logout -->
  <script type="text/javascript" src="internalPage/logoutConfirmation.js"></script>

  <!-- =======================================================
  * Template Name: NiceAdmin - v2.2.0
  * Template URL: https://bootstrapmade.com/nice-admin-bootstrap-admin-html-template/
  * Author: BootstrapMade.com
  * License: https://bootstrapmade.com/license/
  ======================================================== -->
</head>

<body>

 <?php 
  include 'headerUser.php';
 ?>


	<main id="main" class="main" >
	<form class="row g-3" action="edaranUser.php" method="post" >	
    <div class="pagetitle">
      <h1>Edaran Surat Bahagian/Unit <br> <?php echo $row1['bahagian']; ?></h1><br>
		
		<div class="row mb-3">
		  <label for="inputDate" class="col-sm-2 col-form-label">Tarikh Surat Diterima</label>
		  <div class="col-sm-3">
			<input type="date"  class="form-control" name="tarikh" id="tarikh" data-date-format="dd-mm-yy">
			<div class="invalid-feedback">Sila masukkan tarikh mula.</div>
		  </div>
		</div>
		<div class="row mb-3">
		  <label for="inputDate" class="col-sm-2 col-form-label">Hingga</label>
		  <div class="col-sm-3">
			<input type="date"  class="form-control" name="tarikh1" id="tarikh1" data-date-format="dd-mm-yy">
			<div class="invalid-feedback">Sila tarikh hingga.</div>
		  </div>
		</div>
		<button type="submit" class="btn btn-primary">Cari</button>
		<button type="reset" class="btn btn-secondary">Reset</button>
		
	</form>
    </div><!-- End Page Title -->
	
      <div class="row">
        <div class="col-lg-12">
		
          <div class="card" style="overflow:scroll">			  
				
				<?php
						if(count($_POST)>0)
						{
							$bahagian=$row1['bahagian'];
							$tarikh=$_POST['tarikh'];	
							$tarikh1 =$_POST['tarikh1'];
							$tarikhNew = date("Y-m-d", strtotime($tarikh));
							$tarikhNew1 = date("Y-m-d", strtotime($tarikh1));
							
							
							if($bahagian=='UNIT PENTADBIRAN TANAH')
							{
								$query="SELECT `surat_masuk`.`no_rujukan_surat`,`surat_masuk`.`id_surat_masuk`, `surat_masuk`.`id_user`, `surat_masuk`.`tarikh_surat`,
								`surat_masuk`.`tarikh_terima_surat`, `surat_masuk`.`tajuk1`, `surat_masuk`.`tajuk2`, `surat_masuk`.`tajuk3`, `surat_masuk`.`bahagian`, `surat_masuk`.`tindakan`,
								`surat_masuk`.`nama_pengirim`,`surat_masuk`.`sesi`, `surat_masuk`.`file_upload`,
								`files`.`id`, `files`.`name`, `files`.`size` FROM `surat_masuk` LEFT JOIN `files` ON `surat_masuk`.`id_surat_masuk`=`files`.`id_surat_masuk`	
								WHERE  `surat_masuk`.`bahagian`='$bahagian' OR `surat_masuk`.`bahagian`='UNIT PEMBANGUNAN HARTANAH DAN HARTA WAKAF' AND `surat_masuk`.`tarikh_terima_surat` BETWEEN '$tarikhNew' AND '$tarikhNew1' ORDER BY `surat_masuk`.`id_surat_masuk` DESC ;";
								$result= mysqli_query($conn,$query);
							}
							else
							{
								$query="SELECT `surat_masuk`.`no_rujukan_surat`,`surat_masuk`.`id_surat_masuk`, `surat_masuk`.`id_user`, `surat_masuk`.`tarikh_surat`,
								`surat_masuk`.`tarikh_terima_surat`, `surat_masuk`.`tajuk1`, `surat_masuk`.`tajuk2`, `surat_masuk`.`tajuk3`, `surat_masuk`.`bahagian`, `surat_masuk`.`tindakan`,
								`surat_masuk`.`nama_pengirim`,`surat_masuk`.`sesi`, `surat_masuk`.`file_upload`,
								`files`.`id`, `files`.`name`, `files`.`size` FROM `surat_masuk` LEFT JOIN `files` ON `surat_masuk`.`id_surat_masuk`=`files`.`id_surat_masuk`	
								WHERE  `surat_masuk`.`bahagian`='$bahagian' AND `surat_masuk`.`tarikh_terima_surat` BETWEEN '$tarikhNew' AND '$tarikhNew1' ORDER BY `surat_masuk`.`id_surat_masuk` DESC ;";
								$result= mysqli_query($conn,$query);
							}
				?>
				
					 <h5 class="card-title" style="font-size:12px;color:black;text-align: center;">
					 <b>MAJLIS AGAMA ISLAM NEGERI PULAU PINANG</b>
					 <br>
					 <b>LAPORAN EDARAN SURAT  <?php echo $bahagian;?>
					</b></h5>	
		
				  <!-- Table with stripped rows -->
				  <table class="table datatable">
                <thead>
                  <tr style="font-size:14px;color:black">
                    <th scope="col">BIL</th>
					<th scope="col">TARIKH TERIMA SURAT</th>
                    <th scope="col">NO.RUJUKAN SURAT</th>
                    <th scope="col">TARIKH SURAT</th>
                    <th scope="col">PERKARA</th>														
					<th scope="col">FILE</th>
					<th scope="col">MAKLUMAT EDARAN</th>
					
					
                  </tr>
                </thead>
                <tbody style="font-size:12px;color:black">
				<?php
				
					$no=0;
					
					while ($row = mysqli_fetch_array($result))
					{
						$no++;
						
						$sql3 = "select * from `edaran_surat` where id_surat_masuk = '".$row["id_surat_masuk"]."' ";
						$result3 = mysqli_query($conn,$sql3);
						$row3 = mysqli_num_rows($result3);
						
						if ($row3>=1)
						{
				
				?>
                  <tr style="background-color:powderblue;" >
				  <?php
						}
						else
						{
					?>
						<tr>
						<?php
						}
						?>
                    <th scope="row"><?php echo $no; ?></th>
					<td><?php echo date('d-m-Y',strtotime($row["tarikh_terima_surat"])) ; ?></td>
                    <td><?php echo $row["no_rujukan_surat"]; ?></td>
                    <td><?php echo date('d-m-Y',strtotime($row['tarikh_surat'])) ?></td>
                    <td><?php echo $row["tajuk1"]; echo $row["tajuk2"]; echo $row["tajuk3"]; ?></td>										
					<?php
					
					if ($row['name'] == "")
					{	
					?>
					<td></td>
					<?php
					}
					
					else
					{
					
					?>
					<td><a href="uploads/<?php echo $row['name']; ?>" target="_blank"><i class="bi bi-file-earmark-fill"></i></a></td>
					<?php
					}
					
					?>
					<td><a href="ubahsuaiEdaranUser.php?idSuratMasuk=<?php echo $row["id_surat_masuk"]; ?>&tarikhNew=<?php echo $tarikhNew;?>&tarikhNew1=<?php echo $tarikhNew1;?>"><center><i class="bi bi-arrow-right-square-fill"></i></center></a></td>

                  </tr>
                <?php
					}
					}
					else
					{
							$bahagian=$row1['bahagian'];
							
							if($bahagian=='UNIT PENTADBIRAN TANAH')
							{
								$query="SELECT `surat_masuk`.`no_rujukan_surat`,`surat_masuk`.`id_surat_masuk`, `surat_masuk`.`id_user`, `surat_masuk`.`tarikh_surat`,
								`surat_masuk`.`tarikh_terima_surat`, `surat_masuk`.`tajuk1`, `surat_masuk`.`tajuk2`, `surat_masuk`.`tajuk3`, `surat_masuk`.`bahagian`, `surat_masuk`.`tindakan`,
								`surat_masuk`.`nama_pengirim`,`surat_masuk`.`sesi`, `surat_masuk`.`file_upload`,
								`files`.`id`, `files`.`name`, `files`.`size` FROM `surat_masuk` LEFT JOIN `files` ON `surat_masuk`.`id_surat_masuk`=`files`.`id_surat_masuk` 
								WHERE  `surat_masuk`.`bahagian`='$bahagian' OR `surat_masuk`.`bahagian`='UNIT PEMBANGUNAN HARTANAH DAN HARTA WAKAF' ORDER BY `surat_masuk`.`id_surat_masuk` DESC";
								$result= mysqli_query($conn,$query);
							}
							else
							{
								$query="SELECT `surat_masuk`.`no_rujukan_surat`,`surat_masuk`.`id_surat_masuk`, `surat_masuk`.`id_user`, `surat_masuk`.`tarikh_surat`,
								`surat_masuk`.`tarikh_terima_surat`, `surat_masuk`.`tajuk1`, `surat_masuk`.`tajuk2`, `surat_masuk`.`tajuk3`, `surat_masuk`.`bahagian`, `surat_masuk`.`tindakan`,
								`surat_masuk`.`nama_pengirim`,`surat_masuk`.`sesi`, `surat_masuk`.`file_upload`,
								`files`.`id`, `files`.`name`, `files`.`size` FROM `surat_masuk` LEFT JOIN `files` ON `surat_masuk`.`id_surat_masuk`=`files`.`id_surat_masuk` 
								WHERE  `surat_masuk`.`bahagian`='$bahagian' ORDER BY `surat_masuk`.`id_surat_masuk` DESC";
								$result= mysqli_query($conn,$query);
							}
				
				?>
						<h5 class="card-title" style="font-size:12px;color:black;text-align: center;">
					 <b>MAJLIS AGAMA ISLAM NEGERI PULAU PINANG</b>
					 <br>
					 <b>LAPORAN EDARAN SURAT  <?php echo $bahagian;?>
					 </b></h5>
		
				  <!-- Table with stripped rows -->
				  <table class="table datatable">
                <thead>
                  <tr style="font-size:14px;color:black">
                    <th scope="col">BIL</th>
					<th scope="col">TARIKH TERIMA SURAT</th>
                    <th scope="col">NO.RUJUKAN SURAT</th>
                    <th scope="col">TARIKH SURAT</th>
                    <th scope="col">PERKARA</th>														
					<th scope="col">FILE</th>
					<th scope="col">MAKLUMAT EDARAN</th>
					
					
                  </tr>
                </thead>
                <tbody style="font-size:12px;color:black">
				<?php
				
					$no=0;
					
					while ($row = mysqli_fetch_array($result))
					{
						$no++;
						$sql3 = "select * from `edaran_surat` where id_surat_masuk = '".$row["id_surat_masuk"]."' ";
						$result3 = mysqli_query($conn,$sql3);
						$row3 = mysqli_num_rows($result3);
						
						if ($row3>=1)
						{
				
				?>
                  <tr style="background-color:powderblue;" >
				  <?php
						}
						else
						{
					?>
						<tr>
						<?php
						}
						?>
                    <th scope="row"><?php echo $no; ?></th>
					<td><?php echo date('d-m-Y',strtotime($row["tarikh_terima_surat"])) ; ?></td>
                    <td><?php echo $row["no_rujukan_surat"]; ?></td>
                    <td><?php echo date('d-m-Y',strtotime($row['tarikh_surat'])) ?></td>
                    <td><?php echo $row["tajuk1"]; echo $row["tajuk2"]; echo $row["tajuk3"]; ?></td>										
					<?php
					
					if ($row['name'] == "")
					{	
					?>
					<td></td>
					<?php
					}
					
					else
					{
					
					?>
					<td><a href="uploads/<?php echo $row['name']; ?>" target="_blank"><i class="bi bi-file-earmark-fill"></i></a></td>
					<?php
					}
					
					?>
					<td><a href="ubahsuaiEdaranUser.php?idSuratMasuk=<?php echo $row["id_surat_masuk"]; ?>"><center><i class="bi bi-arrow-right-square-fill"></i></center></a></td>

                  </tr>	
				<?php
					}
					}	
				?>
                </tbody>
              </table>
				
			</div>	
          </div>

        </div>
      </div>

  </main><!-- End #main -->

  <!-- ======= Footer ======= -->
  <footer id="footer" class="footer">
    <div class="copyright">
      &copy; Copyright <strong><span>Majlis Agama Islam Negeri Pulau Pinang @ 2022</span></strong>.
    </div>
  </footer><!-- End Footer -->

  <a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i class="bi bi-arrow-up-short"></i></a>

  <!-- Vendor JS Files -->
  <script src="assets/vendor/apexcharts/apexcharts.min.js"></script>
  <script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  <script src="assets/vendor/chart.js/chart.min.js"></script>
  <script src="assets/vendor/echarts/echarts.min.js"></script>
  <script src="assets/vendor/quill/quill.min.js"></script>
  <script src="assets/vendor/simple-datatables/simple-datatables.js"></script>
  <script src="assets/vendor/tinymce/tinymce.min.js"></script>
  <script src="assets/vendor/php-email-form/validate.js"></script>
  

  <!-- Template Main JS File -->
  <script src="assets/js/main.js"></script>
  
 

</body>

</html>