<?php
/**
 * 芋头CMS
 * ============================================================================
 * 版权所有 2016-2028 沅陵县乌托邦网络服务有限公司，并保留所有权利。
 * 网站地址: http://www.yutoucms.cn
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷。
 * ============================================================================
 * Author: 芋头 <mxgsf@qq.com>
 * Date: 2026-6-1
 */
$keyword = isset($_GET['keyword']) ? trim($_GET['keyword']) : '';
$results = [];
if($keyword !== '') {
    $kw = '%' . $keyword . '%';
    $articles = $db->fetchAll("SELECT id, title, 'article' as type FROM {$db->table('article')} WHERE status = 1 AND title LIKE ? LIMIT 10", [$kw]);
    $products = $db->fetchAll("SELECT id, title, 'product' as type FROM {$db->table('product')} WHERE status = 1 AND title LIKE ? LIMIT 10", [$kw]);
    foreach($articles as $a) {
        $results[] = ['title' => $a['title'], 'url' => getUrl('article', $a['id']), 'desc' => '文章'];
    }
    foreach($products as $p) {
        $results[] = ['title' => $p['title'], 'url' => getUrl('product', $p['id']), 'desc' => '产品'];
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $keyword ? '搜索:'.htmlspecialchars($keyword) : '搜索'; ?> - <?php echo htmlspecialchars($site['site_name']); ?></title>
<link rel="stylesheet" href="<?php echo $site['template_dir']; ?>css/style.css">
</head>
<body>
<?php include __DIR__ . '/_header.php'; ?>

<div class="channel-banner channel-banner-article">
<div class="container">
<h1>搜索结果</h1>
</div>
</div>

<div class="search-page">
<div class="container">
<form class="search-form" method="GET" action="<?php echo $site['public_dir']; ?>index.php">
<input type="hidden" name="m" value="search">
<input type="text" name="keyword" placeholder="请输入关键词搜索..." value="<?php echo htmlspecialchars($keyword); ?>">
<button type="submit">搜索</button>
</form>
<div class="search-results">
<h3><?php if($keyword): ?>找到 <?php echo count($results); ?> 条关于"<?php echo htmlspecialchars($keyword); ?>"的结果<?php else: ?>请输入关键词搜索<?php endif; ?></h3>
<?php if(!empty($results)): foreach($results as $r): ?>
<div class="search-result-item">
<h4><a href="<?php echo htmlspecialchars($r['url']); ?>"><?php echo htmlspecialchars($r['title']); ?></a></h4>
<p><?php echo htmlspecialchars($r['desc']); ?></p>
</div>
<?php endforeach; elseif($keyword): ?>
<p style="text-align:center;color:#999;padding:40px 0;">未找到"<?php echo htmlspecialchars($keyword); ?>"相关内容</p>
<?php endif; ?>
</div>
</div>
</div>

<?php include __DIR__ . '/_footer.php'; ?>
</body>
</html>