<?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 k_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);

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

<style>

/* PAGE SETUP */
body{
    font-family: Arial;
    font-size: 12px;
    background:#f5f5f5;
}

.page{
    width:210mm;
    min-height:297mm;
    margin:10px auto;
    background:#fff;
    padding:12mm;
    box-sizing:border-box;
    position:relative;
}

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

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

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

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

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

/* FILTER AREA */
.no-print{
    text-align:center;
    margin:15px 0;
}

input[type="date"]{
    padding:6px;
}

/* 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;
}

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

.sign{
    width:40%;
    text-align:center;
}

/* PRINT RULES */
@media print{
    .no-print{
        display:none !important;
    }

    body{
        background:#fff;
    }

    .page{
        margin:0;
        box-shadow:none;
    }

    @page{
        size:A4;
        margin:10mm;
    }
}

</style>

<!-- FILTER + PRINT CONTROLS -->
<div class="no-print">

    <form method="GET">

        From:
        <input type="date" name="from" value="<?= $from ?>">

        To:
        <input type="date" name="to" value="<?= $to ?>">

        <button type="submit" class="btn btn-primary">Filter</button>

        <button type="button" onclick="window.print()" class="btn btn-dark">
            Print
        </button>

    </form>

</div>

<div class="page">

<div class="watermark">KITCHEN REPORT</div>

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

    <div class="title">APPROVED KITCHEN REQUISITIONS</div>

    <div class="period">
        Report 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="25%">Requested By</th>
            <th width="20%">Date</th>
            <th>Items</th>
        </tr>
    </thead>

    <tbody>

    <?php if($data->num_rows == 0): ?>
        <tr>
            <td colspan="4" style="text-align:center;">No approved requisitions found</td>
        </tr>
    <?php endif; ?>

    <?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>
                <?php
                $items = $conn->query("
                    SELECT bri.*, p.name
                    FROM k_requisition_items bri
                    LEFT JOIN products p ON bri.product_id=p.id
                    WHERE bri.requisition_id={$r['id']}
                ");

                while($it = $items->fetch_assoc()){
                    echo "• ".$it['name']." (".$it['quantity'].")<br>";
                }
                ?>
            </td>
        </tr>
    <?php endwhile; ?>

    </tbody>
</table>

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

    <div class="sign">
        _______________________<br>
        Kitchen Manager
    </div>

    <div class="sign">
        _______________________<br>
        Store Supervisor
    </div>

</div>

</div>