<?php
session_start();
require_once '../config.php';

if (!isset($_SESSION['user_logged_in']) || $_SESSION['user_logged_in'] !== true) {
    header('Location: index.php');
    exit;
}

$user_id = $_SESSION['user_id'];
$msg = '';
$msg_type = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $old_pass = $_POST['old_password'] ?? '';
    $new_pass = $_POST['new_password'] ?? '';
    $confirm_pass = $_POST['confirm_password'] ?? '';

    if (empty($old_pass) || empty($new_pass) || empty($confirm_pass)) {
        $msg = '所有选项都不能为空！';
        $msg_type = 'error';
    } elseif (strlen($new_pass) < 6) {
        $msg = '新密码长度不能少于6位！';
        $msg_type = 'error';
    } elseif ($new_pass !== $confirm_pass) {
        $msg = '两次输入的新密码不一致！';
        $msg_type = 'error';
    } else {
        $stmt = mysqli_prepare($conn, "SELECT password FROM users WHERE id = ?");
        mysqli_stmt_bind_param($stmt, "i", $user_id);
        mysqli_stmt_execute($stmt);
        $result = mysqli_stmt_get_result($stmt);
        $user = mysqli_fetch_assoc($result);

        if ($user && password_verify($old_pass, $user['password'])) {
            $new_hash = password_hash($new_pass, PASSWORD_DEFAULT);
            
            // 修改密码，并更新 pwd_time 为当前时间
            $update_stmt = mysqli_prepare($conn, "UPDATE users SET password = ?, pwd_time = NOW() WHERE id = ?");
            mysqli_stmt_bind_param($update_stmt, "si", $new_hash, $user_id);
            
            if (mysqli_stmt_execute($update_stmt)) {
                session_destroy();
                header('Location: index.php?msg=' . urlencode('密码修改成功，请重新登录'));
                exit;
            } else {
                $msg = '数据库更新失败。';
                $msg_type = 'error';
            }
        } else {
            $msg = '原密码输入错误！';
            $msg_type = 'error';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改密码</title>
    <style>
        body { background-color: #f4f6f9; font-family: sans-serif; display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; }
        .box { background: #fff; padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); width: 100%; max-width: 400px; }
        h2 { text-align: center; color: #333; margin-bottom: 20px; }
        .form-group { margin-bottom: 15px; }
        label { display: block; margin-bottom: 5px; color: #666; font-size: 14px; }
        input { width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 4px; box-sizing: border-box; font-size: 14px; }
        input:focus { border-color: #007bff; outline: none; }
        button { width: 100%; padding: 12px; background-color: #007bff; color: white; border: none; border-radius: 4px; font-size: 16px; cursor: pointer; }
        button:hover { background-color: #0056b3; }
        .msg { padding: 10px; margin-bottom: 15px; border-radius: 4px; text-align: center; font-size: 14px; }
        .msg.error { background: #f8d7da; color: #721c24; }
        .msg.success { background: #d4edda; color: #155724; }
        .back-link { display: block; text-align: center; margin-top: 15px; color: #666; font-size: 13px; text-decoration: none; }
    </style>
</head>
<body>
    <div class="box">
        <h2>修改密码</h2>
        <?php if ($msg): ?>
            <div class="msg <?php echo $msg_type; ?>"><?php echo $msg; ?></div>
        <?php endif; ?>
        
        <!-- 去掉了 required 属性，改用 JS 验证 -->
        <form method="post" id="changePwdForm">
            <div class="form-group">
                <label>原密码</label>
                <input type="text" name="old_password" id="old_password" autocomplete="off">
            </div>
            <div class="form-group">
                <label>新密码</label>
                <input type="text" name="new_password" id="new_password" autocomplete="off">
            </div>
            <div class="form-group">
                <label>确认新密码</label>
                <input type="text" name="confirm_password" id="confirm_password" autocomplete="off">
            </div>
            <button type="submit" id="submitBtn">确认修改</button>
        </form>
        <a href="home.php" class="back-link">← 返回用户中心</a>
    </div>

    <script>
    document.getElementById('changePwdForm').addEventListener('submit', function(e) {
        var oldPass = document.getElementById('old_password').value.trim();
        var newPass = document.getElementById('new_password').value.trim();
        var confirmPass = document.getElementById('confirm_password').value.trim();

        if (!oldPass || !newPass || !confirmPass) {
            e.preventDefault(); // 阻止提交
            alert('所有选项都不能为空！');
            return;
        }
        
        if (newPass.length < 6) {
            e.preventDefault();
            alert('新密码长度不能少于6位！');
            return;
        }

        if (newPass !== confirmPass) {
            e.preventDefault();
            alert('两次输入的新密码不一致！');
            return;
        }
        
        // 验证通过，允许提交
        document.getElementById('submitBtn').innerText = '处理中...';
        document.getElementById('submitBtn').disabled = true;
    });
    </script>
</body>
</html>
