<?php 
	include "../dashboard/header.php";
	include "../koneksi.php"; 
?>
<div class="row">
<div class="col-md-6">
<p>
<button type="button" class="btn btn-primary" id="simulasi"><i class="bi bi-calendar-week">
</i>Range Tanggal</button>
<?php if( isset($_GET['date_awal'] )  && isset($_GET['date_akhir'] )) { ?>
<a href="excel_pengiriman.php?date_awal=<?php echo $_GET['date_awal']; ?>&date_akhir=<?php echo $_GET['date_akhir']; ?>" class="btn btn-primary" ><i class="bi bi-file-earmark-excel"></i> Export Excel</a>
<?php } else { ?>
<a href="excel_pengiriman.php" class="btn btn-primary" ><i class="bi bi-file-earmark-excel-fill"></i> Export Excel</a>
<?php } ?>
</p>	
</div>
<div class="col-md-6">
	 <nav>
    <ol class="breadcrumb">
		<li class="breadcrumb-item">Pengiriman</li>
        <li class="breadcrumb-item">Table Pengiriman</li>
    </ol>
</nav>
</div>
</div>
<div style="width:100%;height500px;overflow:scroll;" >
<table id="example" class="display nowrap" style="width:100%">
        <thead>
		<tr>
			<th width="10%">
				Date Kirim 
			</th>
			<th width="25%">
				Nama Customer
			</th>
			<th width="10%">
				NO DN
			</th>
			<th width="25%">
				NO PO
			</th>
			<th width="20%">
				Product Name
			</th>
			<th width="10%">
				Amount
			</th>
		</tr>
		</thead>
		<tbody>
			<?php 
				if( isset($_GET['date_awal'] )  && isset($_GET['date_akhir'] )) {
				

				$data = mysqli_query ($koneksi, "SELECT 
									DateKirim,
									customer.CustomerName,
									NoDN,
									po_tbl.No_PO,
									product.ItemDescription,
									(deliverytbl.QtyKirim*product.QtyUnit) AS Amount
							FROM deliverytbl
							INNER JOIN detail_po
							ON deliverytbl.DetailPO_ID=detail_po.DetailPO_ID
							INNER JOIN po_tbl
							ON detail_po.PO_ID=po_tbl.PO_ID
							INNER JOIN product
							ON detail_po.ProductID=product.ProductID
							INNER JOIN customer
							ON po_tbl.CustomerID=customer.CustomerID
							where DateKirim>='$_GET[date_awal]' && DateKirim<='$_GET[date_akhir]'
							ORDER BY DateKirim DESC");
					
				} else {

				$bln = date('m');
				$thn = date('Y');
				$data = mysqli_query ($koneksi, "SELECT 
									DateKirim,
									customer.CustomerName,
									NoDN,
									po_tbl.No_PO,
									product.ItemDescription,
									(deliverytbl.QtyKirim*product.QtyUnit) AS Amount
							FROM deliverytbl
							INNER JOIN detail_po
							ON deliverytbl.DetailPO_ID=detail_po.DetailPO_ID
							INNER JOIN po_tbl
							ON detail_po.PO_ID=po_tbl.PO_ID
							INNER JOIN product
							ON detail_po.ProductID=product.ProductID
							INNER JOIN customer
							ON po_tbl.CustomerID=customer.CustomerID
							WHERE MONTH(DateKirim)='$bln' && YEAR(DateKirim)='$thn'
							ORDER BY DateKirim DESC");
				
				
				}
				while($row = mysqli_fetch_array ($data)):
			?>
		<tr>
			<td>
				<?php echo date ("d-m-Y", strtotime($row['DateKirim'])); ?>
			</td>
			<td>
				<?php echo $row['CustomerName']; ?>
			</td>
			<td>
				<?php echo $row['NoDN']; ?>
			</td>
			<td>
				<?php echo $row['No_PO']; ?>
			</td>
			<td>
				<?php echo $row['ItemDescription']; ?>
			</td>
			<td>
				<?php echo number_format($row['Amount'],0,".","."); ?>
			</td>
		</tr>
			<?php endwhile; ?>
		</tbody>
	</table>
	</div>
		<!-- Modal -->
		<div id="modal-simulasi" class="modal fade" role="dialog">
		<div class="modal-dialog modal-md">
			<div class="modal-content">
				<form role="form" id="form-edit" method="post" action="link_pengiriman.php" autocomplete="off">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">&times;</button>
					<h4 class="modal-title">Cari Data Berdasarkan Range Tanggal</h4>
				</div>
					<div class="modal-body">
					<table style="width: 100%; background-color: white;">
					<tr>
						<th width="30%">Tanggal Awal</th>
						<td width="70%"><input style="width: 70%;" type="text" class="input-tanggal" id="date_awal" name="date_awal" data-date-format="dd-mm-yy">
						<p style="color:red" id="error_date_awal"></td>
					</tr>
					<tr>
						<th>Tanggal Akhir</th>
						<td><input style="width: 70%;" type="text" class="input-tanggal" id="date_akhir" name="date_akhir" data-date-format="dd-mm-yy">
							<p style="color:red" id="error_date_akhir"></td>
					</tr>
					</div>
				</table>
				</div>
					<div class="modal-footer">
          <button type="button" class="btn btn-info" data-dismiss="modal">Batal</button>
						<button type="submit" class="btn btn-primary" >Simpan</button>
					</div>
				</form>		
			</div>
		</div>
	</div>

	<link rel="stylesheet" type="text/css" href="../style/jquery-ui.css">
	<script type="text/javascript" src="../style/jquery-ui.js"></script>

	<script>

	$(document).ready(function () {
		$('#example').DataTable({
			scrollX: true,
		});
	});

	
	$(document).on('click','#simulasi',function(e){
		e.preventDefault();
		$("#modal-simulasi").modal('show');
	});

	$(document).ready(function(){
		$('.input-tanggal').datepicker({
			dateFormat: 'yy-mm-dd'
		});		
	});	

    </script>
