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

// ===================== FILTERS =====================
$filters = [];

if(!empty($_GET['meal_id'])) {
    $filters[] = "kr.meal_production_id IN (SELECT id FROM meal_production WHERE meal_id=".intval($_GET['meal_id']).")";
}

if(!empty($_GET['product_id'])) {
    $filters[] = "kr.product_id=".intval($_GET['product_id']);
}

if(!empty($_GET['requested_by'])) {
    $filters[] = "kr.requested_by LIKE '%".$conn->real_escape_string($_GET['requested_by'])."%'";
}

if(!empty($_GET['date_from']) && !empty($_GET['date_to'])) {
    $filters[] = "DATE(kr.requested_at) BETWEEN '".$conn->real_escape_string($_GET['date_from'])."' 
                   AND '".$conn->real_escape_string($_GET['date_to'])."'";
}

$filter_sql = count($filters) ? "WHERE ".implode(" AND ", $filters) : "";

// ===================== FETCH DATA =====================
$requisitions = $conn->query("
    SELECT kr.*, p.name AS product_name, p.unit AS product_unit, m.meal_name, mp.people_count
    FROM kitchen_requisitions kr
    JOIN products p ON kr.product_id = p.id
    JOIN meal_production mp ON kr.meal_production_id = mp.id
    JOIN meals m ON mp.meal_id = m.id
    $filter_sql
    ORDER BY kr.requested_at DESC
    LIMIT 500
");

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Kitchen Requisitions Report</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.2/css/bootstrap.min.css">
    <style>
        @media print {
            .no-print { display: none; }
        }
        body { font-size: 12px; }
        table th, table td { vertical-align: middle !important; }
        h2 { text-align: center; margin-bottom: 20px; }
    </style>
</head>
<body>

<div class="container mt-3 mb-3">
     <!-- ================= WELCOME MESSAGE ================= -->
    <div class="welcome-msg">
        <i class="fa fa-user-circle"></i> Welcome, <strong><?= htmlspecialchars($username) ?></strong>!
    </div>
    <h2>📋 Kitchen Requisitions Report</h2>
    <div class="mb-3 no-print">
        <button onclick="window.print()" class="btn btn-primary">
            <i class="fa fa-print"></i> Print / Save as PDF
        </button>
        <a href="kitchen_requisitions.php" class="btn btn-secondary">
            <i class="fa fa-arrow-left"></i> Back
        </a>
    </div>

    <table class="table table-bordered table-striped">
        <thead class="bg-dark text-white">
            <tr>
                <th>#</th>
                <th>Meal</th>
                <th>People</th>
                <th>Product</th>
                <th>Quantity</th>
                <th>Unit</th>
                <th>Requested By</th>
                <th>Date</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            <?php $i=1; while($r = $requisitions->fetch_assoc()): ?>
            <tr>
                <td><?= $i++ ?></td>
                <td><?= htmlspecialchars($r['meal_name']) ?></td>
                <td><?= intval($r['people_count']) ?></td>
                <td><?= htmlspecialchars($r['product_name']) ?></td>
                <td><?= number_format($r['quantity'],2) ?></td>
                <td><?= htmlspecialchars($r['product_unit']) ?></td>
                <td><?= htmlspecialchars($r['requested_by']) ?></td>
                <td><?= date("d-M-Y H:i", strtotime($r['requested_at'])) ?></td>
                <td>
                    <?php if($r['status']=='Approved'): ?>
                        <span class="badge bg-success"><?= $r['status'] ?></span>
                    <?php else: ?>
                        <span class="badge bg-warning"><?= $r['status'] ?></span>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endwhile; ?>
        </tbody>
    </table>

    <div class="mt-3 text-center">
        <small>Generated on <?= date("d-M-Y H:i") ?> by <?= $_SESSION['username'] ?? 'System' ?></small>
    </div>
</div>

</body>
</html>
