<?php include "../koneksi.php"; ?>
<nav>
    <ol class="breadcrumb">
		<li class="breadcrumb-item">Incoming</li>
        <li class="breadcrumb-item">COA Material</li>
    </ol>
</nav>
<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="30%">
				Material 
			</th>
			<th width="20%">
				Lot
			</th>
			<th width="25%">
				COA
			</th>
			<th width="15%">
				Opsi
			</th>
		</tr>
		</thead>
		<tbody>
			<?php 
				$no = 1;
				$data = mysqli_query ($koneksi, "SELECT incoming.Date_Inc,
												material.MaterialID, 
												material.RawMat,
												Lot
												FROM pbb
												INNER JOIN incoming
												ON pbb.IncomingID=incoming.IncomingID
												INNER JOIN material
												ON incoming.MaterialID=material.MaterialID
												WHERE material.Material IS NOT NULL
												GROUP BY Lot,material.RawMat
												ORDER BY incoming.Date_Inc ASC");
				
				while($row = mysqli_fetch_array ($data)):
			?>
		<tr>
			<td>
				<?php echo $row['Date_Inc']; ?>
			</td>
			<td>
				<?php echo $row['RawMat']; ?>
			</td>
			<td>
				<?php echo $row['Lot']; ?>
			</td>
			<td>
				<?php $data1 = mysqli_query ($koneksi, "SELECT * from coa_bb WHERE Lot='$row[Lot]' Order by CoabbID DESC LIMIT 1"); 
				if(mysqli_num_rows($data1)>0) {
					$row1=mysqli_fetch_array($data1);
					echo '<a id="coa" data-id="'.$row1['CoabbID'].'">'.$row1['File'].'</a>';	
				}
				
				?>
			</td>
			<td>

				<a id="foto" data-lot="<?php echo $row['Lot'] ?>" data-prod="<?php echo $row['MaterialID'] ?>" style="color: #00008B" ><i class="bi bi-cloud-arrow-up-fill"></i> Upload COA</a>
			</td>
		</tr>
			<?php endwhile; ?>
		</tbody>
	</table>
	</div>

	<div id="modal-foto" class="modal fade" role="dialog">
    <div class="modal-dialog modal-md">
      <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-foto">
            
            </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> 

  <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> 
	
	<script>
  		$.fn.dataTableExt.oSort['time-date-sort-pre'] = function(value) {      
			return Date.parse(value);
		};
		$.fn.dataTableExt.oSort['time-date-sort-asc'] = function(a,b) {      
			return a-b;
		};
		$.fn.dataTableExt.oSort['time-date-sort-desc'] = function(a,b) {
			return b-a;
		};

		var table = $('#example').DataTable({
			columnDefs : [
				{ type: 'time-date-sort', 
				targets: [0],
				}
			],
			order: [[ 0, "desc" ]]
		});

		// $(document).on('click','#foto',function(e){
		//     e.preventDefault();
		//     $("#modal-foto").modal('show');
		//   });

		 $(document).on('click','#foto',function(e){
		    e.preventDefault();
		    $("#modal-foto").modal('show');
		    $.post('coa_bb/input.php',
		      {
		        lot:$(this).attr('data-lot'),
		        prod:$(this).attr('data-prod')
		      },
		      function(html){
		        $("#data-foto").html(html);
		      }   
		    );
		  });

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