<?php
$title='我的';
include __DIR__ . '/head.php';

if($userrow['upzid'] == '1'){
	$type = '0,1';
}else{
	$type = '0';
}
$msgread = trim($userrow['msgread'],',');
if(empty($msgread))$msgread='0';
$msg_count_num = (int)$DB->getColumn("SELECT count(*) FROM pre_message WHERE id NOT IN ($msgread) and type IN ($type)");
$msg_count = $msg_count_num==0?'':'<span class="ml-2 px-1.5 py-0.5 bg-red-100 text-red-700 rounded-full text-xs font-medium">'.$msg_count_num.'</span>';

$rs=$DB->query("SELECT * FROM pre_user_vip");
$vip_name = array();
while($res = $rs->fetch(PDO::FETCH_ASSOC)){
	$vip_name[$res['vip']] = $res['name'];
}
$max_vip = $DB->getColumn("SELECT MAX(vip) FROM pre_user_vip");

// QQ 数量：云端 + 代挂 + 秒赞，同一 QQ 只计一次
$zid = intval($userrow['zid'] ?? 0);
$qq_unique_set = array();
$rs = $DB->query("SELECT qq FROM pre_qq WHERE zid='{$zid}'");
if ($rs) {
	while ($row = $rs->fetch(PDO::FETCH_ASSOC)) {
		$q = trim((string)($row['qq'] ?? ''));
		if ($q !== '') {
			$qq_unique_set[$q] = true;
		}
	}
}
if ($DB->getColumn("SHOW TABLES LIKE 'pre_dg_qq'")) {
	$rs = $DB->query("SELECT qq FROM pre_dg_qq WHERE zid='{$zid}'");
	if ($rs) {
		while ($row = $rs->fetch(PDO::FETCH_ASSOC)) {
			$q = trim((string)($row['qq'] ?? ''));
			if ($q !== '') {
				$qq_unique_set[$q] = true;
			}
		}
	}
}
$qzone_table = (defined('DBQZ') ? DBQZ : 'pre_') . 'qzone_qq';
if ($DB->getColumn("SHOW TABLES LIKE '" . str_replace("'", "\\'", $qzone_table) . "'")) {
	$rs = $DB->query("SELECT qq FROM " . DBQZ . "qzone_qq WHERE user_id='{$zid}'");
	if ($rs) {
		while ($row = $rs->fetch(PDO::FETCH_ASSOC)) {
			$q = trim((string)($row['qq'] ?? ''));
			if ($q !== '') {
				$qq_unique_set[$q] = true;
			}
		}
	}
}
$qq_unique_count = count($qq_unique_set);
?>

