﻿<?php
header('Content-Type: application/xml; charset=utf-8');
require_once 'includes/db.php';
require_once 'includes/functions.php';
$pdo = DB::getInstance()->getPDO();

$baseUrl = 'https://www.kodostyle.com'; // 替换为实际域名

$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"/>');

// 静态页面
$static_pages = [
    ['loc' => '', 'priority' => '1.0', 'changefreq' => 'daily'],
    ['loc' => '/models', 'priority' => '0.8', 'changefreq' => 'weekly'],
    ['loc' => '/shares', 'priority' => '0.6', 'changefreq' => 'weekly'],
    ['loc' => '/news', 'priority' => '0.7', 'changefreq' => 'weekly'],
    ['loc' => '/about', 'priority' => '0.5', 'changefreq' => 'monthly'],
    ['loc' => '/contact', 'priority' => '0.5', 'changefreq' => 'monthly'],
];
foreach ($static_pages as $sp) {
    $url = $xml->addChild('url');
    $url->addChild('loc', $baseUrl . $sp['loc']);
    $url->addChild('lastmod', date('Y-m-d'));
    $url->addChild('changefreq', $sp['changefreq']);
    $url->addChild('priority', $sp['priority']);
}

// 产品
$products = $pdo->query("SELECT id, name, updated_at, created_at FROM products WHERE status=1");
while ($p = $products->fetch()) {
    $url = $xml->addChild('url');
    $url->addChild('loc', $baseUrl . '/product/' . getSlug($p['name']));
    $lastmod = $p['updated_at'] ?: $p['created_at'];
    $url->addChild('lastmod', date('Y-m-d', strtotime($lastmod)));
    $url->addChild('changefreq', 'weekly');
    $url->addChild('priority', '0.9');
}

// 车型分类
$cars = $pdo->query("SELECT name, slug FROM car_categories");
while ($c = $cars->fetch()) {
    $url = $xml->addChild('url');
    $url->addChild('loc', $baseUrl . '/models/' . ($c['slug'] ?: getSlug($c['name'])));
    $url->addChild('lastmod', date('Y-m-d'));
    $url->addChild('changefreq', 'weekly');
    $url->addChild('priority', '0.7');
}

// 功能分类
$funcs = $pdo->query("SELECT name, slug FROM func_categories");
while ($f = $funcs->fetch()) {
    $url = $xml->addChild('url');
    $url->addChild('loc', $baseUrl . '/functions/' . ($f['slug'] ?: getSlug($f['name'])));
    $url->addChild('lastmod', date('Y-m-d'));
    $url->addChild('changefreq', 'weekly');
    $url->addChild('priority', '0.7');
}

// 车主分享
$shares = $pdo->query("SELECT id, created_at FROM shares");
while ($s = $shares->fetch()) {
    $url = $xml->addChild('url');
    $url->addChild('loc', $baseUrl . '/share/' . $s['id']);
    $url->addChild('lastmod', date('Y-m-d', strtotime($s['created_at'])));
    $url->addChild('changefreq', 'monthly');
    $url->addChild('priority', '0.6');
}

// 品牌动态
$news = $pdo->query("SELECT id, created_at FROM news");
while ($n = $news->fetch()) {
    $url = $xml->addChild('url');
    $url->addChild('loc', $baseUrl . '/news/' . $n['id']);
    $url->addChild('lastmod', date('Y-m-d', strtotime($n['created_at'])));
    $url->addChild('changefreq', 'weekly');
    $url->addChild('priority', '0.7');
}

echo $xml->asXML();