<?php
/**
 * 字体安装辅助工具
 * 用于检查和安装支持多语言的字体
 */

require __DIR__ . '/../../app/bootstrap.php';
require_login();

header('Content-Type: text/html; charset=utf-8');

$adminName = isset($_SESSION['admin_name']) ? (string)$_SESSION['admin_name'] : 'Admin';
$adminUsername = isset($_SESSION['admin_username']) ? (string)$_SESSION['admin_username'] : 'admin';
$avatarText = strtoupper(substr($adminName !== '' ? $adminName : 'A', 0, 1));

$fontStatus = array();
$recommendations = array();

// 只检查项目内的字体（避免 open_basedir 限制）
$notoProjectPath = __DIR__ . '/../../assets/fonts/NotoSansCJK-Regular.ttc';
$msyhProjectPath = __DIR__ . '/../../assets/fonts/msyh.ttc';
$msyhProjectPath2 = __DIR__ . '/../../assets/fonts/msyh.ttf';
$simheiProjectPath = __DIR__ . '/../../assets/fonts/simhei.ttf';
$simsunProjectPath = __DIR__ . '/../../assets/fonts/simsun.ttc';

$fontStatus['Noto Sans CJK'] = array(
    'path' => 'assets/fonts/NotoSansCJK-Regular.ttc',
    'exists' => is_file($notoProjectPath),
    'languages' => array('中文', '日文', '韩文（推荐）'),
    'priority' => 'high',
    'description' => 'Google 开源字体，完美支持中日韩'
);

$fontStatus['Microsoft YaHei'] = array(
    'path' => 'assets/fonts/msyh.ttc 或 msyh.ttf',
    'exists' => is_file($msyhProjectPath) || is_file($msyhProjectPath2),
    'languages' => array('中文', '日文', '韩文（支持不佳）'),
    'priority' => 'medium',
    'description' => '系统通常已预装，可选择复制到项目'
);

$fontStatus['SimHei'] = array(
    'path' => 'assets/fonts/simhei.ttf',
    'exists' => is_file($simheiProjectPath),
    'languages' => array('中文'),
    'priority' => 'low',
    'description' => '黑体，仅支持中文'
);

$fontStatus['SimSun'] = array(
    'path' => 'assets/fonts/simsun.ttc',
    'exists' => is_file($simsunProjectPath),
    'languages' => array('中文'),
    'priority' => 'low',
    'description' => '宋体，仅支持中文'
);

// 生成建议
if (!$fontStatus['Noto Sans CJK']['exists']) {
    $recommendations[] = array(
        'level' => 'warning',
        'title' => '缺少 Noto Sans CJK 字体',
        'message' => '建议安装 Noto Sans CJK 以获得最佳的多语言支持，特别是韩文和日文。',
        'action' => '下载并放到 assets/fonts/ 目录'
    );
}

