<?php
require __DIR__ . '/factory_config.php';
require __DIR__ . '/_site_chrome.php';

$q   = trim((string)($_GET['q'] ?? ''));
$kw  = trim((string)($_GET['kw'] ?? ''));
$pg  = max(1, (int)($_GET['page'] ?? 1));
$per = 12;

$where = ["status='published'"];
$args  = [];
if ($q !== '') { $where[] = "(title LIKE ? OR keyword LIKE ?)"; $args[] = "%$q%"; $args[] = "%$q%"; }
if ($kw !== '') { $where[] = "keyword=?"; $args[] = $kw; }

$st = db()->prepare("SELECT id,title,slug,excerpt,keyword,created_at,cover_image FROM articles WHERE " . implode(' AND ', $where) . " ORDER BY id DESC");
$st->execute($args);
$rows  = $st->fetchAll();
$total = count($rows);
$pages = max(1, (int)ceil($total / $per));
$pg    = min($pg, $pages);
$items = array_slice($rows, ($pg - 1) * $per, $per);

$tops = db()->query("SELECT keyword, COUNT(*) c FROM articles WHERE status='published' AND keyword<>'' AND CHAR_LENGTH(keyword)<=14 GROUP BY keyword ORDER BY c DESC LIMIT 12")->fetchAll();

