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

$stmt = $pdo->prepare('SELECT id, code, total, status, created_at FROM orders WHERE user_id = ? ORDER BY created_at DESC');
$stmt->execute([current_user()['id']]);
$orders = $stmt->fetchAll();
?>

<div class="row align-items-center mb-4">
    <div class="col">
        <h2 class="fw-bold mb-0">Pesanan Saya</h2>
        <p class="text-muted small mb-0">Riwayat belanja Anda di Al-Qanuni Group</p>
    </div>
</div>

<?php if (!$orders): ?>
    <div class="text-center py-5">
        <div class="mb-3 text-muted opacity-50"><i class="bi bi-box-open" style="font-size: 4rem;"></i></div>
        <h5 class="fw-bold text-muted">Belum ada pesanan</h5>
        <a href="<?= base_url('public/index.php'); ?>" class="btn btn-primary mt-3 px-4 rounded-pill">Mulai Belanja</a>
    </div>
<?php else: ?>
    <div class="row g-4">
        <?php foreach ($orders as $order): ?>
            <div class="col-md-6 col-lg-4">
                <div class="card border-0 shadow-sm h-100 rounded-4 overflow-hidden position-relative">
                    <div class="card-header bg-white p-3 border-bottom-0 d-flex justify-content-between align-items-center">
                        <span class="badge rounded-pill bg-light text-dark fw-bold border">
                            <i class="bi bi-hash small"></i> <?= sanitize($order['code']); ?>
                        </span>
                        <div class="text-end">
                            <small class="d-block text-muted" style="font-size: 0.75rem;">Tanggal</small>
                            <span class="fw-bold small"><?= sanitize(date('d M Y', strtotime($order['created_at']))); ?></span>
                        </div>
                    </div>
                    
                    <div class="card-body px-3 pt-0 pb-3">
                        <div class="py-3 text-center border-top border-bottom border-light">
                            <div class="text-muted small mb-1">Status Pesanan</div>
                            <span class="badge rounded-pill bg-info bg-opacity-10 text-primary fs-6 px-3">
                                <?= sanitize(strtoupper($order['status'])); ?>
                            </span>
                        </div>
                        <div class="mt-3 text-center">
                            <small class="text-muted text-uppercase ls-1">Total Belanja</small>
                            <h3 class="fw-bold text-primary-end mb-0"><?= format_currency($order['total']); ?></h3>
                        </div>
                    </div>

                    <div class="card-footer bg-white p-3 border-top-0">
                        <div class="d-grid gap-2">
                            <a href="<?= base_url('public/index.php?page=payment_info&order_id=' . (int) $order['id']); ?>" class="btn btn-primary text-white rounded-pill fw-bold btn-sm shadow-sm" style="background: linear-gradient(135deg, var(--primary-start), var(--primary-end)); border:none;">
                                <i class="bi bi-info-circle me-1"></i> Detail & Pembayaran
                            </a>
                            <div class="row g-2">
                                <div class="col-6">
                                    <a href="<?= base_url('public/index.php?page=invoice&order_id=' . (int) $order['id']); ?>" target="_blank" class="btn btn-outline-dark w-100 btn-sm rounded-pill">
                                        <i class="bi bi-receipt me-1"></i> Nota
                                    </a>
                                </div>
                                <div class="col-6">
                                    <?php if (in_array($order['status'], ['pending', 'gagal', 'ditolak'])): ?>
                                    <a href="<?= base_url('public/index.php?page=payment_confirm&order_id=' . (int) $order['id']); ?>" class="btn btn-outline-primary w-100 btn-sm rounded-pill">
                                        <i class="bi bi-upload me-1"></i> Konfirmasi
                                    </a>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

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