<?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);	
	
	$userID = $row1["id_user"];
	
	$idSuratMasuk=$_GET['idSuratMasuk'];
	
	$query="SELECT `surat_masuk`.`id_surat_masuk`, `surat_masuk`.`no_rujukan_surat`, `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 `files`.`id_surat_masuk`=`surat_masuk`.`id_surat_masuk` 
	WHERE `surat_masuk`.`id_surat_masuk`='$idSuratMasuk';";
	$result= mysqli_query($conn,$query);
	$row = mysqli_fetch_array($result);
	
	$bahagian=$row['bahagian'];
	
	$sql2 = "SELECT * FROM `bahagian/unit` WHERE `nama_bahagian`='$bahagian'";
	$result2 = mysqli_query($conn,$sql2);
	$row2 = mysqli_fetch_array($result2);
	
	$id_bahagian= $row2['id_bahagian'];
	
	
	$sql3="select * from `staff` where `id_bahagian`='$id_bahagian'";
	$result3 = mysqli_query($conn,$sql3);
	
	$sql4="select * from `surat_masuk` LEFT JOIN `edaran_surat` ON `edaran_surat`.`id_surat_masuk`=`surat_masuk`.`id_surat_masuk`  WHERE `edaran_surat`.`id_surat_masuk` = '$idSuratMasuk'";
	$result4 = mysqli_query($conn,$sql4);
	
	$sql5="SELECT * FROM `edaran_surat` WHERE `id_surat_masuk`='$idSuratMasuk' ORDER BY `id_edaran` DESC";
	$result5=mysqli_query($conn,$sql5);
	$row5 = mysqli_fetch_array($result5);
	
	 $edaranKepada=$row5['edaran_kepada'];
	 $namastaff=$row1['nama_user'];

	
?>


