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

$username = $_SESSION['username'] ?? 'Guest';
$today = date('Y-m-d');

// ================= FILTERS =================
$from_date = $_GET['from'] ?? $today;
$to_date   = $_GET['to'] ?? $today;
$meal_type = $_GET['meal_type'] ?? '';
$search    = $_GET['search'] ?? '';

// ================= BUILD WHERE =================
$where = [];
$where[] = "mp.created_at >= '".$conn->real_escape_string($from_date)." 00:00:00'";
$where[] = "mp.created_at <= '".$conn->real_escape_string($to_date)." 23:59:59'";
if($meal_type){
    $where[] = "mp.meal_type = '".$conn->real_escape_string($meal_type)."'";
}
if($search){
    $s = $conn->real_escape_string($search);
    $where[] = "m.meal_name LIKE '%$s%'";
}
$where_sql = "WHERE ".implode(" AND ", $where);

// ================= FETCH PRODUCTION =================
$query = "
SELECT mp.*, m.meal_name
FROM meal_production mp
LEFT JOIN meals m ON m.id = mp.meal_id
$where_sql
ORDER BY mp.created_at DESC
";

$result = $conn->query($query);

// ================= SUMMARY =================
$summary = $conn->query("
SELECT 
    COUNT(*) AS total_productions,
    SUM(mp.number_of_people) AS total_people
FROM meal_production mp
LEFT JOIN meals m ON m.id = mp.meal_id
$where_sql
")->fetch_assoc();

$total_productions = $summary['total_productions'] ?? 0;
$total_people      = $summary['total_people'] ?? 0;
?>

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

    <h4><i class="fa fa-utensils text-warning"></i> Kitchen Production Report</h4>
    <hr>

    <!-- FILTERS -->
    <form method="GET" class="row g-2 mb-3">
        <div class="col-md-2"><input type="date" name="from" class="form-control" value="<?= htmlspecialchars($from_date) ?>"></div>
        <div class="col-md-2"><input type="date" name="to" class="form-control" value="<?= htmlspecialchars($to_date) ?>"></div>
        <div class="col-md-2">
            <select name="meal_type" class="form-control">
                <option value="">All Types</option>
                <option <?= $meal_type=='Walk-in'?'selected':'' ?>>Walk-in</option>
                <option <?= $meal_type=='Conference'?'selected':'' ?>>Conference</option>
            </select>
        </div>
        <div class="col-md-4"><input type="text" name="search" class="form-control" placeholder="Meal Name" value="<?= htmlspecialchars($search) ?>"></div>
        <div class="col-md-2 d-flex gap-1">
            <button class="btn btn-primary w-100"><i class="fa fa-filter"></i> Filter</button>
            <a href="kitchen_production_report.php" class="btn btn-secondary w-100">Reset</a>
        </div>
    </form>

    <!-- EXPORT BUTTONS -->
    <div class="mb-3 d-flex gap-2">
        <a href="export_kitchen_report_excel.php?from=<?= $from_date ?>&to=<?= $to_date ?>&meal_type=<?= urlencode($meal_type) ?>&search=<?= urlencode($search) ?>" class="btn btn-success">
            <i class="fa fa-file-excel"></i> Export Excel
        </a>
        <a href="export_kitchen_report_pdf.php?from=<?= $from_date ?>&to=<?= $to_date ?>&meal_type=<?= urlencode($meal_type) ?>&search=<?= urlencode($search) ?>" class="btn btn-danger">
            <i class="fa fa-file-pdf"></i> Export PDF
        </a>
    </div>

    <!-- SUMMARY CARDS -->
    <div class="row mb-3">
        <div class="col-md-3"><div class="card text-center p-2"><h6>Total Productions</h6><h4><?= $total_productions ?></h4></div></div>
        <div class="col-md-3"><div class="card text-center p-2"><h6>Total People Served</h6><h4><?= $total_people ?></h4></div></div>
    </div>

    <!-- TABLE -->
    <div class="table-responsive">
        <table class="table table-bordered table-striped table-sm">
            <thead class="table-dark">
                <tr>
                    <th>ID</th>
                    <th>Meal</th>
                    <th>Type</th>
                    <th>Number of People</th>
                    <th>Produced By</th>
                    <th>Produced At</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                <?php if($result && $result->num_rows): ?>
                    <?php while($row = $result->fetch_assoc()): ?>
                        <tr>
                            <td><?= $row['id'] ?></td>
                            <td><?= $row['meal_name'] ?></td>
                            <td><?= $row['meal_type'] ?></td>
                            <td><?= $row['number_of_people'] ?></td>
                            <td><?= $row['produced_by'] ?></td>
                            <td><?= $row['created_at'] ?></td>
                            <td><?= $row['status'] ?></td>
                        </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr><td colspan="7" class="text-center text-danger">No production found</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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