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

$message = '';
$message_type = '';

/* ================= DELETE MEAL ================= */
if(isset($_GET['delete'])){
    $delete_id = intval($_GET['delete']);

    $stmt = $conn->prepare("DELETE FROM meals WHERE id=?");
    $stmt->bind_param("i", $delete_id);

    if($stmt->execute()){
        $message = "Meal deleted successfully";
        $message_type = "success";
    }else{
        $message = "Error deleting meal";
        $message_type = "danger";
    }
}

/* ================= UPDATE MEAL ================= */
if(isset($_POST['update_meal'])){
    $id        = intval($_POST['id']);
    $meal_name = trim($_POST['meal_name']);
    $category  = trim($_POST['category']);
    $section   = trim($_POST['section']);

    if($meal_name=='' || $category=='' || $section==''){
        $message = "All fields are required";
        $message_type = "danger";
    }else{

        $stmt = $conn->prepare("
            UPDATE meals
            SET meal_name=?, category=?, section=?
            WHERE id=?
        ");
        $stmt->bind_param("sssi", $meal_name, $category, $section, $id);

        if($stmt->execute()){
            $message = "Meal updated successfully";
            $message_type = "success";
        }else{
            $message = "Error updating meal";
            $message_type = "danger";
        }
    }
}

/* ================= SAVE MEAL ================= */
if(isset($_POST['save_meal'])){
    $meal_name = trim($_POST['meal_name']);
    $category  = trim($_POST['category']);
    $section   = trim($_POST['section']);

    if($meal_name=='' || $category=='' || $section==''){
        $message = "All fields are required";
        $message_type = "danger";
    }else{

        $stmt = $conn->prepare("
            INSERT INTO meals (meal_name, category, section)
            VALUES (?,?,?)
        ");
        $stmt->bind_param("sss", $meal_name, $category, $section);

        if($stmt->execute()){
            $message = "Meal added successfully";
            $message_type = "success";
        }else{
            $message = "Error adding meal";
            $message_type = "danger";
        }
    }
}
?>

<div class="container mt-4">

    <div class="d-flex gap-2 mb-3">
        <button class="btn btn-secondary" onclick="history.back()">
            <i class="fa fa-arrow-left"></i> Back
        </button>
    </div>

    <h4><i class="fa fa-utensils"></i> Add New Meal</h4>
    <hr>

    <?php if($message): ?>
        <div class="alert alert-<?= $message_type ?>">
            <?= htmlspecialchars($message) ?>
        </div>
    <?php endif; ?>

    <!-- ================= FORM ================= -->
    <div class="card shadow-sm">
        <div class="card-body">

            <form method="POST">
                <div class="row g-3">

                    <div class="col-md-4">
                        <label class="form-label fw-bold">Meal Name</label>
                        <input type="text" name="meal_name"
                               class="form-control"
                               required>
                    </div>

                    <div class="col-md-4">
                        <label class="form-label fw-bold">Category</label>
                        <select name="category" class="form-control" required>
                            <option value="">-- Select Category --</option>
                            <option>Breakfast</option>
                            <option>Lunch</option>
                            <option>Dinner</option>
                            <option>Snack</option>
                            <option>Beverage</option>
                        </select>
                    </div>

                    <div class="col-md-4">
                        <label class="form-label fw-bold">Section</label>
                        <select name="section" class="form-control" required>
                            <option value="">-- Select Section --</option>
                            <option>Kitchen</option>
                            <option>Restaurant</option>
                            <option>Conference</option>
                            <option>Bar</option>
                        </select>
                    </div>

                    <div class="col-md-3">
                        <button type="submit"
                                name="save_meal"
                                class="btn btn-success w-100">
                            <i class="fa fa-save"></i> Save Meal
                        </button>
                    </div>

                </div>
            </form>

        </div>
    </div>

    <!-- ================= MEAL LIST ================= -->
    <div class="card mt-4 shadow-sm">
        <div class="card-header bg-dark text-white">
            <i class="fa fa-list"></i> Existing Meals
        </div>
        <div class="card-body">

            <table class="table table-bordered table-striped table-sm">
                <thead class="table-dark">
                    <tr>
                        <th>#</th>
                        <th>Meal Name</th>
                        <th>Category</th>
                        <th>Section</th>
                        <th width="160">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $res = $conn->query("SELECT * FROM meals ORDER BY id DESC");
                    $i=1;
                    while($row=$res->fetch_assoc()):
                    ?>
                    <tr>
                        <td><?= $i++ ?></td>
                        <td><?= htmlspecialchars($row['meal_name']) ?></td>
                        <td><?= htmlspecialchars($row['category']) ?></td>
                        <td><?= htmlspecialchars($row['section']) ?></td>
                        <td>

                            <!-- EDIT BUTTON -->
                            <button class="btn btn-warning btn-sm"
                                onclick="editMeal(
                                    <?= $row['id'] ?>,
                                    '<?= htmlspecialchars($row['meal_name'], ENT_QUOTES) ?>',
                                    '<?= htmlspecialchars($row['category'], ENT_QUOTES) ?>',
                                    '<?= htmlspecialchars($row['section'], ENT_QUOTES) ?>'
                                )">
                                <i class="fa fa-edit"></i>
                            </button>

                            <!-- DELETE BUTTON -->
                            <a href="?delete=<?= $row['id'] ?>"
                               class="btn btn-danger btn-sm"
                               onclick="return confirm('Delete this meal?')">
                                <i class="fa fa-trash"></i>
                            </a>

                        </td>
                    </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>

        </div>
    </div>
</div>

<!-- ================= EDIT MODAL ================= -->
<div class="modal fade" id="editModal">
    <div class="modal-dialog">
        <div class="modal-content">

            <form method="POST">
                <div class="modal-header bg-dark text-white">
                    <h5 class="modal-title">Edit Meal</h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                </div>

                <div class="modal-body">

                    <input type="hidden" name="id" id="edit_id">

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

                    <div class="mb-2">
                        <label class="fw-bold">Category</label>
                        <input type="text" name="category" id="edit_category" class="form-control" required>
                    </div>

                    <div class="mb-2">
                        <label class="fw-bold">Section</label>
                        <input type="text" name="section" id="edit_section" class="form-control" required>
                    </div>

                </div>

                <div class="modal-footer">
                    <button type="submit" name="update_meal" class="btn btn-success">
                        <i class="fa fa-save"></i> Update
                    </button>
                </div>
            </form>

        </div>
    </div>
</div>

<script>
function editMeal(id,name,category,section){
    document.getElementById('edit_id').value=id;
    document.getElementById('edit_meal_name').value=name;
    document.getElementById('edit_category').value=category;
    document.getElementById('edit_section').value=section;

    new bootstrap.Modal(document.getElementById('editModal')).show();
}
</script>

<style>
.card{border-radius:12px;}
.btn-success{background:#198754;border:none;}
</style>