function fix_kw(string $s, string $kw): string {
    return str_replace(['{{kw}}', '{{KW}}'], $kw !== '' ? $kw : 'this product', $s);
}
function cover_of(int $id, string $kw): string {
    $g = ['linear-gradient(135deg,#0b2b4d,#1d4f8c)', 'linear-gradient(135deg,#123a5f,#2b6cb0)',
          'linear-gradient(135deg,#0d2b50,#2f4b7c)', 'linear-gradient(135deg,#0a2540,#1f4e79)',
          'linear-gradient(135deg,#15385c,#3a5f8a)', 'linear-gradient(135deg,#092143,#245c8f)'];
    return $g[abs($id) % count($g)];
}
function dt(string $d): string { $t = strtotime($d); return $t ? date('M j, Y', $t) : ''; }
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>News &amp; sourcing guides<?= $kw !== '' ? ' · ' . h($kw) : '' ?> | ShopEg.cn</title>
<meta name="description" content="Factory notes, sourcing guides and shipment checklists from verified Chinese manufacturers on ShopEg.cn.">
<?php site_css(); ?>
<style>
.nw{max-width:1440px;margin:0 auto;padding:0 15px}
.nhero{background:linear-gradient(120deg,#092143,#123a5f 60%,#1d4f8c);color:#fff;padding:34px 0 30px;margin-bottom:26px}
.nhero h1{margin:0 0 8px;font-size:29px;letter-spacing:-.4px}
.nhero p{margin:0;color:rgba(255,255,255,.72);font-size:14px;max-width:760px;line-height:1.6}
.nhero .srch{margin-top:18px;display:flex;gap:9px;max-width:560px}
.nhero .srch input{flex:1;padding:11px 14px;border:1px solid rgba(255,255,255,.22);background:rgba(255,255,255,.1);color:#fff;border-radius:7px;font-size:13.5px}
.nhero .srch input::placeholder{color:rgba(255,255,255,.55)}
.nhero .srch button{padding:11px 20px;border:0;background:#F4A51C;color:#092143;font-weight:700;border-radius:7px;cursor:pointer}
.kws{display:flex;flex-wrap:wrap;gap:8px;margin:0 0 26px}
.kws a{padding:6px 11px;border:1px solid #e2e8f0;border-radius:20px;font-size:12px;color:#334155;text-decoration:none;background:#fff}
.kws a:hover,.kws a.on{border-color:#F4A51C;color:#092143;font-weight:600}
.grid{display:grid;grid-template-columns:repeat(3,1fr);gap:20px}
.card{text-decoration:none;border:1px solid #e6ebf2;border-radius:10px;overflow:hidden;background:#fff;display:flex;flex-direction:column;transition:box-shadow .18s,transform .18s}
.card:hover{box-shadow:0 8px 24px rgba(9,33,67,.1);transform:translateY(-2px)}
.thumb{height:190px;overflow:hidden;position:relative;background:#0b2b4d}
.thumb img{width:100%;height:100%;object-fit:cover;display:block}
.thumb .ph{position:absolute;inset:0;display:flex;align-items:center;justify-content:center;color:#fff;font-weight:800;font-size:38px;letter-spacing:-1px;text-transform:uppercase}
.body{padding:14px 16px 16px;display:flex;flex-direction:column;flex:1}
.body small{color:#8a97a8;font-size:11.5px;letter-spacing:.02em}
.body h3{margin:7px 0 6px;text-decoration:none;font-size:16.5px;line-height:1.35;color:#092143;letter-spacing:-.2px}
.body p{margin:0;font-size:13px;color:#5b6676;line-height:1.55;flex:1}
.body .more{margin-top:11px;font-size:12.5px;font-weight:700;color:#B8860B}
.pager{display:flex;gap:8px;justify-content:center;margin:30px 0 10px}
.pager a,.pager span{padding:8px 13px;border:1px solid #e2e8f0;border-radius:7px;font-size:13px;color:#334155;text-decoration:none;background:#fff}
.pager span{border-color:#092143;background:#092143;color:#fff}
.empty{padding:60px 0;text-align:center;color:#7b8797}
@media(max-width:960px){.grid{grid-template-columns:1fr 1fr}}
@media(max-width:640px){.grid{grid-template-columns:1fr}.nhero h1{font-size:23px}}
</style>
<link rel="canonical" href="https://shopeg.cn/news.php">
</head>
<body>
<?php site_head('News'); ?>
<main>
<div class="nhero"><div class="nw">
  <h1>News &amp; sourcing guides</h1>
  <p>Factory floor notes, cost breakdowns and shipment checklists written from real orders — 1,000 guides across categories, updated with what buyers actually asked.</p>
  <form class="srch" method="get" action="/news.php">
    <input type="text" name="q" value="<?= h($q) ?>" placeholder="Search guides: valve, tube, toy...">
    <button type="submit">Search</button>
  </form>
</div></div>
<div class="nw">
<?php if ($tops): ?>
  <div class="kws">
    <a href="/news.php" class="<?= ($q === '' && $kw === '') ? 'on' : '' ?>">All</a>
    <?php foreach ($tops as $t): ?>
      <a href="/news.php?kw=<?= urlencode($t['keyword']) ?>" class="<?= ($kw === $t['keyword']) ? 'on' : '' ?>"><?= h($t['keyword']) ?> <span style="color:#9aa4b2"><?= (int)$t['c'] ?></span></a>
    <?php endforeach; ?>
  </div>
<?php endif; ?>
<?php if (!$items): ?>
  <div class="empty">No articles match &#8220;<?= h($q ?: $kw) ?>&#8221;. Try another keyword.</div>
<?php else: ?>
  <div class="grid">
  <?php foreach ($items as $a): $k = (string)$a['keyword']; ?>
    <a class="card" href="/article.php?id=<?= (int)$a['id'] ?>">
      <div class="thumb"<?= $a['cover_image'] ? '' : ' style="background:' . cover_of((int)$a['id'], '') . '"' ?>><?php if (!empty($a['cover_image'])): ?><img src="<?= h($a['cover_image']) ?>" alt="<?= h($k) ?>" loading="lazy"><?php else: ?><div class="ph"><?= h(mb_substr($k !== '' ? $k : 'ShopEg', 0, 2)) ?></div><?php endif; ?></div>
      <div class="body">
        <small><?= h(dt((string)$a['created_at'])) ?><?= $k !== '' ? ' · ' . h($k) : '' ?></small>
        <h3><?= h(fix_kw((string)$a['title'], $k)) ?></h3>
        <p><?= h(rtrim(mb_substr(fix_kw((string)$a['excerpt'], $k), 0, 150), " .;:,-—")) ?>…</p>
        <span class="more">Read the guide →</span>
      </div>
    </a>
  <?php endforeach; ?>
  </div>
  <?php if ($pages > 1): $base = '/news.php?' . ($q !== '' ? 'q=' . urlencode($q) . '&' : '') . ($kw !== '' ? 'kw=' . urlencode($kw) . '&' : ''); ?>
  <div class="pager">
    <?php if ($pg > 1): ?><a href="<?= $base ?>page=<?= $pg - 1 ?>">← Prev</a><?php endif; ?>
    <span><?= $pg ?> / <?= $pages ?></span>
    <?php if ($pg < $pages): ?><a href="<?= $base ?>page=<?= $pg + 1 ?>">Next →</a><?php endif; ?>
  </div>
  <?php endif; ?>
  <p style="text-align:center;color:#8a97a8;font-size:12px;margin:8px 0 26px"><?= $total ?> guides · page <?= $pg ?> of <?= $pages ?></p>
<?php endif; ?>
</div>
</main>
<?php site_foot(); ?>
</body>
</html>