<div class="space-y-3 lg:space-y-5">
    <div class="bg-gradient-to-br from-indigo-600 via-purple-600 to-pink-600 rounded-xl lg:rounded-2xl shadow-lg overflow-hidden">
        <div class="p-3 lg:p-5">
            <div class="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-3 mb-3">
                <div class="flex items-center gap-2.5 lg:gap-3">
                    <div class="relative">
                        <img src="//q4.qlogo.cn/headimg_dl?dst_uin=<?php echo $userrow['qq']; ?>&spec=100" class="w-12 h-12 lg:w-16 lg:h-16 rounded-full border-2 border-white/30">
                        <div class="absolute -bottom-1 -right-1 w-5 h-5 lg:w-6 lg:h-6 bg-gradient-to-br from-yellow-400 to-orange-500 rounded-full flex items-center justify-center border-2 border-white">
                            <i class="fas fa-crown text-white text-[10px] lg:text-xs"></i>
                        </div>
                    </div>
                    <div>
                        <div class="text-lg lg:text-xl font-bold text-white"><?php echo $userrow['user']; ?></div>
                        <div class="flex gap-2 mt-1">
                            <span class="bg-gradient-to-r from-yellow-400 to-amber-500 rounded-full px-2.5 py-0.5 text-xs text-white font-semibold"><?php echo $vip_name[$userrow['vip']] ?? 'VIP'.$userrow['vip']; ?></span>
                            <span class="bg-white/20 backdrop-blur-sm rounded-full px-2.5 py-0.5 text-xs text-white">ID: <?php echo $userrow['zid']; ?></span>
                        </div>
                    </div>
                </div>
                <div class="flex items-center gap-2 flex-wrap">
                    <a href="./recharge.php" class="px-3 lg:px-4 py-1.5 lg:py-2 bg-white/20 backdrop-blur-sm hover:bg-white/30 rounded-lg text-white text-sm font-medium transition-all">
                        <i class="fas fa-plus mr-1"></i>充值
                    </a>
                    <?php if($conf['tixian']>=1){ ?>
                    <a href="./tixian.php" class="px-3 lg:px-4 py-1.5 lg:py-2 bg-white/20 backdrop-blur-sm hover:bg-white/30 rounded-lg text-white text-sm font-medium transition-all">
                        <i class="fas fa-wallet mr-1"></i>提现
                    </a>
                    <?php }
                    if($userrow['vip'] < $max_vip){
                        echo '<a href="./upgrade.php" class="px-3 lg:px-4 py-1.5 lg:py-2 bg-gradient-to-r from-orange-500 to-pink-500 hover:from-orange-600 hover:to-pink-600 rounded-lg text-white text-sm font-medium transition-all shadow-md"><i class="fas fa-arrow-up mr-1"></i>升级代理</a>';
                    } ?>
                </div>
            </div>
            <div class="grid grid-cols-3 gap-2 lg:gap-3 text-center">
                <div class="bg-white/10 backdrop-blur-sm rounded-lg p-2.5 lg:p-4 hover:bg-white/20 transition-all">
                    <div class="font-bold text-white text-base lg:text-lg" id="rmb-count"><?php echo $userrow['rmb']; ?></div>
                    <div class="text-xs lg:text-sm text-indigo-200 mt-0.5">余额(元)</div>
                </div>
                <div class="bg-white/10 backdrop-blur-sm rounded-lg p-2.5 lg:p-4 hover:bg-white/20 transition-all">
                    <div class="font-bold text-white text-base lg:text-lg" id="rmb-tc-count"><?php echo $userrow['rmb_tc']; ?></div>
                    <div class="text-xs lg:text-sm text-indigo-200 mt-0.5">佣金(元)</div>
                </div>
                <div class="bg-white/10 backdrop-blur-sm rounded-lg p-2.5 lg:p-4 hover:bg-white/20 transition-all">
                    <div class="font-bold text-white text-base lg:text-lg" id="qq-count"><?php echo $qq_unique_count; ?></div>
                    <div class="text-xs lg:text-sm text-indigo-200 mt-0.5">QQ数量</div>
                </div>
            </div>
        </div>
    </div>

    <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
        <div class="px-3 lg:px-4 py-2 lg:py-2.5 bg-gray-50 border-b border-gray-100">
            <h3 class="font-medium text-gray-800 text-sm lg:text-base">我的功能</h3>
        </div>
        <div class="p-3 lg:p-4">
            <div class="grid grid-cols-4 sm:grid-cols-6 gap-2 lg:gap-3">
                <?php if($conf_frame['open_msg']=='1'){ ?>
                <a href="./mass_msg.php" class="flex flex-col items-center justify-center p-2 lg:p-3 rounded-lg hover:bg-indigo-50 transition-colors border border-gray-100 hover:border-indigo-200 group">
                    <div class="w-8 h-8 lg:w-10 lg:h-10 bg-blue-100 rounded-lg flex items-center justify-center text-blue-600 group-hover:scale-110 transition-transform">
                        <i class="fas fa-comment-alt text-sm lg:text-base"></i>
                    </div>
                    <div class="mt-1 text-xs lg:text-sm text-gray-700">消息群发</div>
                </a>
                <?php } ?>
                <a href="./mail.php" class="flex flex-col items-center justify-center p-2 lg:p-3 rounded-lg hover:bg-green-50 transition-colors border border-gray-100 hover:border-green-200 group">
                    <div class="w-8 h-8 lg:w-10 lg:h-10 bg-green-100 rounded-lg flex items-center justify-center text-green-600 group-hover:scale-110 transition-transform">
                        <i class="fas fa-envelope text-sm lg:text-base"></i>
                    </div>
                    <div class="mt-1 text-xs lg:text-sm text-gray-700">邮件通知</div>
                </a>
                <a href="./message.php" class="flex flex-col items-center justify-center p-2 lg:p-3 rounded-lg hover:bg-purple-50 transition-colors border border-gray-100 hover:border-purple-200 group relative overflow-visible">
                    <div class="relative w-8 h-8 lg:w-10 lg:h-10 bg-purple-100 rounded-lg flex items-center justify-center text-purple-600 group-hover:scale-110 transition-transform overflow-visible">
                        <i class="fas fa-bell text-sm lg:text-base"></i>
                        <?php if($msg_count_num > 0){ ?><span class="absolute top-0 right-0 min-w-[16px] h-4 px-1 flex items-center justify-center bg-red-500 text-white text-[10px] font-bold rounded-full leading-none transform translate-x-1/2 -translate-y-1/2 z-10"><?php echo $msg_count_num > 99 ? '99+' : $msg_count_num; ?></span><?php } ?>
                    </div>
                    <div class="mt-1 text-xs lg:text-sm text-gray-700">消息通知</div>
                </a>
                <a href="./record.php" class="flex flex-col items-center justify-center p-2 lg:p-3 rounded-lg hover:bg-indigo-50 transition-colors border border-gray-100 hover:border-indigo-200 group">
                    <div class="w-8 h-8 lg:w-10 lg:h-10 bg-indigo-100 rounded-lg flex items-center justify-center text-indigo-600 group-hover:scale-110 transition-transform">
                        <i class="fas fa-history text-sm lg:text-base"></i>
                    </div>
                    <div class="mt-1 text-xs lg:text-sm text-gray-700">收支明细</div>
                </a>
                <?php if($conf['kami_open'] == 1){ ?>
                <a href="./kami.php" class="flex flex-col items-center justify-center p-2 lg:p-3 rounded-lg hover:bg-yellow-50 transition-colors border border-gray-100 hover:border-yellow-200 group">
                    <div class="w-8 h-8 lg:w-10 lg:h-10 bg-yellow-100 rounded-lg flex items-center justify-center text-yellow-600 group-hover:scale-110 transition-transform">
                        <i class="fas fa-key text-sm lg:text-base"></i>
                    </div>
                    <div class="mt-1 text-xs lg:text-sm text-gray-700">卡密管理</div>
                </a>
                <?php }
                if($conf['ssapi_open']=='1'){ ?>
                <a onclick="ssapi()" class="flex flex-col items-center justify-center p-2 lg:p-3 rounded-lg hover:bg-red-50 transition-colors border border-gray-100 hover:border-red-200 cursor-pointer group">
                    <div class="w-8 h-8 lg:w-10 lg:h-10 bg-red-100 rounded-lg flex items-center justify-center text-red-600 group-hover:scale-110 transition-transform">
                        <i class="fas fa-thumbs-up text-sm lg:text-base"></i>
                    </div>
                    <div class="mt-1 text-xs lg:text-sm text-gray-700">说说赞接口</div>
                </a>
                <?php }
                if($conf['open_invite']=='1' && $userrow['vip']>1) {
                    $invite_link = $hosturl.'/user/reg.php?invite='.$userrow['zid']; ?>
                <a href="javascript:void(0)" data-invite-link="<?php echo $invite_link; ?>" class="flex flex-col items-center justify-center p-2 lg:p-3 rounded-lg hover:bg-teal-50 transition-colors border border-gray-100 hover:border-teal-200 cursor-pointer group invite-link-btn">
                    <div class="w-8 h-8 lg:w-10 lg:h-10 bg-teal-100 rounded-lg flex items-center justify-center text-teal-600 group-hover:scale-110 transition-transform">
                        <i class="fas fa-link text-sm lg:text-base"></i>
                    </div>
                    <div class="mt-1 text-xs lg:text-sm text-gray-700">推广链接</div>
                </a>
                <?php } ?>
                <a href="./info.php" class="flex flex-col items-center justify-center p-2 lg:p-3 rounded-lg hover:bg-pink-50 transition-colors border border-gray-100 hover:border-pink-200 group">
                    <div class="w-8 h-8 lg:w-10 lg:h-10 bg-pink-100 rounded-lg flex items-center justify-center text-pink-600 group-hover:scale-110 transition-transform">
                        <i class="fas fa-user-cog text-sm lg:text-base"></i>
                    </div>
                    <div class="mt-1 text-xs lg:text-sm text-gray-700">个人信息</div>
                </a>
            </div>
        </div>
    </div>

    <?php if(intval($userrow['vip']) > 1){ ?>
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
        <div class="px-3 lg:px-4 py-2 lg:py-2.5 bg-gradient-to-r from-indigo-50 to-purple-50 border-b border-gray-100">
            <h3 class="font-medium text-gray-800 text-sm lg:text-base flex items-center gap-1.5">
                <i class="fas fa-star text-yellow-500 text-xs lg:text-sm"></i>高级功能
            </h3>
        </div>
        <div class="p-3 lg:p-4">
            <div class="grid grid-cols-4 sm:grid-cols-6 gap-2 lg:gap-3">
                <a href="./user.php" class="flex flex-col items-center justify-center p-2 lg:p-3 rounded-lg hover:bg-indigo-50 transition-colors border border-gray-100 hover:border-indigo-200 group">
                    <div class="w-8 h-8 lg:w-10 lg:h-10 bg-indigo-100 rounded-lg flex items-center justify-center text-indigo-600 group-hover:scale-110 transition-transform">
                        <i class="fas fa-users text-sm lg:text-base"></i>
                    </div>
                    <div class="mt-1 text-xs lg:text-sm text-gray-700">用户列表</div>
                </a>
                <a href="./fenzhan.php" class="flex flex-col items-center justify-center p-2 lg:p-3 rounded-lg hover:bg-green-50 transition-colors border border-gray-100 hover:border-green-200 group">
                    <div class="w-8 h-8 lg:w-10 lg:h-10 bg-green-100 rounded-lg flex items-center justify-center text-green-600 group-hover:scale-110 transition-transform">
                        <i class="fas fa-globe text-sm lg:text-base"></i>
                    </div>
                    <div class="mt-1 text-xs lg:text-sm text-gray-700">分站域名</div>
                </a>
                <?php if($conf['fenzhan_template']==1){ ?>
                <a href="./template.php" class="flex flex-col items-center justify-center p-2 lg:p-3 rounded-lg hover:bg-yellow-50 transition-colors border border-gray-100 hover:border-yellow-200 group">
                    <div class="w-8 h-8 lg:w-10 lg:h-10 bg-yellow-100 rounded-lg flex items-center justify-center text-yellow-600 group-hover:scale-110 transition-transform">
                        <i class="fas fa-paint-brush text-sm lg:text-base"></i>
                    </div>
                    <div class="mt-1 text-xs lg:text-sm text-gray-700">首页模板</div>
                </a>
                <?php } ?>
                <a href="./site.php" class="flex flex-col items-center justify-center p-2 lg:p-3 rounded-lg hover:bg-purple-50 transition-colors border border-gray-100 hover:border-purple-200 group">
                    <div class="w-8 h-8 lg:w-10 lg:h-10 bg-purple-100 rounded-lg flex items-center justify-center text-purple-600 group-hover:scale-110 transition-transform">
                        <i class="fas fa-cog text-sm lg:text-base"></i>
                    </div>
                    <div class="mt-1 text-xs lg:text-sm text-gray-700">网站配置</div>
                </a>
            </div>
        </div>
    </div>
    <?php } ?>
