<?php
require_once __DIR__ . '/../includes/auth.php';
require_owner_or_admin();
require_once __DIR__ . '/../includes/header_admin.php';
$pdo = require __DIR__ . '/../config/pdo.php';

$sales = $pdo->query('SELECT s.id, s.code, s.sold_at, s.total, u.name AS cashier_name
    FROM offline_sales s
    JOIN users u ON u.id = s.cashier_id
    ORDER BY s.sold_at DESC')->fetchAll();
?>

<h2 class="fw-bold mb-4">Penjualan Offline</h2>

<div class="d-flex justify-content-end mb-3">
    <a class="btn" href="<?= base_url('admin/sales_offline_add.php'); ?>">Tambah Penjualan</a>
</div>

<div class="card shadow-sm">
    <div class="card-body table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th>Kode</th>
                    <th>Tanggal</th>
                    <th>Kasir</th>
                    <th>Total</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!$sales): ?>
                    <tr>
                        <td colspan="5">Belum ada transaksi offline.</td>
                    </tr>
                <?php endif; ?>
                <?php foreach ($sales as $sale): ?>
                    <tr>
                        <td><?= sanitize($sale['code']); ?></td>
                        <td><?= sanitize(date('d M Y H:i', strtotime($sale['sold_at']))); ?></td>
                        <td><?= sanitize($sale['cashier_name']); ?></td>
                        <td><?= format_currency($sale['total']); ?></td>
                        <td>
                            <a class="btn btn-sm" href="<?= base_url('admin/sales_offline_invoice.php?id=' . (int) $sale['id']); ?>">Nota</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<?php require_once __DIR__ . '/../includes/footer_admin.php'; ?>
