<?php include "../koneksi.php";
$datasloc = mysqli_query ($koneksi, "SELECT SLoc
                              FROM product
                                WHERE ProductID='$_GET[prod]'");
$row2= mysqli_fetch_array($datasloc);
?>
<table class="table table-bordered">
		<tr>
			<th width="25%">
				Kode
			</th>
			<th width="20%">
				NoLot
			</th>
			<th width="20%">
				Date
			</th>
		<?php if($row2['SLoc']==='BOX2') { ?>
			<th width="25%">
				Quantity
			</th>
		<?php } else { ?>
			<th width="25%">
				Nick
			</th>
		<?php } ?>
			<th width="5%" >
				#
			</th>
			
		</tr>
			<?php 
				$data = mysqli_query ($koneksi, " SELECT 
                                        stpj.STPJ_ID,
                                        stpj.Del_ID,
                                        plan.PlanID,
                                        plan.DatePlan,
                                        plan.POOR,
                                        product.ProductID,
                                        product.ItemDescription,
                                        product.SLoc,
                                        product.QtyUnit,
                                        stpj.Kode,
                                        stpj.NoLot,
                                        stpj.Nick,
                                        stpj.Qty
                                FROM stpj 
                                INNER JOIN plan
                                ON stpj.PlanID=plan.PlanID
                                INNER JOIN product
                                ON plan.ProductID=product.ProductID
                                where stpj.Del_ID='$_GET[id]'
                                Order by plan.DatePlan, stpj.kode ASC");
	

				while($row = mysqli_fetch_array ($data)): ?>
		<tr>
			
			<td width="25%">
				<?php echo $row['POOR']; ?>-<?php if ($row['Kode']<10) {
					echo '00'.$row['Kode'];
				} else if ($row['Kode']<100) {
				    echo '0'.$row['Kode'];
				} else {
					echo $row['Kode'];
				} 
				?>-<?php echo substr(preg_replace('/[^A-Za-z0-9]/', '', $row['DatePlan']), 2, 4); ?>
			</td>
			<td width="20%">
				<?php echo $row['NoLot']; ?>
			</td>
			<td width="20%">
				<?php echo date ("d-m-Y", strtotime($row['DatePlan'])); ?>
			</td>
		<?php if($row2['SLoc']==='BOX2') { ?>
			<td width="20%">
				<a href="#" style="color: #0000FF" class='editbtn' id="tambah" data-id="<?php echo $row['STPJ_ID']; ?>"><?php echo $row['Qty']; ?> <?php echo substr($row['SLoc'], 0, 3); ?>@<?php echo $row['QtyUnit']*$row['Qty']; ?>pcs </a>
			</td>
		<?php } else { ?>
			<td width="20%">
				<?php echo $row['Nick']; ?>
			</td>
		<?php } ?>
			<td width="5%" >
				<a href="in.php?id=<?php echo $row['STPJ_ID']; ?>&del_id=<?php echo $_GET['id']; ?>&prod=<?php echo $row['ProductID']; ?>" style="color: black;" ><i class="bi bi-arrow-right-square-fill"></i></a>
			</td>
		</tr>
			<?php endwhile; ?>
	</table>

	<div id="modal-tambah" class="modal fade" role="dialog">
		<div class="modal-dialog modal-md">
			<div class="modal-content">
				<form role="form" id="form-edit" method="post" action="inputdetailpo.php" autocomplete="off">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">&times;</button>
					<h4 class="modal-title">Edit Quantity Box</h4>
				</div>
					<div class="modal-body">
						<div id="data-tambah">
						
						</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>

	<script>
	$(document).on('click','#tambah',function(e){
		e.preventDefault();
		$("#modal-tambah").modal('show');
		$.post('adjust.php',
			{id:$(this).attr('data-id')},
			function(html){
				$("#data-tambah").html(html);
			}   
		);
	});

	$("#form-tambah").submit(function(e) {
		e.preventDefault();
		
		$("#error_qty").html('');
		
		var dataform = $("#form-tambah").serialize();
		$.ajax({
			url: "kirim_plist.php",
			type: "post",
			data: dataform,
			success: function(result) {
				var hasil = JSON.parse(result);
				if (hasil.hasil !== "sukses") {
					$("#error_qty").html(hasil.error.qty);
				} else {
					$('#modal-tambah').modal('hide');
					$("#stpj_id").val('');
					$("#plan_id").val('');
					$("#del_id").val('');
					$("#prod_id").val('');
					$("#kode").val('');
					$("#nolot").val('');
					$("#qty").val('');
				}
			}
		});
	});
	
	</script>