<?php
include __DIR__ . '/auth.php';
admin_check();
include __DIR__ . '/layout.php';

admin_header('行业主题');

$registry = admin_site_theme_registry();
$profileFile = __DIR__ . '/../data/theme_profiles.php';
$assetFile = __DIR__ . '/../data/theme_assets.php';
$contentFile = __DIR__ . '/../data/content.php';
$productFile = __DIR__ . '/../data/products.php';
$caseFile = __DIR__ . '/../data/cases.php';
$faqFile = __DIR__ . '/../data/faq.php';

$profiles = is_file($profileFile) ? include $profileFile : array();
$assets = is_file($assetFile) ? include $assetFile : array();
$content = is_file($contentFile) ? include $contentFile : array();
$products = is_file($productFile) ? include $productFile : array();
$cases = is_file($caseFile) ? include $caseFile : array();
$faqs = is_file($faqFile) ? include $faqFile : array();

if(!is_array($profiles)) $profiles = array();
if(!is_array($assets)) $assets = array();
if(!is_array($content)) $content = array();
if(!is_array($products)) $products = array();
if(!is_array($cases)) $cases = array();
if(!is_array($faqs)) $faqs = array();

function theme_admin_count($arr, $key){
    return isset($arr[$key]) && is_array($arr[$key]) ? count($arr[$key]) : 0;
}

function theme_admin_dir_status($key){
    $base = __DIR__ . '/../themes/' . $key;
    return array(
        'theme' => is_dir($base),
        'css' => is_file($base . '/assets/css/theme.css'),
        'views' => is_dir($base . '/views'),
    );
}
?>

<div class="card">
    <h2>固定行业主题说明</h2>
    <p class="help">当前项目采用固定行业主题模式。后台不再提供“新增主题”入口，避免生成第 13 个主题后造成数据、素材和模板维护成本上升。</p>
    <p class="help">日常运营只需要在“站群管理”里为域名选择已有主题；主题代码、素材和默认数据由开发侧维护。</p>
</div>

<div class="card">
    <h2>推荐维护方式</h2>
    <div class="mvc-checklist">
        <div class="mini-card"><strong>主题选择</strong><span>在站群管理中绑定固定主题，不在后台新增主题。</span></div>
        <div class="mini-card"><strong>主题样式</strong><span>修改 <code>themes/{theme}/assets/css/theme.css</code>。</span></div>
        <div class="mini-card"><strong>主题视图</strong><span>只在需要差异化时覆盖 <code>themes/{theme}/views/</code>。</span></div>
        <div class="mini-card"><strong>默认数据</strong><span>维护 <code>data/theme_profiles.php</code>、<code>data/content.php</code> 等固定数据文件。</span></div>
        <div class="mini-card"><strong>内容发布</strong><span>新闻、产品、案例、FAQ 继续走内容中心。</span></div>
        <div class="mini-card"><strong>扩展主题</strong><span>如确实要新增行业，建议作为版本升级处理，而不是后台临时创建。</span></div>
    </div>
</div>

<div class="card">
    <h2>已固定行业主题</h2>
    <table>
        <tr>
            <th>主题标识</th>
            <th>主题名称</th>
            <th>版本</th>
            <th>目录</th>
            <th>资料数</th>
            <th>产品</th>
            <th>案例</th>
            <th>FAQ</th>
            <th>主题文件</th>
        </tr>
        <?php foreach($registry as $key=>$item): $status = theme_admin_dir_status($key); ?>
        <tr>
            <td><code><?php echo admin_h($key); ?></code></td>
            <td><?php echo admin_h($item['name'] ?? ($item['industry_name'] ?? $key)); ?></td>
            <td><?php echo admin_h($item['version'] ?? '1.0'); ?></td>
            <td><code><?php echo admin_h($item['dir'] ?? ('themes/' . $key)); ?></code></td>
            <td><?php echo intval(theme_admin_count($profiles, $key)); ?></td>
            <td><?php echo intval(theme_admin_count($products, $key)); ?></td>
            <td><?php echo intval(theme_admin_count($cases, $key)); ?></td>
            <td><?php echo intval(theme_admin_count($faqs, $key)); ?></td>
            <td>
                <?php echo $status['theme'] ? '目录正常' : '目录缺失'; ?> /
                <?php echo $status['css'] ? 'CSS正常' : 'CSS缺失'; ?> /
                <?php echo $status['views'] ? '视图目录有' : '走公共视图'; ?>
            </td>
        </tr>
        <?php endforeach; ?>
    </table>
</div>

<?php admin_footer(); ?>
