<?php
$title='激活续费';
include __DIR__ . '/head.php';
?>
<div class="grid grid-cols-1 lg:grid-cols-3 gap-4">
    <div class="lg:col-span-2">
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
            <div class="px-4 py-3 bg-gray-50 border-b border-gray-100 flex items-center gap-2">
                <i class="fas fa-key text-indigo-500"></i>
                <h3 class="font-medium text-gray-800">激活续费</h3>
            </div>
            <div class="p-4">
                <form id="kamiForm" onsubmit="buyKami(this);return false;">
                    <div class="space-y-3">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">QQ账号</label>
                            <div class="flex gap-2 overflow-hidden">
                                <input type="number" class="flex-1 w-full px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500" name="qq" required placeholder="输入QQ账号" style="min-width:0">
                                <?php
                                $qq_sources = get_user_qq_sources($userrow['zid']);
                                $qq_list = array();
                                foreach($qq_sources['cloud'] as $q){ $qq_list[$q] = true; }
                                foreach($qq_sources['dg'] as $q){ $qq_list[$q] = true; }
                                foreach($qq_sources['qzone'] as $q){ $qq_list[$q] = true; }
                                $option = array();
                                foreach(array_keys($qq_list) as $q){ $option[] = '<option value="'.$q.'">'.$q.'</option>'; }
                                if(count($option)>0){
                                    echo '<select class="shrink-0 px-2 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm" id="getqq">'.implode('',$option).'</select>';
                                }
                                ?>
                            </div>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">卡密</label>
                            <input type="text" class="w-full px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500" name="km" required placeholder="输入卡密">
                        </div>
                        <div class="pt-2">
                            <button type="submit" class="w-full bg-indigo-600 hover:bg-indigo-700 text-white font-medium py-2.5 rounded-lg transition-colors flex items-center justify-center gap-1.5">
                                <i class="fas fa-check-circle text-sm"></i><span>立即激活</span>
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="lg:col-span-1">
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
            <div class="px-4 py-3 bg-gray-50 border-b border-gray-100 flex items-center gap-2">
                <i class="fas fa-info-circle text-indigo-500"></i>
                <h3 class="font-medium text-gray-800">激活须知</h3>
            </div>
            <div class="p-4 space-y-2 text-sm text-gray-600">
                <div class="flex gap-2"><i class="fas fa-check-circle text-green-500 mt-0.5"></i><p>请确保输入的QQ账号正确无误</p></div>
                <div class="flex gap-2"><i class="fas fa-check-circle text-green-500 mt-0.5"></i><p>卡密只能使用一次，请妥善保管</p></div>
                <div class="flex gap-2"><i class="fas fa-check-circle text-green-500 mt-0.5"></i><p>激活成功后立即生效</p></div>
                <div class="flex gap-2"><i class="fas fa-check-circle text-green-500 mt-0.5"></i><p>如有疑问请联系客服</p></div>
            </div>
            <div class="px-4 pb-4">
                <a href="./upgrade.php" class="block w-full bg-gray-50 hover:bg-gray-100 text-gray-700 font-medium py-2 rounded-lg text-center text-sm transition-colors">
                    <i class="fas fa-arrow-up mr-1"></i>升级代理
                </a>
            </div>
        </div>
    </div>
</div>

<?php include 'foot.php'; ?>

<div id="appModal" class="fixed inset-0 z-50 hidden">
    <div class="fixed inset-0 bg-black/50 backdrop-blur-sm" onclick="closeModal()"></div>
    <div class="fixed inset-0 flex items-center justify-center p-4">
        <div class="bg-white rounded-2xl shadow-2xl w-full max-w-md max-h-[85vh] flex flex-col">
            <div class="flex items-center justify-between px-5 py-4 border-b border-gray-100 bg-gray-50/50 rounded-t-2xl">
                <h3 id="modalTitle" class="text-lg font-semibold text-gray-800"></h3>
                <button onclick="closeModal()" class="w-8 h-8 flex items-center justify-center rounded-full hover:bg-gray-200 text-gray-400 hover:text-gray-600 transition-colors"><i class="fas fa-times"></i></button>
            </div>
            <div id="modalBody" class="px-5 py-4 overflow-y-auto"></div>
            <div id="modalFooter" class="px-5 py-3 border-t border-gray-100 hidden"></div>
        </div>
    </div>
</div>

<script>
function openModal(title, content, footerCallback) {
    document.getElementById('modalTitle').textContent = title;
    document.getElementById('modalBody').innerHTML = content;
    const modalFooter = document.getElementById('modalFooter');
    if (footerCallback) {
        modalFooter.innerHTML = footerCallback();
        modalFooter.classList.remove('hidden');
    } else {
        modalFooter.classList.add('hidden');
    }
    document.getElementById('appModal').classList.remove('hidden');
    document.body.style.overflow = 'hidden';
}

function closeModal() {
    document.getElementById('appModal').classList.add('hidden');
    document.body.style.overflow = '';
}

$(document).ready(function(){
    $('#getqq').change(function(){ $('input[name="qq"]').val($('#getqq').val()); });
    if($('#getqq').length > 0){ $('input[name="qq"]').val($('#getqq').val()); }
});

function buyKami(obj){
    var qq = $('input[name="qq"]').val();
    var km = $('input[name="km"]').val();
    if(!qq){ showToast('请输入QQ账号'); return; }
    if(!km){ showToast('请输入卡密'); return; }

    showLoading();
    $.ajax({
        type: 'POST',
        url: 'ajax.php?act=pay_kami',
        data: $(obj).serialize(),
        dataType: 'json',
        success: function(data) {
            hideLoading();
            if(data.code == '1'){
                var loginUrl = (data.version === 'qzone') ? './qzone.php' : ((data.version === 'dg') ? ('./add.php?biz=dg&qq='+qq) : ('./add.php?qq='+qq));
                var tmp = '<p class="text-center">'+data.msg+'</p><div class="grid gap-2 mt-4"><a class="block py-2 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg font-medium text-center text-sm" href="./">返回首页</a><a class="block py-2 bg-gray-600 hover:bg-gray-700 text-white rounded-lg font-medium text-center text-sm" href="'+loginUrl+'">前去登录QQ</a></div>';
                openModal('激活成功', tmp, function(){ return '<button onclick="closeModal();location.reload();" class="w-full py-2 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg font-medium transition-colors">确定</button>'; });
            }else if(data.code == '-3'){
                openModal('提示', data.msg, function(){ return '<div class="flex gap-2"><button onclick="closeModal()" class="flex-1 py-2 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg font-medium transition-colors">取消</button><a href="./" class="flex-1 py-2 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg font-medium text-center transition-colors">前往找回</a></div>'; });
            }else{ showToast(data.msg); }
        },
        error: function(){ hideLoading(); showToast('服务器错误'); }
    });
}
</script>
