const router = require('express').Router();
const db = require('../db');

// 列表
router.get('/', (req, res) => {
  const merchants = db.prepare('SELECT * FROM merchants ORDER BY id DESC').all();
  res.render('admin/merchants', { merchants });
});

// 新建
router.post('/', (req, res) => {
  const { name, website, notes } = req.body;
  if (!name) return res.redirect('/admin/merchants');
  db.prepare('INSERT INTO merchants (name, website, notes) VALUES (?, ?, ?)').run(name, website || null, notes || null);
  res.redirect('/admin/merchants');
});

// 删除
router.post('/:id/delete', (req, res) => {
  db.prepare('DELETE FROM merchants WHERE id = ?').run(req.params.id);
  res.redirect('/admin/merchants');
});

module.exports = router;