<!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">

  <!-- 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">

    <div class="pagetitle">
      <h1>Edaran Surat Bahagian/Unit</h1>
     
    </div><!-- End Page Title -->
	<?php
	if ($row1['peranan_user']=='USER')
	{
	?>
    <section class="section">
      <div class="row">
         <div class="card">
            <div class="card-body">
              <h5 class="card-title">Maklumat Surat Masuk<?php echo $id_bahagian; ?></h5>

              <!-- No Labels Form -->
              
                <div class="row mb-2">
                  <label  class="col-sm-8 col-form-label">Rujukan Surat: <b><?php echo $row["no_rujukan_surat"];?></b></label>
                </div>
               
			   <div class="row mb-2">
				<label for="inputDate" class="col-sm-5 col-form-label">Tarikh Surat:&nbsp;&nbsp;&nbsp;    <b><?php echo date('d-m-Y',strtotime($row['tarikh_surat'])) ?></b> </label> 
				<label for="inputDate" class="col-sm-4 col-form-label">Tarikh Terima Surat: <b><?php echo date('d-m-Y',strtotime($row["tarikh_terima_surat"])) ; ?></b></label>
			   </div>
               
                <div class="row mb-2">
                  <label for="inputNumber" class="col-sm-8 col-form-label">Perkara:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;     <b><?php echo $row["tajuk1"]; echo $row["tajuk2"]; echo $row["tajuk3"]; ?></b></label>
                </div>
				<div class="row mb-2">
					 <label class="col-sm-5 col-form-label">Bahagian/Unit: <b><?php echo $row["bahagian"]; ?></b></label>             
					<label class="col-sm-4 col-form-label">Tindakan: <b><?php echo $row["tindakan"]; ?></b></label>	
                </div>
				<div class="row mb-2">
				  <label class="col-sm-2 col-form-label">Sesi: <b><?php echo $row["sesi"]; ?></b></label>
                  
				  <label for="inputNumber" class="col-sm-6 col-form-label">File Upload:
				  <?php
				  if($row['name']=="")
				  {
				  ?>
                   <label for="inputNumber" class="col-sm-6 col-form-label"><b>TIADA FILE YANG DIMUAT NAIK</b></label>
				<?php
				  }
				  else
				  {
				?>
				<a href="uploads/<?php echo $row['name']; ?>" target="_blank"><i class="bi bi-file-earmark-fill"></i></a>
				<?php
				  }
				  ?>
				  </label>
                </div>				
                
   
              <!-- End No Labels Form -->
			  <h5 class="card-title">Maklumat Edaran</h5>
			  <form class="row g-3" action="internalPage/insertEdaranUser.php?idSuratMasuk=<?php echo $idSuratMasuk ?>&iduser=<?php echo $row1['id_user']; ?>" method="post" />
				
			  <?php
				if($namastaff==$edaranKepada)
				{
			  ?>
				
				<div class="row mb-3">                 
                  <label class="col-sm-2 col-form-label">Kepada:</label>
                  <div class="col-sm-6">                   	
					<select class="form-select" aria-label="Default select example" name="staff" id="staff" >
					  <option value="" >--Pilih--</option>
						<?php 
							while ($row3 = mysqli_fetch_array($result3))
							{
						?>
					  <option value="<?php echo $row3["staff_name"]; ?>"><?php echo $row3["staff_name"]; ?></option>
					  <?php
							}
					  ?>
					</select>	  
                  </div>
                
				  <label for="inputDate" class="col-sm-2 col-form-label" data-date-format="dd/mm/yy">Tarikh Edaran:</label>
                  <div class="col-sm-2">
                    <input type="date" class="form-control" name="tarikhEdaran" id="tarikhEdaran" >
                  </div>
				  
                </div>
                <div class="row mb-3">
                  <label for="inputNumber" class="col-sm-2 col-form-label" >Catatan:</label>
                  <div class="col-sm-6">
                    <textarea type="text" class="form-control" style="text-transform:uppercase;" name="catatan" id="catatan" ></textarea>					
                  </div>
                </div>
				<?php
					
				}
				else
				{
				?>
				<div class="row mb-3">                 
                  <label class="col-sm-2 col-form-label">Kepada:</label>
                  <div class="col-sm-6">                   	
					<select class="form-select" aria-label="Default select example" name="staff" id="staff" disabled>
					  <option value="" >--Pilih--</option>
						<?php 
							while ($row3 = mysqli_fetch_array($result3))
							{
						?>
					  <option value="<?php echo $row3["staff_name"]; ?>"><?php echo $row3["staff_name"]; ?></option>
					  <?php
							}
					  ?>
					</select>	  
                  </div>
                
				  <label for="inputDate" class="col-sm-2 col-form-label" data-date-format="dd/mm/yy">Tarikh Edaran:</label>
                  <div class="col-sm-2">
                    <input type="date" class="form-control" name="tarikhEdaran" id="tarikhEdaran" disabled>
                  </div>
				  
                </div>
				
				<div class="row mb-3">                 
                  <label class="col-sm-2 col-form-label">Kepada:</label>
                  <div class="col-sm-6">                   	
					<select class="form-select" aria-label="Default select example" name="staff" id="staff" disabled>
					  <option value="" >--Pilih--</option>
						<?php 
							while ($row3 = mysqli_fetch_array($result3))
							{
						?>
					  <option value="<?php echo $row3["staff_name"]; ?>"><?php echo $row3["staff_name"]; ?></option>
					  <?php
							}
					  ?>
					</select>	  
                  </div>
                
				  <label for="inputDate" class="col-sm-2 col-form-label" data-date-format="dd/mm/yy">Tarikh Edaran:</label>
                  <div class="col-sm-2">
                    <input type="date" class="form-control" name="tarikhEdaran" id="tarikhEdaran" disabled>
                  </div>
				  
                </div>
                <div class="row mb-3">
                  <label for="inputNumber" class="col-sm-2 col-form-label" >Catatan:</label>
                  <div class="col-sm-6">
                    <textarea type="text" class="form-control" style="text-transform:uppercase;" name="catatan" id="catatan" disabled></textarea>					
                  </div>
                </div>
				<?php
				}
				?>
				<div class="text-center">
                  <button type="submit" class="btn btn-primary">Simpan</button>
                  <button type="reset" class="btn btn-secondary">Reset</button>
                </div>
			 </form>

            </div>
          </div>
          </div>

        </div>
      </div>
    </section>
	<?php
	}
	else
	{
	?>
	<section class="section">
      <div class="row">
         <div class="card">
            <div class="card-body">
              <h5 class="card-title">Maklumat Surat Masuk<?php echo $id_bahagian?></h5>

              <!-- No Labels Form -->
              
                <div class="row mb-2">
                  <label  class="col-sm-8 col-form-label">Rujukan Surat: <b><?php echo $row["no_rujukan_surat"]; ?></b></label>
                </div>
               
			   <div class="row mb-2">
				<label for="inputDate" class="col-sm-5 col-form-label">Tarikh Surat:&nbsp;&nbsp;&nbsp;    <b><?php echo date('d-m-Y',strtotime($row['tarikh_surat'])) ?></b> </label> 
				<label for="inputDate" class="col-sm-4 col-form-label">Tarikh Terima Surat: <b><?php echo date('d-m-Y',strtotime($row["tarikh_terima_surat"])) ; ?></b></label>
			   </div>
               
                <div class="row mb-2">
                  <label for="inputNumber" class="col-sm-8 col-form-label">Perkara:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b><?php echo $row["tajuk1"]; echo $row["tajuk2"]; echo $row["tajuk3"]; ?></b></label>
                </div>
				<div class="row mb-2">
					 <label class="col-sm-5 col-form-label">Bahagian/Unit: <b><?php echo $row["bahagian"]; ?></b></label>             
					<label class="col-sm-4 col-form-label">Tindakan: <b><?php echo $row["tindakan"]; ?></b></label>	
                </div>
				<div class="row mb-2">
				  <label class="col-sm-2 col-form-label">Sesi: <b><?php echo $row["sesi"]; ?></b></label>
                  
				  <label for="inputNumber" class="col-sm-6 col-form-label">File Upload:
				  <?php
				  if($row['name']=="")
				  {
				  ?>
                   <label for="inputNumber" class="col-sm-6 col-form-label"><b>TIADA FILE YANG DIMUAT NAIK</b></label>
				<?php
				  }
				  else
				  {
				?>
				<a href="uploads/<?php echo $row['name']; ?>" target="_blank"><i class="bi bi-file-earmark-fill"></i></a>
				<?php
				  }
				  ?>
				  </label>
                </div>				
                
   
              <!-- End No Labels Form -->
			  <h5 class="card-title">Maklumat Edaran</h5>
			  <form class="row g-3" action="internalPage/insertEdaranUser.php?idSuratMasuk=<?php echo $idSuratMasuk ?>&iduser=<?php echo $row1['id_user'] ?>" method="post" >
				<div class="row mb-3">                 
                  <label class="col-sm-2 col-form-label">Kepada:</label>
                  <div class="col-sm-6">                   	
					<select class="form-select" aria-label="Default select example" name="staff" id="staff" >
					  <option value="" >--Pilih--</option>
						<?php 
							while ($row3 = mysqli_fetch_array($result3))
							{
						?>
					  <option value="<?php echo $row3["staff_name"]; ?>"><?php echo $row3["staff_name"]; ?></option>
					  <?php
							}
					  ?>
					</select>	  
                  </div>
                
				  <label for="inputDate" class="col-sm-2 col-form-label" data-date-format="dd/mm/yy">Tarikh Edaran:</label>
                  <div class="col-sm-2">
                    <input type="date" class="form-control" name="tarikhEdaran" id="tarikhEdaran" >
                  </div>
				  
                </div>
                <div class="row mb-3">
                  <label for="inputNumber" class="col-sm-2 col-form-label" >Catatan:</label>
                  <div class="col-sm-6">
                    <textarea type="text" class="form-control" style="text-transform:uppercase;" name="catatan" id="catatan" ></textarea>					
                  </div>
                </div>
				
				<div class="text-center">
                  <button type="submit" class="btn btn-primary">Simpan</button>
                  <button type="reset" class="btn btn-secondary">Reset</button>
                </div>
			 </form>

            </div>
          </div>
          </div>

        </div>
      </div>
    </section>
	<?php
	}
	?>
  </main><!-- End #main -->
  
  <main id="main" class="main" style="margin-top:0px;">
    <div class="pagetitle">
		  <h1>History Edaran Surat</h1>
		  <nav>
			<ol class="breadcrumb">
			  <li class="breadcrumb-item"><a href="">No. Rujukan Surat: <?php echo $row['no_rujukan_surat']?></a></li>          
			</ol>
		  </nav>
		</div><!-- End Page Title -->
		
		<section class="section">
		  <div class="row">
					<section class="section">
						<div class="col-lg-12">

						  <div class="card" style="overflow:scroll">
							<div class="card-body">

							  <!-- 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 SURAT</th>
									<th scope="col">NO.RUJUKAN SURAT</th>
									
									<th scope="col">EDARAN KEPADA</th>
									<th scope="col">CATATAN</th>
									<th scope="col">TARIKH EDARAN</th>	
								  </tr>
								</thead>
								<tbody style="font-size:12px;color:black">
								<?php
								
									$no=0;
									
									while ($row4 = mysqli_fetch_array($result4))
									{
										$no++;
										
										
								
								?>
								  <tr >
									<th scope="row"><?php echo $no; ?></th>
									<td><?php echo date('d-m-Y',strtotime($row4["tarikh_terima_surat"])) ; ?></td>
									<td><?php echo $row4["no_rujukan_surat"]; ?></td>																		
									<td><?php echo $row4["edaran_kepada"];?></td>
									<td><?php echo $row4["catatan_edaran"];?></td>
									<td><?php echo $row4["tarikh_edaran"];?></td>
									<?php
									}
										
									?>	
								</tbody>
							  </table>
							  <!-- End Table with stripped rows -->

							</div>
						</div>
					</section>
			</div>
			
		  </div>
	    </section>
	</main>

  <!-- ======= Footer ======= -->
  <footer id="footer" class="footer">
    <!--<div class="copyright">
      &copy; Copyright <strong><span>NiceAdmin</span></strong>. All Rights Reserved
    </div>-->
    <div class="credits">
      <!-- All the links in the footer should remain intact. -->
      <!-- You can delete the links only if you purchased the pro version. -->
      <!-- Licensing information: https://bootstrapmade.com/license/ -->
      <!-- Purchase the pro version with working PHP/AJAX contact form: https://bootstrapmade.com/nice-admin-bootstrap-admin-html-template/ -->
      <!--Designed by <a href="https://bootstrapmade.com/">BootstrapMade</a>-->
    </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>