<?php

/***
//终端参数				$WEB_PC_MO
//PC模版				$WebMobanPC
//手机模版				$WebMobanWAP
//网站标题				$WebTitle
//网站关键字			$WebKeywords
//网站描述				$WebDescription
//网站logo链接			$WebLogo
//网站邮箱				$WebEmail
//网站统计				$WebCnzz
//友链数组				$AdminIeUrl
//头部横幅广告数组		$AdminTop
//播放横幅广告数组		$AdminVideo
//对联广告数组			$AdminCouplets
//MO底部浮漂广告数组    $AdminFloat
//联盟JS广告			$AdminAdJs
***/
include_once './Php/Home/Template.php';
$tpl = new Template();
$tpl->assign('WEBUrl', $WEBUrl);
//终端参数				$WEB_PC_MO
$tpl->assign('终端', $WEB_PC_MO);
//PC模版				$WebMobanPC
$tpl->assign('PC模版', $WebMobanPC);
//手机模版				$WebMobanWAP
$tpl->assign('手机模版', $WebMobanWAP);
//网站标题				$WebTitle
$tpl->assign('WebTitle', $WebTitle);
//网站关键字			$WebKeywords
$tpl->assign('WebKeywords', $WebKeywords);
//网站描述				$WebDescription
$tpl->assign('WebDescription', $WebDescription);
//网站logo链接			$WebLogo
$tpl->assign('WebLogo', $WebLogo);
//网站邮箱				$WebEmail
$tpl->assign('WebEmail', $WebEmail);
//友链数组				$AdminIeUrl
$tpl->assign('IeUrl', $AdminIeUrl);
//头部横幅广告数组		$AdminTop
$tpl->assign('TopWebAd', $AdminTop);
//播放横幅广告数组		$AdminVideo
$tpl->assign('VideoWebAd', $AdminVideo);
//对联广告，移动底部悬浮广告，统计代码

define('JCCMS',$_SERVER['DOCUMENT_ROOT']);

$AdS=$AdS.$WebCnzz;
$tpl->assign('WebAdS', $AdS);

//随机数字
$tpl->assign('Rand',rand(5, 10000));
//样式路径
$WEB_Static=NULL; if($WEB_PC_MO =='PC'){$WEB_Static=$WebMobanPC;}if($WEB_PC_MO =='MO'){$WEB_Static=$WebMobanWAP;}
$tpl->assign('StylePath','/Template/'.$WEB_Static);
$tpl->assign('Header',$WEB_Static.'/html/header');
$tpl->assign('Footer',$WEB_Static.'/html/footer');

//分类
$vodtypeRaw = json_decode(file_get_contents('./PPQSQL/type.php'), true);
$vodtype = array();
foreach ((array)$vodtypeRaw as $typeRow) {
    $typeId = isset($typeRow['id']) ? (int)$typeRow['id'] : 0;
    if ($typeId > 0) $vodtype[$typeId] = $typeRow;
}
$tpl->assign('数据分类', $vodtype);

// 首页展示始终按当前已绑定的视频分类运行，不依赖资源站固定 ID。
$adminBasic = @json_decode(@file_get_contents('./PPQSQL/Admin/Basic/AdminBasic.php'), true);
if (!is_array($adminBasic)) $adminBasic = array();
if (is_file('./PPQSQL/collect_lib.php')) require_once './PPQSQL/collect_lib.php';

// 分类条数：原来要挨个把每个数据文件全部解析一遍，仅为数一下有多少条。
// 数据少时无所谓，每类上万条后访客每打开一次首页就要解析上百 MB。
// 改为读采集时写好的账本（tmp/type_counts.json），
// 账本缺哪个分类再单独回退读文件，保证数字不会算错。
$typeCounts = array();
if (function_exists('ppq_read_type_counts')) {
    $typeCounts = ppq_read_type_counts(array_keys($vodtype));
} else {
    foreach ($vodtype as $typeId => $typeRow) {
        $items = @json_decode(@file_get_contents('./PPQSQL/Home/' . $typeId . '.txt'), true);
        $typeCounts[$typeId] = is_array($items) ? count($items) : 0;
    }
}
$manualNames = isset($adminBasic['HomeModuleNames']) && is_array($adminBasic['HomeModuleNames'])
    ? $adminBasic['HomeModuleNames'] : array();
