<?php
$title = '首页';
include __DIR__ . '/head.php';

if (isset($_GET['action']) && $_GET['action'] == 'check_qq_bind') {
    if (isset($_SESSION['quick_login_qq']) && isset($_SESSION['quick_login_time'])) {
        if (time() - $_SESSION['quick_login_time'] < 300) {
            $qq_openid = $_SESSION['quick_login_qq'];
            unset($_SESSION['quick_login_qq']);
            unset($_SESSION['quick_login_time']);
            
            $bind_user = $DB->getRow("SELECT user FROM pre_user WHERE openid_qq='{$qq_openid}' LIMIT 1");
            
            if ($bind_user && $bind_user['user'] == $user) {
                $msg = "此QQ已绑定到您的账号";
            } elseif ($bind_user) {
                $msg = "此QQ已绑定到其他用户，无法找回";
            } else {
                $res = $DB->exec("UPDATE pre_user SET openid_qq='{$qq_openid}' WHERE user='{$user}'");
                $msg = $res !== false ? "QQ绑定成功！" : "QQ绑定失败，请重试";
            }
        } else {
            $msg = "登录已过期，请重新登录";
            unset($_SESSION['quick_login_qq']);
            unset($_SESSION['quick_login_time']);
        }
    } else {
        $msg = "未找到登录信息，请重新登录";
    }
    echo "<script>alert('{$msg}');window.location.href='./index.php';</script>";
    exit;
}

$urow = $DB->getRow("SELECT gonggao, alert_gonggao FROM `pre_user` WHERE zid='{$userrow['upzid']}' LIMIT 1");
$app_download_url = trim($conf['app_download_url'] ?? '');
$app_download_name = trim($conf['app_download_name'] ?? '');
if ($app_download_name === '') $app_download_name = '下载App';
$has_app_download = $app_download_url !== '';
$quick_cols = 3;
if ($CLOUD_ENABLED || $DG_ENABLED) $quick_cols++;
if ($has_app_download) $quick_cols++;
$quick_grid_class = 'grid-cols-' . $quick_cols . ($quick_cols >= 5 ? ' gap-1.5' : ' gap-2');
$quick_item_pad = $quick_cols >= 5 ? 'p-1.5' : 'p-2.5';
$quick_icon_class = $quick_cols >= 5 ? 'w-8 h-8 lg:w-10 lg:h-10' : 'w-9 h-9 lg:w-10 lg:h-10';
$quick_text_class = $quick_cols >= 5 ? 'text-[10px] lg:text-sm whitespace-nowrap' : 'text-xs lg:text-sm';

// 版本名称
$version_name_cloud = $conf['version_name_cloud'] ?? '云端版';
$version_name_dg = $conf['version_name_dg'] ?? '代挂版';
$version_name_qzone = $conf['version_name_qzone'] ?? '秒赞版';

// 统计QQ数量
$cloud_qq_count = $CLOUD_ENABLED ? intval($DB->getColumn("SELECT COUNT(*) FROM pre_qq WHERE zid='{$userrow['zid']}'")) : 0;
$dg_qq_count = $DG_ENABLED ? intval($DB->getColumn("SELECT COUNT(*) FROM pre_dg_qq WHERE zid='{$userrow['zid']}'")) : 0;
$qzone_qq_count = 0;
$qzone_table = (defined('DBQZ') ? DBQZ : 'pre_') . 'qzone_qq';
$has_qzone_table = $DB->getColumn("SHOW TABLES LIKE '" . str_replace("'","\\'",$qzone_table) . "'");
if($has_qzone_table){
    $qzone_qq_count = intval($DB->getColumn("SELECT COUNT(*) FROM `{$qzone_table}` WHERE user_id='{$userrow['zid']}'"));
}

