/**
 * VR票务 - 票夹核心JS
 * 
 * 被 ticket_wallet.html 和用户中心页面引用
 * 提供 VrTicketWallet 对象用于票夹功能
 */
var VrTicketWallet = (function() {
    var apiBase = '/api.php?s=plugins/index&pluginsname=vr_ticket&pluginscontrol=ticket&pluginsaction=';
    var token = '';
    var tickets = [];
    var currentTicket = null;

    /**
     * 初始化
     * @param {string} userToken - 用户登录Token
     */
    function init(userToken) {
        token = userToken || '';
        
        // 绑定点击空白关闭弹窗
        document.addEventListener('click', function(e) {
            var modal = document.getElementById('vrTicketModal');
            if (modal && e.target === modal) {
                closeModal();
            }
        });

        // 绑定 ESC 关闭弹窗
        document.addEventListener('keydown', function(e) {
            if (e.key === 'Escape') {
                closeModal();
            }
        });
    }

    /**
     * 加载票列表
     * @param {string} containerId - 票列表容器ID
     * @param {function} callback - 加载完成回调
     */
    function loadTickets(containerId, callback) {
        containerId = containerId || 'vrTicketList';
        var container = document.getElementById(containerId);
        if (!container) {
            console.error('VrTicketWallet: 容器 #' + containerId + ' 不存在');
            return;
        }

        container.innerHTML = '<div class="vr-ticket-loading" style="text-align:center;padding:40px;color:#999;">加载中...</div>';

        $.ajax({
            url: apiBase + 'list',
            type: 'GET',
            dataType: 'json',
            headers: token ? {'X-Token': token} : {},
            success: function(res) {
                if (res.code === 0) {
                    tickets = res.data.tickets || [];
                    renderTickets(container);
                    if (typeof callback === 'function') {
                        callback(tickets);
                    }
                } else if (res.code === 401) {
                    container.innerHTML = '<div class="vr-ticket-empty" style="text-align:center;padding:40px;color:#999;">请先登录后查看我的票</div>';
                } else {
                    container.innerHTML = '<div class="vr-ticket-error" style="text-align:center;padding:40px;color:#f56c6c;">' + (res.msg || '加载失败') + '</div>';
                }
            },
            error: function() {
                container.innerHTML = '<div class="vr-ticket-error" style="text-align:center;padding:40px;color:#f56c6c;">网络错误，请稍后重试</div>';
            }
        });
    }

    /**
     * 渲染票列表
     */
    function renderTickets(container) {
        if (tickets.length === 0) {
            container.innerHTML = '<div class="vr-ticket-empty" style="text-align:center;padding:40px;color:#999;">暂无电子票</div>';
            return;
        }

        var html = '';
        tickets.forEach(function(ticket) {
            var statusMap = {
                0: {text: '未核销', class: 'unverified'},
                1: {text: '已核销', class: 'verified'},
                2: {text: '已退款', class: 'refunded'}
            };
            var status = statusMap[ticket.verify_status] || statusMap[0];

            html += '<div class="vr-ticket-card' + (ticket.verify_status > 0 ? ' ' + ticket.verify_status : '') + '" data-ticket-id="' + ticket.id + '">' +
                '<div class="vr-ticket-card-header">' +
                    '<div class="vr-ticket-goods-title">' + escapeHtml(ticket.goods_title) + '</div>' +
                    '<div class="vr-ticket-status ' + status.class + '">' + status.text + '</div>' +
                '</div>' +
                '<div class="vr-ticket-info">' +
                    '<div class="vr-ticket-info-row"><span class="vr-ticket-info-icon">📅</span><span>' + escapeHtml(ticket.session_time) + '</span></div>' +
                    '<div class="vr-ticket-info-row"><span class="vr-ticket-info-icon">📍</span><span>' + escapeHtml(ticket.venue_name) + '</span></div>' +
                    '<div class="vr-ticket-info-row"><span class="vr-ticket-info-icon">💺</span><span>' + escapeHtml(ticket.seat_info) + '</span></div>' +
                    '<div class="vr-ticket-info-row"><span class="vr-ticket-info-icon">👤</span><span>' + escapeHtml(ticket.real_name) + ' ' + escapeHtml(ticket.phone) + '</span></div>' +
                '</div>' +
                '<div class="vr-ticket-footer">' +
                    '<div class="vr-ticket-short-code">短码: ' + escapeHtml(ticket.short_code) + '</div>' +
                    '<a href="javascript:;" class="vr-ticket-view-btn" onclick="VrTicketWallet.viewTicket(' + ticket.id + ')">查看票码 →</a>' +
                '</div>' +
            '</div>';
        });

        container.innerHTML = html;
    }

    /**
     * 查看单个票详情
     */
    function viewTicket(ticketId) {
        // 查找本地缓存
        var ticket = tickets.find(function(t) { return t.id === ticketId; });
        
        var modalBody = document.getElementById('vrTicketModalBody');
        if (!modalBody) {
            // 如果弹窗不存在，先创建
            createModal();
            modalBody = document.getElementById('vrTicketModalBody');
        }
        
        modalBody.innerHTML = '<div style="text-align:center;padding:40px;">加载中...</div>';
        document.getElementById('vrTicketModal').classList.add('active');

        if (ticket) {
            showTicketBasic(ticket);
            loadQrPayload(ticketId);
        } else {
            $.ajax({
                url: apiBase + 'detail&id=' + ticketId,
                type: 'GET',
                dataType: 'json',
                headers: token ? {'X-Token': token} : {},
                success: function(res) {
                    if (res.code === 0 && res.data.ticket) {
                        currentTicket = res.data.ticket;
                        showTicketDetail(currentTicket);
                    } else {
                        modalBody.innerHTML = '<div style="text-align:center;padding:40px;color:#f56c6c;">' + (res.msg || '加载失败') + '</div>';
                    }
                },
                error: function() {
                    modalBody.innerHTML = '<div style="text-align:center;padding:40px;color:#f56c6c;">网络错误</div>';
                }
            });
        }
    }

    /**
     * 显示票基本信息
     */
    function showTicketBasic(ticket) {
        var statusMap = {
            0: {text: '未核销', class: 'unverified'},
            1: {text: '已核销', class: 'verified'},
            2: {text: '已退款', class: 'refunded'}
        };
        var status = statusMap[ticket.verify_status] || statusMap[0];

        var modalBody = document.getElementById('vrTicketModalBody');
        modalBody.innerHTML = 
            '<div class="vr-ticket-qr-section">' +
                '<div class="vr-ticket-qr-wrapper" id="vrQrcodeBox"></div>' +
                '<div class="vr-ticket-qr-expire" id="vrQrExpire"></div>' +
            '</div>' +
            '<div class="vr-ticket-short-code-display">' +
                '<div class="vr-ticket-short-code-label">短码（人工核销）</div>' +
                '<div class="vr-ticket-short-code-value">' + escapeHtml(ticket.short_code) + '</div>' +
            '</div>' +
            '<div class="vr-ticket-detail-row">' +
                '<div class="vr-ticket-detail-label">状态</div>' +
                '<div class="vr-ticket-detail-value"><span class="vr-ticket-status ' + status.class + '">' + status.text + '</span></div>' +
            '</div>' +
            '<div class="vr-ticket-detail-row">' +
                '<div class="vr-ticket-detail-label">场次</div>' +
                '<div class="vr-ticket-detail-value">' + escapeHtml(ticket.session_time) + '</div>' +
            '</div>' +
            '<div class="vr-ticket-detail-row">' +
                '<div class="vr-ticket-detail-label">场馆</div>' +
                '<div class="vr-ticket-detail-value">' + escapeHtml(ticket.venue_name) + '</div>' +
            '</div>' +
            '<div class="vr-ticket-detail-row">' +
                '<div class="vr-ticket-detail-label">座位</div>' +
                '<div class="vr-ticket-detail-value">' + escapeHtml(ticket.seat_info) + '</div>' +
            '</div>' +
            '<div class="vr-ticket-detail-row">' +
                '<div class="vr-ticket-detail-label">观演人</div>' +
                '<div class="vr-ticket-detail-value">' + escapeHtml(ticket.real_name) + ' ' + escapeHtml(ticket.phone) + '</div>' +
            '</div>' +
            (ticket.verify_status === 0 ? '<button class="vr-ticket-refresh-btn" id="vrRefreshBtn" onclick="VrTicketWallet.refreshQr(' + ticket.id + ')">刷新二维码</button>' : '');
    }

    /**
     * 显示票详细信息（含 QR）
     */
    function showTicketDetail(ticket) {
        var statusMap = {
            0: {text: '未核销', class: 'unverified'},
            1: {text: '已核销', class: 'verified'},
            2: {text: '已退款', class: 'refunded'}
        };
        var status = statusMap[ticket.verify_status] || statusMap[0];

        var modalBody = document.getElementById('vrTicketModalBody');
        
        var verifiedBadge = ticket.verify_status === 1 
            ? '<div class="vr-ticket-verified-badge">✓ 已核销</div>' 
            : '';

        modalBody.innerHTML = 
            '<div class="vr-ticket-qr-section">' +
                '<div class="vr-ticket-qr-wrapper" id="vrQrcodeBox"></div>' +
                '<div class="vr-ticket-qr-expire" id="vrQrExpire"></div>' +
            '</div>' +
            '<div class="vr-ticket-short-code-display">' +
                '<div class="vr-ticket-short-code-label">短码（人工核销）</div>' +
                '<div class="vr-ticket-short-code-value">' + escapeHtml(ticket.short_code) + '</div>' +
            '</div>' +
            verifiedBadge +
            '<div class="vr-ticket-detail-row">' +
                '<div class="vr-ticket-detail-label">场次</div>' +
                '<div class="vr-ticket-detail-value">' + escapeHtml(ticket.session_time) + '</div>' +
            '</div>' +
            '<div class="vr-ticket-detail-row">' +
                '<div class="vr-ticket-detail-label">场馆</div>' +
                '<div class="vr-ticket-detail-value">' + escapeHtml(ticket.venue_name) + '</div>' +
            '</div>' +
            '<div class="vr-ticket-detail-row">' +
                '<div class="vr-ticket-detail-label">座位</div>' +
                '<div class="vr-ticket-detail-value">' + escapeHtml(ticket.seat_info) + '</div>' +
            '</div>' +
            '<div class="vr-ticket-detail-row">' +
                '<div class="vr-ticket-detail-label">观演人</div>' +
                '<div class="vr-ticket-detail-value">' + escapeHtml(ticket.real_name) + ' ' + escapeHtml(ticket.phone) + '</div>' +
            '</div>' +
            (ticket.verify_status === 0 ? '<button class="vr-ticket-refresh-btn" id="vrRefreshBtn" onclick="VrTicketWallet.refreshQr(' + ticket.id + ')">刷新二维码</button>' : '');

        if (ticket.qr_payload) {
            renderQrCode(ticket.qr_payload, ticket.qr_expires_in);
        } else {
            document.getElementById('vrQrcodeBox').innerHTML = '<div style="color:#999;">QR加载中...</div>';
        }
    }

    /**
     * 加载 QR payload
     */
    function loadQrPayload(ticketId) {
        var cacheKey = 'vr_qr_' + ticketId;
        var cached = localStorage.getItem(cacheKey);
        var now = Math.floor(Date.now() / 1000);

        if (cached) {
            try {
                var data = JSON.parse(cached);
                var remaining = data.expires_at - now;
                
                if (remaining > 900) {
                    renderQrCode(data.payload, remaining);
                    return;
                }
            } catch (e) {
                localStorage.removeItem(cacheKey);
            }
        }

        $.ajax({
            url: apiBase + 'detail&id=' + ticketId,
            type: 'GET',
            dataType: 'json',
            headers: token ? {'X-Token': token} : {},
            success: function(res) {
                if (res.code === 0 && res.data.ticket) {
                    var ticket = res.data.ticket;
                    var expiresIn = ticket.qr_expires_in || 0;

                    localStorage.setItem(cacheKey, JSON.stringify({
                        payload: ticket.qr_payload,
                        expires_at: now + expiresIn
                    }));

                    renderQrCode(ticket.qr_payload, expiresIn);
                }
            },
            error: function() {
                var qrBox = document.getElementById('vrQrcodeBox');
                if (qrBox) qrBox.innerHTML = '<div style="color:#f56c6c;">QR加载失败</div>';
            }
        });
    }

    /**
     * 渲染 QR 码
     */
    function renderQrCode(payload, expiresIn) {
        var qrBox = document.getElementById('vrQrcodeBox');
        var expireEl = document.getElementById('vrQrExpire');

        if (!qrBox) return;

        try {
            var json = atob(payload);
            
            qrBox.innerHTML = '';
            $(qrBox).qrcode({
                text: json,
                width: 200,
                height: 200
            });

            if (expiresIn > 0 && expireEl) {
                var minutes = Math.floor(expiresIn / 60);
                expireEl.textContent = '有效期: ' + minutes + ' 分钟';
            }
        } catch (e) {
            qrBox.innerHTML = '<div style="color:#f56c6c;">QR解析失败</div>';
        }
    }

    /**
     * 刷新 QR
     */
    function refreshQr(ticketId) {
        var btn = document.getElementById('vrRefreshBtn');
        if (btn) {
            btn.disabled = true;
            btn.textContent = '刷新中...';
        }

        $.ajax({
            url: apiBase + 'refreshQr&id=' + ticketId,
            type: 'GET',
            dataType: 'json',
            headers: token ? {'X-Token': token} : {},
            success: function(res) {
                if (res.code === 0 && res.data.ticket) {
                    var ticket = res.data.ticket;

                    var cacheKey = 'vr_qr_' + ticketId;
                    var now = Math.floor(Date.now() / 1000);
                    localStorage.setItem(cacheKey, JSON.stringify({
                        payload: ticket.qr_payload,
                        expires_at: now + ticket.qr_expires_in
                    }));

                    renderQrCode(ticket.qr_payload, ticket.qr_expires_in);

                    if (btn) {
                        btn.textContent = '已刷新';
                        setTimeout(function() {
                            btn.textContent = '刷新二维码';
                            btn.disabled = false;
                        }, 2000);
                    }
                } else {
                    alert(res.msg || '刷新失败');
                    if (btn) {
                        btn.disabled = false;
                        btn.textContent = '刷新二维码';
                    }
                }
            },
            error: function() {
                alert('网络错误');
                if (btn) {
                    btn.disabled = false;
                    btn.textContent = '刷新二维码';
                }
            }
        });
    }

    /**
     * 创建弹窗
     */
    function createModal() {
        var html = '<div id="vrTicketModal" class="vr-ticket-modal">' +
            '<div class="vr-ticket-modal-content">' +
                '<div class="vr-ticket-modal-header">' +
                    '<div class="vr-ticket-modal-title">电子票</div>' +
                    '<button class="vr-ticket-modal-close" onclick="VrTicketWallet.closeModal()">×</button>' +
                '</div>' +
                '<div id="vrTicketModalBody"></div>' +
            '</div>' +
        '</div>' +
        '<style>' +
        '.vr-ticket-modal{position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,0.5);z-index:9999;display:none;align-items:center;justify-content:center;}' +
        '.vr-ticket-modal.active{display:flex;}' +
        '.vr-ticket-modal-content{background:#fff;border-radius:16px;width:90%;max-width:400px;max-height:90vh;overflow-y:auto;padding:24px;}' +
        '.vr-ticket-modal-header{display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;}' +
        '.vr-ticket-modal-title{font-size:18px;font-weight:600;color:#333;}' +
        '.vr-ticket-modal-close{width:28px;height:28px;border-radius:50%;background:#f0f0f0;border:none;font-size:18px;cursor:pointer;}' +
        '.vr-ticket-qr-section{text-align:center;padding:20px;background:#fafafa;border-radius:12px;margin-bottom:16px;}' +
        '.vr-ticket-qr-wrapper{display:inline-block;padding:12px;background:#fff;border-radius:8px;border:1px solid #eee;}' +
        '.vr-ticket-qr-expire{font-size:12px;color:#999;margin-top:8px;}' +
        '.vr-ticket-short-code-display{text-align:center;margin:16px 0;padding:12px;background:#f5f5f5;border-radius:8px;}' +
        '.vr-ticket-short-code-label{font-size:12px;color:#999;margin-bottom:4px;}' +
        '.vr-ticket-short-code-value{font-size:20px;font-family:\'Courier New\',monospace;font-weight:700;color:#333;letter-spacing:2px;}' +
        '.vr-ticket-detail-row{display:flex;padding:8px 0;border-bottom:1px solid #f0f0f0;}' +
        '.vr-ticket-detail-label{width:80px;font-size:13px;color:#999;}' +
        '.vr-ticket-detail-value{flex:1;font-size:13px;color:#333;}' +
        '.vr-ticket-verified-badge{display:inline-block;padding:4px 12px;background:#52c41a;color:#fff;border-radius:4px;font-size:14px;font-weight:600;}' +
        '.vr-ticket-refresh-btn{display:block;width:100%;padding:12px;background:#1890ff;color:#fff;border:none;border-radius:8px;font-size:14px;cursor:pointer;margin-top:12px;}' +
        '.vr-ticket-refresh-btn:disabled{background:#ccc;cursor:not-allowed;}' +
        '.vr-ticket-card{background:#fff;border-radius:12px;padding:16px;margin-bottom:12px;box-shadow:0 2px 8px rgba(0,0,0,0.06);cursor:pointer;transition:all 0.2s ease;}' +
        '.vr-ticket-card:hover{box-shadow:0 4px 16px rgba(0,0,0,0.12);transform:translateY(-2px);}' +
        '.vr-ticket-card.verified{opacity:0.7;background:#f5f5f5;}' +
        '.vr-ticket-card-header{display:flex;justify-content:space-between;align-items:flex-start;margin-bottom:12px;}' +
        '.vr-ticket-goods-title{font-size:16px;font-weight:600;color:#333;flex:1;margin-right:12px;}' +
        '.vr-ticket-status{font-size:12px;padding:2px 8px;border-radius:4px;font-weight:500;}' +
        '.vr-ticket-status.unverified{background:#e6f7ff;color:#1890ff;}' +
        '.vr-ticket-status.verified{background:#f6ffed;color:#52c41a;}' +
        '.vr-ticket-status.refunded{background:#fff1f0;color:#ff4d4f;}' +
        '.vr-ticket-info{font-size:13px;color:#666;line-height:1.6;}' +
        '.vr-ticket-info-row{display:flex;align-items:center;margin-bottom:4px;}' +
        '.vr-ticket-info-icon{width:16px;color:#999;margin-right:6px;}' +
        '.vr-ticket-footer{display:flex;justify-content:space-between;align-items:center;margin-top:12px;padding-top:12px;border-top:1px solid #f0f0f0;}' +
        '.vr-ticket-short-code{font-size:14px;font-family:\'Courier New\',monospace;color:#333;font-weight:600;letter-spacing:1px;}' +
        '.vr-ticket-view-btn{font-size:13px;color:#1890ff;text-decoration:none;}' +
        '.vr-ticket-view-btn:hover{text-decoration:underline;}' +
        '</style>';
        document.body.insertAdjacentHTML('beforeend', html);
    }

    /**
     * 关闭弹窗
     */
    function closeModal() {
        var modal = document.getElementById('vrTicketModal');
        if (modal) modal.classList.remove('active');
    }

    /**
     * HTML 转义
     */
    function escapeHtml(str) {
        if (!str) return '';
        var div = document.createElement('div');
        div.textContent = str;
        return div.innerHTML;
    }

    // 导出公开接口
    return {
        init: init,
        loadTickets: loadTickets,
        viewTicket: viewTicket,
        refreshQr: refreshQr,
        closeModal: closeModal
    };
})();