if (is_file('./PPQSQL/collect_lib.php')) require_once './PPQSQL/collect_lib.php';
if (function_exists('ppq_home_module_slots')) {
    $validSlots = ppq_home_module_slots($manualNames, $vodtype, $typeCounts);
} else {
    $validSlots = array();
    foreach ($vodtype as $typeId => $unused) {
        if (!empty($typeCounts[$typeId])) $validSlots[] = (int)$typeId;
        if (count($validSlots) >= 6) break;
    }
}
$tpl->assign('首页模块', $validSlots);
$homeCardLimit = isset($adminBasic['HomeModuleLimit']) ? (int)$adminBasic['HomeModuleLimit'] : 16;
$tpl->assign('首页每类条数', max(10, min(20, $homeCardLimit)));
$listPageSize = isset($_GET['size']) ? (int)$_GET['size'] : 30;
if (!in_array($listPageSize, array(30, 35, 40, 45, 50, 60), true)) $listPageSize = 30;
$tpl->assign('分类每页条数', $listPageSize);

// 由公共控制器附加公告，所有模板共用同一份配置而不改动模板自身布局。
if (!function_exists('ppq_notice_html')) {
    function ppq_notice_html($config, $pageName) {
        if (empty($config['PopupNoticeOpen'])) return '';
        $scope = isset($config['PopupNoticeScope']) ? $config['PopupNoticeScope'] : 'home';
        if ($scope === 'home' && $pageName !== 'index') return '';
        $title = isset($config['PopupNoticeTitle']) ? trim($config['PopupNoticeTitle']) : '';
        $content = isset($config['PopupNoticeContent']) ? trim($config['PopupNoticeContent']) : '';
        if ($title === '' && $content === '') return '';
        $frequency = isset($config['PopupNoticeFrequency']) ? $config['PopupNoticeFrequency'] : 'session';
        if (!in_array($frequency, array('visit', 'session', 'day'), true)) $frequency = 'session';
        $autoClose = isset($config['PopupNoticeAutoClose']) ? max(0, min(3600, (int)$config['PopupNoticeAutoClose'])) : 0;
        $h = function($value) { return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); };
        $buttons = array();
        for ($i = 1; $i <= 2; $i++) {
            $name = isset($config['PopupNoticeButton' . $i . 'Name']) ? trim($config['PopupNoticeButton' . $i . 'Name']) : '';
            $url = isset($config['PopupNoticeButton' . $i . 'Url']) ? trim($config['PopupNoticeButton' . $i . 'Url']) : '';
            if ($name !== '' && preg_match('#^https?://#i', $url)) $buttons[] = '<a class="ppq-notice-link" href="' . $h($url) . '" target="_blank" rel="noopener noreferrer">' . $h($name) . '</a>';
        }
        $key = 'ppq-notice-' . md5($title . "\n" . $content);
        $payload = json_encode(array('key' => $key, 'frequency' => $frequency, 'autoClose' => $autoClose));
        return '<div id="ppq-notice" class="ppq-notice" aria-hidden="true"><div class="ppq-notice-card" role="dialog" aria-modal="true"><button type="button" class="ppq-notice-close" aria-label="关闭">×</button><h2>' . $h($title !== '' ? $title : '公告') . '</h2><div class="ppq-notice-content">' . nl2br($h($content)) . '</div>' . (!empty($buttons) ? '<div class="ppq-notice-actions">' . implode('', $buttons) . '</div>' : '') . '<button type="button" class="ppq-notice-confirm">我知道了</button></div></div><style>.ppq-notice{position:fixed;z-index:2147483000;inset:0;display:none;align-items:center;justify-content:center;padding:20px;background:rgba(15,23,42,.58)}.ppq-notice.show{display:flex}.ppq-notice-card{position:relative;width:min(560px,100%);padding:30px;border-radius:18px;background:#fff;box-shadow:0 24px 72px rgba(0,0,0,.3);color:#21314d}.ppq-notice-card h2{margin:0 36px 14px 0;font-size:24px}.ppq-notice-content{line-height:1.8;color:#53627b}.ppq-notice-actions{display:flex;gap:10px;flex-wrap:wrap;margin:22px 0 0}.ppq-notice-link,.ppq-notice-confirm{display:inline-flex;align-items:center;justify-content:center;min-height:42px;padding:0 18px;border:0;border-radius:9px;background:#439a85;color:#fff;text-decoration:none;font:inherit;cursor:pointer}.ppq-notice-confirm{margin-top:22px;background:#243652}.ppq-notice-close{position:absolute;right:14px;top:12px;border:0;background:transparent;color:#69758a;font-size:28px;cursor:pointer}@media(max-width:600px){.ppq-notice-card{padding:24px}.ppq-notice-card h2{font-size:21px}}</style><script>(function(c){var el=document.getElementById("ppq-notice");if(!el)return;var seen=false;try{if(c.frequency==="session")seen=sessionStorage.getItem(c.key)==="1";if(c.frequency==="day")seen=localStorage.getItem(c.key)===new Date().toDateString()}catch(e){}if(seen)return;el.className+=" show";el.setAttribute("aria-hidden","false");var close=function(){el.className="ppq-notice";el.setAttribute("aria-hidden","true");try{if(c.frequency==="session")sessionStorage.setItem(c.key,"1");if(c.frequency==="day")localStorage.setItem(c.key,new Date().toDateString())}catch(e){}};el.querySelector(".ppq-notice-close").onclick=close;el.querySelector(".ppq-notice-confirm").onclick=close;el.onclick=function(e){if(e.target===el)close()};if(c.autoClose>0)setTimeout(close,c.autoClose*1000)})(' . $payload . ');</script>';
    }
}
if (!empty($adminBasic['PopupNoticeOpen'])) {
    $noticeConfig = $adminBasic;
    $noticePage = isset($GetMb_tmp) ? $GetMb_tmp : 'index';
    register_shutdown_function(function() use ($noticeConfig, $noticePage) { echo ppq_notice_html($noticeConfig, $noticePage); });
}

