<?php
session_start();
if(!isset($_SESSION['admin'])){
    header("Location: /frozenfood/");
    exit;
}
include 'koneksi.php';

if(isset($_POST['simpan'])){
    $nama       = $_POST['nama_produk'];
    $harga      = $_POST['harga'];
    $stok       = $_POST['stok'];
    $deskripsi  = $_POST['deskripsi'];
    $gambar     = $_FILES['gambar']['name'];
    $tmp        = $_FILES['gambar']['tmp_name'];

    move_uploaded_file($tmp, "images/".$gambar);

    $query = "INSERT INTO produk (nama_produk, harga, stok, gambar, deskripsi)
              VALUES ('$nama', '$harga', '$stok', '$gambar', '$deskripsi')";

    mysqli_query($conn, $query);

    echo "<script>
            alert('Produk berhasil ditambahkan');
            window.location='admin.php';
          </script>";
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Tambah Produk</title>
    <style>
        body{
            font-family: Arial;
            background: #f4f6f9;
            margin: 0;
            padding: 0;
        }
        .container{
            width: 500px;
            margin: 40px auto;
            background: white;
            padding: 25px;
            border-radius: 12px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        }
        h2{
            text-align: center;
            color: #f44336;
        }
        input, textarea{
            width: 100%;
            padding: 12px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 8px;
            box-sizing: border-box;
        }
        textarea{
            resize: none;
        }
        .btn{
            padding: 12px 18px;
            border: none;
            border-radius: 8px;
            color: white;
            font-weight: bold;
            cursor: pointer;
        }
        .save{ background:#28a745; }
        .back{
            background:#007bff;
            text-decoration:none;
            display:inline-block;
            margin-right:10px;
        }
    </style>
</head>
<body>

<div class="container">
    <h2>➕ Tambah Produk</h2>

    <form method="POST" enctype="multipart/form-data">
        <label>Nama Produk</label>
        <input type="text" name="nama_produk" required>

        <label>Harga</label>
        <input type="number" name="harga" required>

        <label>Stok</label>
        <input type="number" name="stok" required>

        <label>Deskripsi Produk</label>
        <textarea name="deskripsi" rows="4" required></textarea>

        <label>Gambar</label>
        <input type="file" name="gambar" required>

        <a href="admin.php" class="btn back">⬅️ Kembali</a>
        <button type="submit" name="simpan" class="btn save">💾 Simpan</button>
    </form>
</div>

</body>
</html>