<?php
include "db.php";

if(!isset($_GET['id'])) { echo "<p>No ID provided.</p>"; exit; }
$id = intval($_GET['id']);

$result = $conn->query("SELECT * FROM inventory WHERE id=$id");
if($result->num_rows == 0){ echo "<p>Item not found.</p>"; exit; }
$item = $result->fetch_assoc();

if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $quantity = $_POST['quantity'];
    $unit_price = $_POST['unit_price'];
    $description = $_POST['description'];

    $sql = "UPDATE inventory SET name='$name', quantity='$quantity', unit_price='$unit_price', description='$description' WHERE id=$id";

    if($conn->query($sql)){
        $success = "Item updated successfully!";
        $item = $conn->query("SELECT * FROM inventory WHERE id=$id")->fetch_assoc();
    } else { $error = $conn->error; }
}
?>

<h2>Edit Inventory Item</h2>
<?php if(isset($success)) echo "<p style='color:green;'>$success</p>"; ?>
<?php if(isset($error)) echo "<p style='color:red;'>$error</p>"; ?>

<form method="POST">
    <label>Name:</label><br>
    <input type="text" name="name" value="<?php echo $item['name']; ?>" required><br><br>

    <label>Quantity:</label><br>
    <input type="number" name="quantity" value="<?php echo $item['quantity']; ?>" required><br><br>

    <label>Unit Price:</label><br>
    <input type="number" step="0.01" name="unit_price" value="<?php echo $item['unit_price']; ?>" required><br><br>

    <label>Description:</label><br>
    <textarea name="description"><?php echo $item['description']; ?></textarea><br><br>

    <input type="submit" name="submit" value="Update Item">
</form>

<style>
form{max-width:600px;background:#fff;padding:20px;border-radius:8px;box-shadow:0 2px 5px rgba(0,0,0,0.2);}
input,textarea{width:100%;padding:8px;margin:4px 0;box-sizing:border-box;}
input[type="submit"]{width:auto;background:#333;color:#fff;cursor:pointer;}
input[type="submit"]:hover{background:#555;}
@media screen and (max-width:768px){form{width:100%;padding:10px;}}
</style>
