<?php
require 'config.php';

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}

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

$stmt = $pdo->prepare("SELECT * FROM transactions WHERE DATE(created_at) = ? ORDER BY created_at ASC");
$stmt->execute([$date]);
$transactions = $stmt->fetchAll();

$total_transaksi = count($transactions);
$total_omzet = 0;
$total_dinein = $total_takeaway = $total_catering = 0;
$total_diskon = 0;
$total_fee = 0;

foreach ($transactions as $row) {
    $total_omzet  += (int)$row['total'];
    $total_diskon += (int)$row['discount'];
    $total_fee    += (int)$row['takeaway_fee'];
    if ($row['order_type'] == 'dinein') $total_dinein++;
    elseif ($row['order_type'] == 'takeaway') $total_takeaway++;
    elseif ($row['order_type'] == 'catering') $total_catering++;
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <link rel="icon" href="favicon.ico" type="image/x-icon">
    <meta charset="UTF-8">
    <title>Rekap <?= date('d/m/Y', strtotime($date)) ?></title>
    <style>
        @page { size: 56mm auto; margin: 0; }
        body {
            font-family: 'Courier New', monospace;
            margin: 0; padding: 0; width: 56mm; font-size: 11px;
            -webkit-print-color-adjust: exact; print-color-adjust: exact;
        }
        .receipt { word-wrap: break-word; overflow-wrap: break-word;  padding: 8px 8px 20mm 8px; width: 100%; box-sizing: border-box; }
        .text-center { text-align: center; }
        .bold { font-weight: bold; }
        .flex { display: flex; justify-content: space-between; }
        hr { border: none; border-top: 1px dashed #000; margin: 4px 0; }
        table { width: 100%; border-collapse: collapse; }
        table td { padding: 2px 0; font-size: 10px; }
        .t-right { text-align: right; }
        .t-center { text-align: center; }
    </style>
</head>
<body onload="setTimeout(function(){ window.print(); }, 2500);">

<div class="receipt">
    <div class="text-center">
        <h2 class="bold" style="font-size:14px;margin:0;">GOLDENWOK RESTO</h2>
        <p style="font-size:9px;margin:2px 0;">Jl. Ciater, Palapa Tangerang Selatan</p>
        <hr>
        <p class="bold" style="font-size:11px;margin:2px 0;">REKAPAN HARIAN</p>
        <p style="font-size:10px;margin:2px 0;"><?= date('d F Y', strtotime($date)) ?></p>
        <hr>
    </div>

    <!-- RINGKASAN -->
    <div style="font-size:10px;">
        <div class="flex"><span>Total Transaksi</span><span class="bold"><?= $total_transaksi ?></span></div>
        <div class="flex"><span>Dine In</span><span><?= $total_dinein ?></span></div>
        <div class="flex"><span>Take Away</span><span><?= $total_takeaway ?></span></div>
        <div class="flex"><span>Catering</span><span><?= $total_catering ?></span></div>
    </div>

    <hr>

    <!-- DETAIL TRANSAKSI -->
    <table>
        <thead>
            <tr style="border-bottom:1px solid #000;">
                <th style="text-align:left;font-size:9px;">Invoice</th>
                <th style="text-align:right;font-size:9px;">Total</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($transactions as $row): 
                $short = substr($row['invoice'], -8);
            ?>
            <tr>
                <td><?= htmlspecialchars($short) ?> <span style="font-size:8px;"><?= ucfirst($row['order_type']) ?></span></td>
                <td class="t-right bold"><?= number_format($row['total']) ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <hr>

    <div style="font-size:11px;">
        <div class="flex"><span>Omzet Kotor</span><span>Rp <?= number_format($total_omzet + $total_diskon) ?></span></div>
        <?php if ($total_diskon > 0): ?>
        <div class="flex"><span>Diskon</span><span>-Rp <?= number_format($total_diskon) ?></span></div>
        <?php endif; ?>
        <?php if ($total_fee > 0): ?>
        <div class="flex"><span>Fee</span><span>Rp <?= number_format($total_fee) ?></span></div>
        <?php endif; ?>
    </div>

    <hr>

    <div class="flex bold" style="font-size:13px;">
        <span>TOTAL OMZET</span>
        <span>Rp <?= number_format($total_omzet) ?></span>
    </div>

    <hr>
    <div class="text-center" style="font-size:9px;">
        <?= date('d/m/Y H:i') ?> | GoldenWok POS
    </div>

    <!-- SOBEK -->
    <div style="text-align:center;margin-top:8mm;padding-top:6mm;border-top:2px dashed #000;">
        <div style="font-size:8px;color:#999;letter-spacing:2px;">- - SOBEK DISINI - -</div>
        <div style="height:15mm;"></div>
    </div>
</div>

</body>
</html>
