<?php 
include 'header.php';
$tanggal = date('Y-m-d H:i:s'); 
$sortage = mysqli_query($conn, "SELECT * FROM produksi where cek = '1'");
$cek_sor = mysqli_num_rows($sortage);
?>

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Master Produk - UMKM Galeri 3D</title>
<style>
body {
    background: #f3f4f6;
    font-family: 'Poppins', sans-serif;
    margin: 0;
    padding: 0;
}
.container {
  	max-width: 1200px;
    margin: 5px auto;
    padding: 0 30px;
}
</style>
</head>
<body>
<div class="container">
	<h2 style=" width: 100%; border-bottom: 4px solid gray"><b>Daftar Pesanan</b></h2>
	<br>
	<h5 class="bg-success" style="padding: 7px; width: 360px; font-weight: bold;"><marquee>Lakukan Reload Setiap Masuk Halaman ini, untuk menghindari terjadinya kesalahan data dan informasi</marquee></h5>
	<a href="produksi.php" class="btn btn-default"><i class="glyphicon glyphicon-refresh"></i> Reload</a>
	<br>
	<table class="table table-striped">
		<thead>
			<tr>
				<th scope="col">No</th>
				<th scope="col">Detail</th>
				<th scope="col">Status & Lokasi</th>
				<th scope="col">Action</th>
			</tr>
		</thead>
		<tbody>

			<?php 
			$result = mysqli_query($conn, "SELECT DISTINCT invoice, kode_customer, status, kode_produk, qty, terima, tolak, cek, tanggal, lokasi FROM produksi  GROUP BY invoice") or die("Query Error: " . mysqli_error($conn));
			$no = 1;
			$array = 0;
			while($row = mysqli_fetch_assoc($result)){
				$kodep = $row['kode_produk'];
				$inv = $row['invoice'];
				?>

				<tr>
					<td><?= $no; ?></td>
					<td>
						<b><?= $row['invoice']; ?></b><br>
						<?= $row['kode_customer']; ?><br>
						<small style="color: gray;">
							<?= $row['tanggal']; ?>
						</small>
					</td>
					<?php if($row['terima'] == 1){ ?>
						<td style="color: green;font-weight: bold;">Pesanan Diterima (Siap Kirim)
							<?php
						}else if($row['tolak'] == 1){
							?>
							<td style="color: red;font-weight: bold;">Pesanan Ditolak
								<?php 
							}
							if($row['terima'] == 0 && $row['tolak'] == 0){
								?>
								<td style="color: orange;font-weight: bold;"><?= $row['status']; ?>
								<?php 
							}
							$t_bom = mysqli_query($conn, "SELECT * FROM bom_produk WHERE kode_produk = '$kodep'");

							while($row1 = mysqli_fetch_assoc($t_bom)){
								$kodebk = $row1['kode_bk'];

								$inventory = mysqli_query($conn, "SELECT * FROM inventory WHERE kode_bk = '$kodebk'");
								$r_inv = mysqli_fetch_assoc($inventory);

								$kebutuhan = $row1['kebutuhan'];	
								$qtyorder = $row['qty'];
								$inventory = $r_inv['qty'];

								$bom = ($kebutuhan * $qtyorder);
								$hasil = $inventory - $bom;
								if($hasil < 0 && $row['tolak'] == 0){
									$nama_material[] = $r_inv['nama'];
									mysqli_query($conn, "UPDATE produksi SET cek = '1' where invoice = '$inv'");
									?>



									<?php 
								}
							}
							?>
							<br>
							<?php 
							$lokasi = $row['lokasi']; // format: "lat, lng"
							if (!empty($lokasi)) {
								// embed iframe google maps
								echo "<iframe 
										width='150' 
										height='150' 
										style='border:0; border-radius:8px;' 
										loading='lazy' 
										allowfullscreen 
										referrerpolicy='no-referrer-when-downgrade'
										src='https://www.google.com/maps?q=$lokasi&output=embed'>
									</iframe>";
							} else {
								echo "Lokasi tidak tersedia";
							}
							?>
						</td>
						<td>
							<?php if( $row['tolak']==0 && $row['cek']==1 && $row['terima']==0){ ?>
								<a href="inventory.php?cek=0" id="rq" class="btn btn-warning"><i class="glyphicon glyphicon-warning-sign"></i></a> 
								<a href="proses/tolak.php?inv=<?= $row['invoice']; ?>" class="btn btn-danger" onclick="return confirm('Yakin Ingin Menolak ?')"><i class="glyphicon glyphicon-remove-sign"></i></a> 
							<?php }else if($row['terima'] == 0 && $row['cek']==0){ ?>

								<a href="proses/terima.php?inv=<?= $row['invoice']; ?>&kdp=<?= $row['kode_produk']; ?>" class="btn btn-success"><i class="glyphicon glyphicon-ok-sign"></i></a> 
								<a href="proses/tolak.php?inv=<?= $row['invoice']; ?>" class="btn btn-danger" onclick="return confirm('Yakin Ingin Menolak ?')"><i class="glyphicon glyphicon-remove-sign"></i></a> 
							<?php } ?>

							<a href="detailorder.php?inv=<?= $row['invoice']; ?>&cs=<?= $row['kode_customer']; ?>" type="submit" class="btn btn-primary"><i class="glyphicon glyphicon-eye-open"></i></a>
							<a href="proses/hapus_pesanan.php?inv=<?= $row['invoice']; ?>" 
								class="btn btn-danger" 
								onclick="return confirm('Apakah Anda yakin ingin menghapus pesanan ini?')">
								<i class="glyphicon glyphicon-trash"></i>
							</a>
						</td>
					</tr>
					<?php
					$no++; 
				}
				?>

			</tbody>
		</table>

	<?php if($cek_sor > 0 && !empty($nama_material)){ ?>
    <br><br>
    <div class="row">
        <div class="col-md-4 bg-danger" style="padding:10px;">
            <h4>Kekurangan Material </h4>
            <h5 style="color: red;font-weight: bold;">Silahkan Tambah Stok Material dibawah ini : </h5>
            <table class="table table-striped">
                <tr>
                    <th>No</th>
                    <th>Material</th>
                </tr>
                <?php 
                $arr = array_values(array_unique($nama_material));
                for ($i = 0; $i < count($arr); $i++) { 
                ?>
                    <tr>
                        <td><?= $i+1 ?></td>
                        <td><?= $arr[$i]; ?></td>
                    </tr>
                <?php } ?>
            </table>
        </div>
    </div>
<?php } ?>
	</div>

</body>
</html>
