<?php
include 'db.php';

/* ================= FILTERS ================= */
$from   = $_GET['from'] ?? date('Y-m-01');
$to     = $_GET['to'] ?? date('Y-m-d');
$status = $_GET['status'] ?? '';

$where = "WHERE DATE(br.created_at) BETWEEN '$from' AND '$to'";

if($status != ''){
    $where .= " AND br.status='$status'";
}

/* ================= DATA ================= */
$sql = "
    SELECT br.*, u.username
    FROM bar_requisitions br
    LEFT JOIN users u ON br.requested_by = u.id
    $where
    ORDER BY br.created_at DESC
";

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

if(!$data){
    die("<div style='color:red;padding:10px'>
        SQL ERROR: ".$conn->error."
    </div>");
}

$logo = "assets/logo.png";

/* ================= SUMMARY ================= */
$total_requisitions = 0;
$total_items = 0;
?>
<!DOCTYPE html>
<html>
<head>
<title>Bar Requisitions Report</title>

<style>
body{
    font-family: Arial;
    font-size: 12px;
    background:#fff;
}

.page{
    width:210mm;
    min-height:297mm;
    margin:auto;
    padding:15mm;
    position:relative;
    box-sizing:border-box;
}

/* ================= WATERMARK ================= */
.watermark{
    position:absolute;
    top:50%;
    left:50%;
    transform:translate(-50%,-50%) rotate(-30deg);
    font-size:90px;
    font-weight:bold;
    color:#0a7f2e;
    opacity:0.06;
    letter-spacing:5px;
    pointer-events:none;
}

/* ================= HEADER ================= */
.header{
    text-align:center;
    margin-bottom:10px;
}

.logo{
    width:80px;
}

.title{
    font-size:22px;
    font-weight:bold;
    color:#0a7f2e;
    margin-top:5px;
}

.period{
    margin-top:5px;
    font-size:13px;
}

/* ================= TABLE ================= */
table{
    width:100%;
    border-collapse:collapse;
    margin-top:15px;
}

th, td{
    border:1px solid #000;
    padding:6px;
    font-size:11px;
    vertical-align:top;
}

th{
    background:#0a7f2e;
    color:#fff;
}

/* ================= SUMMARY BOX ================= */
.summary{
    margin-top:10px;
    padding:8px;
    border:1px solid #0a7f2e;
    background:#f6fff7;
}

/* ================= SIGNATURES ================= */
.signatures{
    margin-top:60px;
    display:flex;
    justify-content:space-between;
    text-align:center;
}

.sign{
    width:30%;
}

.line{
    margin-top:40px;
    border-top:1px solid #000;
}

/* ================= PRINT ================= */
.no-print{
    text-align:center;
    margin-bottom:10px;
}

@media print{
    .no-print{ display:none; }
    @page{ size:A4; margin:10mm; }
}
</style>
</head>

<body>

<!-- PRINT BUTTON -->
<div class="no-print">
    <button onclick="window.print()" style="padding:10px 18px;">
        🖨️ Print Report
    </button>
</div>

<div class="page">

<!-- WATERMARK -->
<div class="watermark">BAR SYSTEM</div>

<!-- HEADER -->
<div class="header">
    <img src="<?= $logo ?>" class="logo">

    <div class="title">BAR REQUISITIONS REPORT</div>

    <div class="period">
        Period:
        <b><?= date('d-m-Y', strtotime($from)) ?></b>
        -
        <b><?= date('d-m-Y', strtotime($to)) ?></b>

        <?php if($status): ?>
            | Status: <b><?= $status ?></b>
        <?php endif; ?>
    </div>
</div>

<hr>

<!-- ================= TABLE ================= -->
<table>
<thead>
<tr>
    <th>ID</th>
    <th>Requested By</th>
    <th>Date</th>
    <th>Status</th>
    <th>Items</th>
    <th>Total Qty</th>
</tr>
</thead>

<tbody>

<?php if($data->num_rows > 0): ?>

    <?php while($r = $data->fetch_assoc()): ?>

        <?php
        $total_requisitions++;

        $items = $conn->query("
            SELECT bri.quantity, p.name
            FROM bar_requisition_items bri
            LEFT JOIN products p ON bri.product_id = p.id
            WHERE bri.requisition_id = {$r['id']}
        ");

        $text = "";
        $total = 0;

        while($it = $items->fetch_assoc()){
            $text .= $it['name']." (".$it['quantity'].")<br>";
            $total += $it['quantity'];
        }

        $total_items += $total;
        ?>

        <tr>
            <td>#<?= $r['id'] ?></td>
            <td><?= htmlspecialchars($r['username']) ?></td>
            <td><?= date('d-m-Y H:i', strtotime($r['created_at'])) ?></td>
            <td><?= $r['status'] ?></td>
            <td><?= $text ?></td>
            <td><?= $total ?></td>
        </tr>

    <?php endwhile; ?>

<?php else: ?>

<tr>
    <td colspan="6" style="text-align:center;">
        No records found
    </td>
</tr>

<?php endif; ?>

</tbody>
</table>

<!-- ================= SUMMARY ================= -->
<div class="summary">
    <strong>Summary:</strong><br>
    Total Requisitions: <?= $total_requisitions ?><br>
    Total Items Issued: <?= $total_items ?>
</div>

<!-- ================= SIGNATURES ================= -->
<div class="signatures">

    <div class="sign">
        Requested By
        <div class="line"></div>
        Name & Signature
    </div>

    <div class="sign">
        Store Keeper
        <div class="line"></div>
        Name & Signature
    </div>

    <div class="sign">
        Approved By
        <div class="line"></div>
        Name & Signature
    </div>

</div>

</div>

</body>
</html>