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

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

// ===== DATE RANGE =====
$start_date = $_POST['start_date'] ?? date('Y-m-d');
$end_date   = $_POST['end_date'] ?? date('Y-m-d');

// ===== FETCH PERISHABLES =====
$perishables_total_costs = 0;
$perishables_total_qty   = 0;
$perishables_total_items = 0;

$query = $conn->prepare("
    SELECT perishable_name, unit, qty_per_person, SUM(total_qty) AS total_qty,
           unit_cost, SUM(total_costs) AS total_costs
    FROM walkin_production
    WHERE item_type='perishable' AND production_date BETWEEN ? AND ?
    GROUP BY perishable_name, unit, qty_per_person, unit_cost
");
$query->bind_param("ss", $start_date, $end_date);
$query->execute();
$result = $query->get_result();
$perishables = $result->fetch_all(MYSQLI_ASSOC);

foreach ($perishables as $row) {
    $perishables_total_costs += $row['total_costs'];
    $perishables_total_qty   += $row['total_qty'];
    $perishables_total_items++;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Perishables Report</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <style>
        body {
            padding: 20px;
            background: #f8f9fa;
        }
        .card {
            padding: 20px;
            text-align: center;
            border-radius: 10px;
            background-color: #ffffff;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .total-row {
            font-weight: bold;
            background-color: #e9ecef;
        }
        .print-btn {
            background-color: #198754;
            color: #fff;
            border: none;
            padding: 8px 16px;
            border-radius: 5px;
        }
        .print-btn:hover {
            background-color: #157347;
        }
        table th, table td {
            vertical-align: middle !important;
        }
    </style>
</head>
<body>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h4>Perishables Report</h4>
    <button class="print-btn" onclick="window.print()"><i class="fa fa-print"></i> Print</button>
</div>

<!-- ===== FILTER FORM ===== -->
<form method="POST" class="row g-3 mb-4">
    <div class="col-md-3">
        <label class="form-label">Start Date</label>
        <input type="date" class="form-control" name="start_date" value="<?= $start_date ?>">
    </div>
    <div class="col-md-3">
        <label class="form-label">End Date</label>
        <input type="date" class="form-control" name="end_date" value="<?= $end_date ?>">
    </div>
    <div class="col-md-2 d-flex align-items-end">
        <button class="btn btn-primary w-100" type="submit"><i class="fa fa-filter"></i> Filter</button>
    </div>
</form>

<!-- ===== SUMMARY CARDS ===== -->
<div class="row mb-4">
    <div class="col-md-4">
        <div class="card">
            <h5>Total Items</h5>
            <p><?= $perishables_total_items ?></p>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card">
            <h5>Total Quantity</h5>
            <p><?= number_format($perishables_total_qty, 2) ?></p>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card">
            <h5>Total Costs (MWK)</h5>
            <p><?= number_format($perishables_total_costs, 2) ?></p>
        </div>
    </div>
</div>

<!-- ===== PERISHABLES TABLE ===== -->
<?php if (!empty($perishables)): ?>
<table class="table table-bordered table-hover bg-white">
    <thead class="table-light">
        <tr>
            <th>#</th>
            <th>Perishable Name</th>
            <th>Unit</th>
            <th>Qty per Person</th>
            <th>Total Qty</th>
            <th>Unit Cost (MWK)</th>
            <th>Total Costs (MWK)</th>
        </tr>
    </thead>
    <tbody>
        <?php $i = 1; foreach ($perishables as $row): ?>
        <tr>
            <td><?= $i ?></td>
            <td><?= htmlspecialchars($row['perishable_name']) ?></td>
            <td><?= htmlspecialchars($row['unit']) ?></td>
            <td><?= $row['qty_per_person'] ?></td>
            <td><?= number_format($row['total_qty'], 2) ?></td>
            <td><?= number_format($row['unit_cost'], 2) ?></td>
            <td><?= number_format($row['total_costs'], 2) ?></td>
        </tr>
        <?php $i++; endforeach; ?>
        <tr class="total-row">
            <td colspan="4">TOTAL</td>
            <td></td>
            <td>-</td>
            <td><?= number_format($perishables_total_costs, 2) ?></td>
        </tr>
    </tbody>
</table>
<?php else: ?>
<p class="text-center text-muted">No perishable productions found for the selected date range.</p>
<?php endif; ?>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>