$enabled_versions = [];
if ($CLOUD_ENABLED) $enabled_versions[] = 'cloud';
if ($DG_ENABLED) $enabled_versions[] = 'dg';
if ($QZONE_ENABLED) $enabled_versions[] = 'qzone';
$default_version = $CLOUD_ENABLED ? 'cloud' : ($DG_ENABLED ? 'dg' : ($QZONE_ENABLED ? 'qzone' : 'cloud'));
if ($CLOUD_ENABLED && $cloud_qq_count > 0) {
    $default_version = 'cloud';
} elseif ($DG_ENABLED && $dg_qq_count > 0) {
    $default_version = 'dg';
} elseif ($QZONE_ENABLED && $qzone_qq_count > 0) {
    $default_version = 'qzone';
}
$quick_add_url = $CLOUD_ENABLED ? './add.php' : ($DG_ENABLED ? './add.php?biz=dg' : './add.php');
$quick_add_text = $CLOUD_ENABLED ? '添加QQ' : ($DG_ENABLED ? '添加代挂QQ' : '添加QQ');

// 获取全站在线统计（增加值 + 真实数据）
$cloud_add = isset($conf['stats_cloud_add']) ? intval($conf['stats_cloud_add']) : 0;
$dg_add = isset($conf['stats_dg_add']) ? intval($conf['stats_dg_add']) : 0;
$qzone_add = isset($conf['stats_qzone_add']) ? intval($conf['stats_qzone_add']) : 0;

$cloud_online_real = $CLOUD_ENABLED ? intval($DB->getColumn("SELECT COUNT(*) FROM pre_qq WHERE login_zt = '1'")) : 0;
$dg_online_real = $DG_ENABLED ? intval($DB->getColumn("SELECT COUNT(*) FROM pre_dg_qq WHERE login_zt = '1'")) : 0;
$qzone_valid_real = $QZONE_ENABLED ? intval($DB->getColumn("SELECT COUNT(*) FROM " . DBQZ . "qzone_qq WHERE login_status=0")) : 0;

$cloud_online = $cloud_online_real + $cloud_add;
$dg_online = $dg_online_real + $dg_add;
$qzone_valid = $qzone_valid_real + $qzone_add;
?>

