<?php
include "../db.php";

$id = intval($_GET['id']);
$stmt = $pdo->prepare("SELECT * FROM categories WHERE id=?");
$stmt->execute([$id]);
$c = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$c) die("Category Not Found!");

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $newName = trim($_POST['category_name']);
    $newImage = $c['image_path'];

    if (!empty($_FILES['image']['name'])) {
        $upload_dir = "../lbfood/uploads/categories/";
        if (!is_dir($upload_dir)) mkdir($upload_dir, 0777, true);

        $clean = str_replace(' ', '-', $_FILES['image']['name']);
        $newImage = time() . "_" . $clean;
        move_uploaded_file($_FILES['image']['tmp_name'], $upload_dir . $newImage);
    }

    $pdo->prepare("UPDATE categories SET category_name=?, image_path=? WHERE id=?")
        ->execute([$newName, $newImage, $id]);

    echo "<script>
        alert('Category Updated Successfully!');
        window.parent.loadPage(null,'view_categories.php');
    </script>";
    exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Category</title>
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap" rel="stylesheet">

<style>
:root{
    --primary:#0b4f27;
    --primary-dark:#083a1d;
    --bg:#f3f4f6;
    --card:#ffffff;
    --text:#1f2937;
    --muted:#6b7280;
}

body{
    font-family:'Poppins',sans-serif;
    background:var(--bg);
    color:var(--text);
    padding:20px;
}

.container{
    max-width:600px;
    margin:auto;
}

.card{
    background:var(--card);
    padding:25px;
    border-radius:14px;
    box-shadow:0 8px 20px rgba(0,0,0,.06);
}

h3{
    color:var(--primary);
    margin-bottom:15px;
}

label{
    font-weight:500;
    margin-top:14px;
    display:block;
}

input[type="text"],
input[type="file"]{
    width:100%;
    padding:10px 12px;
    margin-top:6px;
    border-radius:8px;
    border:1px solid #d1d5db;
    font-size:14px;
}

.preview{
    margin-top:10px;
    width:120px;
    height:120px;
    object-fit:cover;
    border-radius:10px;
    border:1px solid #e5e7eb;
}

.btn{
    padding:10px 18px;
    border-radius:8px;
    border:none;
    font-weight:600;
    cursor:pointer;
    margin-top:18px;
}

.btn-success{
    background:var(--primary);
    color:#fff;
}
.btn-success:hover{
    background:var(--primary-dark);
}

.btn-secondary{
    background:#6b7280;
    color:#fff;
    margin-left:10px;
}
</style>
</head>

<body>

<div class="container">
<div class="card">

<h3>Edit Category</h3>

<form method="POST" enctype="multipart/form-data">

    <label>Category Name</label>
    <input type="text" name="category_name"
           value="<?= htmlspecialchars($c['category_name']) ?>" required>

    <label>Existing Image</label>
    <img src="../lbfood/uploads/categories/<?= htmlspecialchars($c['image_path']) ?>"
         class="preview">

    <label>Replace Image (Optional)</label>
    <input type="file" name="image">

    <div>
        <button class="btn btn-success">Update Category</button>
        <button type="button" class="btn btn-secondary"
                onclick="window.parent.loadPage(null,'view_categories.php')">
            Cancel
        </button>
    </div>

</form>

</div>
</div>

</body>
</html>
