<?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 hk_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>
body{
    font-family: Arial, sans-serif;
    font-size: 12px;
    background:#f5f5f5;
}

/* PAGE */
.page{
    width:210mm;
    min-height:297mm;
    margin:20px auto;
    background:#fff;
    padding:8mm 10mm 10mm 10mm;
    box-sizing:border-box;
    position:relative;
    box-shadow:0 0 10px rgba(0,0,0,0.1);
}

/* LOGO */
.logo{
    height:110px;
}

/* HEADER */
.header{
    text-align:center;
}

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

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

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

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

.table th{
    background:#0a7f2e;
    color:#fff;
    text-transform:uppercase;
    font-size:11px;
}

.table tbody tr:nth-child(even){
    background:#f9f9f9;
}

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

/* BUTTONS */
.no-print{
    margin:15px auto;
    text-align:center;
}

.btn{
    padding:8px 15px;
    border:none;
    cursor:pointer;
    border-radius:5px;
    font-size:13px;
}

.btn-primary{background:#0a7f2e;color:#fff;}
.btn-dark{background:#333;color:#fff;}
.btn-secondary{background:#777;color:#fff;}

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

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

/* PRINT */
@media print{

    body{
        background:#fff;
    }

    .no-print{
        display:none !important;
    }

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

    @page{
        size:A4;
        margin:10mm;
    }
}
</style>

<!-- FILTER + BUTTONS -->
<div class="no-print">
    <form method="GET" style="text-align:center; margin-bottom:10px;">

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

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

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

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

<!-- REPORT PAGE -->
<div class="page">

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

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

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

    <div class="period">
        Period: <b><?= $from ?></b> to <b><?= $to ?></b>
    </div>
</div>

<hr>

<table class="table">
    <thead>
        <tr>
            <th>ID</th>
            <th>Requested By</th>
            <th>Date</th>
            <th>Items</th>
        </tr>
    </thead>

    <tbody>

    <?php if($data->num_rows == 0): ?>
        <tr>
            <td colspan="4" style="text-align:center;">No records 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 hk_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>
        Store Manager / Accounts
    </div>

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

</div>

</div>