<?php include "../koneksi.php";
$data3 = mysqli_query ($koneksi, " SELECT plan.PlanID,
												product.QtyUnit,
												product.SLoc
											from 
											plan
											INNER JOIN product
											ON plan.ProductID=product.ProductID
											WHERE PlanID ='$_GET[id]'
");
$row3 = mysqli_fetch_array($data3);
?>
<table class="table table-bordered">
		<tr>
			<th width="5%">
				No
			</th>
			<th width="20%">
				Material
			</th>
			<th width="30%">
				NO Produksi
			</th>
			<th width="15%">
				Jumlah
			</th>
			<th width="15%">
				<?php echo substr($row3['SLoc'], 0, 3); ?>
			</th>
			<?php if ($_GET['shift']=='4') {} else { ?>
			<th width="5%" >
				#
			</th>
		<?php } ?>
		</tr>
			<?php 
			if ($_GET['shift']=='4') {
				$no = 1;
				$data = mysqli_query ($koneksi, " select 
														stpj.STPJ_ID,
														plan.PlanID,
														plan.DatePlan,
														plan.POOR,
														product.QtyUnit,
														product.SLoc,
														stpj.Shift,
														stpj.Material,
														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.PlanID ='$_GET[id]'
												order by stpj.NoLot ,stpj.Kode ASC");
			} else {
				$no = 1;
				$data = mysqli_query ($koneksi, " select 
														stpj.STPJ_ID,
														plan.PlanID,
														plan.DatePlan,
														plan.POOR,
														product.QtyUnit,
														product.SLoc,
														stpj.Material,
														stpj.Shift,
														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.PlanID ='$_GET[id]' AND stpj.Shift ='$_GET[shift]'
												order by stpj.NoLot ,stpj.Kode ASC
												");
				
			}

				while($row = mysqli_fetch_array ($data)): 
			
			?>
		<tr>
			<td width="10%">
				<?php echo $no++; ?>
			</td>
			<td width="20%">
				<?php echo $row['Material']; ?>
			</td>
			<td width="30%">
				<?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); ?>-<?php echo $row['NoLot']; ?>
			</td>
			<td width="15%">
				<?php echo $row['QtyUnit']; ?>
			</td>
			<td width="15%">
				<?php
				if($row3['SLoc']=='NTN2') {
					if($_GET['shift']=='4') {
					echo $row['Nick'];
				 } else { ?> 
				<input class="tdE form-control nick" id="<?php echo $row['STPJ_ID']; ?>" data-id="<?php echo $row['STPJ_ID']; ?>" type="text" value="<?php echo $row['Nick']; ?>"  />
				<?php
					}
				} else {
				 echo $row['Qty'];
				} 
				?> 
			</td>
			<?php if ($_GET['shift']=='4') {} else { ?>
			<td width="10%">
				<a href="#" style="color: #DC143C" data-href="hapus_stpj.php?id=<?php echo $row['STPJ_ID']; ?>&plan_id=<?php echo $row['PlanID']; ?>&shift=<?php echo $row['Shift']; ?>" data-toggle="modal" data-target="#confirm-delete"><i class="bi bi-trash-fill"></i></a> 
			</td>
		<?php } ?>
		</tr>
			<?php endwhile; ?>
	</table>
	<?php
		if($_GET['shift']!=='4') {
		if (mysqli_num_rows($data)>0) {
		$data2 = mysqli_query ($koneksi, " SELECT Kode,
												NoLot,
												Material
												FROM stpj
												WHERE stpj.PlanID ='$_GET[id]' AND stpj.Shift ='$_GET[shift]'
												ORDER by Kode DESC LIMIT 1
										");
		$row2 = mysqli_fetch_array($data2);
	?>
	<form action="input-stpj-once.php" method="POST" autocomplete="off">
    <input type="hidden" name="plan_id" id="plan_id" value="<?php echo $_GET['id']; ?>">
    <input type="hidden" name="shift" id="shift" value="<?php echo $_GET['shift']; ?>">
    <input type="hidden" name="kode" id="kode" value="<?php echo $row2['Kode']+1; ?>">
    <input type="hidden" name="nolot" id="nolot" value="<?php echo $row2['NoLot']; ?>">
    <input type="hidden" name="material" id="material" value="<?php echo $row2['Material']; ?>">
	<button type="submit" class="btn btn-primary" ><i class="bi bi-plus-circle-fill"></i> Tambah STPJ</button>
	</form>
	<?php }} ?>


		<!-- Modal -->
		<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
            
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel">Kofirmasi Hapus</h4>
                </div>
            
                <div class="modal-body">
                    <p>Apakah anda mau melanjutkan menghapus data?</p>
                    <p class="debug-url"></p>
                </div>
                
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <a class="btn btn-danger btn-ok">Delete</a>
                </div>
            </div>
        </div>
    </div>

	<script>

        $('#confirm-delete').on('show.bs.modal', function(e) {
            $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
            
        });

		$('.nick').on('keyup', function(){
    	var id = $(this).attr('data-id');
    	// loc
        //fungsi mencari data dari keyword dengan method get
        $.get('cekinfo.php?id='+ id +'&keyword=' + $('#'+id).val(), function(data) {
        	if (data) {
        		$('#info_'+id).html(data);
        	}else{
        		$('#info_'+id).html(data);
        	}
        	});
    	});

    	$( document ).ready(function() {
	$('body').on('keydown', '.tdE', function(e){moveElementFocus(e);});
	});
	
	function moveElementFocus(e){
		
		if (e.which == 40 || e.which == 13) { // right arrow
				// alert(1);
		//id=$(e.target).closest('.tdE').next().find('input').attr("id");
		id=$('.tdE').elementAfter(e.target).attr("id");
		
					console.log("ID:::"+id);
					if($("#"+id).val()==0){
								$("#"+id).val("");
							}
							$("#"+id).focus();
							e.preventDefault();
			
					} else if (e.which == 38) { // left arrow
				
					//	id=$(e.target).closest('.tdE').prev().find('input').attr("id");
						id=$('.tdE').elementBefore(e.target).attr("id");
						console.log("ID:::"+id);
				if($("#"+id).val()==0){
							$("#"+id).val("");
						}
						$("#"+id).focus();
					
						e.preventDefault();
					}
				
		}


	jQuery.fn.elementAfter = function(other) {
		for(i = 0; i < this.length - 1; i++) {
			if (this[i] == other) {
				return jQuery(this[i + 1]);
			}
		}
		return jQuery;
	};

	jQuery.fn.elementBefore = function(other) {
		if (this.length > 0) {               
			for(i = 1; i < this.length; i++) {
				if (this[i] == other) {
					return jQuery(this[i - 1]);
				}
			}
		}
		return jQuery;
	};
    </script>