<div class="space-y-3 lg:space-y-4">
    <div class="flex flex-col lg:flex-row gap-3">
        <div class="w-full lg:w-2/3 flex flex-col gap-3">
            <div class="relative rounded-xl overflow-hidden" style="min-height: 160px; aspect-ratio: 16/9;">
                <div id="carousel-container" class="absolute inset-0 overflow-hidden">
                    <div class="flex items-center justify-center h-full bg-gray-100">
                        <div class="w-8 h-8 border-3 border-indigo-200 border-t-indigo-600 rounded-full animate-spin"></div>
                    </div>
                </div>
                <div id="carousel-indicators" class="absolute bottom-2 left-1/2 transform -translate-x-1/2 flex gap-1.5"></div>
            </div>
            
            <!-- 在线统计展示 -->
            <div class="grid grid-cols-3 gap-2">
                <?php if($CLOUD_ENABLED): ?>
                <div class="bg-gradient-to-r from-teal-500 to-emerald-500 rounded-xl p-3 text-white shadow-sm">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-white/80 text-xs">云端在线</p>
                            <p class="text-xl font-bold mt-0.5"><?php echo number_format($cloud_online); ?></p>
                        </div>
                        <div class="w-8 h-8 bg-white/20 rounded-lg flex items-center justify-center">
                            <i class="fas fa-cloud text-sm"></i>
                        </div>
                    </div>
                </div>
                <?php endif; ?>
                <?php if($DG_ENABLED): ?>
                <div class="bg-gradient-to-r from-violet-500 to-purple-500 rounded-xl p-3 text-white shadow-sm">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-white/80 text-xs">代挂在线</p>
                            <p class="text-xl font-bold mt-0.5"><?php echo number_format($dg_online); ?></p>
                        </div>
                        <div class="w-8 h-8 bg-white/20 rounded-lg flex items-center justify-center">
                            <i class="fas fa-server text-sm"></i>
                        </div>
                    </div>
                </div>
                <?php endif; ?>
                <?php if($QZONE_ENABLED): ?>
                <div class="bg-gradient-to-r from-rose-500 to-pink-500 rounded-xl p-3 text-white shadow-sm">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-white/80 text-xs">秒赞有效</p>
                            <p class="text-xl font-bold mt-0.5"><?php echo number_format($qzone_valid); ?></p>
                        </div>
                        <div class="w-8 h-8 bg-white/20 rounded-lg flex items-center justify-center">
                            <i class="fas fa-heart text-sm"></i>
                        </div>
                    </div>
                </div>
                <?php endif; ?>
            </div>
            
            <div class="grid <?php echo $quick_grid_class; ?>">
                <a href="<?php echo $quick_add_url; ?>" class="group flex flex-col items-center justify-center <?php echo $quick_item_pad; ?> bg-white rounded-xl shadow-sm border border-gray-100 hover:shadow-md hover:border-blue-200 hover:bg-blue-50/50 transition-all">
                    <div class="<?php echo $quick_icon_class; ?> bg-gradient-to-br from-blue-500 to-blue-600 rounded-xl flex items-center justify-center mb-1.5 shadow-sm group-hover:scale-110 transition-transform">
                        <i class="fas fa-plus text-white text-xs lg:text-sm"></i>
                    </div>
                    <span class="<?php echo $quick_text_class; ?> font-medium text-gray-700 group-hover:text-blue-600"><?php echo $quick_add_text; ?></span>
                </a>
                <?php if ($CLOUD_ENABLED || $DG_ENABLED): ?>
                <a href="javascript:recoverQQ()" class="group flex flex-col items-center justify-center <?php echo $quick_item_pad; ?> bg-white rounded-xl shadow-sm border border-gray-100 hover:shadow-md hover:border-green-200 hover:bg-green-50/50 transition-all" id="findqqbtn">
                    <div class="<?php echo $quick_icon_class; ?> bg-gradient-to-br from-green-500 to-green-600 rounded-xl flex items-center justify-center mb-1.5 shadow-sm group-hover:scale-110 transition-transform">
                        <i class="fas fa-search text-white text-xs lg:text-sm"></i>
                    </div>
                    <span class="<?php echo $quick_text_class; ?> font-medium text-gray-700 group-hover:text-green-600">找回QQ</span>
                </a>
                <?php endif; ?>
                <a href="javascript:showNotice()" class="group flex flex-col items-center justify-center <?php echo $quick_item_pad; ?> bg-white rounded-xl shadow-sm border border-gray-100 hover:shadow-md hover:border-amber-200 hover:bg-amber-50/50 transition-all">
                    <div class="<?php echo $quick_icon_class; ?> bg-gradient-to-br from-amber-500 to-orange-500 rounded-xl flex items-center justify-center mb-1.5 shadow-sm group-hover:scale-110 transition-transform">
                        <i class="fas fa-bullhorn text-white text-xs lg:text-sm"></i>
                    </div>
                    <span class="<?php echo $quick_text_class; ?> font-medium text-gray-700 group-hover:text-amber-600">公告</span>
                </a>
                <a href="javascript:showKf()" class="group flex flex-col items-center justify-center <?php echo $quick_item_pad; ?> bg-white rounded-xl shadow-sm border border-gray-100 hover:shadow-md hover:border-purple-200 hover:bg-purple-50/50 transition-all">
                    <div class="<?php echo $quick_icon_class; ?> bg-gradient-to-br from-purple-500 to-purple-600 rounded-xl flex items-center justify-center mb-1.5 shadow-sm group-hover:scale-110 transition-transform">
                        <i class="fas fa-headset text-white text-xs lg:text-sm"></i>
                    </div>
                    <span class="<?php echo $quick_text_class; ?> font-medium text-gray-700 group-hover:text-purple-600">客服</span>
                </a>
                <?php if($has_app_download){ ?>
                <a href="<?php echo htmlspecialchars($app_download_url); ?>" target="_blank" rel="noopener noreferrer" class="group flex flex-col items-center justify-center <?php echo $quick_item_pad; ?> bg-white rounded-xl shadow-sm border border-gray-100 hover:shadow-md hover:border-cyan-200 hover:bg-cyan-50/50 transition-all">
                    <div class="<?php echo $quick_icon_class; ?> bg-gradient-to-br from-cyan-500 to-blue-500 rounded-xl flex items-center justify-center mb-1.5 shadow-sm group-hover:scale-110 transition-transform">
                        <i class="fas fa-download text-white text-xs lg:text-sm"></i>
                    </div>
                    <span class="<?php echo $quick_text_class; ?> font-medium text-gray-700 group-hover:text-cyan-600"><?php echo htmlspecialchars($app_download_name); ?></span>
                </a>
                <?php } ?>
            </div>
        </div>
        
        <div class="w-full lg:w-1/3 bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
            <div class="px-3 lg:px-4 py-2.5 border-b border-gray-100 flex items-center justify-between bg-gray-50/50">
                <div class="flex items-center gap-2">
                    <i class="fas fa-bell text-indigo-500 text-sm"></i>
                    <h3 class="font-medium text-gray-800 text-sm lg:text-base">站点通知</h3>
                    <span class="px-1.5 py-0.5 bg-red-100 text-red-600 rounded-full text-xs font-medium" id="unreadCount">0</span>
                </div>
                <a href="./message.php" class="text-xs text-indigo-500 hover:text-indigo-700 transition-colors">全部</a>
            </div>
            <div id="notification-list" class="divide-y divide-gray-50 max-h-56 overflow-y-auto"></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.5 border-b border-gray-100 flex flex-col gap-2 bg-gray-50/50 sm:flex-row sm:items-center sm:justify-between sm:gap-0">
            <div class="flex items-center gap-2">
                <i class="fas fa-robot text-indigo-500 text-sm"></i>
                <h3 class="font-medium text-gray-800 text-sm lg:text-base">我的QQ</h3>
            </div>
            <div class="w-full flex justify-center sm:w-auto sm:justify-end sm:items-center sm:gap-2">
                <div class="w-full flex justify-center sm:w-auto">
                    <?php if(count($enabled_versions) > 1): ?>
                    <div class="flex flex-wrap gap-1 bg-gray-100 p-0.5 rounded-lg max-w-full">
                        <?php if($CLOUD_ENABLED): ?>
                        <button type="button" id="btn-cloud" onclick="switchVersion('cloud')" class="text-xs px-2.5 py-1 rounded-md transition-all bg-indigo-600 text-white whitespace-nowrap">
                            <?php echo htmlspecialchars($version_name_cloud); ?> <span class="opacity-75">(<?php echo $cloud_qq_count; ?>)</span>
                        </button>
                        <?php endif; ?>
                        <?php if($DG_ENABLED): ?>
                        <button type="button" id="btn-dg" onclick="switchVersion('dg')" class="text-xs px-2.5 py-1 rounded-md transition-all text-gray-600 hover:bg-gray-200 whitespace-nowrap">
                            <?php echo htmlspecialchars($version_name_dg); ?> <span class="opacity-75">(<?php echo $dg_qq_count; ?>)</span>
                        </button>
                        <?php endif; ?>
                        <?php if($QZONE_ENABLED): ?>
                        <button type="button" id="btn-qzone" onclick="switchVersion('qzone')" class="text-xs px-2.5 py-1 rounded-md transition-all text-gray-600 hover:bg-gray-200 whitespace-nowrap">
                            <?php echo htmlspecialchars($version_name_qzone); ?> <span class="opacity-75">(<?php echo $qzone_qq_count; ?>)</span>
                        </button>
                        <?php endif; ?>
                    </div>
                    <?php elseif($CLOUD_ENABLED): ?>
                    <span class="text-xs text-indigo-600 bg-indigo-50 px-2.5 py-1 rounded-lg font-medium"><?php echo htmlspecialchars($version_name_cloud); ?></span>
                    <?php elseif($DG_ENABLED): ?>
                    <span class="text-xs text-indigo-600 bg-indigo-50 px-2.5 py-1 rounded-lg font-medium"><?php echo htmlspecialchars($version_name_dg); ?></span>
                    <?php elseif($QZONE_ENABLED): ?>
                    <span class="text-xs text-indigo-600 bg-indigo-50 px-2.5 py-1 rounded-lg font-medium"><?php echo htmlspecialchars($version_name_qzone); ?></span>
                    <?php endif; ?>
                </div>
                <span class="hidden sm:inline text-xs lg:text-sm text-gray-500">共 <span id="total" class="font-medium text-indigo-600">0</span> 个</span>
            </div>
        </div>
        <div id="list" class="divide-y divide-gray-50"></div>
        <div class="px-3 lg:px-4 py-2.5 border-t border-gray-100 flex items-center justify-center gap-4 bg-gray-50/30">
            <button onclick="prevPage()" class="flex items-center gap-1 px-3 py-1.5 bg-white hover:bg-indigo-50 border border-gray-200 hover:border-indigo-300 rounded-lg text-xs font-medium text-gray-600 hover:text-indigo-600 transition-all">
                <i class="fas fa-chevron-left text-xs"></i>上一页
            </button>
            <span class="text-xs text-gray-500">第 <span id="current-page" class="font-medium text-gray-700">1</span> 页</span>
            <button onclick="nextPage()" class="flex items-center gap-1 px-3 py-1.5 bg-white hover:bg-indigo-50 border border-gray-200 hover:border-indigo-300 rounded-lg text-xs font-medium text-gray-600 hover:text-indigo-600 transition-all">
                下一页<i class="fas fa-chevron-right text-xs"></i>
            </button>
        </div>
    </div>
