<?php
require_once 'config.php';

// 设置Content-Type为XML
header('Content-Type: application/xml; charset=utf-8');

// 获取数据库连接
$conn = get_db_connection();

// 开始输出XML
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
';

// 添加首页URL
$xml .= '  <url>
';
$xml .= '    <loc>' . SITE_URL . '</loc>
';
$xml .= '    <lastmod>' . date('Y-m-d') . '</lastmod>
';
$xml .= '    <changefreq>daily</changefreq>
';
$xml .= '    <priority>1.0</priority>
';
$xml .= '  </url>
';

// 添加移动端首页URL
$xml .= '  <url>
';
$xml .= '    <loc>' . SITE_URL . '/mobile/index.php</loc>
';
$xml .= '    <lastmod>' . date('Y-m-d') . '</lastmod>
';
$xml .= '    <changefreq>daily</changefreq>
';
$xml .= '    <priority>0.9</priority>
';
$xml .= '  </url>
';

// 添加发布页URL
$xml .= '  <url>
';
$xml .= '    <loc>' . SITE_URL . '/post_create.php</loc>
';
$xml .= '    <lastmod>' . date('Y-m-d') . '</lastmod>
';
$xml .= '    <changefreq>weekly</changefreq>
';
$xml .= '    <priority>0.8</priority>
';
$xml .= '  </url>
';

// 添加移动端发布页URL
$xml .= '  <url>
';
$xml .= '    <loc>' . SITE_URL . '/mobile/post_create.php</loc>
';
$xml .= '    <lastmod>' . date('Y-m-d') . '</lastmod>
';
$xml .= '    <changefreq>weekly</changefreq>
';
$xml .= '    <priority>0.7</priority>
';
$xml .= '  </url>
';

// 添加分类列表页URL
$categories_sql = "SELECT id, name, updated_at FROM categories ORDER BY id";
$categories_result = $conn->query($categories_sql);

while ($category = $categories_result->fetch_assoc()) {
    $xml .= '  <url>
';
    $xml .= '    <loc>' . SITE_URL . '/category.php?id=' . $category['id'] . '</loc>
';
    $xml .= '    <lastmod>' . date('Y-m-d', strtotime($category['updated_at'])) . '</lastmod>
';
    $xml .= '    <changefreq>weekly</changefreq>
';
    $xml .= '    <priority>0.8</priority>
';
    $xml .= '  </url>
';
    
    // 添加移动端分类列表页URL
    $xml .= '  <url>
';
    $xml .= '    <loc>' . SITE_URL . '/mobile/category.php?id=' . $category['id'] . '</loc>
';
    $xml .= '    <lastmod>' . date('Y-m-d', strtotime($category['updated_at'])) . '</lastmod>
';
    $xml .= '    <changefreq>weekly</changefreq>
';
    $xml .= '    <priority>0.7</priority>
';
    $xml .= '  </url>
';
}

// 添加商品详情页URL
$products_sql = "SELECT id, updated_at FROM products WHERE status = 1 ORDER BY id";
$products_result = $conn->query($products_sql);

while ($product = $products_result->fetch_assoc()) {
    $xml .= '  <url>
';
    $xml .= '    <loc>' . SITE_URL . '/product_view.php?id=' . $product['id'] . '</loc>
';
    $xml .= '    <lastmod>' . date('Y-m-d', strtotime($product['updated_at'])) . '</lastmod>
';
    $xml .= '    <changefreq>monthly</changefreq>
';
    $xml .= '    <priority>0.6</priority>
';
    $xml .= '  </url>
';
    
    // 添加移动端商品详情页URL
    $xml .= '  <url>
';
    $xml .= '    <loc>' . SITE_URL . '/mobile/product_view.php?id=' . $product['id'] . '</loc>
';
    $xml .= '    <lastmod>' . date('Y-m-d', strtotime($product['updated_at'])) . '</lastmod>
';
    $xml .= '    <changefreq>monthly</changefreq>
';
    $xml .= '    <priority>0.5</priority>
';
    $xml .= '  </url>
';
}

// 关闭URL集
$xml .= '</urlset>';

// 输出XML
echo $xml;

// 关闭数据库连接
$conn->close();
?>