<?php 
include 'header.php';
include '../koneksi/koneksi.php';

// generate kode material
$kode = mysqli_query($conn, "SELECT kode_produk from produk order by kode_produk desc");
$data = mysqli_fetch_assoc($kode);
$num = substr($data['kode_produk'], 1, 4);
$add = (int) $num + 1;
if(strlen($add) == 1){
	$format = "P000".$add;
}else if(strlen($add) == 2){
	$format = "P00".$add;
}
else if(strlen($add) == 3){
	$format = "P0".$add;
}else{
	$format = "P".$add;
}
?>

<!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;
}
/* Umum (untuk semua perangkat) */
.button-row .btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-weight: 500;
}

/* Untuk tampilan HP (maksimal 767px) */
@media (max-width: 767px) {
  .button-row {
    display: block;
    margin-top: 15px;
  }
  .button-row .col-md-6 {
    width: 100%;
    padding: 0 10px;
  }
  .button-row .btn {
    width: 100%;
    margin-bottom: 10px;
  }
}

/* Untuk tampilan Desktop (min 768px) */
@media (min-width: 768px) {
  .button-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 20px;
    margin-bottom: 40px;
  }
  .button-row .col-md-6 {
    flex: 0 0 48%;
  }
}
</style>
</head>
<body>

<div class="container">
	<h2 style="width:100%; border-bottom:3px solid gray; font-size:30px; margin-bottom: 20px; padding-bottom: 10px;"><b>Master Produk</b></h2>

	<form action="proses/tm_produk.php" method="POST" enctype="multipart/form-data">
		<div class="row">
			<div class="col-md-6">
				<div class="form-group">
					<label for="exampleInputFile">Pilih Gambar</label>
					<input type="file" id="exampleInputFile" name="files">
					<p class="help-block">Pilih Gambar untuk Produk</p>
				</div>
			</div>

			<div class="col-md-6">
				<div class="form-group">
					<label>Kode Produk</label>
					<input type="text" class="form-control" disabled value="<?= $format; ?>">
					<input type="hidden" name="kode" value="<?= $format; ?>">
				</div>
			</div>
		</div>	
		<div class="row">
			<div class="col-md-6">
				<div class="form-group">
					<label>Nama Produk</label>
					<input type="text" class="form-control" name="nama" placeholder="Masukkan Nama Produk">
				</div>
			</div>

			<div class="col-md-6">
				<div class="form-group">
					<label>Stok</label>
					<input type="number" class="form-control" name="stok" placeholder="Masukkan Jumlah Stok">
				</div>
			</div>
		</div>

		<div class="row">
			<div class="col-md-6">
				<div class="form-group">
					<label>Harga</label>
					<input type="number" class="form-control" name="harga" placeholder="Contoh : 12000">
					<p class="help-block">Isi harga tanpa menggunakan titik (.) atau koma (,)</p>
				</div>
			</div>

			<div class="col-md-6">
				<div class="form-group">
					<label>Diskon</label>
					<input type="number" class="form-control" name="diskon" placeholder="Contoh : 12000">
					<p class="help-block">Isi diskon tanpa menggunakan titik (.) atau koma (,)</p>
				</div>
			</div>
		</div>

		<div class="form-group">
			<label>Deskripsi</label>
			<textarea name="desk" class="form-control" placeholder="Masukkan deskripsi produk..."></textarea>
		</div>

		<hr>
		<h2 style="width:100%; border-bottom:3px solid gray; font-size:30px; margin-bottom: 20px; padding-bottom: 10px;"><b>Bahan Produk</b></h2>
		<br>

		<div class="row">
			<div class="col-md-6">
				<h4>Daftar Material yang Ada di Gudang / Inventory</h4>
				<div class="table-responsive">
					<table class="table table-striped">
						<thead>
							<tr>
								<th>No</th>
								<th>Kode Material</th>
								<th>Nama Material</th>
							</tr>
						</thead>
						<tbody>
							<?php 
							$result2 = mysqli_query($conn, "SELECT * FROM inventory ORDER BY kode_bk ASC");
							$no2 = 1;
							while ($row2 = mysqli_fetch_assoc($result2)) {
							?>
							<tr>
								<td><?= $no2; ?></td>
								<td><?= $row2['kode_bk']; ?></td>
								<td><?= $row2['nama']; ?></td>
							</tr>
							<?php 
								$no2++;
							}
							?>
						</tbody>
					</table>
				</div>
			</div>

			<div class="col-md-6">
				<h4>Pilih Material yang Dibutuhkan</h4>
				<div class="bg-danger" style="padding: 8px;">
					<p style="color:red; font-weight:bold;">NB: Form di bawah tidak harus diisi semua. Kode material tidak boleh sama.</p>
				</div>
				<br>
				<?php 
				$result3 = mysqli_query($conn, "SELECT * FROM inventory");
				$jml = mysqli_num_rows($result3);
				$no3 = 1;
				while ($no3 <= $jml) {
				?>
				<div class="row">
					<div class="col-md-6">
						<div class="form-group">
							<label>Kode Material</label>
							<input type="text" name="material[]" class="form-control" placeholder="Masukkan Kode Material">
						</div>
					</div>

					<div class="col-md-6">
						<div class="form-group">
							<label>Kebutuhan Material</label>
							<input type="text" name="keb[]" class="form-control" placeholder="Contoh: 250 atau 0.2">
						</div>
					</div>
				</div>
				<?php 
					$no3++;
				}
				?>
			</div>
		</div>

		<div class="row button-row" style="margin-top: 20px; display: flex; align-items: center; justify-content: space-between; margin-bottom: 40px;">
			<div class="col-md-6 mb-2">
				<button type="submit" class="btn btn-success btn-block">
				<i class="glyphicon glyphicon-plus"></i> Tambah
				</button>
			</div>
			<div class="col-md-6 mb-2">
				<a href="m_produk.php" class="btn btn-danger btn-block">
				<i class="glyphicon glyphicon-remove"></i> Cancel
				</a>
			</div>
		</div>
	</form>
</div>
</body>
</html>