</div>

<script>
var noticeContent = <?php echo json_encode(html_entity_decode($conf['anounce'] ?? '')); ?>;
var noticeUserContent = <?php echo json_encode(html_entity_decode($conf['anounce_user'] ?? '')); ?>;
var noticeUpContent = <?php echo json_encode(html_entity_decode($urow['gonggao'] ?? '')); ?>;
var alertContentAll = <?php echo json_encode(html_entity_decode($conf['anounce_alert_all'] ?? '')); ?>;
var alertContent = <?php echo json_encode(html_entity_decode($conf['anounce_alert'] ?? '')); ?>;
var alertGonggao = <?php echo json_encode(html_entity_decode($urow['alert_gonggao'] ?? '')); ?>;
var alertFreq = '<?php echo $conf['anounce_alert_freq'] ?? '0'; ?>';
var kfQQ = '<?php echo $conf['qq'] ?? ''; ?>';
var kfWX = '<?php echo $conf['wx'] ?? ''; ?>';
window.INDEX_DEFAULT_VERSION = <?php echo json_encode($default_version, JSON_UNESCAPED_UNICODE); ?>;
window.INDEX_CLOUD_ENABLED = <?php echo $CLOUD_ENABLED ? 'true' : 'false'; ?>;
window.INDEX_DG_ENABLED = <?php echo $DG_ENABLED ? 'true' : 'false'; ?>;
window.VERSION_NAME_CLOUD = <?php echo json_encode($version_name_cloud, JSON_UNESCAPED_UNICODE); ?>;
window.VERSION_NAME_DG = <?php echo json_encode($version_name_dg, JSON_UNESCAPED_UNICODE); ?>;
</script>

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

<div id="appModal" class="fixed inset-0 z-50 hidden">
    <div class="fixed inset-0 bg-black/50 backdrop-blur-sm 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 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 src="./assets/js/index.js?ver=<?php echo VERSION ?>"></script>
