<?php
require_once __DIR__ . '/layout.php';
$db = db();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['change_pw'])) {
        $old = $_POST['old_password'];
        $new = $_POST['new_password'];
        $confirm = $_POST['confirm_password'];
        
        $stmt = $db->prepare("SELECT * FROM admins WHERE id = ?");
        $stmt->execute([$_SESSION['admin_id']]);
        $me = $stmt->fetch();
        
        if ($new !== $confirm) {
            $error = "两次新密码不一致";
        } elseif (!password_verify($old, $me['password_hash'])) {
            $error = "原密码错误";
        } else {
            $hash = password_hash($new, PASSWORD_DEFAULT);
            $db->prepare("UPDATE admins SET password_hash = ? WHERE id = ?")->execute([$hash, $_SESSION['admin_id']]);
            $success = "密码已修改";
        }
    }
}

$title = '个人设置';
ob_start();
?>
<div class="d-flex justify-content-between align-items-center mb-4">
    <div class="d-flex align-items-center gap-3">
        <a href="index.php" class="btn btn-outline-secondary btn-sm"><i class="bi bi-arrow-left"></i> 返回</a>
        <h4 class="mb-0">个人设置</h4>
    </div>
</div>

<div class="row g-4">
    <div class="col-md-6">
        <div class="table-container">
            <h5 class="mb-4">修改管理员密码</h5>
            <?php if (isset($error)): ?><div class="alert alert-danger small"><?= $error ?></div><?php endif; ?>
            <?php if (isset($success)): ?><div class="alert alert-success small"><?= $success ?></div><?php endif; ?>
            
            <form method="POST">
                <input type="hidden" name="change_pw" value="1">
                <div class="mb-3">
                    <label class="form-label">原密码</label>
                    <input type="password" name="old_password" class="form-control" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">新密码</label>
                    <input type="password" name="new_password" class="form-control" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">确认新密码</label>
                    <input type="password" name="confirm_password" class="form-control" required>
                </div>
                <button type="submit" class="btn btn-primary">更新密码</button>
            </form>
        </div>
    </div>
    
    <div class="col-md-6">
        <div class="table-container h-100">
            <h5 class="mb-4">账户信息</h5>
            <div class="mb-3">
                <label class="form-label text-muted">当前用户</label>
                <div class="fw-bold"><?= htmlspecialchars($admin['username']) ?></div>
            </div>
            <div class="mb-3">
                <label class="form-label text-muted">角色</label>
                <div>
                    <span class="badge <?= $admin['is_agent'] ? 'bg-info' : 'bg-primary' ?>">
                        <?= $admin['is_agent'] ? '代理商' : '超级管理员' ?>
                    </span>
                </div>
            </div>
            <?php if ($admin['is_agent']): ?>
            <div class="mb-3">
                <label class="form-label text-muted">拥有的权限</label>
                <div class="d-flex flex-wrap gap-1">
                    <?php 
                    $perms = json_decode($admin['permissions'] ?? '[]', true);
                    foreach($perms as $p) echo "<span class='badge bg-secondary'>$p</span>";
                    if (empty($perms)) echo "<span class='text-muted'>无</span>";
                    ?>
                </div>
            </div>
            <div class="mt-4 p-3 bg-light rounded border">
                <h6><i class="bi bi-info-circle me-1"></i> 代理商提示</h6>
                <p class="small text-muted mb-0">作为代理商，您只能管理分配给您的用户，并根据您的权限执行操作。如有任何疑问，请联系上级管理员。</p>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php
$content = ob_get_clean();
renderAdminPage($content, $title);
?>
