<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';

if(isset($_POST['save'])){
    $name = trim($_POST['name']);
    $unit = trim($_POST['unit']);
    $qty  = floatval($_POST['quantity']);

    $stmt = $conn->prepare("
        INSERT INTO perishables (name, unit, quantity)
        VALUES (?,?,?)
    ");
    $stmt->bind_param("ssd",$name,$unit,$qty);
    $stmt->execute();

    echo "<div class='alert alert-success'>Item added</div>";
}
?>

<div class="container mt-4">
     <button class="btn btn-secondary btn-custom mb-3" onclick="history.back()">
     <i class="fa fa-arrow-left"></i> Back
 </button>
    <h4>Add Perishable Item</h4>
    <hr>

    <form method="POST" class="row g-2">
        <div class="col-md-4">
            <label>Name</label>
            <input type="text" name="name" class="form-control" required>
        </div>

        <div class="col-md-3">
            <label>Unit</label>
            <input type="text" name="unit" class="form-control" placeholder="Kg, Each">
        </div>

        <div class="col-md-3">
            <label>Opening Stock</label>
            <input type="number" step="0.01" name="quantity" class="form-control" value="0">
        </div>

        <div class="col-md-2 d-flex align-items-end">
            <button class="btn btn-success w-100" name="save">
                <i class="fa fa-save"></i> Save
            </button>
        </div>
    </form>
</div>