<?php
$pageTitle = '发布交友信息';
require_once 'header.php';
require_once 'config.php';

$success = false;
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // 验证和保存数据
    $nickname = trim($_POST['nickname']);
    $gender = $_POST['gender'];
    $age = (int)$_POST['age'];
    $height = trim($_POST['height']);
    $city = trim($_POST['city']);
    $wechat = trim($_POST['wechat']);
    $self_intro = trim($_POST['self_intro']);
    $requirements = trim($_POST['requirements']);
    
    // 基础验证
    if (empty($nickname) || empty($self_intro)) {
        $error = '请填写必填项';
    } elseif ($age < 18 || $age > 100) {
        $error = '年龄不合法';
    } else {
        try {
            // 处理头像上传
            $avatar = 'images/avatar_default.jpg';
            if (isset($_FILES['avatar']) && $_FILES['avatar']['error'] === 0) {
                // 文件大小限制：5MB
                $maxSize = 5 * 1024 * 1024;
                if ($_FILES['avatar']['size'] > $maxSize) {
                    $error = '头像图片大小不能超过5MB。';
                } else {
                    $allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
                    $allowedMimeTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
                    
                    $ext = strtolower(pathinfo($_FILES['avatar']['name'], PATHINFO_EXTENSION));
                    $finfo = finfo_open(FILEINFO_MIME_TYPE);
                    $mime = finfo_file($finfo, $_FILES['avatar']['tmp_name']);
                    finfo_close($finfo);
                    
                    if (!in_array($ext, $allowedExtensions) || !in_array($mime, $allowedMimeTypes)) {
                        $error = '只支持 JPG、PNG、GIF、WebP 格式的图片。';
                    } else {
                        $uploadDir = 'uploads/avatars/';
                        if (!is_dir($uploadDir)) {
                            mkdir($uploadDir, 0755, true);
                        }
                        $avatarName = uniqid() . '_' . basename($_FILES['avatar']['name']);
                        $avatarPath = $uploadDir . $avatarName;
                        if (move_uploaded_file($_FILES['avatar']['tmp_name'], $avatarPath)) {
                            $avatar = $avatarPath;
                        }
                    }
                }
            }
            
            $stmt = $pdo->prepare("INSERT INTO dating_profiles (nickname, gender, age, height, city, wechat, self_intro, requirements, avatar) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
            $stmt->execute([$nickname, $gender, $age, $height, $city, $wechat, $self_intro, $requirements, $avatar]);
            $success = true;
        } catch (PDOException $e) {
            $error = '保存失败，请重试';
        }
    }
}
?>

<main class="site-main">
    <div class="container">
        <div class="form-container">
            <h2>发布交友信息</h2>
            
            <?php if ($success): ?>
                <div class="success-message">
                    <p>✅ 发布成功！</p>
                    <a href="dating.php" class="btn-primary">返回列表</a>
                </div>
            <?php else: ?>
                <?php if ($error): ?>
                    <div class="error-message"><?php echo $error; ?></div>
                <?php endif; ?>
                
                <form method="POST" enctype="multipart/form-data" class="dating-form">
                    <div class="form-group">
                        <label>昵称 *</label>
                        <input type="text" name="nickname" required maxlength="50">
                    </div>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label>性别 *</label>
                            <select name="gender" required>
                                <option value="男">男</option>
                                <option value="女">女</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>年龄 *</label>
                            <input type="number" name="age" min="18" max="100" required>
                        </div>
                        <div class="form-group">
                            <label>身高</label>
                            <input type="text" name="height" placeholder="如: 175cm">
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label>城市</label>
                        <input type="text" name="city">
                    </div>
                    
                    <div class="form-group">
                        <label>微信</label>
                        <input type="text" name="wechat">
                    </div>
                    
                    <div class="form-group">
                        <label>自我描述 *</label>
                        <textarea name="self_intro" rows="5" required></textarea>
                    </div>
                    
                    <div class="form-group">
                        <label>择偶要求</label>
                        <textarea name="requirements" rows="3"></textarea>
                    </div>
                    
                    <div class="form-group">
                        <label>头像</label>
                        <input type="file" name="avatar" accept="image/*">
                    </div>
                    
                    <button type="submit" class="btn-primary btn-block">发布信息</button>
                </form>
            <?php endif; ?>
        </div>
    </div>
</main>

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