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

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

if(isset($_POST['simpan'])) {
    $nama   = mysqli_real_escape_string($conn, $_POST['nama']);
    $harga  = $_POST['harga'];
    
    
    $gambar = $_FILES['gambar']['name'];
    $tmp    = $_FILES['gambar']['tmp_name'];
    $path   = "../assets/img/" . $gambar;

    if(move_uploaded_file($tmp, $path)) {
        
        $query_produk = "INSERT INTO produk (nama, harga, gambar) VALUES ('$nama', '$harga', '$gambar')";
        
        if(mysqli_query($conn, $query_produk)) {
            $id_produk_baru = mysqli_insert_id($conn); 
            
            
            $stok_input = $_POST['stok_ukuran']; 
            
            foreach($stok_input as $ukuran => $jumlah) {
                
                if($jumlah !== "") {
                    $jml = (int)$jumlah;
                    mysqli_query($conn, "INSERT INTO produk_detail (id_produk, ukuran, stok) 
                                        VALUES ('$id_produk_baru', '$ukuran', '$jml')");
                }
            }

            echo "<script>alert('Produk & Stok Berhasil Ditambah!'); window.location='produk.php';</script>";
        }
    } else {
        echo "Gagal upload gambar!";
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <title>Tambah Produk & Stok</title>
    <style>
        body { font-family: sans-serif; background: #f4f4f4; padding: 20px; }
        .form-card { background: white; max-width: 600px; margin: auto; padding: 25px; border-radius: 10px; box-shadow: 0 4px 10px rgba(0,0,0,0.1); }
        input[type="text"], input[type="number"], input[type="file"] { width: 100%; padding: 10px; margin: 10px 0; border: 1px solid #ddd; border-radius: 5px; box-sizing: border-box; }
        
        .stok-container { background: #f9f9f9; padding: 15px; border-radius: 8px; margin: 15px 0; border: 1px solid #eee; }
        .stok-row { display: flex; align-items: center; margin-bottom: 10px; }
        .stok-row label { width: 100px; font-weight: bold; color: #555; }
        .stok-row input { width: 80px !important; margin: 0; text-align: center; }
        
        button { background: #007bff; color: white; border: none; padding: 12px; width: 100%; border-radius: 5px; cursor: pointer; font-size: 16px; font-weight: bold; }
        button:hover { background: #0056b3; }
    </style>
</head>
<body>

<div class="form-card">
    <h2>Tambah Produk Baru</h2>
    <form action="" method="POST" enctype="multipart/form-data">
        <label>Nama Produk</label>
        <input type="text" name="nama" required placeholder="Contoh: Sepatu Jordan High">
        
        <label>Harga (Rp)</label>
        <input type="number" name="harga" required placeholder="Contoh: 1500000">

        <div class="stok-container">
            <label><strong>Input Stok Per Ukuran:</strong></label>
            <p style="font-size: 12px; color: #777;">Isi jumlah stok pada ukuran yang tersedia</p>
            
            <div class="stok-row">
                <label>Ukuran 40</label>
                <input type="number" name="stok_ukuran[40]" placeholder="0">
                <span style="margin-left: 10px; color: #999;">pcs</span>
            </div>
            
            <div class="stok-row">
                <label>Ukuran 41</label>
                <input type="number" name="stok_ukuran[41]" placeholder="0">
                <span style="margin-left: 10px; color: #999;">pcs</span>
            </div>
            
            <div class="stok-row">
                <label>Ukuran 42</label>
                <input type="number" name="stok_ukuran[42]" placeholder="0">
                <span style="margin-left: 10px; color: #999;">pcs</span>
            </div>
        </div>
        
        <label>Gambar Produk</label>
        <input type="file" name="gambar" required>
        
        <button type="submit" name="simpan">Simpan Produk & Stok</button>
        <a href="produk.php" style="display:block; text-align:center; margin-top:15px; color:#666; text-decoration:none;">Kembali</a>
    </form>
</div>

</body>
</html>