<?php
require __DIR__ . '/portal_lib.php';

$q = isset($_GET['q']) ? trim((string) $_GET['q']) : '';
$db = portal_db();
$news = portal_news_list($db);
$goods = portal_goods_list($db, $q);

$body = '<h1>门户</h1>';
$body .= portal_section_nav('index.php');
$body .= '<p class="nav"><a href="catalog.php">商品分类</a><a href="goods.php">商品目录</a></p>';
$body .= '<h2>新闻</h2><ol>';
foreach ($news as $row) {
    $href = 'item.php?kind=news&id=' . rawurlencode((string) $row['id']);
    $body .= '<li><a href="' . portal_h($href) . '">' . portal_h((string) $row['title_cn']) . '</a></li>';
}
$body .= '</ol>';
$body .= '<h2>产品</h2>';
$body .= '<form method="get" action="index.php"><label>产品名称 <input type="search" name="q" value="'
    . portal_h($q) . '"></label><button type="submit">筛选</button></form>';
if ($goods === []) {
    $body .= '<p>没有匹配的产品</p>';
} else {
    $body .= '<ol>';
    foreach ($goods as $row) {
        $href = 'item.php?kind=goods&id=' . rawurlencode((string) $row['id']);
        $price = (string) $row['price'];
        $body .= '<li><a href="' . portal_h($href) . '">' . portal_h((string) $row['name_cn']) . '</a>';
        $body .= '<span class="price">RMB ' . portal_h($price) . '</span></li>';
    }
    $body .= '</ol>';
}

$body .= '<p><a href="/legacy-index.php">旧址归档入口</a></p>';
portal_page('门户', $body);