if (!$fontStatus['Microsoft YaHei']['exists']) {
    $recommendations[] = array(
        'level' => 'info',
        'title' => '项目目录中未找到 Microsoft YaHei',
        'message' => '系统会自动使用 Windows 系统字体目录中的微软雅黑（如果存在）。如需在项目中包含，可复制字体文件到 assets/fonts/。',
        'action' => '可选：复制 msyh.ttc 到 assets/fonts/'
    );
}
?><!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>字体管理工具</title>
    <link rel="stylesheet" href="/assets/css/admin-ui.css">
    <style>
    .font-status-grid { display: grid; gap: 16px; }
    .font-item { padding: 16px; border: 1px solid rgba(148,163,184,0.16); border-radius: 12px; background: #f8fafc; }
    .font-item.exists { border-color: rgba(34,197,94,0.2); background: rgba(34,197,94,0.04); }
    .font-item.missing { border-color: rgba(220,38,38,0.2); background: rgba(220,38,38,0.04); }
    .font-name { font-weight: 600; font-size: 15px; color: #0f172a; margin-bottom: 8px; }
    .font-path { font-size: 13px; color: #64748b; margin: 4px 0; font-family: monospace; }
    .font-languages { font-size: 13px; color: #475569; margin: 8px 0; }
    .font-status { display: inline-block; padding: 2px 8px; border-radius: 4px; font-size: 12px; font-weight: 600; }
    .status-ok { background: rgba(34,197,94,0.1); color: #15803d; }
    .status-missing { background: rgba(220,38,38,0.1); color: #b91c1c; }
    .recommendation { padding: 16px; border-radius: 12px; margin-bottom: 12px; }
    .rec-warning { border-left: 4px solid #f59e0b; background: rgba(245,158,11,0.05); }
    .rec-error { border-left: 4px solid #ef4444; background: rgba(239,68,68,0.05); }
    .rec-info { border-left: 4px solid #3b82f6; background: rgba(59,130,246,0.05); }
    .rec-title { font-weight: 600; color: #0f172a; margin-bottom: 4px; }
    .rec-message { font-size: 14px; color: #475569; margin-bottom: 8px; }
    .rec-action { font-size: 13px; color: #64748b; font-style: italic; }
    .font-item .font-description { font-size: 12px; color: #94a3b8; margin-top: 6px; }
    </style>
</head>
<body>
<div class="admin-shell">
    <?php require __DIR__ . '/../../app/admin-sidebar.php'; render_admin_sidebar('tools', $adminName, $adminUsername, $avatarText); ?>
    <main class="admin-content">
        <div class="content-wrap">
            <header class="topbar">
                <div><h1>字体管理工具</h1><p>检查和管理标题图生成所需的字体。</p></div>
            </header>

            <?php if (!empty($recommendations)): ?>
            <section class="page-card" style="margin-bottom: 24px;">
                <strong style="display: block; margin-bottom: 16px; font-size: 15px;">⚠️ 建议</strong>
                <?php foreach ($recommendations as $rec): ?>
                <div class="recommendation rec-<?php echo e($rec['level']); ?>">
                    <div class="rec-title"><?php echo e($rec['title']); ?></div>
                    <div class="rec-message"><?php echo e($rec['message']); ?></div>
                    <div class="rec-action">→ <?php echo e($rec['action']); ?></div>
                </div>
                <?php endforeach; ?>
            </section>
            <?php endif; ?>

            <section class="page-card">
                <strong style="display: block; margin-bottom: 16px; font-size: 15px;">字体检查结果</strong>
                <div class="font-status-grid">
                    <?php foreach ($fontStatus as $name => $info): ?>
                    <div class="font-item <?php echo $info['exists'] ? 'exists' : 'missing'; ?>">
                        <div class="font-name">
                            <?php echo e($name); ?>
                            <span class="font-status <?php echo $info['exists'] ? 'status-ok' : 'status-missing'; ?>">
                                <?php echo $info['exists'] ? '✓ 已安装' : '✗ 未安装'; ?>
                            </span>
                        </div>
                        <div class="font-path"><?php echo e($info['path']); ?></div>
                        <div class="font-languages">
                            <strong>支持语言：</strong>
                            <?php echo implode('、', $info['languages']); ?>
                        </div>
                        <div class="font-description"><?php echo e($info['description']); ?></div>
                        <div style="margin-top: 8px; font-size: 12px; color: #94a3b8;">
                            优先级：
                            <?php if ($info['priority'] === 'high'): ?>
                                <span style="color: #ef4444;">高</span>
                            <?php elseif ($info['priority'] === 'medium'): ?>
                                <span style="color: #f59e0b;">中</span>
                            <?php else: ?>
                                <span style="color: #94a3b8;">低</span>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
            </section>

            <section class="page-card" style="margin-top: 24px;">
                <strong style="display: block; margin-bottom: 16px; font-size: 15px;">安装指南</strong>
                <div style="line-height: 1.8; color: #475569;">
                    <h3 style="margin-top: 16px; margin-bottom: 8px; color: #0f172a;">推荐：安装 Noto Sans CJK</h3>
                    <p>Noto Sans CJK 是 Google 开发的开源字体，完美支持中文、日文、韩文。</p>
                    
                    <h4 style="margin-top: 12px; margin-bottom: 6px; color: #334155;">Windows</h4>
                    <ol style="margin-left: 20px;">
                        <li>访问 <a href="https://fonts.google.com/noto/specimen/Noto+Sans+CJK+JP" target="_blank" style="color: #4f46e5;">Google Fonts</a></li>
                        <li>下载 NotoSansCJK-Regular.ttc</li>
                        <li>放到 <code style="background: #f1f5f9; padding: 2px 6px; border-radius: 4px;">C:\Windows\Fonts\</code></li>
                        <li>重启 PHP 服务</li>
                    </ol>

                    <h4 style="margin-top: 12px; margin-bottom: 6px; color: #334155;">Linux</h4>
                    <pre style="background: #f1f5f9; padding: 12px; border-radius: 8px; overflow-x: auto; font-size: 13px;">sudo apt-get install fonts-noto-cjk
sudo fc-cache -fv
sudo systemctl restart php-fpm</pre>

                    <h4 style="margin-top: 12px; margin-bottom: 6px; color: #334155;">macOS</h4>
                    <pre style="background: #f1f5f9; padding: 12px; border-radius: 8px; overflow-x: auto; font-size: 13px;">brew install --cask font-noto-sans-cjk</pre>
                </div>
            </section>

            <section class="page-card" style="margin-top: 24px;">
                <strong style="display: block; margin-bottom: 16px; font-size: 15px;">常见问题</strong>
                <div style="line-height: 1.8; color: #475569;">
                    <h4 style="margin-top: 12px; margin-bottom: 6px; color: #334155;">Q: 为什么韩文标题图显示乱码？</h4>
                    <p>A: 微软雅黑虽然包含韩文字符，但 GD 库对韩文的渲染支持有限。建议安装 Noto Sans CJK 字体。</p>

                    <h4 style="margin-top: 12px; margin-bottom: 6px; color: #334155;">Q: 安装字体后仍然不生效？</h4>
                    <p>A: 需要重启 PHP 服务或 Web 服务器。在 Linux 上，还需要运行 <code style="background: #f1f5f9; padding: 2px 6px; border-radius: 4px;">sudo fc-cache -fv</code> 刷新字体缓存。</p>

                    <h4 style="margin-top: 12px; margin-bottom: 6px; color: #334155;">Q: 可以把字体放在项目目录吗？</h4>
                    <p>A: 可以。将字体放到 <code style="background: #f1f5f9; padding: 2px 6px; border-radius: 4px;">assets/fonts/</code> 目录，系统会自动检测。</p>
                </div>
            </section>
        </div>
    </main>
</div>
</body>
</html>
