<?php
require_once 'config.php';

// 获取网站域名
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443) ? 'https' : 'http';
$domain = $_SERVER['HTTP_HOST'] ?? 'www.ok0595.com.cn';

// 获取产品列表
$products = [];
$prodStmt = $conn->prepare("SELECT id, name, updated_at FROM products WHERE status = 'active' ORDER BY updated_at DESC");
$prodStmt->execute();
$prodResult = $prodStmt->get_result();
while ($row = $prodResult->fetch_assoc()) {
    $products[] = $row;
}
$prodStmt->close();

// 获取分类列表
$categories = [];
$catStmt = $conn->prepare("SELECT id, name, updated_at FROM categories WHERE status = 'active' ORDER BY sort_order ASC");
$catStmt->execute();
$catResult = $catStmt->get_result();
while ($row = $catResult->fetch_assoc()) {
    $categories[] = $row;
}
$catStmt->close();

// 生成XML内容
header('Content-Type: application/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
    <!-- 首页 -->
    <url>
        <loc><?php echo "{$protocol}://{$domain}/"; ?></loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    
    <!-- 选品中心页面 -->
    <url>
        <loc><?php echo "{$protocol}://{$domain}/xp.php"; ?></loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>
    
    <!-- 分类页面 -->
    <?php foreach ($categories as $category): ?>
    <url>
        <loc><?php echo "{$protocol}://{$domain}/index.php?category_id={$category['id']}"; ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($category['updated_at'])); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>
    <?php endforeach; ?>
    
    <!-- 产品详情页面 -->
    <?php foreach ($products as $product): ?>
    <url>
        <loc><?php echo "{$protocol}://{$domain}/index.php?product_id={$product['id']}"; ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($product['updated_at'])); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php endforeach; ?>
    
    <!-- 其他重要页面 -->
    <url>
        <loc><?php echo "{$protocol}://{$domain}/favorites.php"; ?></loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.6</priority>
    </url>
    
    <url>
        <loc><?php echo "{$protocol}://{$domain}/selection_cart.php"; ?></loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.6</priority>
    </url>
    
    <url>
        <loc><?php echo "{$protocol}://{$domain}/profile.php"; ?></loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.5</priority>
    </url>
</urlset>