<?php
ob_start();
/**
 * 2026企业CMS - 后台联系表单管理
 */
require_once __DIR__ . '/header.php';
$pageTitle = '联系表单';

// 处理标记为已读
if (isset($_GET['mark_read']) && $_GET['mark_read']) {
    db_exec("UPDATE `contacts` SET `is_read`=1 WHERE `id`=?", [intval($_GET['mark_read'])]);
    header('Location: /admin/contacts.php');
    exit;
}

// 处理删除
if (isset($_GET['delete']) && $_GET['delete']) {
    check_permission('admin');
    db_exec("DELETE FROM `contacts` WHERE `id`=?", [intval($_GET['delete'])]);
    header('Location: /admin/contacts.php?msg=' . urlencode('删除成功！'));
    exit;
}

// 获取联系表单列表
$page = max(1, intval($_GET['page'] ?? 1));
$pageSize = 15;
$where = '1=1';
$params = [];
if (isset($_GET['status']) && $_GET['status'] !== '') {
    if ($_GET['status'] == 'unread') {
        $where = '`is_read`=0';
    } elseif ($_GET['status'] == 'read') {
        $where = '`is_read`=1';
    }
}
$total = db_count('contacts', $where, $params);
$offset = ($page - 1) * $pageSize;
$contacts = db_query("SELECT * FROM `contacts` WHERE {$where} ORDER BY `id` DESC LIMIT {$offset},{$pageSize}", $params);

$unreadCount = db_count('contacts', '`is_read`=0');
?>
<div class="page-header">
    <h2>联系表单</h2>
    <div style="color:#999;font-size:14px;">
        共 <?php echo htmlspecialchars($total, ENT_QUOTES, "UTF-8"); ?> 条留言，<?php echo $unreadCount; ?> 条未读
    </div>
</div>

<!-- 状态筛选 -->
<div style="margin-bottom:20px;display:flex;gap:10px;">
    <a href="/admin/contacts.php" class="btn <?php echo !isset($_GET['status'])?'btn-primary':''; ?>" style="font-size:13px;padding:6px 15px;">全部</a>
    <a href="/admin/contacts.php?status=unread" class="btn <?php echo ($_GET['status']??'')=='unread'?'btn-primary':''; ?>" style="font-size:13px;padding:6px 15px;">未读 (<?php echo $unreadCount; ?>)</a>
    <a href="/admin/contacts.php?status=read" class="btn <?php echo ($_GET['status']??'')=='read'?'btn-primary':''; ?>" style="font-size:13px;padding:6px 15px;">已读</a>
</div>

<div class="table-wrapper">
    <table>
        <thead><tr><th>ID</th><th>姓名</th><th>电话</th><th>邮箱</th><th>公司</th><th>留言内容</th><th>时间</th><th>状态</th><th>操作</th></tr></thead>
        <tbody>
            <?php foreach ($contacts as $c): ?>
            <tr style="<?php echo $c['is_read']?'':'font-weight:bold;background:#fffbe6;'; ?>">
                <td><?php echo $c['id']; ?></td>
                <td><?php echo xss_clean($c['name']); ?></td>
                <td><?php echo xss_clean($c['phone']); ?></td>
                <td><?php echo xss_clean($c['email']); ?></td>
                <td><?php echo xss_clean($c['company']); ?></td>
                <td style="max-width:200px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;" title="<?php echo xss_clean($c['message']); ?>">
                    <?php echo xss_clean(str_cut($c['message'], 30)); ?>
                </td>
                <td><?php echo date('m-d H:i', strtotime($c['created_at'])); ?></td>
                <td>
                    <?php if ($c['is_read']): ?><span class="badge badge-success">已读</span>
                    <?php else: ?><span class="badge badge-danger">未读</span>
                    <?php endif; ?>
                </td>
                <td class="actions">
                    <a href="/admin/contacts.php?mark_read=<?php echo $c['id']; ?>" class="btn btn-sm" style="font-size:12px;">标记已读</a>
                    <a href="javascript:void(0)" onclick="if(confirm('确定删除此留言？')){location.href='/admin/contacts.php?delete=<?php echo $c['id']; ?>'}" class="btn btn-sm btn-danger">删除</a>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

<!-- 留言详情弹窗 -->
<?php if (isset($_GET['view']) && $_GET['view']): ?>
<?php $vc = db_query_one("SELECT * FROM `contacts` WHERE `id`=?", [intval($_GET['view'])]); ?>
<?php if ($vc): ?>
<div style="position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,0.5);z-index:9999;display:flex;align-items:center;justify-content:center;" onclick="if(event.target===this){location.href='/admin/contacts.php'}">
    <div style="background:#fff;border-radius:8px;padding:30px;max-width:600px;width:90%;max-height:80vh;overflow:auto;">
        <h3 style="margin-bottom:20px;font-size:18px;">留言详情</h3>
        <div style="color:#666;line-height:2;">
            <div><strong>姓名：</strong><?php echo xss_clean($vc['name']); ?></div>
            <div><strong>电话：</strong><?php echo xss_clean($vc['phone']); ?></div>
            <div><strong>邮箱：</strong><?php echo xss_clean($vc['email']); ?></div>
            <div><strong>公司：</strong><?php echo xss_clean($vc['company']); ?></div>
            <div><strong>时间：</strong><?php echo $vc['created_at']; ?></div>
            <div style="margin-top:15px;"><strong>留言内容：</strong></div>
            <div style="background:#f5f5f5;padding:15px;border-radius:4px;margin-top:5px;white-space:pre-wrap;"><?php echo xss_clean($vc['message']); ?></div>
        </div>
        <div style="margin-top:20px;text-align:right;">
            <a href="/admin/contacts.php?mark_read=<?php echo $vc['id']; ?>" class="btn btn-primary" style="margin-right:10px;">标记已读</a>
            <a href="/admin/contacts.php" class="btn">关闭</a>
        </div>
    </div>
</div>
<?php endif; ?>
<?php endif; ?>

<?php if ($total > $pageSize): $totalPage=ceil($total/$pageSize); ?>
<div class="pagination">
    <?php for ($i=1; $i<=$totalPage; $i++): ?><a href="/admin/contacts.php?page=<?php echo $i; ?>" <?php echo $i==$page?'class="current"':''; ?>><?php echo $i; ?></a><?php endfor; ?>
</div>
<?php endif; ?>
<?php require_once __DIR__ . '/footer.php'; ?>
