<?php
/**
 * 站点地图
 */
header('Content-Type: application/xml; charset=utf-8');

$base_url = 'https://zhaojiren.com';

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

// 静态页面
$static_pages = [
    ['loc' => '/', 'priority' => '1.0', 'changefreq' => 'daily'],
    ['loc' => '/pages/home/fenlei.php', 'priority' => '0.9', 'changefreq' => 'weekly'],
    ['loc' => '/pages/feishou/index.php', 'priority' => '0.9', 'changefreq' => 'daily'],
    ['loc' => '/pages/product/index.php', 'priority' => '0.8', 'changefreq' => 'daily'],
    ['loc' => '/pages/repair/index.php', 'priority' => '0.8', 'changefreq' => 'weekly'],
    ['loc' => '/pages/training/index.php', 'priority' => '0.8', 'changefreq' => 'weekly'],
    ['loc' => '/pages/project/index.php', 'priority' => '0.8', 'changefreq' => 'daily'],
    ['loc' => '/pages/supply/index.php', 'priority' => '0.7', 'changefreq' => 'daily'],
    ['loc' => '/pages/community/index.php', 'priority' => '0.8', 'changefreq' => 'hourly'],
    ['loc' => '/pages/qa/index.php', 'priority' => '0.7', 'changefreq' => 'daily'],
    ['loc' => '/pages/job/index.php', 'priority' => '0.8', 'changefreq' => 'daily'],
    ['loc' => '/pages/shop/index.php', 'priority' => '0.8', 'changefreq' => 'daily'],
    ['loc' => '/pages/help/index.php', 'priority' => '0.5', 'changefreq' => 'monthly'],
];

foreach ($static_pages as $page) {
    echo '<url>';
    echo '<loc>' . $base_url . $page['loc'] . '</loc>';
    echo '<priority>' . $page['priority'] . '</priority>';
    echo '<changefreq>' . $page['changefreq'] . '</changefreq>';
    echo '<lastmod>' . date('Y-m-d') . '</lastmod>';
    echo '</url>';
}

// 从数据库获取最新内容
try {
    $db = get_db();
    
    // 最新飞手
    $stmt = $db->query("SELECT id, updated_at FROM feishou WHERE status = 1 ORDER BY updated_at DESC LIMIT 50");
    while ($row = $stmt->fetch()) {
        echo '<url>';
        echo '<loc>' . $base_url . '/pages/feishou/detail.php?id=' . $row['id'] . '</loc>';
        echo '<priority>0.7</priority>';
        echo '<changefreq>weekly</changefreq>';
        echo '<lastmod>' . date('Y-m-d', $row['updated_at']) . '</lastmod>';
        echo '</url>';
    }
    
    // 最新帖子
    $stmt = $db->query("SELECT id, created_at FROM posts WHERE status = 1 ORDER BY created_at DESC LIMIT 100");
    while ($row = $stmt->fetch()) {
        echo '<url>';
        echo '<loc>' . $base_url . '/pages/community/detail.php?id=' . $row['id'] . '</loc>';
        echo '<priority>0.6</priority>';
        echo '<changefreq>weekly</changefreq>';
        echo '<lastmod>' . date('Y-m-d', $row['created_at']) . '</lastmod>';
        echo '</url>';
    }
    
} catch (Exception $e) {
    // 忽略数据库错误
}

echo '</urlset>';
