<?php
require __DIR__ . '/../../app/bootstrap.php';
require __DIR__ . '/../../app/database.php';

$domainId = isset($_GET['domain_id']) ? (int)$_GET['domain_id'] : 0;
$result = null;
$error = '';

try {
    $result = rebuild_historical_content_tags($domainId);
} catch (Exception $e) {
    $error = $e->getMessage();
}
?><!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tag 数据回填</title>
    <style>
        body{margin:0;font-family:"PingFang SC","Microsoft YaHei",sans-serif;background:#f8fafc;color:#0f172a}
        .wrap{max-width:1100px;margin:0 auto;padding:24px}
        .card{background:#fff;border:1px solid #e2e8f0;border-radius:16px;padding:18px 20px;margin-bottom:18px}
        table{width:100%;border-collapse:collapse}
        th,td{text-align:left;padding:10px 8px;border-top:1px solid #e2e8f0;font-size:14px;vertical-align:top}
        .bad{color:#b91c1c;font-weight:700}
        .ok{color:#15803d;font-weight:700}
        .mono{font-family:Consolas,Monaco,monospace;word-break:break-all}
    </style>
</head>
<body>
<div class="wrap">
    <div class="card">
        <h1>Tag 数据回填完成</h1>
        <p>可选参数：<span class="mono">?domain_id=2</span></p>
        <?php if ($error !== ''): ?>
            <p class="bad"><?php echo e($error); ?></p>
        <?php elseif (is_array($result)): ?>
            <p class="ok">执行成功</p>
            <p>总内容数：<?php echo (int)$result['total_count']; ?></p>
            <p>处理数量：<?php echo (int)$result['processed_count']; ?></p>
            <p>成功同步 Tag：<?php echo (int)$result['tagged_count']; ?></p>
            <p>跳过数量：<?php echo (int)$result['skipped_count']; ?></p>
        <?php endif; ?>
    </div>
    <?php if (is_array($result) && !empty($result['details'])): ?>
    <div class="card">
        <h2>回填明细</h2>
        <table>
            <thead>
                <tr>
                    <th>content_id</th>
                    <th>domain_id</th>
                    <th>标题</th>
                    <th>关键词</th>
                    <th>tag_count</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($result['details'] as $item): ?>
                <tr>
                    <td><?php echo (int)$item['content_id']; ?></td>
                    <td><?php echo (int)$item['domain_id']; ?></td>
                    <td><?php echo e($item['title']); ?></td>
                    <td class="mono"><?php echo e($item['keywords']); ?></td>
                    <td><?php echo (int)$item['tag_count']; ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>
</body>
</html>
