<?php
$pageTitle = '交友约会';
require_once 'header.php';
require_once 'config.php';

// 分页参数
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = 12;
$offset = ($page - 1) * $perPage;

// 筛选参数
$gender = isset($_GET['gender']) ? $_GET['gender'] : '';
$city = isset($_GET['city']) ? $_GET['city'] : '';
$search = isset($_GET['search']) ? $_GET['search'] : '';

try {
    // 构建查询条件
    $where = "WHERE is_active = 1";
    $params = [];

    if ($gender) {
        $where .= " AND gender = :gender";
        $params[':gender'] = $gender;
    }
    if ($city) {
        $where .= " AND city LIKE :city";
        $params[':city'] = "%$city%";
    }
    if ($search) {
        $where .= " AND (nickname LIKE :search OR self_intro LIKE :search2)";
        $params[':search'] = "%$search%";
        $params[':search2'] = "%$search%";
    }

    // 获取总记录数
    $countStmt = $pdo->prepare("SELECT COUNT(*) FROM dating_profiles $where");
    $countStmt->execute($params);
    $totalProfiles = $countStmt->fetchColumn();
    $totalPages = ceil($totalProfiles / $perPage);

    // 获取当前页数据
    $stmt = $pdo->prepare("SELECT * FROM dating_profiles $where ORDER BY created_at DESC LIMIT :limit OFFSET :offset");
    foreach ($params as $key => $value) {
        $stmt->bindValue($key, $value);
    }
    $stmt->bindValue(':limit', $perPage, PDO::PARAM_INT);
    $stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
    $stmt->execute();
    $profiles = $stmt->fetchAll(PDO::FETCH_ASSOC);

} catch (PDOException $e) {
    $profiles = [];
    $totalProfiles = 0;
    $totalPages = 0;
}
?>

<main class="site-main">
    <!-- 搜索筛选区 -->
    <section class="dating-filters">
        <div class="container">
            <form method="GET" class="filter-form">
                <div class="filter-row">
                    <input type="text" name="search" placeholder="搜索昵称..." value="<?php echo htmlspecialchars($search); ?>">
                    <select name="gender">
                        <option value="">全部性别</option>
                        <option value="男" <?php echo $gender === '男' ? 'selected' : ''; ?>>男</option>
                        <option value="女" <?php echo $gender === '女' ? 'selected' : ''; ?>>女</option>
                    </select>
                    <input type="text" name="city" placeholder="城市" value="<?php echo htmlspecialchars($city); ?>">
                    <button type="submit" class="btn-primary">筛选</button>
                    <a href="dating.php" class="btn-secondary">重置</a>
                </div>
            </form>
        </div>
    </section>

    <!-- 展示用户卡片 -->
    <section class="dating-profiles">
        <div class="container">
            <div class="section-title">
                <h2>💑 交友列表</h2>
                <p>共找到 <?php echo $totalProfiles; ?> 位用户</p>
                <a href="dating_create.php" class="btn-primary">发布交友信息</a>
            </div>

            <div class="profiles-grid">
                <?php if (!empty($profiles)): ?>
                    <?php foreach ($profiles as $profile): ?>
                    <div class="profile-card">
                        <div class="profile-avatar">
                            <div class="avatar-placeholder"><?php echo mb_substr($profile['nickname'], 0, 1); ?></div>
                            <?php if ($profile['gender'] === '男'): ?>
                                <span class="gender-tag male">♂</span>
                            <?php else: ?>
                                <span class="gender-tag female">♀</span>
                            <?php endif; ?>
                        </div>
                        <h3><?php echo htmlspecialchars($profile['nickname']); ?></h3>
                        <div class="profile-info">
                            <span>年龄: <?php echo $profile['age']; ?>岁</span>
                            <span>身高: <?php echo $profile['height'] ?: '未填'; ?></span>
                            <span>城市: <?php echo htmlspecialchars($profile['city']); ?></span>
                        </div>
                        <p class="profile-intro"><?php echo mb_strlen($profile['self_intro']) > 80 ? mb_substr($profile['self_intro'], 0, 80) . '...' : htmlspecialchars($profile['self_intro']); ?></p>
                        <button class="btn-contact" onclick="showContact(<?php echo $profile['id']; ?>)">
                            <i class="fas fa-wechat"></i> 联系TA
                        </button>
                    </div>
                    <?php endforeach; ?>
                <?php else: ?>
                    <div class="empty-state" style="grid-column:1/-1;">
                        <p>暂无交友信息</p>
                        <a href="dating_create.php" class="btn-primary" style="margin-top:15px;">发布第一条交友信息</a>
                    </div>
                <?php endif; ?>
            </div>

            <!-- 分页 -->
            <?php if ($totalPages > 1): ?>
            <div class="pagination">
                <?php if ($page > 1): ?>
                    <a href="?page=<?php echo $page - 1; ?>&gender=<?php echo urlencode($gender); ?>&city=<?php echo urlencode($city); ?>&search=<?php echo urlencode($search); ?>" class="page-link">上一页</a>
                <?php endif; ?>

                <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                    <a href="?page=<?php echo $i; ?>&gender=<?php echo urlencode($gender); ?>&city=<?php echo urlencode($city); ?>&search=<?php echo urlencode($search); ?>"
                       class="page-link <?php echo $i === $page ? 'active' : ''; ?>"><?php echo $i; ?></a>
                <?php endfor; ?>

                <?php if ($page < $totalPages): ?>
                    <a href="?page=<?php echo $page + 1; ?>&gender=<?php echo urlencode($gender); ?>&city=<?php echo urlencode($city); ?>&search=<?php echo urlencode($search); ?>" class="page-link">下一页</a>
                <?php endif; ?>
            </div>
            <?php endif; ?>
        </div>
    </section>
</main>

<!-- 联系信息弹窗 -->
<div id="contactModal" class="modal" style="display:none;">
    <div class="modal-content">
        <span class="close" onclick="closeContactModal()">&times;</span>
        <div id="contactInfo"></div>
    </div>
</div>

<script>
function showContact(id) {
    fetch('get_contact.php?id=' + id)
        .then(function(response) { return response.json(); })
        .then(function(data) {
            document.getElementById('contactInfo').innerHTML =
                '<h3>联系方式</h3>' +
                '<p>微信: ' + (data.wechat || '未公开') + '</p>';
            document.getElementById('contactModal').style.display = 'block';
        });
}

function closeContactModal() {
    document.getElementById('contactModal').style.display = 'none';
}
</script>

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