<?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_pemakaianbb.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_pemakaianbb.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 Pemakaian Bahan Baku</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%">
				Tanggal
			</th>
			<th width="20%">
				Dokumen
			</th>
			<th width="10%">
				Mesin
			</th>
			<th width="30%">
				Material
			</th>
			<th width="30%">
				Quantity
			</th>
		</tr>
		</thead>
		<tbody>
			<?php 
				if( isset($_GET['date_awal'] )  && isset($_GET['date_akhir'] )) {
				
				$data = mysqli_query ($koneksi, "SELECT plan.DatePlan,
									no_pbb.No_PBB,
									material.RawMat,
									material.NoMat,
									COUNT(pbb.PBB_ID) AS jml,
									mesin.NameMesin,
									material.Qty
							FROM plan
							INNER JOIN no_pbb
							ON plan.NoPBB_ID=no_pbb.NoPBB_ID
							INNER JOIN material
							ON plan.MaterialID=material.MaterialID
							INNER JOIN pbb
							ON no_pbb.NoPBB_ID=pbb.NoPBB_ID
							INNER JOIN mesin
							ON plan.MesinID=mesin.MesinID
							where material.MaterialID!='1' && material.MaterialID!='2' && DatePlan>='$_GET[date_awal]' && DatePlan<='$_GET[date_akhir]'
							GROUP BY no_pbb.NoPBB_ID
							ORDER BY plan.DatePlan ASC");
					
				} else {

				$bln = date('m');
				$thn = date('Y');
				$data = mysqli_query ($koneksi, "SELECT plan.DatePlan,
									no_pbb.No_PBB,
									material.RawMat,
									material.NoMat,
									COUNT(pbb.PBB_ID) AS jml,
									mesin.NameMesin,
									material.Qty
							FROM plan
							INNER JOIN no_pbb
							ON plan.NoPBB_ID=no_pbb.NoPBB_ID
							INNER JOIN material
							ON plan.MaterialID=material.MaterialID
							INNER JOIN pbb
							ON no_pbb.NoPBB_ID=pbb.NoPBB_ID
							INNER JOIN mesin
							ON plan.MesinID=mesin.MesinID
							where material.MaterialID!='1' && material.MaterialID!='2' && MONTH(plan.DatePlan)='$bln' && YEAR(plan.DatePlan)='$thn'
							GROUP BY no_pbb.NoPBB_ID
							ORDER BY plan.DatePlan ASC");
				
				
				}
				while($row = mysqli_fetch_array ($data)):
			?>
		<tr>
			<td>
				<?php echo date ("d-m-Y", strtotime($row['DatePlan'])); ?>
			</td>
			<td>
				<?php echo $row['No_PBB']; ?>
			</td>
			<td>
				<?php echo $row['NameMesin']; ?>
			</td>
			<td>
				<?php echo $row['NoMat']; ?> - <?php echo $row['RawMat']; ?>
			</td>
			<td>
				<?php echo ($row['jml']*$row['Qty'])/1000; ?>
			</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_pemakaianbb.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>
