<?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">
';

// 1. 移动端首页（最高优先级）
$xml .= '  <url>
';
$xml .= '    <loc>' . SITE_URL . '/mobile/index.php</loc>
';
$xml .= '    <lastmod>' . date('Y-m-d') . '</lastmod>
';
$xml .= '    <changefreq>daily</changefreq>
';
$xml .= '    <priority>1.0</priority>
';
$xml .= '  </url>
';

// 2. 移动端分类页面
$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 . '/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.9</priority>
';
    $xml .= '  </url>
';
}

// 3. 移动端商品详情页（最近1000条）
$products_sql = "SELECT id, updated_at FROM products WHERE status = 1 ORDER BY updated_at DESC LIMIT 1000";
$products_result = $conn->query($products_sql);

while ($product = $products_result->fetch_assoc()) {
    $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.8</priority>
';
    $xml .= '  </url>
';
}

// 4. 移动端出租详情页（最近500条）
$rental_sql = "SELECT id, updated_at FROM rental WHERE status = 1 ORDER BY updated_at DESC LIMIT 500";
$rental_result = $conn->query($rental_sql);

while ($rental = $rental_result->fetch_assoc()) {
    $xml .= '  <url>
';
    $xml .= '    <loc>' . SITE_URL . '/mobile/rental_detail.php?id=' . $rental['id'] . '</loc>
';
    $xml .= '    <lastmod>' . date('Y-m-d', strtotime($rental['updated_at'])) . '</lastmod>
';
    $xml .= '    <changefreq>monthly</changefreq>
';
    $xml .= '    <priority>0.8</priority>
';
    $xml .= '  </url>
';
}

// 5. 移动端招聘详情页（最近300条）
$job_posts_sql = "SELECT id, updated_at FROM job_posts WHERE status = 1 ORDER BY updated_at DESC LIMIT 300";
$job_posts_result = $conn->query($job_posts_sql);

while ($job = $job_posts_result->fetch_assoc()) {
    $xml .= '  <url>
';
    $xml .= '    <loc>' . SITE_URL . '/mobile/job_post_view.php?id=' . $job['id'] . '</loc>
';
    $xml .= '    <lastmod>' . date('Y-m-d', strtotime($job['updated_at'])) . '</lastmod>
';
    $xml .= '    <changefreq>monthly</changefreq>
';
    $xml .= '    <priority>0.8</priority>
';
    $xml .= '  </url>
';
}

// 6. 移动端求职详情页（最近200条）
$resumes_sql = "SELECT id, updated_at FROM resumes WHERE status = 1 ORDER BY updated_at DESC LIMIT 200";
$resumes_result = $conn->query($resumes_sql);

while ($resume = $resumes_result->fetch_assoc()) {
    $xml .= '  <url>
';
    $xml .= '    <loc>' . SITE_URL . '/mobile/resume_view.php?id=' . $resume['id'] . '</loc>
';
    $xml .= '    <lastmod>' . date('Y-m-d', strtotime($resume['updated_at'])) . '</lastmod>
';
    $xml .= '    <changefreq>monthly</changefreq>
';
    $xml .= '    <priority>0.8</priority>
';
    $xml .= '  </url>
';
}

// 7. 移动端交友详情页（最近200条）
$dating_sql = "SELECT id, updated_at FROM dating_posts WHERE status = 1 ORDER BY updated_at DESC LIMIT 200";
$dating_result = $conn->query($dating_sql);

while ($dating = $dating_result->fetch_assoc()) {
    $xml .= '  <url>
';
    $xml .= '    <loc>' . SITE_URL . '/mobile/dating_view.php?id=' . $dating['id'] . '</loc>
';
    $xml .= '    <lastmod>' . date('Y-m-d', strtotime($dating['updated_at'])) . '</lastmod>
';
    $xml .= '    <changefreq>monthly</changefreq>
';
    $xml .= '    <priority>0.8</priority>
';
    $xml .= '  </url>
';
}

// 8. 移动端活动详情页（最近150条）
$activities_sql = "SELECT id, updated_at FROM activities WHERE status = 1 ORDER BY updated_at DESC LIMIT 150";
$activities_result = $conn->query($activities_sql);

while ($activity = $activities_result->fetch_assoc()) {
    $xml .= '  <url>
';
    $xml .= '    <loc>' . SITE_URL . '/mobile/activity_detail.php?id=' . $activity['id'] . '</loc>
';
    $xml .= '    <lastmod>' . date('Y-m-d', strtotime($activity['updated_at'])) . '</lastmod>
';
    $xml .= '    <changefreq>monthly</changefreq>
';
    $xml .= '    <priority>0.7</priority>
';
    $xml .= '  </url>
';
}

// 9. 移动端寻物详情页（最近100条）
$missing_sql = "SELECT id, updated_at FROM missing_items WHERE status = 1 ORDER BY updated_at DESC LIMIT 100";
$missing_result = $conn->query($missing_sql);

while ($missing = $missing_result->fetch_assoc()) {
    $xml .= '  <url>
';
    $xml .= '    <loc>' . SITE_URL . '/mobile/missing_item_detail.php?id=' . $missing['id'] . '</loc>
';
    $xml .= '    <lastmod>' . date('Y-m-d', strtotime($missing['updated_at'])) . '</lastmod>
';
    $xml .= '    <changefreq>monthly</changefreq>
';
    $xml .= '    <priority>0.7</priority>
';
    $xml .= '  </url>
';
}

