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

<div class="row">
  <div class="col-md-6">
  <div class="pagetitle">
  	<h3>Data PO</h3>
  </div>
  <?php
	$data = mysqli_query ($koneksi, " select 
											po_tbl.PO_ID,
											po_tbl.No_PO,
											customer.CustomerName,
											po_tbl.TanggalPO
										from 
										po_tbl 
										INNER JOIN customer
										ON po_tbl.CustomerID=customer.CustomerID
										WHERE PO_ID ='$_GET[id]'");
	$row = mysqli_fetch_array ($data);
   ?>
   <table class="table">
   	<tr>
   		<td>Nomor PO  :</td>
   		<td><?php echo $row['No_PO']; ?></td>
   	</tr>
   	<tr>
   		<td width="50%">Nama Customer  :</td>
   		<td><?php echo $row['CustomerName']; ?></td>
   	</tr>
   	<tr>
   		<td>Tanggal PO  :</td>
   		<td><?php echo $row['TanggalPO']; ?></td>
   	</tr>
   </table>
   <div class="pagetitle" style="margin-top: 8px">
  	<h3>Detail PO</h3>
  </div>
  <div id="data-mahasiswa"></div>
  <p style="margin-top: 5px">
		<a href="#" style="color: #0000FF" class='editbtn' id="tambah" data-id="<?php echo $row['PO_ID']; ?>"><i class="bi bi-plus-square"></i> Tambah Data</a>
  	</p>

  </div>


  <div class="col-md-6">
  	<div class="pagetitle" style="margin-top: 8px">
  	<h3>Tabel Delivery</h3>
  </div>
  <div style="width:95%;height:540px;overflow:scroll;align: left;" >
  <div id="data-mahasiswa1"></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');
	
$(document).ready(function(){
	var data = "data_detailpo.php?id="+id;
	$('#data-mahasiswa').load(data);
	 });

$(document).ready(function(){
	var data = "data_delivery.php?id="+id;
	$('#data-mahasiswa1').load(data);
  

});
	
	$(document).on('click','#tambah',function(e){
		e.preventDefault();
		$("#modal-tambah").modal('show');
		$.post('addrincian.php',
			{id:$(this).attr('data-id')},
			function(html){
				$("#data-tambah").html(html);
			}   
		);
	});
	
	$("#form-tambah").submit(function(e) {
		e.preventDefault();
		
		$("#error_po_id").html('');
		$("#error_product_id").html('');
		$("#error_qty_po").html('');
		
		var dataform = $("#form-tambah").serialize();
		$.ajax({
			url: "addrincian.php",
			type: "post",
			data: dataform,
			success: function(result) {
				var hasil = JSON.parse(result);
				if (hasil.hasil !== "sukses") {
					$("#error_po_id").html(hasil.error.po_id);
					$("#error_product_id").html(hasil.error.product_id);
					$("#error_qty_po").html(hasil.error.qty_po);
				} else {
					$('#modal-tambah').modal('hide');
					$("#po_id").val('');
					$("#product_id").val('');
					$("#qty_po").val('');
					$("#sample").val('');
					$('#data-mahasiswa').load(data);
				}
			}
		});
	});
	
	$(document).ready(function(){
		$('.input-tanggal').datepicker({
			dateFormat: 'yy-mm-dd'
		});		
	});	

	

  </script>
  <?php include 'detail_po.php';?>