<?php
include 'includes/header.php';

$order_no = $_GET['order_no'] ?? '';
$db = db();
$stmt = $db->prepare("SELECT * FROM orders WHERE order_no = ?");
$stmt->execute([$order_no]);
$order = $stmt->fetch();

if (!$order) {
    echo "<div class='container my-5 py-5 text-center'><div class='alert glass-card text-danger border-danger p-4 p-lg-5'><i class='bi bi-exclamation-octagon display-4 d-block mb-3'></i><h4>订单不存在</h4><p>请检查您的订单号是否正确或联系客服。</p><a href='index.php' class='btn btn-primary mt-3'>返回首页</a></div></div>";
    include 'includes/footer.php';
    exit;
}

// Fetch total quantity
$stmt = $db->prepare("SELECT SUM(quantity) as total_qty FROM order_items WHERE order_id = ?");
$stmt->execute([$order['id']]);
$qty_data = $stmt->fetch();
$total_qty = $qty_data['total_qty'] ?? 1;

$usdt_address = $settings['usdt_address'] ?? 'Txxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$qr_code_custom = $settings['qr_code_custom'] ?? '';
$qr_src = !empty($qr_code_custom) ? $qr_code_custom : "https://api.qrserver.com/v1/create-qr-code/?size=300x300&data=" . urlencode($usdt_address);
?>

<div class="row justify-content-center">
    <div class="col-lg-10 p-2 p-lg-0">
        <div class="glass-card p-0 overflow-hidden bg-white shadow-lg border-0 rounded-4">
            <div class="bg-primary bg-opacity-10 p-3 p-lg-4 border-bottom d-flex justify-content-between align-items-center">
                <h5 class="text-dark mb-0 fw-bold"><i class="bi bi-shield-lock-fill me-2 text-primary"></i>支付收银台</h5>
                <div class="badge bg-primary px-3 py-1 rounded-pill fw-bold small">USDT - TRC20</div>
            </div>
            
            <div class="p-3 p-lg-5">
                <div class="row g-4 g-lg-5">
                    <div class="col-lg-5 text-center">
                        <div class="p-3 rounded-4 d-inline-block border border-light bg-light mb-3">
                            <img src="<?php echo $qr_src; ?>" alt="QR Code" class="img-fluid" style="width: 200px;">
                        </div>
                        <div class="p-3 bg-light rounded-4 mb-3">
                            <label class="text-muted small d-block mb-1 fw-bold">支付倒计时</label>
                            <div id="countdown" class="h2 fw-bold text-primary">60:00</div>
                        </div>
                        <p class="text-muted" style="font-size: 0.7rem;"><i class="bi bi-info-circle me-1"></i> 请在倒计时结束前完成支付</p>
                    </div>

                    <div class="col-lg-7">
                        <div class="mb-4 p-3 bg-light rounded-4">
                            <h6 class="text-dark fw-bold mb-3 border-bottom pb-2">订单信息</h6>
                            <div class="d-flex justify-content-between align-items-center mb-2 small">
                                <span class="text-muted">订单编号:</span>
                                <span class="text-dark fw-bold"><?php echo $order_no; ?></span>
                            </div>
                            <div class="d-flex justify-content-between align-items-center mb-2 small">
                                <span class="text-muted">商品数量:</span>
                                <span class="text-dark fw-bold"><?php echo $total_qty; ?> 件</span>
                            </div>
                            <div class="d-flex justify-content-between align-items-center pt-2 border-top mt-2">
                                <span class="text-dark fw-bold">应付金额:</span>
                                <span class="h3 fw-bold text-primary mb-0"><?php echo $order['total_amount']; ?> <small class="fs-6">USDT</small></span>
                            </div>
                        </div>

                        <div class="mb-4">
                            <h6 class="text-dark fw-bold mb-2">收款地址 (TRC20)</h6>
                            <div class="input-group">
                                <input type="text" id="usdtAddress" class="form-control bg-light border-0 text-dark font-monospace" style="font-size: 0.8rem;" value="<?php echo $usdt_address; ?>" readonly>
                                <button class="btn btn-primary btn-sm px-3" onclick="copyAddress()">
                                    复制
                                </button>
                            </div>
                        </div>

                        <div class="p-3 rounded-4 border border-info border-opacity-10 bg-info bg-opacity-10 mb-4">
                            <h6 class="text-info fw-bold mb-2 small"><i class="bi bi-info-square-fill me-2"></i> 支付说明：</h6>
                            <ul class="text-muted small mb-0 ps-3 lh-sm" style="font-size: 0.75rem;">
                                <li>请使用 <strong class="text-primary">TRC20</strong> 网络转账。</li>
                                <li>金额必须 <strong class="text-primary">完全一致</strong> (<?php echo $order['total_amount']; ?> USDT)。</li>
                                <li>支付后点击下方按钮联系客服。</li>
                            </ul>
                        </div>

                        <div class="d-grid gap-2">
                            <button id="checkPaymentBtn" class="btn btn-primary btn-lg py-3 rounded-pill fw-bold shadow-lg">
                                <i class="bi bi-check-circle-fill me-2"></i> 我已完成支付，联系客服
                            </button>
                            <a href="index.php" class="btn btn-outline-secondary py-2 rounded-pill small">
                                返回首页
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function copyAddress() {
    var copyText = document.getElementById("usdtAddress");
    copyText.select();
    copyText.setSelectionRange(0, 99999);
    navigator.clipboard.writeText(copyText.value);
    
    const btn = document.querySelector('button[onclick="copyAddress()"]');
    const originalText = btn.innerHTML;
    btn.innerHTML = '已复制';
    btn.classList.replace('btn-primary', 'btn-success');
    setTimeout(() => {
        btn.innerHTML = originalText;
        btn.classList.replace('btn-success', 'btn-primary');
    }, 2000);
}

// Countdown Timer
let timeLeft = 60 * 60; 
const countdownEl = document.getElementById('countdown');

const timer = setInterval(() => {
    const minutes = Math.floor(timeLeft / 60);
    let seconds = timeLeft % 60;
    seconds = seconds < 10 ? '0' + seconds : seconds;
    countdownEl.innerHTML = `${minutes}:${seconds}`;
    if (timeLeft <= 0) {
        clearInterval(timer);
        countdownEl.innerHTML = "EXPIRED";
    }
    timeLeft--;
}, 1000);

document.getElementById('checkPaymentBtn').addEventListener('click', function() {
    const message = encodeURIComponent(`您好，我已支付订单.\n\n订单编号：<?php echo $order_no; ?>\n实付金额：<?php echo $order['total_amount']; ?> USDT\n\n请核实。`);
    const tgLink = `<?php echo $tg_link; ?>?text=${message}`;
    window.open(tgLink, '_blank');
});

localStorage.removeItem('cart');
if (typeof updateCartBadge === 'function') updateCartBadge();
</script>

<?php include 'includes/footer.php'; ?>