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

$username = $_SESSION['username'] ?? 'Guest';

// Get filters
$start_date = $_GET['start_date'] ?? '';
$end_date   = $_GET['end_date'] ?? '';
$status     = $_GET['status'] ?? '';

// Build query with filters
$where = [];
if ($start_date) {
    $where[] = "created_at >= '".date('Y-m-d 00:00:00', strtotime($start_date))."'";
}
if ($end_date) {
    $where[] = "created_at <= '".date('Y-m-d 23:59:59', strtotime($end_date))."'";
}
if ($status) {
    $where[] = "status = '".$conn->real_escape_string($status)."'";
}

$where_sql = '';
if(count($where) > 0){
    $where_sql = 'WHERE '.implode(' AND ', $where);
}

// Fetch requisitions
$requisitions = $conn->query("
    SELECT r.*, u.username AS requested_by_name
    FROM low_stock_requisitions r
    LEFT JOIN users u ON r.requested_by = u.id
    $where_sql
    ORDER BY r.created_at DESC
");
?>

<div class="container mt-4">
    <h3 class="text-center mb-3">📋 Low Stock Requisitions Report</h3>

    <!-- FILTER FORM -->
    <form method="GET" class="row g-3 mb-4">
        <div class="col-md-3">
            <label>Start Date</label>
            <input type="date" name="start_date" value="<?= htmlspecialchars($start_date) ?>" class="form-control">
        </div>
        <div class="col-md-3">
            <label>End Date</label>
            <input type="date" name="end_date" value="<?= htmlspecialchars($end_date) ?>" class="form-control">
        </div>
        <div class="col-md-3">
            <label>Status</label>
            <select name="status" class="form-select">
                <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 d-flex align-items-end">
            <button type="submit" class="btn btn-primary w-100">Filter</button>
        </div>
    </form>

    <!-- REPORT TABLE -->
    <table class="table table-bordered table-striped">
        <thead class="bg-dark text-white">
            <tr>
                <th>#</th>
                <th>Requisition ID</th>
                <th>Requested By</th>
                <th>Date</th>
                <th>Status</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php $i=1; while($r = $requisitions->fetch_assoc()): ?>
            <tr>
                <td><?= $i++ ?></td>
                <td>#<?= $r['id'] ?></td>
                <td><?= htmlspecialchars($r['requested_by_name']) ?></td>
                <td><?= date('d M Y H:i', strtotime($r['created_at'])) ?></td>
                <td>
                    <?php
                        $color = match($r['status']){
                            'Approved'=>'green',
                            'Rejected'=>'red',
                            default=>'orange'
                        };
                    ?>
                    <span style="color:<?= $color ?>; font-weight:bold;"><?= $r['status'] ?></span>
                </td>
                <td>
                    <a href="print_r_single.php?id=<?= $r['id'] ?>" class="btn btn-sm btn-success" target="_blank">
                        <i class="fa fa-print"></i> Print
                    </a>
                </td>
            </tr>
            <?php endwhile; ?>
            <?php if($i==1): ?>
            <tr>
                <td colspan="6" class="text-center">No requisitions found</td>
            </tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

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