<?php
require_once __DIR__ . '/header.php';
$pdo = db();

$action = $_GET['action'] ?? 'list';
$id = $_GET['id'] ?? null;

// Handle Delete
if ($action === 'delete' && $id) {
    $stmt = $pdo->prepare("DELETE FROM leads WHERE id = ?");
    $stmt->execute([$id]);
    header('Location: leads.php?msg=deleted');
    exit;
}

// Fetch leads
$leads = $pdo->query("SELECT * FROM leads ORDER BY created_at DESC")->fetchAll();
?>

<div class="card">
    <div class="card-header">咨询记录列表</div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table align-middle">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>姓名</th>
                        <th>邮箱</th>
                        <th>电话</th>
                        <th>时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($leads as $l): ?>
                    <tr>
                        <td><?= $l['id'] ?></td>
                        <td><strong><?= htmlspecialchars($l['name']) ?></strong></td>
                        <td><?= htmlspecialchars($l['email']) ?></td>
                        <td><?= htmlspecialchars($l['phone']) ?></td>
                        <td><?= date('Y-m-d H:i:s', strtotime($l['created_at'])) ?></td>
                        <td>
                            <button type="button" class="btn btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#leadModal<?= $l['id'] ?>">
                                <i class="fas fa-eye"></i> 详情
                            </button>
                            <a href="leads.php?action=delete&id=<?= $l['id'] ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('确定删除此咨询记录吗？')">
                                <i class="fas fa-trash"></i>
                            </a>

                            <!-- Modal -->
                            <div class="modal fade" id="leadModal<?= $l['id'] ?>" tabindex="-1">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h5 class="modal-title">咨询详情 - <?= htmlspecialchars($l['name']) ?></h5>
                                            <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                        </div>
                                        <div class="modal-body">
                                            <div class="mb-3"><strong>姓名:</strong> <?= htmlspecialchars($l['name']) ?></div>
                                            <div class="mb-3"><strong>邮箱:</strong> <?= htmlspecialchars($l['email']) ?></div>
                                            <div class="mb-3"><strong>电话:</strong> <?= htmlspecialchars($l['phone']) ?></div>
                                            <div class="mb-3"><strong>留言内容:</strong><br><div class="p-3 bg-light rounded mt-2"><?= nl2br(htmlspecialchars($l['message'])) ?></div></div>
                                            <div class="mb-3"><strong>咨询时间:</strong> <?= date('Y-m-d H:i:s', strtotime($l['created_at'])) ?></div>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                                            <a href="mailto:<?= htmlspecialchars($l['email']) ?>" class="btn btn-primary">邮件回复</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; if (empty($leads)): ?>
                    <tr>
                        <td colspan="6" class="text-center py-5">暂无咨询记录</td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php require_once __DIR__ . '/footer.php'; ?>