</div>

<div id="appModal" class="fixed inset-0 z-50 hidden">
    <div class="fixed inset-0 bg-black/50 transition-opacity" 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 relative transform transition-all">
            <div class="flex items-center justify-between px-5 py-4 border-b border-gray-100">
                <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-100 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 = '';
}

function ssapi(){
    showLoading();
    $.ajax({
        type: 'GET',
        url: 'ajax.php?act=ssapi',
        dataType: 'html',
        success: function(data) {
            hideLoading();
            openModal('说说赞接口', data, function() {
                return '<button onclick="closeModal()" class="w-full py-2 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg font-medium transition-colors">确定</button>';
            });
        },
        error: function(){
            hideLoading();
            showToast('服务器错误', 'error');
        }
    });
}

function adjustFontSize() {
    $('#rmb-count, #rmb-tc-count, #qq-count').each(function() {
        var $this = $(this);
        var length = $this.text().length;
        var fontSize = length <= 4 ? '1.25rem' : length <= 6 ? '1.1rem' : length <= 8 ? '0.95rem' : length <= 10 ? '0.8rem' : '0.65rem';
        $this.css('font-size', fontSize);
    });
}

$(document).ready(function() {
    adjustFontSize();
    $('.invite-link-btn').on('click', function() {
        var inviteLink = $(this).data('invite-link');
        openModal('推广链接', '用户在你的推广链接上注册账号成为你的下级<br>如果下级用户消费你就有提成<br><br><font color="red">' + inviteLink + '</font>', function() {
            return '<button onclick="closeModal()" class="w-full py-2 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg font-medium transition-colors">确定</button>';
        });
    });
});
</script>
