<?php include "../koneksi.php";
if ($_GET['mat'] == '1' || $_GET['mat'] == '2') { ?>
<table class="table table-bordered">
		<tr>
			<th width="20%">
				Bag/Lot
			</th>
			<th width="15%">
				Material
			</th>
			<th width="15%">
				Batch
			</th>
			<th width="20%">
				Date
			</th>
			<th width="5%" >
				#
			</th>
		</tr>
			<?php 
				$data1 = mysqli_query ($koneksi, " SELECT * from plan where PlanID='$_GET[id]'");
				$row1 = mysqli_fetch_array($data1);
				$data = mysqli_query ($koneksi, " SELECT pbb.PBB_ID,
															pbb.PlanID,
															pbb.Shift,
															pbb.Lot,
															pbb.Bag,
															pbb.Material,
															incoming.MaterialID,
															incoming.Date_Inc
												FROM pbb
												INNER JOIN incoming
												ON pbb.IncomingID=incoming.IncomingID
												INNER JOIN material
												ON incoming.MaterialID=material.MaterialID
												WHERE material.MaterialID='$_GET[mat]' AND pbb.NoPBB_ID='$row1[NoPBB_ID]'
                                				Order by incoming.Date_Inc, pbb.Bag, pbb.Lot, pbb.Material ASC");
	

				while($row = mysqli_fetch_array ($data)): ?>
		<tr>
			<td width="20%">
				<?php echo $row['Bag']; ?>
			</td>
			<td width="15%">
				<?php echo $row['Material']; ?>
			</td>
			<td width="15%">
				<?php echo $row['Lot']; ?>
			</td>
			<td width="20%">
				<?php echo date ("d-m-Y", strtotime($row['Date_Inc'])); ?>
			</td>
			<td width="5%" >
				<?php if($row['PlanID']==NULL && $row['PlanID']==NULL) { ?>
				<a href="in.php?id=<?php echo $row['PBB_ID']; ?>&plan_id=<?php echo $_GET['id']; ?>&mat=<?php echo $row['MaterialID']; ?>" style="color: black;" ><i class="bi bi-arrow-right-square-fill"></i></a>
				<?php } else {} ?>
			</td>
		</tr>
			<?php endwhile; ?>
	</table>
<?php } else { ?>
<table class="table table-bordered">
		<tr>
		<th width="30%">
				LOT
			</th>
			<th width="20%">
				BAG
			</th>
			<th width="20%">
				Date
			</th>
			<th width="5%" >
				#
			</th>
			
		</tr>
			<?php 
				$data1 = mysqli_query ($koneksi, " SELECT * from plan where PlanID='$_GET[id]'");
				$row1 = mysqli_fetch_array($data1);
				$data = mysqli_query ($koneksi, " SELECT pbb.PBB_ID,
														pbb.PlanID,
														pbb.Shift,
														pbb.Lot,
														pbb.Bag,
														incoming.MaterialID,
														incoming.Date_Inc
											FROM pbb
											INNER JOIN incoming
											ON pbb.IncomingID=incoming.IncomingID
											INNER JOIN material
											ON incoming.MaterialID=material.MaterialID
											WHERE pbb.NoPBB_ID='$row1[NoPBB_ID]'
                                			Order by incoming.Date_Inc, pbb.Bag, pbb.Lot ASC");
	

				while($row = mysqli_fetch_array ($data)): ?>
		<tr>
			
		<td width="30%">
				<?php $data2 = mysqli_query ($koneksi, "SELECT * from coa_bb WHERE Lot='$row[Lot]' Order by CoabbID DESC LIMIT 1"); 
				if(mysqli_num_rows($data2)>0) {
					$row2=mysqli_fetch_array($data2);
					echo '<a id="coa" data-id="'.$row2['CoabbID'].'">'.$row['Lot'].'</a>';	
				} else {
					echo $row['Lot'];
				} ?>
			</td>
			<td width="20%">
				<a href="#" style="color: #0000FF" class='editbtn' id="ubah-nomat" data-id="<?php echo $row['PBB_ID']; ?>" data-plan-id="<?php echo $_GET['id']; ?>" data-mat="<?php echo $_GET['mat']; ?>" ><?php echo $row['Bag']; ?></a>
			</td>
			<td width="20%">
				<?php echo date ("d-m-Y", strtotime($row['Date_Inc'])); ?>
			</td>
			<td width="5%" >
				<?php if($row['PlanID']==NULL && $row['PlanID']==NULL) { ?>
				<a href="in.php?id=<?php echo $row['PBB_ID']; ?>&plan_id=<?php echo $_GET['id']; ?>&mat=<?php echo $row['MaterialID']; ?>" style="color: black;" ><i class="bi bi-arrow-right-square-fill"></i></a>
				<?php } else {} ?>
			</td>
		</tr>
			<?php endwhile; ?>
	</table>
<?php } ?>

<div id="modal-coa" class="modal fade" role="dialog">
    <div class="modal-dialog modal-lg">
      <div class="modal-content">
        <form enctype="multipart/form-data" method="post" action="coa_bb/upload1.php" autocomplete="off">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">COA Material</h4>
        </div>
          <div class="modal-body">
             <div id="data-coa">
            
            </div> 
           
  
          </div>
          
          <div class="modal-footer">
            <button type="button" class="btn btn-info" data-dismiss="modal">Ok</button>
          </div>
        </form>   
      </div>
    </div>
  </div> 

  <div id="modal-nomat" class="modal fade" role="dialog">
    <div class="modal-dialog modal-md">
      <div class="modal-content">
        <form role="form" id="form-edit" method="post" action="input-nomat.php" autocomplete="off">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Ubah No Material</h4>
        </div>
          <div class="modal-body">
            <div id="data-nomat">
            
            </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','#coa',function(e){
		    e.preventDefault();
		    $("#modal-coa").modal('show');
		    $.post('coa.php',
		      {
		        id:$(this).attr('data-id')
		      },
		      function(html){
		        $("#data-coa").html(html);
		      }   
		    );
		  });

  	 $(document).on('click','#ubah-nomat',function(e){
    e.preventDefault();
    $("#modal-nomat").modal('show');
    $.post('edit-nomat.php',
      { 
      	id:$(this).attr('data-id'),
      	plan:$(this).attr('data-plan-id'),
      	mat:$(this).attr('data-mat')
      },
      function(html){
        $("#data-nomat").html(html);
      }   
    );
  });
  </script>