<?php
	include "../dashboard/header.php";
  include "../koneksi.php";
  if (!isset($_GET['ideks'])) {
  if (isset($_POST['submit'])) {
  $query = "UPDATE deliverytbl SET
                      DateKirim = '$_POST[date_kirim]',
                      NoDN = '$_POST[no_dn]',
                      Arrival_Time = '$_POST[arr_time]',
                      Expedisi = '$_POST[expedisi]',
                      Cont = '$_POST[cont]',
                      Seal = '$_POST[seal]'
                      WHERE Del_ID = '$_POST[del_id]'
                      ";

  mysqli_query($koneksi, $query)
  or die ("Gagal Perintah SQL".mysql_error());
  }
  } else {
   if (isset($_POST['submit'])) {
      $query = "UPDATE deliverytbl SET
                          DateKirim = '$_POST[date_kirim]',
                          NoDN = '$_POST[no_dn]',
                          Arrival_Time = '$_POST[arr_time]',
                          Expedisi = '$_POST[ekspedisi]',
                          Cont = '$_POST[nopol]',
                          Seal = '$_POST[sopir]-$_POST[no_hp]'
                          WHERE Del_ID = '$_POST[del_id]'
                          ";

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

?>
<style type="text/css">
</style>
<?php 
        $data = mysqli_query ($koneksi, " select 
                              deliverytbl.Del_ID,
                              deliverytbl.DateKirim,
                              deliverytbl.DetailPO_ID,
                              deliverytbl.Arrival_Time,
                              deliverytbl.Expedisi,
                              deliverytbl.Cont,
                              deliverytbl.Seal,
                              detail_po.PO_ID,
                              po_tbl.No_PO,
                              po_tbl.CustomerID,
                              customer.CustomerName,
                              detail_po.ProductID,
                              product.Material,
                              product.ItemDescription,
                              product.SLoc,
                              product.QtyUnit,
                              deliverytbl.DetailEkspedisi_ID,
                              detailekspedisi.EkspedisiID,
                              ekspedisi.Ekspedisi,
                              detailekspedisi.Sopir,
                              detailekspedisi.NoPolisi,
                              detailekspedisi.NoHP,
                              deliverytbl.NoDN,   
                              deliverytbl.SO_No,   
                              deliverytbl.DateDoc,   
                              deliverytbl.QtyKirim    
                      from 
                      deliverytbl
                      INNER JOIN detail_po
                      ON deliverytbl.DetailPO_ID=detail_po.DetailPO_ID
                      INNER JOIN po_tbl
                      ON detail_po.PO_ID=po_tbl.PO_ID
                      INNER JOIN customer
                      ON po_tbl.CustomerID=customer.CustomerID
                      INNER JOIN product
                      ON detail_po.ProductID=product.ProductID
                      INNER JOIN detailekspedisi
                      ON deliverytbl.DetailEkspedisi_ID=detailekspedisi.DetailEkspedisi_ID
                      INNER JOIN ekspedisi
                      ON detailekspedisi.EkspedisiID=ekspedisi.EkspedisiID
                      WHERE Del_ID ='$_GET[id]'");
        
        $row = mysqli_fetch_array ($data);
      ?>
<div class="row">
  <div class="col-md-3"></div>
  <div class="col-md-6">
  <div class="row">
    <div class="col-md-5">
    <div class="pagetitle">
  	<h3>DELIVERY NUMBER</h3>
    </div>
    </div>
    <div class="col-md-6">
    <?php if($row['SO_No'] == NULL && $row['DateDoc'] == NULL) { ?>
    <button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#modal-tambah-sobaru">SO/Doc Date</button>
    <?php } else { ?>
    <button type="button" class="btn btn-primary btn-sm" id="tambah-so-number" data-id="<?php echo $_GET['id']; ?>">SO/Doc Date</button>
    <?php } ?>
    <button type="button" class="btn btn-primary btn-sm" id="tambah-packing-list" data-id="<?php echo $_GET['id']; ?>">Packing List</button>
    <button type="button" class="btn btn-primary btn-sm" id="tambah-coa" data-id="<?php echo $_GET['id']; ?>"> COA </button>
    </div>
    <div class="col-md-1">
    <a onclick="myFunction()" style="color: #00008B; font-size: 25px;" ><i class="bi bi-printer-fill"></i></a>
    </div></div>
   <table class="table">
    <tr style="font-weight: bold" >
   		<td>Delivery Memo  :</td>
   		<td><?php echo $row['CustomerName']; ?></td>
   	</tr>
    <form action="" method="POST" autocomplete="off">
    <input type="hidden" name="del_id" id="del_id" value="<?php echo $row['Del_ID']; ?>">
    <tr>
   		<td>Tanggal Kirim  :</td>
   		<td><input type="text" class="input-tanggal1 form-control" id="date_kirim" name="date_kirim" data-date-format="d-M-Y" value="<?php echo $row['DateKirim']; ?>" ></td>
   	</tr>
    <tr>
      <td>Nomor DN  :</td>
      <td><input class="form-control" aria-label="Small" id="no_dn" name="no_dn" value="<?php echo $row['NoDN']; ?>"></td>
    </tr>
    <tr>
      <td>Product Name  :</td>
      <td style="font-weight: bold"><?php echo $row['Material']; ?> - <?php echo $row['ItemDescription']; ?></td>
    </tr>
    <tr>
      <td>Nomor PO  :</td>
      <td style="font-weight: bold"><?php echo $row['No_PO']; ?></td>
    </tr>
    <tr>
      <td>Arrival Time  :</td>
      <td><input class="form-control" aria-label="Small" id="arr_time" name="arr_time" value="<?php echo $row['Arrival_Time']; ?>"></td>
    </tr>
    <?php if ($row['CustomerID']==='1') { ?>
    <tr>
      <td>Ekspedisi  :</td>
      <td><input class="form-control" aria-label="Small" id="expedisi" name="expedisi" value="<?php echo $row['Expedisi']; ?>" ></td>
    </tr>
    <tr>
      <td>Container  :</td>
      <td><input class="form-control" aria-label="Small" id="cont" name="cont" value="<?php echo $row['Cont']; ?>"></td>
    </tr>
    <tr>
      <td>No. SEAL  :</td>
      <td><input class="form-control" aria-label="Small" id="seal" name="seal" value="<?php echo $row['Seal']; ?>" ></td>
    </tr>  
    <?php }else{ ?>
    <tr>
    <tr>
      <td width="29%">Ekspedisi  :</td>
      <td ><input class="form-control" aria-label="Small" id="ekspedisi" name="ekspedisi" value="<?php echo $row['Ekspedisi']; ?>"></td>
    </tr>
    </tr>
    <tr>
      <td width="29%">Nama Supir  :</td>
      <td ><input class="form-control" aria-label="Small" id="sopir" name="sopir" value="<?php echo $row['Sopir']; ?>"></td>
    </tr>
    <?php if (!empty($row['Cont'])) { ?>
    <tr>
          <td width="29%">Nomor Kendaraan  :</td>
          <td ><input class="form-control" aria-label="Small" id="nopol" name="nopol" value="<?php echo $row['Cont']; ?>"></td>
    </tr>  
    <?php } else { ?>
    <tr>
          <td width="29%">Nomor Kendaraan  :</td>
          <td ><input class="form-control" aria-label="Small" id="nopol" name="nopol" value="<?php echo $row['NoPolisi']; ?>"></td>
    </tr>
    <?php } ?>
    <tr>
          <td width="29%">No HP  :</td>
          <td ><input class="form-control" aria-label="Small" id="no_hp" name="no_hp" value="<?php echo $row['NoHP']; ?>"></td>
    </tr>
    </table>
    <?php } ?>
    <table class="table table-bordered">
    <tr>
      <td width="29%">Batch No  :</td>
      <td class="col-sm-2"><input class="form-control" aria-label="Small" id="batch1" name="batch1"></td>
      <td class="col-sm-2"><input class="form-control" aria-label="Small" id="batch2" name="batch2"></td>
      <td class="col-sm-2"><input class="form-control" aria-label="Small" id="batch3" name="batch3"></td>
      <td style="font-weight: bold; text-align: center;">TOTAL</td>
    </tr>
    <tr>
      <td width="29%">Packing  :</td>
      <td class="col-sm-2"><input class="form-control" aria-label="Small" id="pack1" name="pack1" value="0"></td>
      <td class="col-sm-2"><input class="form-control" aria-label="Small" id="pack2" name="pack2" value="0"></td>
      <td class="col-sm-2"><input class="form-control" aria-label="Small" id="pack3" name="pack3" value="0"></td>
      <td style="font-weight: bold; text-align: center;"><p id="demo"></p></td>
    </tr>
    <tr>
      <td width="29%">Quantity  :</td>
      <td style="font-weight: bold; text-align: center;" class="col-sm-2"><p id="qty1"></td>
      <td style="font-weight: bold; text-align: center;" class="col-sm-2"><p id="qty2"></td>
      <td style="font-weight: bold; text-align: center;" class="col-sm-2"><p id="qty3"></td>
      <td style="font-weight: bold; text-align: center;"><p id="total"></p></td>
    </tr>
    </table>
   <input style="margin-top: 10px;" class="btn btn-success btn-block" type="submit" name="submit" value="Simpan"></input>
  </form>
  </div>
  <div class="col-md-3"></div>

  <div id="modal-tambah-pl" class="modal fade" role="dialog">
		<div class="modal-dialog modal-md">
			<div class="modal-content">
				<form role="form" id="form-edit" method="post" action="" autocomplete="off">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">&times;</button>
					<h4 class="modal-title">Data Packing List</h4>
				</div>
					<div class="modal-body">
						<div id="data-tambah-pl">
						
						</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-coa" class="modal fade" role="dialog">
		<div class="modal-dialog modal-md">
			<div class="modal-content">
				<form role="form" id="form-edit" method="post" action="" autocomplete="off">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">&times;</button>
					<h4 class="modal-title">Data Packing List</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-tambah-so" 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-so.php" autocomplete="off">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">&times;</button>
					<h4 class="modal-title">Tambah Data SO Number</h4>
				</div>
					<div class="modal-body">
						<div id="data-tambah-so">
						
						</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-tambah-sobaru" class="modal fade" role="dialog">
		<div class="modal-dialog modal-md">
			<div class="modal-content">
				<form role="form" id="form-tambah-sobaru" method="post" action="input-sobaru.php" autocomplete="off">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">&times;</button>
					<h4 class="modal-title">Tambah Data SO Number</h4>
				</div>
					<div class="modal-body">
          <div class="form-group">
              <label>SO Number</label>
              <input type="hidden" name="del_id" value="<?php echo $row['Del_ID'] ; ?>">
              <input type="hidden" name="qtyu" value="<?php echo $row['QtyUnit'] ; ?>">
              <input type="hidden" name="ideks" value="<?php echo $row['DetailEkspedisi_ID'] ; ?>">
              <input class="form-control" id="so_no" name="so_no">
              <p style="color:red" id="error_so_no"></p>
            </div>
            <div class="form-group">
              <label>Document Date</label>
              <input type="text" class="input-tanggal1 form-control" id="date_doc" name="date_doc" data-date-format="d-M-Y">
              <p style="color:red" id="error_date_doc"></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>


<link rel="stylesheet" type="text/css" href="../style/jquery-ui.css">
<script type="text/javascript" src="../style/jquery-ui.js"></script>
<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 ideks = GetURLParameter('ideks');
    var qtyu = GetURLParameter('qtyu');

    $(document).ready(function(){
	  var data = "dn4.php?ideks="+ideks;
	    $('#container1').load(data);
    });
    
    function myFunction() {
      window.open("del_numb.php?id="+id);
    }
    
    $(document).on('click','#buka-coa',function(e){
		e.preventDefault();
    var fold = $(this).attr('data-fold');
    var lot = $(this).attr('data-lot');
    var urlcoa = "pdf.php?fold="+fold+"&lot="+lot;
    window.open(urlcoa);
	  });

    $('#ekspedisi').on('change', function(){
		$.get('dn2.php?ekspedisi=' + $('#ekspedisi').val(), function(data) {
				//fungsi eksekusi func. Ajax
				$('#container1').html(data);
		});
	  });

    

    $("#pack1, #pack2, #pack3").on('keyup', function() {
      var pack1 = document.getElementById("pack1").value ;
      var intpack1 = parseInt(pack1);
      var pack2 = document.getElementById("pack2").value ;
      var intpack2 = parseInt(pack2);
      var pack3 = document.getElementById("pack3").value ;
      var intpack3 = parseInt(pack3);
      hasil = intpack1+intpack2+intpack3;
      document.getElementById("demo").innerHTML = "<b>" + hasil +"</b>";
      qty1 = intpack1*qtyu;
      outqty1 = (qty1/1000).toFixed(3);
      document.getElementById("qty1").innerHTML = "<b>" + outqty1 +"</b>";
      qty2 = intpack2*qtyu;
      outqty2 = (qty2/1000).toFixed(3);
      document.getElementById("qty2").innerHTML = "<b>" + outqty2 +"</b>";
      qty3 = intpack3*qtyu;
      outqty3 = (qty3/1000).toFixed(3);
      document.getElementById("qty3").innerHTML = "<b>" + outqty3 +"</b>";
      total = qty1+qty2+qty3;
      outtot = (total/1000).toFixed(3);
      document.getElementById("total").innerHTML = "<b>" + outtot +"</b>";


    });
    
  
  $(document).ready(function(){
		$('.input-tanggal').datepicker({
			dateFormat: 'yy-mm-dd'
		});		
	});	

  $(document).ready(function(){
		$('.input-tanggal1').datepicker({
			dateFormat: 'yy-mm-dd'
		});		
	});	

  $(document).on('click','#tambah-packing-list',function(e){
		e.preventDefault();
		$("#modal-tambah-pl").modal('show');
		$.post('plrinc.php',
			{id:$(this).attr('data-id')},
			function(html){
				$("#data-tambah-pl").html(html);
			}   
		);
	});
  
  $(document).on('click','#tambah-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','#tambah-so-number',function(e){
		e.preventDefault();
		$("#modal-tambah-so").modal('show');
		$.post('so.php',
			{id:$(this).attr('data-id')},
			function(html){
				$("#data-tambah-so").html(html);
			}   
		);
	});

  $("#form-tambah-sobaru").submit(function(e) {
		e.preventDefault();
		
		$("#error_so_no").html('');
		$("#error_date_doc").html('');
		
		var dataform = $("#form-tambah-sobaru").serialize();
		$.ajax({
			url: "input-sobaru.php",
			type: "post",
			data: dataform,
			success: function(result) {
				var hasil = JSON.parse(result);
				if (hasil.hasil !== "sukses") {
					$("#error_so_no").html(hasil.error.so_no);
					$("#error_date_doc").html(hasil.error.date_doc);
				} else {
					$('#modal-tambah-sobaru').modal('hide');
					$("#del_id").val('');
					$("#so_no").val('');
					$("#date_doc").val('');
				}
			}
		});
	});

  $("#form-tambah-so").submit(function(e) {
		e.preventDefault();
		
		$("#error_so_no").html('');
		$("#error_date_doc").html('');
		
		var dataform = $("#form-tambah-so").serialize();
		$.ajax({
			url: "dn.php",
			type: "post",
			data: dataform,
			success: function(result) {
				var hasil = JSON.parse(result);
				if (hasil.hasil !== "sukses") {
					$("#error_so_no").html(hasil.error.so_no);
					$("#error_date_doc").html(hasil.error.date_doc);
				} else {
					$('#modal-tambah-so').modal('hide');
					$("#del_id").val('');
					$("#so_no").val('');
					$("#date_doc").val('');
				}
			}
		});
	});


      
  </script>
  </div>
</div>



  </div>
  </div>

</div>