<?php
session_start();
include('../includes/config.php');


if(!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../login_admin.php");
    exit;
}


if(!isset($_GET['id']) || empty($_GET['id'])) {
    echo "<script>location.href='product_admin.php';</script>";
    exit;
}

$id = mysqli_real_escape_string($conn, $_GET['id']);


$query_p = mysqli_query($conn, "SELECT * FROM produk WHERE id = '$id'");
$p = mysqli_fetch_assoc($query_p);

if(!$p) {
    echo "<script>alert('Produk tidak ditemukan!'); location.href='product_admin.php';</script>";
    exit;
}


if(isset($_POST['update'])) {
    $nama  = mysqli_real_escape_string($conn, $_POST['nama']);
    $harga = (int)$_POST['harga'];
    
    
    $update_main = mysqli_query($conn, "UPDATE produk SET nama='$nama', harga='$harga' WHERE id='$id'");

    if($update_main) {
       
        $stok_input = $_POST['stok_ukuran'];
        foreach($stok_input as $ukuran => $jumlah) {
            $jml = (int)$jumlah;
            
            
            $cek = mysqli_query($conn, "SELECT * FROM produk_detail WHERE id_produk='$id' AND ukuran='$ukuran'");
            
            if(mysqli_num_rows($cek) > 0) {
                mysqli_query($conn, "UPDATE produk_detail SET stok='$jml' WHERE id_produk='$id' AND ukuran='$ukuran'");
            } else {
                mysqli_query($conn, "INSERT INTO produk_detail (id_produk, ukuran, stok) VALUES ('$id', '$ukuran', '$jml')");
            }
        }

        
        if(!empty($_FILES['gambar']['name'])) {
            $gambar = $_FILES['gambar']['name'];
            $tmp    = $_FILES['gambar']['tmp_name'];
            if(move_uploaded_file($tmp, "../assets/img/" . $gambar)) {
                mysqli_query($conn, "UPDATE produk SET gambar='$gambar' WHERE id='$id'");
            }
        }

        
        echo "<script>
                alert('Data berhasil diperbarui!'); 
                window.location.href='product_admin.php'; 
              </script>";
        exit;
    }
}


$stok_sekarang = [];
$res_stok = mysqli_query($conn, "SELECT * FROM produk_detail WHERE id_produk = '$id'");
while($row_s = mysqli_fetch_assoc($res_stok)) {
    $stok_sekarang[$row_s['ukuran']] = $row_s['stok'];
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Edit Produk - Alfateeh Sepatuku</title>
    <style>
        body { font-family: 'Segoe UI', Arial, sans-serif; background: #f4f7f6; padding: 20px; }
        .box { background: #fff; max-width: 500px; margin: auto; padding: 25px; border-radius: 10px; box-shadow: 0 5px 15px rgba(0,0,0,0.1); }
        h2 { border-bottom: 2px solid #eee; padding-bottom: 10px; margin-bottom: 20px; color: #333; }
        label { display: block; margin-bottom: 5px; font-weight: bold; color: #555; }
        input[type="text"], input[type="number"], input[type="file"] { 
            width: 100%; padding: 10px; margin-bottom: 15px; border: 1px solid #ddd; border-radius: 5px; box-sizing: border-box; 
        }
        .stok-group { background: #fff9db; padding: 15px; border-radius: 8px; margin-bottom: 15px; border: 1px solid #ffe58f; }
        .row { display: flex; align-items: center; margin-bottom: 8px; }
        .row label { width: 100px; margin: 0; font-size: 14px; }
        .row input { width: 80px !important; margin: 0; text-align: center; border: 1px solid #ccc; }
        .btn-update { background: #28a745; color: #fff; border: none; padding: 12px; width: 100%; border-radius: 5px; cursor: pointer; font-size: 16px; font-weight: bold; transition: 0.3s; }
        .btn-update:hover { background: #218838; }
        .preview { margin-bottom: 10px; border: 1px solid #ddd; padding: 5px; display: block; border-radius: 4px; }
        .btn-cancel { display:block; text-align:center; margin-top:15px; text-decoration:none; color:#999; font-size: 14px; }
    </style>
</head>
<body>

<div class="box">
    <h2>Edit Produk #<?= $id ?></h2>
    <form action="" method="POST" enctype="multipart/form-data">
        <label>Nama Produk</label>
        <input type="text" name="nama" value="<?= htmlspecialchars($p['nama']) ?>" required>

        <label>Harga (Rp)</label>
        <input type="number" name="harga" value="<?= $p['harga'] ?>" required>

        <div class="stok-group">
            <label>Stok per Ukuran (40, 41, 42):</label>
            <?php 
            $ukuran_pilihan = ['40', '41', '42'];
            foreach($ukuran_pilihan as $uk): 
                $val = isset($stok_sekarang[$uk]) ? $stok_sekarang[$uk] : 0;
            ?>
            <div class="row">
                <label>Ukuran <?= $uk ?></label>
                <input type="number" name="stok_ukuran[<?= $uk ?>]" value="<?= $val ?>" min="0">
                <span style="margin-left: 10px; color: #888;">Pcs</span>
            </div>
            <?php endforeach; ?>
        </div>

        <label>Gambar Saat Ini</label>
        <img src="../assets/img/<?= $p['gambar'] ?>" width="100" class="preview">
        <input type="file" name="gambar">
        <small style="color: #999; display: block; margin-bottom: 15px;">*Biarkan kosong jika tidak ingin mengganti foto.</small>

        <button type="submit" name="update" class="btn-update">Simpan Perubahan</button>
        <a href="product_admin.php" class="btn-cancel">Batal & Kembali</a>
    </form>
</div>

</body>
</html>