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

// Get production ID
$prod_id = intval($_GET['id'] ?? 0);
if($prod_id <= 0){
    die("Invalid production ID");
}

// Fetch production details
$prod = $conn->query("
    SELECT mp.*, m.meal_name, m.category, m.section
    FROM meal_production mp
    JOIN meals m ON mp.meal_id = m.id
    WHERE mp.id = $prod_id
")->fetch_assoc();

if(!$prod){
    die("Production not found");
}

// Prevent editing if already served
if($prod['status'] != 'Pending'){
    die("<div class='alert alert-info'>This production has already been served and cannot be edited.</div>");
}

// Handle form submission
if(isset($_POST['update_production'])){
    $people_count = intval($_POST['people_count']);
    $produced_by  = $conn->real_escape_string($_POST['produced_by']);

    if($people_count <= 0 || empty($produced_by)){
        $error = "Please provide valid values for all fields.";
    } else {
        // Update production table
        $conn->query("
            UPDATE meal_production
            SET people_count=$people_count, produced_by='$produced_by'
            WHERE id=$prod_id
        ");

        $success = "Production updated successfully!";
        // Refresh production data
        $prod['people_count'] = $people_count;
        $prod['produced_by'] = $produced_by;
    }
}
?>

<div class="container mt-4">
      <div class="welcome-msg">
        <i class="fa fa-user-circle"></i> Welcome, <strong><?= htmlspecialchars($_SESSION['username'] ?? 'Guest') ?></strong>!
    </div>
<h4>✏ Edit Production</h4>
<hr>

<?php if(isset($error)): ?>
<div class="alert alert-danger"><?= $error ?></div>
<?php endif; ?>
<?php if(isset($success)): ?>
<div class="alert alert-success"><?= $success ?></div>
<?php endif; ?>

<form method="POST" class="row g-3">
    <div class="col-md-6">
        <label>Meal Name</label>
        <input type="text" class="form-control" value="<?= htmlspecialchars($prod['meal_name']) ?>" disabled>
    </div>
    <div class="col-md-3">
        <label>Category</label>
        <input type="text" class="form-control" value="<?= htmlspecialchars($prod['category']) ?>" disabled>
    </div>
    <div class="col-md-3">
        <label>Section</label>
        <input type="text" class="form-control" value="<?= htmlspecialchars($prod['section']) ?>" disabled>
    </div>

    <div class="col-md-3">
        <label>People Count</label>
        <input type="number" name="people_count" class="form-control" min="1" value="<?= $prod['people_count'] ?>" required>
    </div>
    <div class="col-md-3">
        <label>Produced By</label>
        <input type="text" name="produced_by" class="form-control" value="<?= htmlspecialchars($prod['produced_by']) ?>" required>
    </div>

    <div class="col-md-12">
        <button type="submit" name="update_production" class="btn btn-success">💾 Update Production</button>
        <a href="meal_production_list.php" class="btn btn-secondary">⬅ Back</a>
    </div>
</form>

</div>

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