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

// 检查是否登录
if (!isset($_SESSION['admin_logged_in']) || !$_SESSION['admin_logged_in']) {
    header('Location: login.php');
    exit;
}

// 设置页面标题和当前页面
$page_title = '修改密码';
$active_page = 'password';

// 初始化变量
$error = '';
$success = '';

// 处理密码修改请求
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $old_password = isset($_POST['old_password']) ? trim($_POST['old_password']) : '';
    $new_password = isset($_POST['new_password']) ? trim($_POST['new_password']) : '';
    $confirm_password = isset($_POST['confirm_password']) ? trim($_POST['confirm_password']) : '';

    // 获取当前密码
    $conn = db_connect();
    $stmt = $conn->prepare("SELECT password FROM admin WHERE id = ?");
    $stmt->bind_param("i", $_SESSION['admin_id']);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result && $admin = $result->fetch_assoc()) {
        // 验证旧密码
        $input_old_password_hash = hash('sha256', $old_password . SALT);
        if ($input_old_password_hash !== $admin['password']) {
            $error = '旧密码不正确';
        }
        // 验证新密码
        elseif (strlen($new_password) < 6) {
            $error = '新密码长度不能小于6位';
        }
        // 验证确认密码
        elseif ($new_password !== $confirm_password) {
            $error = '两次输入的密码不一致';
        }
        else {
            // 更新密码
            $new_password_hash = hash('sha256', $new_password . SALT);
            $update_stmt = $conn->prepare("UPDATE admin SET password = ? WHERE id = ?");
            $update_stmt->bind_param("si", $new_password_hash, $_SESSION['admin_id']);
            
            if ($update_stmt->execute()) {
                $success = '密码修改成功';
            } else {
                $error = '密码修改失败：' . $update_stmt->error;
            }
            $update_stmt->close();
        }
    } else {
        $error = '获取用户信息失败';
    }
    
    $stmt->close();
    $conn->close();
}

// 包含头部
include 'includes/header.php';
?>

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-6">
            <div class="card mt-4">
                <div class="card-header">
                    <h5 class="mb-0">修改密码</h5>
                </div>
                <div class="card-body">
                    <?php if ($error): ?>
                        <div class="alert alert-danger"><?php echo $error; ?></div>
                    <?php endif; ?>
                    
                    <?php if ($success): ?>
                        <div class="alert alert-success"><?php echo $success; ?></div>
                    <?php endif; ?>

                    <form method="post" class="needs-validation" novalidate>
                        <div class="mb-3">
                            <label for="old_password" class="form-label">旧密码</label>
                            <input type="password" class="form-control" id="old_password" name="old_password" required>
                        </div>

                        <div class="mb-3">
                            <label for="new_password" class="form-label">新密码</label>
                            <input type="password" class="form-control" id="new_password" name="new_password" required>
                            <div class="form-text">密码长度不能小于6位</div>
                        </div>

                        <div class="mb-3">
                            <label for="confirm_password" class="form-label">确认新密码</label>
                            <input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
                        </div>

                        <div class="d-grid">
                            <button type="submit" class="btn btn-primary">修改密码</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include 'includes/footer.php'; ?>
