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

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

$sql = "
    SELECT r.*, u.username
    FROM r_requisitions r
    LEFT JOIN users u ON r.requested_by = u.id
    WHERE r.status='Approved'
    AND DATE(r.created_at) BETWEEN '$from' AND '$to'
    ORDER BY r.created_at DESC
";

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

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

$logo = "assets/logo.png";
?>

<style>

body{
    font-family: Arial, sans-serif;
    font-size: 12px;
    background: #fff;
    margin: 0;
}

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

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

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

.logo{
    height: 80px;
    margin-bottom: 5px;
}

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

.period{
    font-size: 13px;
    margin-bottom: 10px;
}

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

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

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

/* ITEMS STYLE */
.items{
    line-height: 1.5;
}

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

.sign{
    width: 40%;
    text-align: center;
    font-size: 13px;
}

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

@media print{
    .no-print{ display:none; }
    @page{ size: A4; margin: 10mm; }
}

</style>

<!-- PRINT BUTTON -->
<div class="no-print">
    <button onclick="window.print()" style="
        padding:10px 20px;
        background:#000;
        color:#fff;
        border:none;
        cursor:pointer;
        border-radius:4px;
    ">
        🖨 Print Report
    </button>
</div>

<div class="page">

<div class="watermark">RESTAURANT</div>

<!-- HEADER -->
<div class="header">

    <img src="<?= $logo ?>" class="logo">

    <div class="title">RESTAURANT REQUISITION REPORT</div>

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

</div>

<hr>

<!-- TABLE -->
<table class="table">

<thead>
<tr>
    <th width="8%">ID</th>
    <th width="20%">Requested By</th>
    <th width="20%">Date</th>
    <th>Items</th>
</tr>
</thead>

<tbody>

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

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

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

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

                if($items && $items->num_rows > 0){
                    while($it = $items->fetch_assoc()){
                        echo "• ".htmlspecialchars($it['name'])." (".$it['quantity'].")<br>";
                    }
                } else {
                    echo "<i>No items</i>";
                }
                ?>

            </td>
        </tr>

    <?php endwhile; ?>

<?php else: ?>

<tr>
    <td colspan="4" style="text-align:center;padding:20px;">
        No records found for selected period
    </td>
</tr>

<?php endif; ?>

</tbody>

</table>

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

    <div class="sign">
        _______________________<br>
        Requested By
    </div>

    <div class="sign">
        _______________________<br>
        Approved By
    </div>

</div>

</div>