<?php
/**
 * 芋头CMS
 * ============================================================================
 * 版权所有 2016-2028 沅陵县乌托邦网络服务有限公司，并保留所有权利。
 * 网站地址: http://www.yutoucms.cn
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷。
 * ============================================================================
 * Author: 芋头 <mxgsf@qq.com>
 * Date: 2026-6-1
 */
$error = '';
$success = '';
if($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = isset($_POST['name']) ? trim($_POST['name']) : '';
    $phone = isset($_POST['phone']) ? trim($_POST['phone']) : '';
    $title = isset($_POST['title']) ? trim($_POST['title']) : '';
    $content = isset($_POST['content']) ? trim($_POST['content']) : '';
    if(empty($name) || empty($phone) || empty($title) || empty($content)) {
        $error = '请填写完整信息';
    } else {
        $ip = $_SERVER['REMOTE_ADDR'] ?? '';
        $last = $db->fetch("SELECT id FROM {$db->table('guestbook')} WHERE ip = ? AND create_time > DATE_SUB(NOW(), INTERVAL 60 SECOND)", [$ip]);
        if($last) {
            $error = '请勿重复提交，请稍后再试';
        } else {
            $db->query("INSERT INTO {$db->table('guestbook')} (channel_id, name, phone, title, content, ip, create_time, is_show) VALUES (?, ?, ?, ?, ?, ?, ?, 0)", [
                8, $name, $phone, $title, $content, $ip, date('Y-m-d H:i:s')
            ]);
            $success = '提交成功，我们会尽快与您联系！';
        }
    }
}
$guestbooks = $db->fetchAll("SELECT * FROM {$db->table('guestbook')} WHERE is_show = 1 ORDER BY create_time DESC LIMIT 20");
?>
<!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 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-guestbook">
<div class="container">
<h1>联系我们</h1>
<p>期待与您的合作</p>
</div>
</div>

<div class="guestbook-page">
<div class="container">
<div class="guestbook-form">
<h2>在线留言</h2>
<?php if($error): ?>
<p style="color:red;text-align:center;margin-bottom:20px;"><?php echo htmlspecialchars($error); ?></p>
<?php endif; ?>
<?php if($success): ?>
<p style="color:green;text-align:center;margin-bottom:20px;"><?php echo htmlspecialchars($success); ?></p>
<?php endif; ?>
<form method="POST">
<div class="form-group">
<label>您的姓名</label>
<input type="text" name="name" placeholder="请输入姓名" required>
</div>
<div class="form-group">
<label>联系电话</label>
<input type="text" name="phone" placeholder="请输入手机号" required>
</div>
<div class="form-group">
<label>留言标题</label>
<input type="text" name="title" placeholder="请输入标题" required>
</div>
<div class="form-group">
<label>留言内容</label>
<textarea name="content" placeholder="请输入留言内容" required></textarea>
</div>
<button type="submit" class="form-btn">提交留言</button>
</form>
</div>

<div class="guestbook-list">
<h3>留言列表</h3>
<?php foreach($guestbooks as $g): ?>
<div class="guestbook-item">
<div class="guest-name"><?php echo htmlspecialchars($g['name']); ?></div>
<div class="guest-time"><?php echo date('Y-m-d H:i', strtotime($g['create_time'])); ?></div>
<div class="guest-content"><?php echo nl2br(htmlspecialchars($g['content'])); ?></div>
</div>
<?php endforeach; ?>
<?php if(empty($guestbooks)): ?>
<p style="text-align:center;color:#999;padding:30px 0;">暂无留言</p>
<?php endif; ?>
</div>
</div>
</div>

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