<?php
/* ============================================================
   /sitemap.php — dynamic sitemap.xml generator
   Lists all menu pages + recent board posts
   ============================================================ */
include_once __DIR__ . '/common.php';
header('Content-Type: application/xml; charset=utf-8');

$base = G5_URL;
$lastmod = date('Y-m-d');

$urls = [
    ['loc'=>$base.'/', 'pri'=>'1.0', 'cf'=>'weekly'],
    /* About */
    ['loc'=>$base.'/theme/sama/html/overview.php',     'pri'=>'0.9', 'cf'=>'monthly'],
    ['loc'=>$base.'/theme/sama/html/ceo_message.php',  'pri'=>'0.7', 'cf'=>'yearly'],
    ['loc'=>$base.'/theme/sama/html/vision.php',       'pri'=>'0.8', 'cf'=>'yearly'],
    ['loc'=>$base.'/theme/sama/html/history.php',      'pri'=>'0.7', 'cf'=>'monthly'],
    ['loc'=>$base.'/theme/sama/html/ci.php',           'pri'=>'0.5', 'cf'=>'yearly'],
    ['loc'=>$base.'/theme/sama/html/organization.php', 'pri'=>'0.5', 'cf'=>'yearly'],
    /* Product */
    ['loc'=>$base.'/theme/sama/html/product_mold.php',     'pri'=>'0.9', 'cf'=>'monthly'],
    ['loc'=>$base.'/theme/sama/html/product_molding.php',  'pri'=>'0.9', 'cf'=>'monthly'],
    ['loc'=>$base.'/theme/sama/html/product_mobility.php', 'pri'=>'0.9', 'cf'=>'monthly'],
    /* ESG */
    ['loc'=>$base.'/theme/sama/html/sustain_esg.php',    'pri'=>'0.8', 'cf'=>'monthly'],
    ['loc'=>$base.'/theme/sama/html/sustain_policy.php', 'pri'=>'0.7', 'cf'=>'yearly'],
    ['loc'=>$base.'/theme/sama/html/certification.php',  'pri'=>'0.7', 'cf'=>'monthly'],
    /* Career */
    ['loc'=>$base.'/theme/sama/html/careers_talent.php',   'pri'=>'0.7', 'cf'=>'monthly'],
    ['loc'=>$base.'/theme/sama/html/careers_benefits.php', 'pri'=>'0.6', 'cf'=>'yearly'],
    ['loc'=>$base.'/bbs/board.php?bo_table=incruit',       'pri'=>'0.8', 'cf'=>'weekly'],
    /* Support */
    ['loc'=>$base.'/bbs/board.php?bo_table=notice',     'pri'=>'0.8', 'cf'=>'weekly'],
    ['loc'=>$base.'/bbs/board.php?bo_table=file',       'pri'=>'0.7', 'cf'=>'monthly'],
    ['loc'=>$base.'/theme/sama/html/faq_page.php',      'pri'=>'0.6', 'cf'=>'monthly'],
    ['loc'=>$base.'/bbs/board.php?bo_table=quotation',  'pri'=>'0.7', 'cf'=>'monthly'],
    ['loc'=>$base.'/theme/sama/html/location.php',      'pri'=>'0.6', 'cf'=>'yearly'],
];

/* Recent notice posts */
$notice_table = G5_TABLE_PREFIX.'write_notice';
$res = @sql_query("SELECT wr_id, wr_datetime FROM `{$notice_table}` WHERE wr_is_comment=0 ORDER BY wr_id DESC LIMIT 30", false);
if ($res) {
    while ($row = sql_fetch_array($res)) {
        $urls[] = [
            'loc'=>$base.'/bbs/board.php?bo_table=notice&wr_id='.$row['wr_id'],
            'pri'=>'0.5', 'cf'=>'monthly',
            'lastmod'=>substr($row['wr_datetime'], 0, 10),
        ];
    }
}

echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n";
foreach ($urls as $u) {
    echo "  <url>\n";
    echo "    <loc>".htmlspecialchars($u['loc'])."</loc>\n";
    echo "    <lastmod>".($u['lastmod'] ?? $lastmod)."</lastmod>\n";
    echo "    <changefreq>".$u['cf']."</changefreq>\n";
    echo "    <priority>".$u['pri']."</priority>\n";
    echo "  </url>\n";
}
echo '</urlset>';
