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

$orderId = isset($_GET['order_id']) ? (int) $_GET['order_id'] : 0;

$stmt = $pdo->prepare('SELECT o.*, pm.name AS method_name, pm.account_name, pm.account_number, pm.instructions
    FROM orders o
    LEFT JOIN payment_methods pm ON pm.id = o.payment_method_id
    WHERE o.id = ? AND o.user_id = ? LIMIT 1');
$stmt->execute([$orderId, current_user()['id']]);
$order = $stmt->fetch();

if (!$order) {
    echo '<div class="alert error">Order tidak ditemukan.</div>';
    require_once __DIR__ . '/../includes/footer_customer.php';
    exit;
}
?>

<div class="row justify-content-center">
    <div class="col-lg-8">
        <div class="text-center mb-4">
            <h2 class="fw-bold mb-2">Informasi Pembayaran</h2>
            <p class="text-muted">Terima kasih telah berbelanja di Al-Qanuni Group</p>
        </div>

        <div class="card shadow-sm border-0 rounded-4 overflow-hidden mb-4">
            <div class="card-header bg-white p-4 border-bottom-0 text-center">
                <span class="badge rounded-pill bg-warning text-dark fs-6 px-3 py-2">
                    Status: <?= sanitize(strtoupper($order['status'])); ?>
                </span>
                <h1 class="display-6 fw-bold mt-3 text-primary-end"><?= format_currency($order['total']); ?></h1>
                <p class="text-muted small">Kode Order: <strong class="text-dark"><?= sanitize($order['code']); ?></strong></p>
            </div>
            
            <div class="card-body p-4 bg-light bg-opacity-25">
                <div class="row g-4">
                    <!-- Payment Instructions -->
                    <div class="col-md-7 border-end-md">
                        <h6 class="fw-bold mb-3 text-uppercase small text-muted ls-1">Transfer Pembayaran Ke:</h6>
                        <?php if (!empty($order['method_name'])): ?>
                            <div class="p-3 bg-white rounded-3 border shadow-sm mb-3">
                                <div class="d-flex align-items-center mb-2">
                                    <i class="bi bi-bank fs-4 text-primary me-2"></i>
                                    <span class="fw-bold"><?= sanitize($order['method_name']); ?></span>
                                </div>
                                <div class="mb-1 text-muted small">Nomor Rekening:</div>
                                <div class="d-flex align-items-center justify-content-between bg-light p-2 rounded">
                                    <span class="fs-5 fw-bold font-monospace"><?= sanitize($order['account_number']); ?></span>
                                </div>
                                <div class="mt-2 text-muted small">Atas Nama: <strong><?= sanitize($order['account_name']); ?></strong></div>
                            </div>
                            <div class="alert alert-info d-flex align-items-start small">
                                <i class="bi bi-info-circle-fill me-2 mt-1"></i>
                                <div><?= nl2br(sanitize($order['instructions'])); ?></div>
                            </div>
                        <?php else: ?>
                            <div class="alert alert-warning">Metode pembayaran belum dipilih.</div>
                        <?php endif; ?>
                    </div>

                    <!-- Actions -->
                    <div class="col-md-5 d-flex flex-column justify-content-center">
                        <h6 class="fw-bold mb-3 text-uppercase small text-muted ls-1">Langkah Selanjutnya:</h6>
                        
                        <a href="<?= base_url('public/index.php?page=invoice&order_id=' . (int) $order['id']); ?>" target="_blank" class="btn btn-outline-secondary mb-2 d-flex align-items-center justify-content-between">
                            <span><i class="bi bi-receipt me-2"></i>Cetak / Lihat Nota</span>
                            <i class="bi bi-chevron-right small"></i>
                        </a>

                        <?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-primary text-white mb-2 d-flex align-items-center justify-content-between" style="background: linear-gradient(135deg, var(--primary-start), var(--primary-end)); border:none;">
                            <span><i class="bi bi-upload me-2"></i>Konfirmasi Pembayaran</span>
                            <i class="bi bi-chevron-right small"></i>
                        </a>
                        <?php endif; ?>

                        <a href="<?= base_url('public/index.php?page=orders'); ?>" class="btn btn-outline-dark mb-2 d-flex align-items-center justify-content-between">
                            <span><i class="bi bi-box-seam me-2"></i>Cek Status Pesanan Saya</span>
                            <i class="bi bi-chevron-right small"></i>
                        </a>
                    </div>
                </div>
            </div>
            <div class="card-footer bg-light p-3 text-center small text-muted border-top-0">
                Lakukan pembayaran sebelum 1x24 jam agar pesanan tidak dibatalkan otomatis.
            </div>
        </div>
    </div>
</div>

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