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

$startDate = trim($_GET['start_date'] ?? '');
$endDate = trim($_GET['end_date'] ?? '');
$status = trim($_GET['status'] ?? '');

$allowedStatuses = ['paid', 'packed', 'shipped', 'done'];

$where = ["o.status IN ('paid','packed','shipped','done')"];
$params = [];

if ($status !== '' && in_array($status, $allowedStatuses, true)) {
    $where[] = 'o.status = ?';
    $params[] = $status;
}

if ($startDate !== '' && $endDate !== '') {
    $where[] = 'o.created_at BETWEEN ? AND ?';
    $params[] = date('Y-m-d 00:00:00', strtotime($startDate));
    $params[] = date('Y-m-d 23:59:59', strtotime($endDate));
} elseif ($startDate !== '') {
    $where[] = 'o.created_at >= ?';
    $params[] = date('Y-m-d 00:00:00', strtotime($startDate));
} elseif ($endDate !== '') {
    $where[] = 'o.created_at <= ?';
    $params[] = date('Y-m-d 23:59:59', strtotime($endDate));
}

$whereSql = 'WHERE ' . implode(' AND ', $where);

$stmt = $pdo->prepare("SELECT o.id, o.code, o.total, o.status, o.created_at, u.name AS customer_name
    FROM orders o
    JOIN users u ON u.id = o.user_id
    {$whereSql}
    ORDER BY o.created_at DESC");
$stmt->execute($params);
$orders = $stmt->fetchAll();
?>

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

<div class="card shadow-sm mb-4">
    <div class="card-body">
        <form class="row g-3" method="get">
            <div class="col-md-3">
                <label>Tanggal Mulai</label>
                <input class="form-control" type="date" name="start_date" value="<?= sanitize($startDate); ?>">
            </div>
            <div class="col-md-3">
                <label>Tanggal Akhir</label>
                <input class="form-control" type="date" name="end_date" value="<?= sanitize($endDate); ?>">
            </div>
            <div class="col-md-3">
                <label>Status</label>
                <select class="form-select" name="status">
                    <option value="">Semua</option>
                    <?php foreach ($allowedStatuses as $item): ?>
                        <option value="<?= $item; ?>" <?= $status === $item ? 'selected' : ''; ?>><?= strtoupper($item); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3 d-flex align-items-end">
                <button class="btn w-100" type="submit">Filter</button>
            </div>
        </form>
    </div>
</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>Customer</th>
                    <th>Total</th>
                    <th>Status</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!$orders): ?>
                    <tr>
                        <td colspan="6">Tidak ada transaksi online.</td>
                    </tr>
                <?php endif; ?>
                <?php foreach ($orders as $order): ?>
                    <tr>
                        <td><?= sanitize($order['code']); ?></td>
                        <td><?= sanitize(date('d M Y', strtotime($order['created_at']))); ?></td>
                        <td><?= sanitize($order['customer_name']); ?></td>
                        <td><?= format_currency($order['total']); ?></td>
                        <td><?= strtoupper(sanitize($order['status'])); ?></td>
                        <td>
                            <a class="btn btn-sm" href="<?= base_url('admin/order_manage.php?id=' . (int) $order['id']); ?>">Lihat Invoice</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

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