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

// ====== FILTERS ======
$where = "1=1"; // default

$department_id = $_GET['department_id'] ?? '';
$status = $_GET['status'] ?? '';
$from_date = $_GET['from_date'] ?? '';
$to_date = $_GET['to_date'] ?? '';

if($department_id != '') $where .= " AND ps.department_id = ".intval($department_id);
if($status != '') $where .= " AND br.status = '".$conn->real_escape_string($status)."'";
if($from_date != '') $where .= " AND br.created_at >= '".$conn->real_escape_string($from_date)."'";
if($to_date != '') $where .= " AND br.created_at <= '".$conn->real_escape_string($to_date)."'";

// ====== GET REQUISITIONS ======
$sql = "
SELECT 
    br.*, 
    d.name AS department_name, 
    u.username AS requested_by,
    COUNT(bri.id) AS total_items
FROM bar_requisitions br
JOIN bar_requisition_items bri ON br.id = bri.requisition_id
JOIN product_stock ps ON bri.product_id = ps.product_id
JOIN departments d ON ps.department_id = d.id
LEFT JOIN users u ON br.requested_by = u.id
WHERE $where
GROUP BY br.id
ORDER BY br.created_at DESC
";

$requisitions = $conn->query($sql);

// ====== GET DEPARTMENTS FOR FILTER ======
$departments = $conn->query("SELECT * FROM departments ORDER BY name ASC");
?>

<div class="container mt-4">
    <h3><i class="fa fa-list"></i> Bar Requisitions</h3>
    <hr>

    <!-- FILTER FORM -->
    <form method="GET" class="row g-3 mb-4">
        <div class="col-md-3">
            <label>Department</label>
            <select name="department_id" class="form-control">
                <option value="">-- All --</option>
                <?php while($d = $departments->fetch_assoc()): ?>
                    <option value="<?= $d['id'] ?>" <?= ($department_id==$d['id'])?'selected':'' ?>><?= htmlspecialchars($d['name']) ?></option>
                <?php endwhile; ?>
            </select>
        </div>
        <div class="col-md-3">
            <label>Status</label>
            <select name="status" class="form-control">
                <option value="">-- All --</option>
                <option value="Pending" <?= ($status=='Pending')?'selected':'' ?>>Pending</option>
                <option value="Approved" <?= ($status=='Approved')?'selected':'' ?>>Approved</option>
                <option value="Rejected" <?= ($status=='Rejected')?'selected':'' ?>>Rejected</option>
            </select>
        </div>
        <div class="col-md-3">
            <label>From Date</label>
            <input type="date" name="from_date" value="<?= htmlspecialchars($from_date) ?>" class="form-control">
        </div>
        <div class="col-md-3">
            <label>To Date</label>
            <input type="date" name="to_date" value="<?= htmlspecialchars($to_date) ?>" class="form-control">
        </div>
        <div class="col-md-12 mt-2">
            <button type="submit" class="btn btn-primary"><i class="fa fa-filter"></i> Apply Filters</button>
            <a href="requisition_view.php" class="btn btn-secondary"><i class="fa fa-sync"></i> Reset</a>
        </div>
    </form>

    <!-- REQUISITION CARDS -->
    <div class="row">
        <?php if($requisitions->num_rows > 0): ?>
            <?php while($r = $requisitions->fetch_assoc()): ?>
                <div class="col-md-4 mb-3">
                    <div class="card shadow-sm">
                        <div class="card-header bg-primary text-white">
                            <strong>Req #<?= $r['id'] ?></strong> - <?= htmlspecialchars($r['department_name']) ?>
                        </div>
                        <div class="card-body">
                            <p><strong>Requested By:</strong> <?= htmlspecialchars($r['requested_by'] ?? 'N/A') ?></p>
                            <p><strong>Status:</strong> <?= htmlspecialchars($r['status']) ?></p>
                            <p><strong>Date:</strong> <?= date('d-M-Y H:i', strtotime($r['created_at'])) ?></p>
                            <p><strong>Total Items:</strong> <?= $r['total_items'] ?></p>
                        </div>
                        <div class="card-footer text-end">
                            <a href="requisition_details.php?id=<?= $r['id'] ?>" class="btn btn-sm btn-info">
                                <i class="fa fa-eye"></i> View
                            </a>
                        </div>
                    </div>
                </div>
            <?php endwhile; ?>
        <?php else: ?>
            <div class="col-12">
                <div class="alert alert-warning">No requisitions found.</div>
            </div>
        <?php endif; ?>
    </div>
</div>

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