<?php
	include "../dashboard/header.php";
  include "../koneksi.php";
?>
<style type="text/css">
</style>
<div class="row">
  <div class="col-md-6">
  <div class="row">
    <div class="col-md-10">
    <div class="pagetitle">
  	<h4>PENYERAHAN BAHAN BAKU</h4>
    </div>
    </div><div class="col-md-2">
    <a onclick="myFunction()" class="btn btn-success" >Print</a>
    </div></div>
  <?php 
      $data = mysqli_query ($koneksi, " select 
                                              plan.PlanID,
                                              plan.DatePlan,
                                              plan.MesinID,
                                              mesin.Mesin,
                                              plan.ProductID,
                                              product.ItemDescription,
                                              product.SLoc,
                                              product.QtyUnit,
                                              product.Pewarna,
                                              plan.QtyPlan,
                                              plan.Prioritas,
                                              plan.MaterialID,
                                              material.RawMat,
                                              plan.POOR,
                                              plan.NoPBB_ID2
                                      from 
                                      plan
                                      INNER JOIN mesin
                                      ON plan.MesinID=mesin.MesinID
                                      INNER JOIN product
                                      ON plan.ProductID=product.ProductID
                                      INNER JOIN material
                                      ON plan.MaterialID=material.MaterialID
                                      WHERE PlanID ='$_GET[id]'");
      
      $row = mysqli_fetch_array ($data);
    ?>
   <table class="table">
   	<tr>
      <td>
      <a href="#" style="color: #0000FF" class='editbtn' id="tambah-nopbb" data-id="<?php echo $row['PlanID']; ?>">No. PBB  :</a></td>
      <td><?php
          if($row['NoPBB_ID2']!=="") { 
          $data1 = mysqli_query ($koneksi, " SELECT * from no_pbb where NoPBB_ID='$row[NoPBB_ID2]'");
          $row1 = mysqli_fetch_array($data1);
          echo $row1['No_PBB'];
       ?></td>
      <?php } ?>
    </tr>
    <tr>
   		<td>Tanggal Plan  :</td>
   		<td><?php echo date ("d-m-Y", strtotime( $row['DatePlan'])); ?></td>
   	</tr>
   	<tr>
   		<td width="50%">Mesin  :</td>
   		<td><?php echo $row['Mesin']; ?></td>
   	</tr>
   	<tr>
   		<td>Product  :</td>
   		<td><?php echo $row['ItemDescription']; ?></td>
   	</tr>
   	<tr>
   		<td>Material  :</td>
   		<td><?php echo $row['RawMat']; ?></td>
   	</tr>
   	<tr>
   		<td>Quantity  :</td>
   		<td>
        <?php echo $row['QtyPlan']; ?>
        <?php echo substr($row['SLoc'], 0, 3); ?> @
        <?php echo $row['QtyUnit']; ?> PCS
      </td>
   	</tr>
   </table>
<?php 
    $data2 = mysqli_query ($koneksi, " SELECT count(pbb.PBB_ID) jml,
                                              pbb.PlanID,
                                              material.RawMat,
                                              sum(material.Qty) AS total
                                      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]'");


    $row2 = mysqli_fetch_array ($data2);
?>
  <div class="row">
    <div class="col-md-8">
    <h4><?php echo $row2['RawMat']; ?></h4>
    </div><div class="col-md-4">
    <h4 style="text-align: right;"><?php echo $row2['jml']; ?>JB @ <?php echo $row2['total']; ?> </h4>
    </div></div>
  <div style="width:100%;height:350px;overflow:scroll;align: left;" >
  <div id="data-kirim"></div>
  </div>
  </div>

<?php 
    $data1 = mysqli_query ($koneksi, " SELECT count(pbb.PBB_ID) jml,
                                              incoming.MaterialID,
                                              material.RawMat,
                                              sum(material.Qty) AS total
                            FROM pbb
                            INNER JOIN incoming
                            ON pbb.IncomingID=incoming.IncomingID
                            INNER JOIN material
                            ON incoming.MaterialID=material.MaterialID
                            WHERE material.MaterialID='$row[Pewarna]' AND pbb.NoPBB_ID IS NULL");


    $row1 = mysqli_fetch_array ($data1); 
?>
  <div class="col-md-6">
    <div class="row">
    <div class="col-md-7">
  	<h4><?php echo $row1['RawMat']; ?></h4>
  </div><div class="col-md-4">
    <h4 style="text-align: right;"><?php echo $row1['jml']; ?>JB @ <?php echo $row1['total']; ?> </h4>
    </div></div>

  <div style="width:95%;height:540px;overflow:scroll;align: left;" >
  <div id="data-mahasiswa"></div>
  </div>

  <div id="modal-tambah-nopbb" class="modal fade" role="dialog">
    <div class="modal-dialog modal-sm">
      <div class="modal-content">
        <form role="form" id="form-edit" method="post" action="input-nopbb.php" autocomplete="off">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Masukkan No PBB</h4>
        </div>
          <div class="modal-body">
            <div id="data-tambah-nopbb">
            
            </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>
	function GetURLParameter(sParam)
		{
			var sPageURL = window.location.search.substring(1);
			var sURLVariables = sPageURL.split('&');
			for (var i = 0; i < sURLVariables.length; i++) 
			{
				var sParameterName = sURLVariables[i].split('=');
				if (sParameterName[0] == sParam) 
				{
					return sParameterName[1];
				}
			}
		}
		var id = GetURLParameter('id');
    var mat = GetURLParameter('mat');

function myFunction() {
  window.open("laporan.php?id="+id);
}
	
$(document).ready(function(){
	var data = "data_pbb.php?id="+id;
	$('#data-mahasiswa').load(data);
	

});

$(document).ready(function(){
  var data = "kirim_pbb.php?mat="+mat+"&id="+id;
  $('#data-kirim').load(data);
  

});

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

$("#form-tambah-nopbb").submit(function(e) {
    e.preventDefault();
    
    $("#error_no_pbb").html('');
    
    var dataform = $("#form-tambah-nopbb").serialize();
    $.ajax({
      url: "pbb.php",
      type: "post",
      data: dataform,
      success: function(result) {
        var hasil = JSON.parse(result);
        if (hasil.hasil !== "sukses") {
          $("#error_no_pbb").html(hasil.error.no_pbb);
        } else {
          $('#modal-tambah-nopbb').modal('hide');
          $("#plan_id").val('');
          $("#no_pbb").val('');
        }
      }
    });
  });
  </script>
  </div>
</div>



  </div>
  </div>

</div>