<?php require __DIR__ . '/settings_nav.php'; ?>
<div class="mb-3 d-flex flex-wrap align-items-center gap-3">
    <div class="me-auto">
        <h2 class="mb-1"><?= h(t('settings.users')) ?></h2>
        <div class="text-muted"><?= h(t('settings.users_intro')) ?></div>
    </div><a class="btn btn-dark" href="/settings/permissions">🔐 <?= h(t('settings.configure_permissions')) ?></a>
</div>
<div class="card mb-3">
    <div class="card-header">
        <h3 class="card-title"><?= h(t('settings.add_user')) ?></h3>
    </div>
    <form method="post" action="/settings/users/save">
        <div class="card-body row g-3">
            <div class="col-md-3"><label class="form-label"><?= h(t('settings.display_name')) ?></label><input
                    class="form-control" name="display_name" required></div>
            <div class="col-md-3"><label class="form-label"><?= h(t('settings.username')) ?></label><input
                    class="form-control" name="username" required></div>
            <div class="col-md-2"><label class="form-label"><?= h(t('settings.role')) ?></label><select
                    class="form-select" name="role"><?php foreach ($roles as $key => $label): ?><option
                        value="<?= h($key) ?>"><?= h(PermissionService::roleLabel($key)) ?></option>
                    <?php endforeach; ?></select></div>
            <div class="col-md-3"><label class="form-label"><?= h(t('settings.initial_password')) ?></label><input
                    class="form-control" type="password" name="password" minlength="10" required></div>
            <div class="col-md-1 d-flex align-items-end"><label class="form-check mb-2"><input class="form-check-input"
                        type="checkbox" name="active" checked><span
                        class="form-check-label"><?= h(t('settings.active')) ?></span></label></div>
        </div>
        <div class="card-footer text-end"><button class="btn btn-primary"><?= h(t('settings.create')) ?></button></div>
    </form>
</div>
<div class="vstack gap-2"><?php foreach ($users as $user): ?><form method="post" action="/settings/users/save"
        class="card card-sm"><input type="hidden" name="id" value="<?= (int)$user['id'] ?>">
        <div class="card-body row g-2 align-items-end">
            <div class="col-md-3"><label class="form-label"><?= h(t('settings.display_name')) ?></label><input
                    class="form-control" name="display_name"
                    value="<?= h($user['display_name'] ?: $user['username']) ?>" required></div>
            <div class="col-md-2"><label class="form-label"><?= h(t('settings.username')) ?></label><input
                    class="form-control" name="username" value="<?= h($user['username']) ?>" required></div>
            <div class="col-md-2"><label class="form-label"><?= h(t('settings.role')) ?></label><select
                    class="form-select" name="role"><?php foreach ($roles as $key => $label): ?><option
                        value="<?= h($key) ?>" <?= $user['role'] === $key ? 'selected' : '' ?>>
                        <?= h(PermissionService::roleLabel($key)) ?></option><?php endforeach; ?></select></div>
            <div class="col-md-2"><label class="form-label"><?= h(t('settings.new_password')) ?></label><input
                    class="form-control" type="password" name="password" minlength="10"
                    placeholder="<?= h(t('settings.unchanged')) ?>"></div>
            <div class="col-md-1"><label class="form-check mb-2"><input class="form-check-input" type="checkbox"
                        name="active" <?= (int)$user['active'] === 1 ? 'checked' : '' ?>><span
                        class="form-check-label"><?= h(t('settings.active')) ?></span></label></div>
            <div class="col-md-2 text-end"><button class="btn btn-outline-primary"><?= h(t('common.save')) ?></button>
                <div class="small text-muted mt-1">
                    <?= $user['last_login_at'] ? t('settings.seen', ['date' => $user['last_login_at']]) : t('settings.never_logged') ?>
                </div>
            </div>
        </div>
    </form><?php endforeach; ?></div>
