<?php

$path_to_root = "";
include $path_to_root . '../database.php';

$db = new database();

if (isset($_GET['action']) && $_GET['action']!="" && $_GET['action']=='delete') {
	$id = $_GET['produk_id'];
	$sql = "SELECT foto FROM barang WHERE id_barang='$id'";
	$result = $db->db_query($sql);

	list($picture) = $db->db_fetch_array($result);
	$path="../img-barang/$picture";

	if (file_exists($path)==true) {
		unlink($path);
	}

	$sql_delete = "DELETE FROM barang WHERE id_barang='$id'";
	$db->db_query($sql_delete);
}

?>

<div class="barang">
	<div class="barang-title">
		<div class="judul">Kelola Produk</div>
		<a href="index.php?menu=tambah barang"><div class="btn-barang">Tambah Barang</div></a>
	</div>
	<div class="tabel-barang-title">
		<table>
			<thead>
				<tr>
					<th class="tabel-barang-no">No</th>
					<th class="tabel-barang-foto">Foto</th>
					<th class="tabel-barang-produk">Nama Produk</th>
					<th class="tabel-barang-harga">Harga Sewa</th>
					<th class="tabel-barang-stok">Stok Awal</th>
					<th class="tabel-barang-stok">Sisa Stok</th>
					<th>Deskripsi</th>
					<th class="tabel-barang-tools">Tools</th>
				</tr>
			</thead>
		</table>
	</div>
	<div class="tabel-barang-content">
		<table>
			<tbody>
				<?php

				$sql = "SELECT * FROM barang";

				$result = $db->db_query($sql);
				$num_rows = $db->db_num_rows($result);

				if ($num_rows > 0) {
					$i=1;
					while ($rows = $db -> db_fetch_array($result)) {
						list_barang($db, $i, $rows['id_barang'], $rows['foto'], $rows['nama'],"Rp. " . $rows['harga'], $rows['stok'], $rows['deskripsi']);
						$i++;
					}
				} else {
					for ($i=1; $i <= 50; $i++) { 
						list_barang($db, $i, 'foto '.$i, "produk ".$i, "10k", "100", "deskripsi ".$i);
					}
				}

				?>
			</tbody>
		</table>
	</div>
</div>

<?php

function list_barang($db, $no, $id_produk, $foto, $produk, $harga, $stok, $deskripsi) {
	echo '
	<tr>
		<td class="tabel-barang-no">'.$no.'</td>
		<td class="list-tabel-barang-foto"><img src="../img-barang/' . $foto . '" width="130" height="130"></td>
		<td class="list-tabel-barang-produk"><div class="data-tabel">'.$produk.'</div></td>
		<td class="list-tabel-barang-harga"><div class="data-tabel">'.$harga.'</div></td>
		<td class="tabel-barang-stok"><div class="data-tabel">'.$stok.'</div></td>
		<td class="tabel-barang-stok"><div class="data-tabel">'.cek_sisa_stok($db, $id_produk, $stok).'</div></td>
		<td><div class="data-tabel">'.$deskripsi.'</div></td>
		<td class="tabel-barang-tools">
			<div class="content-tools">
				<a href="index.php?menu=barang&produk_id='.$id_produk.'&action=delete"><div class="hapus">Hapus</div></a>
				<a href="index.php?menu=tambah barang&produk_id='.$id_produk.'&action=update"><div class="edit">Edit</div></a>
			</div>
		</td>
	</tr>
	';
}

function cek_sisa_stok($db, $id_produk, $stok) {
	$sql = 'SELECT b.quantity FROM `barang` a
		LEFT JOIN `order_produk` b ON b.id_barang = a.id_barang
		LEFT JOIN `order` c ON c.id = b.id_order
		WHERE a.id_barang = '.$id_produk.' AND (c.status = \'barang sudah diambil\' OR c.status = \'verifikasi pembayaran sukses\')';

	$result = $db->db_query($sql);
	$num_rows = $db->db_num_rows($result);
	$jumlah_sewa = 0;

	if ($num_rows > 0) {
		while ($sewa = $db -> db_fetch_array($result)) {
			$jumlah_sewa = $jumlah_sewa + $sewa['quantity'];
		}
		$sisa_stok = $stok - $jumlah_sewa;

		 return $sisa_stok;
	} else {
		return $stok;
	}
}

?>