<?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>Data Bulan </button>
<?php if( isset($_GET['bln'] )  && isset($_GET['thn'] )) { ?>
<a href="excel_planactual.php?bln=<?php echo $_GET['bln']; ?>&thn=<?php echo $_GET['thn']; ?>" class="btn btn-primary" ><i class="bi bi-file-earmark-excel"></i> Export Excel</a>
<?php } else { ?>
<a href="excel_planactual.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 Plan Actual</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="30%">
				Product
			</th>
			<th width="15%">
				Plan
			</th>
			<th width="15%">
				Actual
			</th>
			<th width="15%">
				Packaging
			</th>
			<th width="25%">
				Uom
			</th>
		</tr>
		</thead>
		<tbody>
			<?php 
				if( isset($_GET['bln'] )  && isset($_GET['thn'] )) {
				
				$bln = $_GET['bln'];
				$thn = $_GET['thn'];
				$data = mysqli_query ($koneksi, "SELECT product.Material,
									product.ProductID,
									product.ItemDescription,
									product.SLoc,
									SUM(plan.QtyPlan) AS QtyPlan,
									product.QtyUnit
							FROM plan
							INNER JOIN product
							ON plan.ProductID=product.ProductID
							WHERE MONTH(DatePlan)='$bln' && YEAR(DatePlan)='$thn'
							GROUP BY product.ProductID");
					
				} else {

				$bln = date('m');
				$thn = date('Y');
				$data = mysqli_query ($koneksi, "SELECT product.Material,
									product.ProductID,
									product.ItemDescription,
									product.SLoc,
									SUM(plan.QtyPlan) AS QtyPlan,
									product.QtyUnit
							FROM plan
							INNER JOIN product
							ON plan.ProductID=product.ProductID
							WHERE MONTH(DatePlan)='$bln' && YEAR(DatePlan)='$thn'
							GROUP BY product.ProductID");
				
				
				}
				while($row = mysqli_fetch_array ($data)):
			?>
		<tr>
			<td>
				<a href="#" style="color: #4682B4" class='editbtn' id="detail" data-bln="<?php echo $bln; ?>" data-thn="<?php echo $thn; ?>" data-prod="<?php echo $row['ProductID']; ?>"><?php echo $row['Material']; ?> - <?php echo $row['ItemDescription']; ?></a>
			</td>
			<td>
				<?php echo number_format($row['QtyPlan']*$row['QtyUnit'],0,".","."); ?>
			</td>
			<td>
				<?php $data2 = mysqli_query ($koneksi, "SELECT SUM(Qty) AS Qty
									FROM stpj
									INNER JOIN plan
									ON stpj.PlanID=plan.PlanID
									INNER JOIN product
									ON plan.ProductID=product.ProductID
									WHERE MONTH(plan.DatePlan)='$bln' && YEAR(plan.DatePlan)='$thn' && product.ProductID='$row[ProductID]'
									GROUP BY product.ProductID");
				$row2 = mysqli_fetch_array ($data2);
				echo number_format($row2['Qty']*$row['QtyUnit'],0,".",".");
				?>
			</td>
			<td>
				<?php echo $row['SLoc']; ?>
			</td>
			<td>
				<?php echo number_format($row['QtyUnit'],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_planactual.php" autocomplete="off">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">&times;</button>
					<h4 class="modal-title">Cari Data Rekap Produksi</h4>
				</div>
					<div class="modal-body">
					<table style="width: 100%; background-color: white;">
					<tr>
						<th width="30%">Bulan & Tahun</th>
						<td width="70%">
							<select style="width: 40%;" name="bln" id="bln">
								<option  selected></option>
								<option value="01">Januari</option>
								<option value="02">Februari</option>
								<option value="03">Maret</option>
								<option value="04">April</option>
								<option value="05">Mei</option>
								<option value="06">Juni</option>
								<option value="07">Juli</option>
								<option value="08">Agustus</option>
								<option value="09">September</option>
								<option value="10">Oktober</option>
								<option value="11">November</option>
								<option value="12">Desember</option>
							</select>
							<select style="width: 40%;" name="thn" id="thn">
								<option  selected></option>
								<option value="2022">2022</option>
								<option value="2023">2023</option>
								<option value="2024">2024</option>
								<option value="2025">2025</option>
								<option value="2026">2026</option>
							</select>
						<p style="color:red" id="error_date_awal"></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>

	<div id="modal-detail" class="modal fade" role="dialog">
	    <div class="modal-dialog modal-lg">
	      <div class="modal-content">
	        <form role="form" id="form-edit" method="post" action="input-detailplanactual.php" autocomplete="off">
	        <div class="modal-header">
	          <button type="button" class="close" data-dismiss="modal">&times;</button>
	          <h4 class="modal-title">Detail Plan - Actual</h4>
	        </div>
	          <div class="modal-body">
	            <div id="data-detail">
	            
	            </div>        
	          </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'
		});		
	});	

	$(document).on('click','#detail',function(e){
	    e.preventDefault();
	    $("#modal-detail").modal('show');
	    $.post('detail_planactual.php',
	      {
	        bln:$(this).attr('data-bln'),
	        thn:$(this).attr('data-thn'),
	        prod:$(this).attr('data-prod')
	      },
	      function(html){
	        $("#data-detail").html(html);
	      }   
	    );
	  });

    </script>