$this_WebMoban =NULL;
if($WEB_PC_MO =='PC'){$this_WebMoban=$WebMobanPC;}
if($WEB_PC_MO =='MO'){$this_WebMoban=$WebMobanWAP;}	
//首页php


// === PPQCMS SEO: 给所有页面注入 canonical / OG / JSON-LD ===
if (!function_exists('ppq_seo_head')) { require_once __DIR__ . '/Seo.php'; }
$_ppqSeoType = ($GetMb_tmp === 'index') ? 'home' : $GetMb_tmp;
$_ppqSeoTitle = ($GetMb_tmp === 'index')
    ? (!empty($adminBasic['SeoHomeTitle']) ? $adminBasic['SeoHomeTitle'] : $WebTitle)
    : $WebTitle;
$_ppqSeoDesc = ($GetMb_tmp === 'index')
    ? (!empty($adminBasic['SeoHomeDesc']) ? $adminBasic['SeoHomeDesc'] : $WebDescription)
    : $WebDescription;
$_ppqSeoUrl = ppq_canonical_url($_ppqSeoType, $GetMb_id, $GetMb_page);
$tpl->assign('SeoTitle', $_ppqSeoTitle);
$tpl->assign('SeoExtra', ppq_seo_head($_ppqSeoType, array(
    'title' => $_ppqSeoTitle,
    'description' => $_ppqSeoDesc,
    'url' => $_ppqSeoUrl,
)));
include('./Php/Home/index/'.$GetMb_tmp.'.php');
?>
