<?php
include 'db.php';

// Hər hansı xəta mesajının XML-i korlamaması üçün
error_reporting(0);
ini_set('display_errors', 0);

$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;

$urlset = $dom->createElement('urlset');
$urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$dom->appendChild($urlset);

// Ana səhifə
$url = $dom->createElement('url');
$url->appendChild($dom->createElement('loc', 'https://vinlycheck.com/'));
$url->appendChild($dom->createElement('priority', '1.0'));
$urlset->appendChild($url);

// Maşınların linkləri
$res = $conn->query("SELECT id FROM cars");
if ($res) {
    while($row = $res->fetch_assoc()) {
        $full_url = "https://vinlycheck.com/detail.php?id=db_" . $row['id'];
        
        $urlTag = $dom->createElement('url');
        
        // DOMDocument avtomatik olaraq & simvolunu &amp; edəcək
        $urlTag->appendChild($dom->createElement('loc', $full_url));
        $urlTag->appendChild($dom->createElement('lastmod', date('Y-m-d')));
        $urlTag->appendChild($dom->createElement('changefreq', 'weekly'));
        
        $urlset->appendChild($urlTag);
    }
}

header("Content-Type: application/xml; charset=utf-8");
echo $dom->saveXML();
exit;