// 10. 移动端宠物救助详情页（最近100条）
$pet_sql = "SELECT id, updated_at FROM pet_charity WHERE status = 1 ORDER BY updated_at DESC LIMIT 100";
$pet_result = $conn->query($pet_sql);

while ($pet = $pet_result->fetch_assoc()) {
    $xml .= '  <url>
';
    $xml .= '    <loc>' . SITE_URL . '/mobile/pet_charity_detail.php?id=' . $pet['id'] . '</loc>
';
    $xml .= '    <lastmod>' . date('Y-m-d', strtotime($pet['updated_at'])) . '</lastmod>
';
    $xml .= '    <changefreq>monthly</changefreq>
';
    $xml .= '    <priority>0.7</priority>
';
    $xml .= '  </url>
';
}

// 11. 移动端回收详情页（最近100条）
$recycling_sql = "SELECT id, updated_at FROM recycling WHERE status = 1 ORDER BY updated_at DESC LIMIT 100";
$recycling_result = $conn->query($recycling_sql);

while ($recycle = $recycling_result->fetch_assoc()) {
    $xml .= '  <url>
';
    $xml .= '    <loc>' . SITE_URL . '/mobile/recycling_detail.php?id=' . $recycle['id'] . '</loc>
';
    $xml .= '    <lastmod>' . date('Y-m-d', strtotime($recycle['updated_at'])) . '</lastmod>
';
    $xml .= '    <changefreq>monthly</changefreq>
';
    $xml .= '    <priority>0.7</priority>
';
    $xml .= '  </url>
';
}

// 12. 移动端婚礼详情页（最近100条）
$wedding_sql = "SELECT id, updated_at FROM wedding_celebration WHERE status = 1 ORDER BY updated_at DESC LIMIT 100";
$wedding_result = $conn->query($wedding_sql);

while ($wedding = $wedding_result->fetch_assoc()) {
    $xml .= '  <url>
';
    $xml .= '    <loc>' . SITE_URL . '/mobile/wedding_detail.php?id=' . $wedding['id'] . '</loc>
';
    $xml .= '    <lastmod>' . date('Y-m-d', strtotime($wedding['updated_at'])) . '</lastmod>
';
    $xml .= '    <changefreq>monthly</changefreq>
';
    $xml .= '    <priority>0.7</priority>
';
    $xml .= '  </url>
';
}

// 13. 移动端重要功能页面
$important_pages = array(
    'mobile/micro_posts.php' => '碎语社区',
    'mobile/advertisements.php' => '供求信息',
    'mobile/announcements.php' => '网站公告',
    'mobile/advertisement_intro.php' => '广告投放',
    'mobile/gift_mall.php' => '积分商城',
    'mobile/user_center.php' => '个人中心',
    'mobile/register.php' => '用户注册',
    'mobile/login.php' => '用户登录',
    'mobile/search.php' => '搜索页面',
    'mobile/about_us.php' => '关于我们',
    'mobile/contact_us.php' => '联系我们',
    'mobile/privacy_policy.php' => '隐私政策',
    'mobile/user_agreement.php' => '用户协议'
);

foreach ($important_pages as $page => $title) {
    $xml .= '  <url>
';
    $xml .= '    <loc>' . SITE_URL . '/' . $page . '</loc>
';
    $xml .= '    <lastmod>' . date('Y-m-d') . '</lastmod>
';
    $xml .= '    <changefreq>weekly</changefreq>
';
    $xml .= '    <priority>0.6</priority>
';
    $xml .= '  </url>
';
}

// 14. 移动端社区服务页面
$community_pages = array(
    'mobile/community_posts.php' => '社区帖子',
    'mobile/community_service_form.php' => '便民服务',
    'mobile/offer_service.php' => '服务承接',
    'mobile/service_order.php' => '服务订单'
);

foreach ($community_pages as $page => $title) {
    $xml .= '  <url>
';
    $xml .= '    <loc>' . SITE_URL . '/' . $page . '</loc>
';
    $xml .= '    <lastmod>' . date('Y-m-d') . '</lastmod>
';
    $xml .= '    <changefreq>weekly</changefreq>
';
    $xml .= '    <priority>0.5</priority>
';
    $xml .= '  </url>
';
}

// 15. 移动端其他特色页面
$feature_pages = array(
    'mobile/buddha_prayer.php' => '许愿墙',
    'mobile/memorial.php' => '纪念堂',
    'mobile/donations.php' => '爱心捐赠',
    'mobile/poetry_art_list.php' => '诗文艺术',
    'mobile/tourist_spots_list.php' => '旅游景点',
    'mobile/wechat_groups.php' => '微信群',
    'mobile/wall_ads.php' => '墙贴广告'
);

foreach ($feature_pages as $page => $title) {
    $xml .= '  <url>
';
    $xml .= '    <loc>' . SITE_URL . '/' . $page . '</loc>
';
    $xml .= '    <lastmod>' . date('Y-m-d') . '</lastmod>
';
    $xml .= '    <changefreq>monthly</changefreq>
';
    $xml .= '    <priority>0.4</priority>
';
    $xml .= '  </url>
';
}

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

// 输出XML
echo $xml;

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

// 立即退出，避免任何额外的输出
exit();