(() => {
    const params = new URLSearchParams(location.search);
    const code = params.get('code');
    const loading = document.getElementById('loading');
    const error = document.getElementById('error');
    const errorMsg = document.getElementById('errorMsg');
    const content = document.getElementById('content');

    const setTextAll = (selector, value) => {
        document.querySelectorAll(selector).forEach(el => { el.textContent = value ?? ''; });
    };

    const setTextById = (id, value) => {
        const el = document.getElementById(id);
        if (el) el.textContent = value ?? '';
    };

    const applyStatus = (el, text, cls) => {
        if (!el) return;
        el.textContent = text;
        el.className = cls;
    };

    const formatTrainType = (type) => {
        const t = String(type || '').toLowerCase();
        if (t === 'express' || t === 'ltd.exp' || t === '特急') return '特急';
        return '普通';
    };
    
    if (!code) {
        loading.style.display = 'none';
        error.style.display = 'block';
        errorMsg.textContent = '无效的凭证码';
        return;
    }
    
    fetch(`/api/public/orders/${encodeURIComponent(code)}`)
        .then(r => r.json())
        .then(res => {
            if (res.ok && res.data) {
                loading.style.display = 'none';
                error.style.display = 'none';
                const d = res.data;
                content.style.display = 'flex';
                document.getElementById('vCode').textContent = d.code;
                const codeTop = document.getElementById('vCodeTop');
                if (codeTop) codeTop.textContent = d.code;
                
                const statusTag = document.getElementById('vStatusTag');
                const statusTop = document.getElementById('vStatusTop');
                if (d.consumed) {
                    applyStatus(statusTag, '已使用', 'jr-status-pill jr-status-expired');
                    applyStatus(statusTop, '已使用', 'jr-status-pill jr-status-expired');
                } else {
                    applyStatus(statusTag, '可使用', 'jr-status-pill jr-status-valid');
                    applyStatus(statusTop, '可使用', 'jr-status-pill jr-status-valid');
                }

                const startName = d.start_name || d.start || '';
                const startEn = d.start_en || '';
                const terminalName = d.terminal_name || d.terminal || '';
                const terminalEn = d.terminal_en || '';
                const trips = d.trips ?? '';
                const type = formatTrainType(d.train_type);
                const rideDate = d.ride_date ?? '';
                const price = d.price ?? '';

                setTextAll('.vStartName', startName);
                setTextAll('.vStartEn', startEn);
                setTextAll('.vStartCode', d.start || '');
                setTextAll('.vTermName', terminalName);
                setTextAll('.vTermEn', terminalEn);
                setTextAll('.vTermCode', d.terminal || '');

                setTextById('vCode', d.code);

                setTextById('vTypeTop', type);
                setTextById('vTripsTop', trips);
                setTextById('vDateTop', rideDate);
                setTextById('vPriceTop', price);
                
                document.getElementById('copyBtn').onclick = () => {
                    navigator.clipboard.writeText(d.code).then(() => {
                        alert('已复制凭证码');
                    });
                };
            } else {
                loading.style.display = 'none';
                error.style.display = 'flex';
                let msg = res.error || '未找到凭证信息';
                if (msg.includes('not found')) msg = '凭证不存在';
                errorMsg.textContent = msg;
            }
        })
        .catch(e => {
            loading.style.display = 'none';
            error.style.display = 'flex';
            errorMsg.textContent = '加载失败: ' + e.message;
        });
})();
