<?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(fr.created_at) BETWEEN '$from' AND '$to'";

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

/* ================= DATA ================= */
$sql = "
    SELECT fr.*, u.username
    FROM front_desk_requisitions fr
    LEFT JOIN users u ON fr.requested_by = u.id
    $where
    ORDER BY fr.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";
?>

<!DOCTYPE html>
<html>
<head>
    <title>Front Desk Requisitions Report</title>

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

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

        .header{
            text-align: center;
        }

        .logo{
            width: 80px;
        }

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

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

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

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

        .no-print{
            text-align:center;
            margin-bottom:10px;
        }

        .signature{
            margin-top: 60px;
            display:flex;
            justify-content:space-between;
        }

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

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

<body>

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

<div class="page">

<div class="watermark">FRONT DESK</div>

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

    <div class="title">FRONT DESK REQUISITION 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
        $items = $conn->query("
            SELECT fdi.quantity, p.name
            FROM front_desk_requisition_items fdi
            LEFT JOIN products p ON fdi.product_id = p.id
            WHERE fdi.requisition_id = {$r['id']}
        ");

        $text = "";
        $total = 0;

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

        <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>

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

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

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

</div>

</div>

</body>
</html>