<?php include "../koneksi.php"; ?>
<div class="row">
<div class="col-md-6">
<p>
	<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-tambah">
	<i class="bi bi-plus-square"></i>&ensp;Tambah Data</button>
</p>
</div>
<div class="col-md-6">
	 <nav>
    <ol class="breadcrumb">
		<li class="breadcrumb-item">Pengiriman</li>
        <li class="breadcrumb-item">Table Delivery</li>
    </ol>
</nav>
</div>
</div>
<div style="width:100%;height500px;overflow:scroll;" >
<table id="example" class="display nowrap" style="width:100%">
        <thead>
		<tr>
			<th width="7%">
				Tanggal
			</th>
			<th width="23%">
				Customer
			</th>
			<th width="20%">
				Product
			</th>
			<th width="10%">
				No.PO
			</th>
			<th width="10%">
				No.DN
			</th>
			<th width="10%">
				QtyKirim
			</th>
			<th width="20%">
				Opsi
			</th>
		</tr>
		</thead>
		<tbody>
			<?php 
				$data = mysqli_query ($koneksi, " select 
															deliverytbl.Del_ID,
															deliverytbl.DateKirim,
															deliverytbl.DetailPO_ID,
															detail_po.PO_ID,
															po_tbl.No_PO,
															po_tbl.CustomerID,
															customer.CustomerName,
															detail_po.ProductID,
															product.ItemDescription,
															product.SLoc,
															product.QtyUnit,
															deliverytbl.DetailEkspedisi_ID,
															detailekspedisi.EkspedisiID,
															ekspedisi.Ekspedisi,
															detailekspedisi.Sopir,
															deliverytbl.NoDN,		
															deliverytbl.QtyKirim,
															deliverytbl.Sample		
											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
											order by deliverytbl.DateKirim DESC");
				
				while($row = mysqli_fetch_array ($data)):
			?>
		<tr>
			<td width="10%">
				<?php 
				$tanggal = $row['DateKirim'];
				$day = date('D', strtotime($tanggal));
				$dayList = array(
					'Sun' => 'Minggu',
					'Mon' => 'Senin',
					'Tue' => 'Selasa',
					'Wed' => 'Rabu',
					'Thu' => 'Kamis',
					'Fri' => 'Jumat',
					'Sat' => 'Sabtu'
				);
				echo date ("d-m-Y", strtotime($tanggal)).' '.$dayList[$day]; ?>
			</td>
			<td width="20%">
				<?php echo $row['CustomerName']; ?>
			</td>
			<td width="25%">
				<?php echo $row['ItemDescription']; ?>
			</td>
			<td width="10%">
				<?php echo $row['No_PO']; ?>
			</td>
			<td width="10%">
				<?php echo $row['NoDN']; ?>
			</td>
			<td width="10%">
				<?php echo $row['QtyKirim']; ?>
				<?php echo substr($row['SLoc'], 0, 3); ?>
			</td>
			<td width="20%">
			    <a href="delivery/plist.php?id=<?php echo $row['Del_ID']; ?>&prod=<?php echo $row['ProductID']; ?>" style="color: #00008B" ><i class="bi bi-journal-check"></i> P.List</a>&ensp;|&ensp;
				<a href="#" style="color: #DAA520" class='editbtn' id="edit" data-id="<?php echo $row['Del_ID']; ?>"><i class="bi bi-pen-fill"></i> Edit</a>&ensp;|&ensp; 
				<a href="#" style="color: #DC143C" data-href="delivery/hapus.php?id=<?php echo $row['Del_ID']; ?>&po=<?php echo $row['DetailPO_ID']; ?>&qty=<?php echo $row['QtyKirim']; ?>" data-toggle="modal" data-target="#confirm-delete"><i class="bi bi-trash-fill"></i> Hapus</a> 
			</td>
		</tr>
			<?php endwhile; ?>
		</tbody>
	</table>
	</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>

	<script>
		$('#confirm-delete').on('show.bs.modal', function(e) {
			$(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
			
		});

		$.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" ]]
		});
    </script>
