<?php
require __DIR__ . '/../../app/bootstrap.php';
require __DIR__ . '/../../app/database.php';
require __DIR__ . '/../../app/admin-sidebar.php';
require_login();

$adminName = isset($_SESSION['admin_name']) ? (string)$_SESSION['admin_name'] : 'Admin';
$adminUsername = isset($_SESSION['admin_username']) ? (string)$_SESSION['admin_username'] : 'admin';
$avatarText = strtoupper(substr($adminName !== '' ? $adminName : 'A', 0, 1));
$error = '';
$success = '';
$recordExists = false;
$adminId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$form = array(
    'password' => '',
    'password_confirm' => ''
);
$targetAdmin = array(
    'username' => '',
    'display_name' => ''
);

if ($adminId <= 0) {
    $error = '缺少有效的管理员ID。';
} else {
    try {
        $row = find_admin_by_id($adminId);
        if (!$row) {
            $error = '未找到对应的管理员记录。';
        } else {
            $recordExists = true;
            $targetAdmin['username'] = isset($row['username']) ? $row['username'] : '';
            $targetAdmin['display_name'] = isset($row['display_name']) ? $row['display_name'] : '';
        }
    } catch (Exception $e) {
        $error = '数据库读取失败：' . $e->getMessage();
    }
}

if ($recordExists && $_SERVER['REQUEST_METHOD'] === 'POST') {
    require_csrf();
    $form['password'] = isset($_POST['password']) ? (string)$_POST['password'] : '';
    $form['password_confirm'] = isset($_POST['password_confirm']) ? (string)$_POST['password_confirm'] : '';

    if ($form['password'] === '') {
        $error = '请输入新密码。';
    } elseif (strlen($form['password']) < 6) {
        $error = '新密码长度不能少于 6 位。';
    } elseif ($form['password'] !== $form['password_confirm']) {
        $error = '两次输入的新密码不一致，请重新输入。';
    } else {
        try {
            $result = reset_admin_password($adminId, $form['password']);
            $success = '管理员 ' . $result['username'] . ' 的密码已重置成功。';
            $form = array('password' => '', 'password_confirm' => '');
        } catch (Exception $e) {
            $error = $e->getMessage();
        }
    }
}
?><!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo e(APP_NAME); ?> - 重置管理员密码</title>
    <link rel="stylesheet" href="/assets/css/admin-ui.css">
</head>
<body>
<div class="admin-shell">
    <?php render_admin_sidebar('admins', $adminName, $adminUsername, $avatarText); ?>
    <main class="admin-content">
        <div class="content-wrap">
            <header class="topbar">
                <div><h1>重置管理员密码</h1><p>为指定管理员设置新的登录密码，提交后立即生效。</p></div>
                <div class="topbar-actions"><a class="btn-ghost" href="/admin/user/list.php">返回列表</a></div>
            </header>
            <section class="page-card">
                <?php if ($error !== ''): ?>
                    <div class="mini-item" style="margin-bottom:16px; border-color: rgba(220,38,38,0.18); background: rgba(220,38,38,0.06);">
                        <strong style="color:#dc2626;">处理失败</strong>
                        <p style="margin:0; color:#b91c1c;"><?php echo e($error); ?></p>
                    </div>
                <?php endif; ?>
                <?php if ($success !== ''): ?>
                    <div class="mini-item" style="margin-bottom:16px; border-color: rgba(34,197,94,0.18); background: rgba(34,197,94,0.06);">
                        <strong style="color:#15803d;">重置成功</strong>
                        <p style="margin:0; color:#166534;"><?php echo e($success); ?></p>
                    </div>
                <?php endif; ?>
                <?php if ($recordExists): ?>
                    <div class="mini-item" style="margin-bottom:16px;">
                        <strong>目标管理员</strong>
                        <p style="margin:6px 0 0;">账号：<?php echo e($targetAdmin['username']); ?> / 显示名称：<?php echo e($targetAdmin['display_name']); ?></p>
                    </div>
                    <form class="form-stack" method="post" action="/admin/user/password.php?id=<?php echo (int)$adminId; ?>">
                        <?php echo csrf_field(); ?>
                        <div class="input-group">
                            <span>新密码</span>
                            <input class="input" type="password" name="password" placeholder="请输入新密码（至少6位）">
                        </div>
                        <div class="input-group">
                            <span>确认新密码</span>
                            <input class="input" type="password" name="password_confirm" placeholder="请再次输入新密码">
                        </div>
                        <div class="filter-row">
                            <button class="btn-primary" type="submit">确认重置</button>
                            <a class="btn-ghost" href="/admin/user/list.php">取消</a>
                        </div>
                    </form>
                <?php endif; ?>
            </section>
        </div>
    </main>
</div>
<script src="/assets/js/admin-ui.js"></script>
</body>
</html>
