<?php

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

$db = new database();

$update = false;

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

	if ($num_rows > 0) {
		while ($rows = $db -> db_fetch_array($result)) {
			$nama = $rows['nama'];
			$harga = $rows['harga'];
			$deskripsi = $rows['deskripsi'];
			$stok = $rows['stok'];
		}
	}
}

?>

<div class="tambah-barang-page">
	<div class="form-tambah-barang">
		<div class="tambah-barang-title">
			<?php
			if ($update == false) {
				echo 'Tambah Barang';
			} else {
				echo 'Update Barang';
			}
			?>
		</div>
		<form <?php if ($update == false) { echo 'action="function-tambah-barang.php"'; } else { echo 'action="function-tambah-barang.php?produk_id='.$id.'&action=update"'; } ?> method="post" enctype="multipart/form-data">
			<div class="field">
				<div class="title-field">Nama Barang</div>
				<input type="text" name="nama" <?php if ($update == true) {
					echo 'value="'.$nama.'"';
				} ?> required>
			</div>
			<div class="field">
				<div class="title-field">Harga Sewa</div>
				<input type="number" name="harga" <?php if ($update == true) {
					echo 'value="'.$harga.'"';
				} ?> required>
			</div>
			<div class="field">
				<div class="title-field">Stok</div>
				<input type="number" name="stok" <?php if ($update == true) {
					echo 'value="'.$stok.'"';
				} ?> required>
			</div>
			<div class="field">
				<div class="title-field">Deskripsi</div>
				<textarea type="text" name="deskripsi" required><?php if ($update == true) {
					echo $deskripsi;
				} ?></textarea> 
			</div>
			<?php
			if ($update == false) {
				echo '
				<div class="field">
					<div class="title-field">Gambar Produk</div>
					<input type="file" name="gambar">
				</div>
					';
			}
			?>
			<div class="tombol-tambah-barang">
				<a href="index.php?menu=barang"><div class="tombol-tambah-barang-batal">Batal</div></a>
				<div class="tombol-tambah-barang-simpan"><input type="submit" value="Simpan"></div>
			</div>
		</form>
	</div>
</div>