<?php
session_start();
require '../auth/config.php';

// 检查是否登录
if (!isset($_SESSION['user_id']) || !isset($_SESSION['username'])) {
    header("Location: ../auth/index.php"); // 未登录时跳转到登录页面
    exit();
}

// 获取管理员信息
$admin_id = $_SESSION['user_id'];
$admin_username = $_SESSION['username'];

// 检查是否是管理员
$stmt = $conn->prepare("SELECT level FROM users WHERE id = ?");
$stmt->bind_param("i", $admin_id);
$stmt->execute();
$stmt->bind_result($admin_level);
$stmt->fetch();
$stmt->close();

if ($admin_level != 4) {
    echo "您没有权限访问此页面。";
    exit();
}

// 获取要编辑的用户ID
$user_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

// 查询用户信息
$stmt = $conn->prepare("SELECT id, username, email FROM users WHERE id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$stmt->bind_result($id, $username, $email);
$stmt->fetch();
$stmt->close();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // 获取表单提交的数据
    $new_username = $_POST['username'];
    $new_email = $_POST['email'];
    $new_password = $_POST['password'];
    $confirm_password = $_POST['confirm_password'];

    // 验证邮箱格式
    if (!filter_var($new_email, FILTER_VALIDATE_EMAIL)) {
        echo "邮箱格式不正确，请输入有效的邮箱地址。";
        exit();
    }

    // 验证密码是否一致
    if ($new_password !== $confirm_password) {
        echo "密码不一致，请重新输入。";
        exit();
    }

    // 更新用户名和邮箱
    $stmt = $conn->prepare("UPDATE users SET username = ?, email = ? WHERE id = ?");
    $stmt->bind_param("ssi", $new_username, $new_email, $user_id);
    $stmt->execute();

    // 如果密码不为空，则更新密码
    if (!empty($new_password)) {
        $hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
        $stmt = $conn->prepare("UPDATE users SET password = ? WHERE id = ?");
        $stmt->bind_param("si", $hashed_password, $user_id);
        $stmt->execute();
    }

    echo "用户信息更新成功！";
    header("Location: user_management.php");
    exit();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑用户信息</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="top-bar">
    <div class="top-bar-content">
        <h2>编辑用户信息</h2>
    </div>
    <div class="blue-bar"></div>
</div>
<div class="combined-container">
    <div class="header">
        <h1>编辑用户信息</h1>
    </div>
    <div class="user-info">
        <p>管理员: <?php echo htmlspecialchars($admin_username); ?> (UID: <?php echo htmlspecialchars($admin_id); ?>)</p>
    </div>
    <div class="instance-container">
        <form action="edit_user.php?id=<?php echo $user_id; ?>" method="post">
            <label for="username">账号</label>
            <input type="text" id="username" name="username" value="<?php echo htmlspecialchars($username); ?>" required>

            <label for="email">邮箱</label>
            <input type="email" id="email" name="email" value="<?php echo htmlspecialchars($email); ?>" required>

            <label for="password">新密码</label>
            <input type="password" id="password" name="password" placeholder="留空则不修改密码">

            <label for="confirm_password">确认新密码</label>
            <input type="password" id="confirm_password" name="confirm_password" placeholder="留空则不修改密码">

            <button type="submit">保存修改</button>
        </form>
    </div>
    <div class="logout-container">
        <a href="./user_management.php" class="logout-button menu-item">返回用户管理系统</a>
    </div>
</div>
</body>
</html>