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

// 🔥 show errors (remove in production)
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
error_reporting(E_ALL);
ini_set('display_errors', 1);

if(isset($_POST['save'])){

    $name  = $conn->real_escape_string($_POST['meal_name'] ?? '');
        $section  = $conn->real_escape_string($_POST['section'] ?? '');
    $cat   = $conn->real_escape_string($_POST['category'] ?? '');
    $price = floatval($_POST['selling_price'] ?? 0);

    if($name == ''){
        echo "<div class='alert alert-danger'>Meal name is required</div>";
    } else {

        $conn->query("
            INSERT INTO meals(meal_name,section, category, selling_price)
            VALUES('$name','$section','$cat','$price')
        ");

        echo "<script>
                alert('Meal saved successfully');
                window.location='meal_parameters.php';
              </script>";
        exit;
    }
}
?>

<div class="container mt-4">
      <!-- ================= WELCOME MESSAGE ================= -->
  
<h4>➕ Add Meal</h4>

<form method="POST">

    <div class="mb-2">
        <label>Meal Name</label>
        <input type="text" name="meal_name" class="form-control" required>
    </div>

  <div class="mb-2">
    <label>Category</label>
    <select name="category" class="form-control">
        <option value="breakfast">Breakfast</option>
        <option value="lunch">Lunch</option>
        <option value="supper">Supper</option>
        <option value="snack">Snack</option>
        <option value="adhoc">Adhoc</option>
    </select>
</div>

<div class="mb-2">
    <label>Section</label>
    <select name="section" class="form-control">
        <option value="Conference">Conference</option>
        <option value="WalkIn">WalkIn</option>
    </select>
</div>


    <div class="mb-2">
       
        <input type="hidden" step="0.01" name="selling_price" class="form-control" value="0">
    </div>

    <!-- ✅ IMPORTANT FIX -->
    <button type="submit" name="save" class="btn btn-success">
        Save Meal
    </button>

</form>
</div>

<?php include 'includes/footer.php'; ?>
