<?php
	include "header.php";
  include "../koneksi.php";

  if (isset($_POST['submit'])) {
    if ($_POST['lap_prod_id']>0) {
      $hitungsisa = $_POST['awal']+$_POST['ok']-$_POST['stpj'];
      $query = "UPDATE lap_prod SET
                      PlanID = '$_POST[plan_id]',
                      Shift = '$_POST[shift]',
                      ProdOK = '$_POST[ok]',
                      Pending = '$_POST[pdg]',
                      Awal = '$_POST[awal]',
                      Sisa = '$hitungsisa',
                      SetupTime = '$_POST[setup]',
                      UpTime = '$_POST[up]',
                      Catat = '$_POST[catat]'
                      WHERE LapProdID = '$_POST[lap_prod_id]'
                      ";

      mysqli_query($koneksi, $query)
      or die ("Gagal Perintah SQL".mysql_error());
    } else {
    $hitungsisa = $_POST['awal']+$_POST['ok']-$_POST['stpj'];
    $query = "insert INTO lap_prod SET
                    PlanID = '$_POST[plan_id]',
                    Shift = '$_POST[shift]',
                    ProdOK = '$_POST[ok]',
                    Pending = '$_POST[pdg]',
                    Awal = '$_POST[awal]',
                    Sisa = '$hitungsisa',
                    SetupTime = '$_POST[setup]',
                    UpTime = '$_POST[up]',
                    Catat = '$_POST[catat]'
                    ";

    mysqli_query($koneksi, $query)
    or die ("Gagal Perintah SQL".mysql_error());
    }
  }

  $data = mysqli_query ($koneksi, " select 
                                            plan.PlanID,
                                            plan.DatePlan,
                                            plan.MesinID,
                                            mesin.Mesin,
                                            plan.ProductID,
                                            product.ItemDescription,
                                            product.SLoc,
                                            product.QtyUnit,
                                            plan.QtyPlan,
                                            plan.Prioritas,
                                            plan.MaterialID,
                                            material.RawMat,
                                            plan.POOR
                                    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);
    $qtyunit = $row['QtyUnit'];

    $data5 = mysqli_query ($koneksi, " SELECT sum(stpj.Qty) AS stpj
                        FROM stpj
                        WHERE stpj.PlanID ='$_GET[id]' AND stpj.Shift ='$_GET[shift]'");
    $row5 = mysqli_fetch_array($data5);

    if ($row5['stpj']>0) {
      $packqty = $row5['stpj']*$row['QtyUnit'];
      $pack = $row5['stpj'];
    } else {
      $packqty = 0;
      $pack = 0;
    }
  if($_GET['shift']!=='4'){
    $data11 = mysqli_query ($koneksi, " SELECT SUM(Qty) as Qty
                              FROM reject
                              WHERE PlanID ='$_GET[id]' AND Shift ='$_GET[shift]' AND Jenis ='1'");
    $row11 = mysqli_fetch_array($data11);

    $data12 = mysqli_query ($koneksi, " SELECT SUM(Qty) as Qty
                              FROM reject
                              WHERE PlanID ='$_GET[id]' AND Shift ='$_GET[shift]' AND Jenis ='2'");
    $row12 = mysqli_fetch_array($data12);
  }
  $data1 = mysqli_query ($koneksi, " select *
                          from 
                          lap_prod 
                          where PlanID='$_GET[id]' and Shift='$_GET[shift]'");
  $count = mysqli_num_rows($data1);
  if ($count > 0) {
    $row1 = mysqli_fetch_array ($data1);     
    $lap_prod_id = $row1['LapProdID'];
    $ok = $row1['ProdOK'];
    $pdg = $row1['Pending'];
    $awal = $row1['Awal'];
    $setup = $row1['SetupTime'];
    $up = $row1['UpTime'];
    $catat = $row1['Catat'];
    $total = $ok+$row11['Qty']+$row12['Qty']+$pdg;
    $pack = $row5['stpj'];
    $sisa = $awal+$ok-$packqty;

  } else {
    $lap_prod_id = 0;
    $ok = 0;
    $pdg = 0;
    $awal = 0;
    $stpj = 0;
    $speed = 0;
    $setup = '0.00';
    $up = '0.00';
    $eqp = '0.00';
    $tool = '0.00';
    $catat = '';
    $total= 0;
    $sisa= 0;
  }

?>
<div class="row">
  <div class="col-md-7">
  <div class="row">
    <div class="col-md-9">
    <div class="pagetitle">
  	<h4>LAPORAN HASIL PRODUKSI</h4>
    </div>
    </div><div class="col-md-3" style="text-align: right;">
    <button type="button" class="btn btn-primary" onclick="myFunction()">
  <i class="bi bi-printer"></i>&ensp;Print Laporan</button>
  </div></div>
   <table class="table table-bordered">
   	<tr>
   		<td width="20%">TANGGAL:</td>
   		<td width="30%"><?php echo date ("d-m-Y", strtotime( $row['DatePlan'])); ?></td>
      <td width="20%">MESIN :</td>
   		<td width="30%"><?php echo $row['Mesin']; ?></td>
   	</tr>
   	<tr>
      <td colspan=4 style="text-align: center; background-color: lightgrey; font-weight: bold; font-size: 16px">
      <?php if($_GET['shift']=='1') { ?>
      SHIFT 1
      <a href="lap_prod.php?id=<?php echo $_GET['id']; ?>&shift=2">
      <i class='bi bi-caret-right-fill' ></i></a>
      <?php } else if($_GET['shift']=='2') { ?>
      <a href="lap_prod.php?id=<?php echo $_GET['id']; ?>&shift=1">
      <i class='bi bi-caret-left-fill' ></i></a>
      SHIFT 2
      <a href="lap_prod.php?id=<?php echo $_GET['id']; ?>&shift=3">
      <i class='bi bi-caret-right-fill' ></i></a>
      <?php } else if($_GET['shift']=='3') { ?>
      <a href="lap_prod.php?id=<?php echo $_GET['id']; ?>&shift=2">
      <i class='bi bi-caret-left-fill' ></i></a>
      SHIFT 3
      <a href="lap_prod.php?id=<?php echo $_GET['id']; ?>&shift=4">
      <i class='bi bi-caret-right-fill' ></i></a>
      <?php } else if($_GET['shift']=='4') { ?>
      <a href="lap_prod.php?id=<?php echo $_GET['id']; ?>&shift=3">
      <i class='bi bi-caret-left-fill' ></i></a>
      TOTAL SHIFT
      <?php } ?>
      </td>
   	</tr>
     <tr>
     <td width="20%">POOR:</td>
   		<td width="30%"><?php echo $row['POOR']; ?></td>
      <td width="20%">KEMASAN :</td>
   		<td width="30%"><?php echo $row['SLoc']; ?>&ensp;<?php echo $row['QtyUnit']; ?></td>
   	</tr>
   	<tr>
      <td width="20%">PRODUCT NAME:</td>
   		<td width="30%"><?php echo $row['ItemDescription']; ?></td>
      <td width="20%">MATERIAL :</td>
   		<td width="30%"><?php echo $row['RawMat']; ?></td>
   	</tr>
    <?php if($_GET['shift']=='4'){
    $data6 = mysqli_query ($koneksi, " SELECT sum(stpj.Qty) AS stpj
                        FROM stpj
                        WHERE stpj.PlanID ='$_GET[id]'");
    $row6 = mysqli_fetch_array($data6);
    $data2 = mysqli_query ($koneksi, "SELECT SUM(ProdOk) AS ProdOk,
                                      SUM(Pending) AS Pending,
                                      SUM(Awal) AS Awal,
                                      Catat,
                                      sec_to_time(SUM(TIME_TO_SEC(SetupTime))) AS SetupTime,
                                      sec_to_time(SUM(TIME_TO_SEC(UpTime))) AS UpTime
                          FROM lap_prod
                          where PlanID='$_GET[id]'");
    $row2 = mysqli_fetch_array($data2);
    $pack4 =  $row6['stpj'];
    $stpj1 = $row6['stpj']*$row['QtyUnit'];
    $sisa2 = $row2['ProdOk']-$stpj1;
    ?>
    <tr>
     <td width="20%">PRODUCT OK:</td>
      <td width="30%"><?php echo number_format($row2['ProdOk'], 0, '', '.'); ?></td>
      <td width="20%">OP 0020 :</td>
      <td width="30%"><?php echo number_format($stpj1, 0, '', '.'); ?></td>
    </tr>
    <tr>
     <?php if($row['ProductID']=='8'||$row['ProductID']=='9') { ?>
     <td width="20%"><a href="#" style="color: #4682B4" class='editbtn' id="reject" data-id="<?php echo $_GET['id']; ?>" data-shift="<?php echo $_GET['shift']; ?>" data-jenis="1"></i> REJECT BOTOL:</a></td>
     <?php } else { ?>
      <td width="20%"><a href="#" style="color: #4682B4" class='editbtn' id="reject" data-id="<?php echo $_GET['id']; ?>" data-shift="<?php echo $_GET['shift']; ?>" data-jenis="1"></i> REJECT PREFORM:</a></td>
      <?php } ?>
      <td width="30%">
      <?php
         $data11 = mysqli_query ($koneksi, " SELECT SUM(Qty) as Qty
                              FROM reject
                              WHERE PlanID ='$_GET[id]' AND Jenis ='1'");
          $row11 = mysqli_fetch_array($data11);
        ?>
        <text><?php echo $row11['Qty']; ?></text>
      </td>
      <td width="20%">SET UP TIME :</td>
      <td width="30%"><?php echo date("H:i", strtotime($row2['SetupTime'])); ?></td>
    </tr>
    <tr>
     <?php if($row['ProductID']=='8'||$row['ProductID']=='9') { ?>
     <td width="20%"><a href="#" style="color: #4682B4" class='editbtn' id="reject" data-id="<?php echo $_GET['id']; ?>" data-shift="<?php echo $_GET['shift']; ?>" data-jenis="2"></i> REJECT PREFORM:</a></td>
     <?php } else { ?>
      <td width="20%"><a href="#" style="color: #4682B4" class='editbtn' id="reject" data-id="<?php echo $_GET['id']; ?>" data-shift="<?php echo $_GET['shift']; ?>" data-jenis="2"></i> REJECT GUMPALAN:</a></td>
      <?php } ?>
      <td width="30%">
      <?php
         $data12 = mysqli_query ($koneksi, " SELECT SUM(Qty) as Qty
                              FROM reject
                              WHERE PlanID ='$_GET[id]' AND Jenis ='2'");
          $row12 = mysqli_fetch_array($data12);
        ?>
        <text><?php echo $row12['Qty']; ?></text>
      </td>
      <td width="20%">UP TIME :</td>
      <td width="30%"><?php echo date("H:i", strtotime($row2['UpTime'])); ?></td>
    </tr>
    <tr>
     <td width="20%">PENDING:</td>
      <td width="30%"><?php echo number_format($row2['Pending'], 0, '', '.'); ?></td>
      <td width="20%"><a href="#" style="color: #4682B4" class='editbtn' id="dt-machine" data-id="<?php echo $_GET['id']; ?>" data-shift="<?php echo $_GET['shift']; ?>" data-jenis="1"></i> DT MACHINE :</a></td>
      <td width="30%"><?php
         $data7 = mysqli_query ($koneksi, " SELECT sec_to_time(SUM(TIME_TO_SEC(`Time`))) as totdtm
                              FROM downtime
                              WHERE PlanID ='$_GET[id]' AND Jenis='1'");
          $row7 = mysqli_fetch_array($data7);
        ?>
        <text><?php echo date("H:i", strtotime($row7['totdtm'])); ?>
        
      </text>
      </td>
    </tr>
    <tr>
     <td width="20%">TOTAL:</td>
      <td width="30%"><?php echo number_format($row2['ProdOk']+$row11['Qty']+$row12['Qty']+$row2['Pending'], 0, '', '.'); ?></td>
      <td width="20%"><a href="#" style="color: #4682B4" class='editbtn' id="dt-machine" data-id="<?php echo $_GET['id']; ?>" data-shift="<?php echo $_GET['shift']; ?>" data-jenis="2"></i> DT EQUIPMENT:</a></td>
      <td width="30%"><?php
         $data8 = mysqli_query ($koneksi, " SELECT sec_to_time(SUM(TIME_TO_SEC(`Time`))) as totdtm
                              FROM downtime
                              WHERE PlanID ='$_GET[id]' AND Jenis='2'");
          $row8 = mysqli_fetch_array($data8);
        ?>
        <text><?php echo date("H:i", strtotime($row8['totdtm'])); ?>
        
      </text>
      </td>
    </tr>
    <tr>
     <td width="20%">AWAL :</td>
      <td width="30%"><?php echo number_format($row2['Awal'], 0, '', '.'); ?></td>
      <td width="20%"><a href="#" style="color: #4682B4" class='editbtn' id="dt-machine" data-id="<?php echo $_GET['id']; ?>" data-shift="<?php echo $_GET['shift']; ?>" data-jenis="3"></i> DT TOOLS:</a></td>
      <td width="30%">
        <?php
         $data9 = mysqli_query ($koneksi, " SELECT sec_to_time(SUM(TIME_TO_SEC(`Time`))) as totdtm
                              FROM downtime
                              WHERE PlanID ='$_GET[id]' AND Jenis='3'");
          $row9 = mysqli_fetch_array($data9);
        ?>
        <text><?php echo date("H:i", strtotime($row9['totdtm'])); ?>
        
      </text>
      </td>
    </tr>
    <tr>
     <td rowspan=2 width="20%">STPJ :</td>
      <td width="30%"><?php echo $pack4; ?></td>
      <td rowspan=4 width="20%">CATATAN :</td>
      <td rowspan=4 width="30%">
        <?php echo $row2['Catat'].'<br>';
          $data10 = mysqli_query ($koneksi, "SELECT Reason
                                              FROM downtime
                                        WHERE PlanID='$_GET[id]'
                                        ORDER BY Shift, Jenis ASC");
          
          $totaldt= '';
          while($row10 =mysqli_fetch_array($data10)) {
          $dt = $row10['Reason'].',';
          $totaldt .= $dt;
          }
          echo $totaldt;
        ?>    
      </td>
    </tr>
     <tr>
      <td width="30%"><?php echo number_format($stpj1, 0, '', '.'); ?></td>
    </tr>
     <tr>
     <td width="20%">SISA PRODUKSI:</td>
      <td width="30%"><?php echo $sisa2; ?></td>
    </tr>
     <tr>
     <td width="20%">SPEED:</td>
      <td width="30%">16.800</td>
    </tr>
    <?php } else { ?>
    <form action="" method="POST" autocomplete="off">
    <input type="hidden" name="lap_prod_id" id="lap_prod_id" value="<?php echo $lap_prod_id; ?>">
    <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']; ?>">
   	<tr>
     <td width="20%">PRODUCT OK:</td>
   		<td width="30%" class="col-sm-3"><input class="form-control" aria-label="Small" type="text" id="ok" name="ok" value="<?php echo $ok; ?>"></td>
      <td width="20%">OP 0010 :</td>
   		<td width="30%"><text style="font-weight: bold" id="total2"><?php echo number_format($total, 0, '', '.'); ?></text><text id="total1"></text></td>
   	</tr>
   	<tr>
      <?php if($row['ProductID']=='8'||$row['ProductID']=='9') { ?>
      <td width="20%"><a href="#" style="color: #4682B4" class='editbtn' id="reject" data-id="<?php echo $_GET['id']; ?>" data-shift="<?php echo $_GET['shift']; ?>" data-jenis="1"></i> REJECT BOTOL:</a></td>
      <?php } else { ?>
      <td width="20%"><a href="#" style="color: #4682B4" class='editbtn' id="reject" data-id="<?php echo $_GET['id']; ?>" data-shift="<?php echo $_GET['shift']; ?>" data-jenis="1"></i> REJECT PREFORM:</a></td>
      <?php } ?>
   		<td width="30%" class="col-sm-3">
        <text style="font-weight: bold"><?php echo $row11['Qty']; ?></text>
        <input class="form-control" aria-label="Small" type="hidden" id="rjk1" name="rjk1" value="<?php echo $row11['Qty']; ?>">
      </td>
      <td width="20%">SET UP TIME :</td>
   		<td width="30%" class="col-sm-3"><input class="form-control" type="time" aria-label="Small" id="setup" name="setup" value="<?php echo date("H:i", strtotime($setup)); ?>"></td>
   	</tr>
   	<tr>
     <?php if($row['ProductID']=='8'||$row['ProductID']=='9') { ?>
     <td width="20%"><a href="#" style="color: #4682B4" class='editbtn' id="reject" data-id="<?php echo $_GET['id']; ?>" data-shift="<?php echo $_GET['shift']; ?>" data-jenis="2"></i> REJECT PREFORM:</a></td>
     <?php } else { ?>
      <td width="20%"><a href="#" style="color: #4682B4" class='editbtn' id="reject" data-id="<?php echo $_GET['id']; ?>" data-shift="<?php echo $_GET['shift']; ?>" data-jenis="2"></i> REJECT GUMPALAN:</a></td>
      <?php } ?>
   		<td width="30%" class="col-sm-3">
        <text style="font-weight: bold"><?php echo $row12['Qty']; ?></text>
        <input class="form-control" aria-label="Small" type="hidden" id="rjk2" name="rjk2" value="<?php echo $row12['Qty']; ?>">
      </td>
      <td width="20%">UP TIME :</td>
   		<td width="30%" class="col-sm-3"><input class="form-control" type="time" aria-label="Small" id="up" name="up" value="<?php echo date("H:i", strtotime($up)); ?>"></td>
   	</tr>
   	<tr>
     <td width="20%">PENDING:</td>
   		<td width="30%" class="col-sm-3"><input class="form-control" aria-label="Small" type="text" id="pdg" name="pdg" value="<?php echo $pdg; ?>"></td>
      <td width="20%"><a href="#" style="color: #4682B4" class='editbtn' id="dt-machine" data-id="<?php echo $_GET['id']; ?>" data-shift="<?php echo $_GET['shift']; ?>" data-jenis="1"></i> DT MACHINE:</a></td>
   		<td width="30%" class="col-sm-3">
        <?php
         $data7 = mysqli_query ($koneksi, " SELECT sec_to_time(SUM(TIME_TO_SEC(`Time`))) as totdtm
                              FROM downtime
                              WHERE PlanID ='$_GET[id]' AND Shift ='$_GET[shift]' AND Jenis ='1'");
          $row7 = mysqli_fetch_array($data7);
        ?>
        <text style="font-weight: bold"><?php echo date("H:i", strtotime($row7['totdtm'])); ?></text>
      </td>
   	</tr>
   	<tr>
     <td width="20%">TOTAL:</td>
   		<td width="30%"><text style="font-weight: bold" id="total3"><?php echo number_format($total, 0, '', '.'); ?></text><text id="total"></text></td>
      <td width="20%"><a href="#" style="color: #4682B4" class='editbtn' id="dt-machine" data-id="<?php echo $_GET['id']; ?>" data-shift="<?php echo $_GET['shift']; ?>" data-jenis="2"></i> DT EQUIPMENT:</a></td>
   		<td width="30%" class="col-sm-3">
        <?php
         $data8 = mysqli_query ($koneksi, " SELECT sec_to_time(SUM(TIME_TO_SEC(`Time`))) as totdtm
                              FROM downtime
                              WHERE PlanID ='$_GET[id]' AND Shift ='$_GET[shift]' AND Jenis ='2'");
          $row8 = mysqli_fetch_array($data8);
        ?>
        <text style="font-weight: bold"><?php echo date("H:i", strtotime($row8['totdtm'])); ?></text>
      </td>
   	</tr>
    <tr>
     <td width="20%">AWAL :</td>
   		<td width="30%" class="col-sm-3">
        <?php if($_GET['shift']=='1') { ?>
        <input class="form-control" aria-label="Small" type="text" id="awal" name="awal" value="<?php echo $awal; ?>">
        <?php } else if ($_GET['shift']=='2') {
        $before = $_GET['shift']-1; 
        $data3 = mysqli_query ($koneksi, " select Sisa
        from 
        lap_prod 
        where PlanID='$_GET[id]' and Shift='$before'");
        if(mysqli_num_rows($data3)>0) {
          $row3 = mysqli_fetch_array($data3);
          $sisa1 = $row3['Sisa'];
        } else {
          $sisa1 = 0;
        }
        ?>
        <input class="form-control" aria-label="Small" type="text" id="awal" name="awal" value="<?php echo $sisa1; ?>">
        <?php } else if ($_GET['shift']=='3') {
        $before = $_GET['shift']-1; 
        $data3 = mysqli_query ($koneksi, " select Sisa
        from 
        lap_prod 
        where PlanID='$_GET[id]' and Shift='$before'");
        if(mysqli_num_rows($data3)>0) {
          $row3 = mysqli_fetch_array($data3); 
          $sisa1 = $row3['Sisa'];
        } else {
          $before1 = $_GET['shift']-2; 
          $data4 = mysqli_query ($koneksi, " select Sisa
          from 
          lap_prod 
          where PlanID='$_GET[id]' and Shift='$before1'");
          $row4 = mysqli_fetch_array($data4); 
          $sisa1 = $row4['Sisa'];
        }
        ?>
        <input class="form-control" aria-label="Small" type="text" id="awal" name="awal" value="<?php echo $sisa1; ?>">
        <?php } ?>
      </td>
      <td width="20%"><a href="#" style="color: #4682B4" class='editbtn' id="dt-machine" data-id="<?php echo $_GET['id']; ?>" data-shift="<?php echo $_GET['shift']; ?>" data-jenis="3"></i> DT TOOLS:</a></td>
   		<td width="30%" class="col-sm-3">
       <?php
         $data9 = mysqli_query ($koneksi, " SELECT sec_to_time(SUM(TIME_TO_SEC(`Time`))) as totdtm
                              FROM downtime
                              WHERE PlanID ='$_GET[id]' AND Shift ='$_GET[shift]' AND Jenis ='3'");
          $row9 = mysqli_fetch_array($data9);
        ?>
        <text style="font-weight: bold"><?php echo date("H:i", strtotime($row9['totdtm'])); ?></text> 
      </td>
   	</tr>
    <tr>
     <td rowspan=2 width="20%">STPJ :</td>
   		<td width="30%"><text style="font-weight: bold" id="pack1"><?php echo $pack; ?></text><text id="pack"></text></td>
      <td rowspan=3 width="20%">CATATAN :</td>
   		<td rowspan=3 width="30%" class="col-sm-3"><textarea class="form-control" type="text" id="catat" name="catat" rows="4"><?php echo $catat; ?></textarea></td>
   	</tr>
     <tr>
   		<td width="30%" class="col-sm-3"><text style="font-weight: bold" id="pack1"><?php echo $packqty; ?></text>
        <input type="hidden" class="form-control" type="text" id="stpj" name="stpj" value="<?php echo $packqty; ?>">
      </td>
   	</tr>
     <tr>
     <td width="20%">SISA PRODUKSI:</td>
   		<td width="30%">
        <text style="font-weight: bold" id="sisa1"><?php echo $sisa; ?></text><text id="sisa"></text>
      </td>
   	</tr>
     <tr>
     <td width="20%">SPEED:</td>
   		<td width="30%" class="col-sm-3">16800</td>
      <td>
        <select class="form-control material" name="material" id="material">
                <option  selected>Operator</option>
                <option value="Sukardi" >Sukardi</option>
                <option value="Herman" >Herman</option>
                <option value="Alen" >Alen</option>
                <option value="Seftian" >Seftian</option>
                <option value="Nasir" >Nasir</option>
                <option value="Ahmad" >Ahmad</option>
              </select>
      </td>
      <td style="text-align: right"><input class="btn btn-primary" type="submit" name="submit" value="Simpan"></input></td>
   	</tr>
    </form>
    <?php } ?>
   </table>

  </div>


  <div class="col-md-5">
  <p>
  <?php if($row5['stpj']>0) { ?>
  <button type="button" class="btn btn-danger" data-href="hapus_stpjall.php?id=<?php echo $_GET['id']; ?>&shift=<?php echo $_GET['shift']; ?>" data-toggle="modal" data-target="#confirm-delete">
  <i class="bi bi-trash2-fill"></i>&ensp;Hapus Tabel STPJ</button>
  <?php } else {
    if ($_GET['shift']=='4') { ?>
  <button type="button" class="btn btn-primary" onclick="myFunction1()">
  <i class="bi bi-printer"></i>&ensp;Print S T P J</button>
  <?php } else { ?>
  <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-tambah-stpj">
  <i class="bi bi-layout-text-sidebar-reverse"></i>&ensp;Tambah S T P J</button>
  <?php }} ?>
  </p>
  <div style="width:100%;height:540px;overflow:scroll;align: left;" >
  <div id="data-mahasiswa"></div>
  </div>



  </div>
  </div>

</div>

	<!-- 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>

    <div id="modal-tambah-stpj" class="modal fade" role="dialog">
    <div class="modal-dialog modal-md">
      <div class="modal-content">
        <form role="form" id="form-tambah-stpj" method="post" action="input-stpj.php" autocomplete="off">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Tambah Data S T P J</h4>
        </div>
          <div class="modal-body">
          <div class="form-group">
            <label>Nomor Produksi Awal</label>
            <input type="hidden" name="plan_id" value="<?php echo $_GET['id']; ?>">
            <input type="hidden" name="shift" value="<?php echo $_GET['shift']; ?>">
            <input class="form-control" id="kode" name="kode">
            <p style="color:red" id="error_kode"></p>
          </div>
          <div class="form-group">
            <label>Nomor Lot</label>
            <input class="form-control" id="nolot" name="nolot">
            <p style="color:red" id="error_nolot"></p>
          </div>
          <div class="form-group">
              <label>Material</label>
              <select class="form-control material" name="material" id="material">
                <option  selected></option>
                <option value="PNR" >PNR</option>
                <option value="JADE" >JADE</option>
                <option value="DSR" >DSR</option>
                <option value="RAMAPET" >RAMAPET</option>
                <option value="EASTLON" >EASTLON</option>
                <option value="RPET VEOLIA" >RPET VEOLIA</option>
                <option value="supplain" >OTHERS</option>
              </select>
              <input class="form-control" type="text" id="material2" name="material2" placeholder="Masukkan Material Baru..">
            </div>                
          <div class="form-group">
            <label>Jumlah Pack</label>
            <input class="form-control" id="jml" name="jml">
            <p style="color:red" id="error_jml"></p>
          </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-dt-machine" 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-dt-machine.php" autocomplete="off">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Input Down Time</h4>
        </div>
          <div class="modal-body">
            <div id="data-dt-machine">
            
            </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-reject" 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-reject.php" autocomplete="off">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Input Reject</h4>
        </div>
          <div class="modal-body">
            <div id="data-reject">
            
            </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 shift = GetURLParameter('shift');
	
	// $(document).on('click','#tambah',function(e){
	// 	e.preventDefault();
	// 	$("#modal-tambah").modal('show');
	// 	$.post('addstpj.php',
	// 		{id:$(this).attr('data-id')},
	// 		function(html){
	// 			$("#data-tambah").html(html);
	// 		}   
	// 	);
	// });
  function myFunction() {
  window.open("laporan.php?id="+id);
  }

  function myFunction1() {
  window.open("laporan-stpj.php?id="+id);
  }
	
	$("#form-tambah-stpj").submit(function(e) {
		e.preventDefault();
		
		$("#error_kode").html('');
		$("#error_nolot").html('');
		$("#error_jml").html('');
		
		var dataform = $("#form-tambah-stpj").serialize();
		$.ajax({
			url: "input-stpj.php",
			type: "post",
			data: dataform,
			success: function(result) {
				var hasil = JSON.parse(result);
				if (hasil.hasil !== "sukses") {
					$("#error_kode").html(hasil.error.kode);
					$("#error_nolot").html(hasil.error.nolot);
					$("#error_jml").html(hasil.error.jml);
				} else {
					$('#modal-tambah-stpj').modal('hide');
					$("#kode").val('');
					$("#nolot").val('');
					$("#jml").val('');
          $("#material").val('');
          $("#material2").val('');
          location.reload(true);
				}
			}
		});
	});

  $(document).ready(function() {
    $('#material2').hide();
    $("#material").change(function() {
        var val = $(this).val();      
        if (val == 'supplain') {
          $('#material2').show();
        } else {
          $('#material2').hide();
        }
      }).change();
    });

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

  $("#ok ,#rjk1, #rjk2, #pdg, #awal, #stpj").on('keyup', function() {
      var ok = document.getElementById("ok").value ;
      var intOk = parseInt(ok);
      var rjk1 = document.getElementById("rjk1").value ;
      var intrjk1 = parseInt(rjk1);
      var rjk2 = document.getElementById("rjk2").value ;
      var intrjk2 = parseInt(rjk2);
      var pdg = document.getElementById("pdg").value ;
      var intpdg = parseInt(pdg);
      var awal = document.getElementById("awal").value ;
      var intawal = parseInt(awal);
      var stpj = document.getElementById("stpj").value ;
      var intstpj = parseInt(stpj);
      total = intOk+intrjk1+intrjk2+intpdg;
      pack = intstpj/<?php echo json_encode($qtyunit); ?>;
      sisa = intawal+intOk-intstpj;
      tot = (total/1000).toFixed(3);
      document.getElementById("total").innerHTML = "<b>" + tot +"</b>";
      document.getElementById("total1").innerHTML = "<b>" + tot +"</b>";
      document.getElementById("pack").innerHTML = "<b>" + pack +"</b>";
      document.getElementById("sisa").innerHTML = "<b>" + sisa +"</b>";
      $("#total2").hide();
      $("#total3").hide();
      $("#pack1").hide();
      $("#sisa1").hide();
    });
	
	$('#confirm-delete').on('show.bs.modal', function(e) {
            $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
            
        });

        
  $("#form-dt-machine").submit(function(e) {
		e.preventDefault();
		
		var dataform = $("#form-dt-machine").serialize();
		$.ajax({
			url: "input-dt-machine.php",
			type: "post",
			data: dataform,
			success: function(result) {
				var hasil = JSON.parse(result);
				if (hasil.hasil !== "sukses") {
				} else {
					$('#modal-dt-machine').modal('hide');
          $("#planid").val('');
					$("#shift").val('');
          $("#jenis").val('');
					$("#mytime").val('');
					$("#mydtm").val('');
				}
			}
		});
	});

  $("#form-reject").submit(function(e) {
		e.preventDefault();
		
		var dataform = $("#form-reject").serialize();
		$.ajax({
			url: "input-reject.php",
			type: "post",
			data: dataform,
			success: function(result) {
				var hasil = JSON.parse(result);
				if (hasil.hasil !== "sukses") {
				} else {
					$('#modal-dt-machine').modal('hide');
          $("#planid").val('');
					$("#shift").val('');
          $("#jenis").val('');
					$("#myreject").val('');
					$("#myqty").val('');
				}
			}
		});
	});

    $(document).ready(function(){
      var data = "data_stpj.php?id="+id+"&shift="+shift;
      $('#data-mahasiswa').load(data);
    });


  </script>
