<?php $pageTitle = t('group_requests_title'); ?>
<div class="page-header">
    <div class="page-header-left">
        <a href="/admin/users" class="btn btn-ghost btn-sm">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><path d="M19 12H5M12 19l-7-7 7-7"/></svg>
            <?= t('users') ?>
        </a>
        <h1 class="page-title"><?= t('group_requests_title') ?></h1>
        <?php if ($pendingCount > 0): ?>
        <span class="badge badge-warning"><?= $pendingCount ?> <?= t('pending_count') ?></span>
        <?php endif; ?>
    </div>
</div>

<?php if (!empty($success)): ?>
<div class="alert alert-success"><?= Helper::e($success) ?></div>
<?php endif; ?>

<div class="card" style="margin-bottom:1.5rem">
    <div class="card-header">
        <h2 class="card-title"><?= t('group_creation_requests') ?></h2>
    </div>
    <?php if (empty($requests)): ?>
    <div class="card-body">
        <p class="text-muted"><?= t('no_requests') ?></p>
    </div>
    <?php else: ?>
    <div class="card-body" style="padding:0">
        <table class="data-table">
            <thead>
                <tr>
                    <th><?= t('username') ?></th>
                    <th><?= t('email') ?></th>
                    <th><?= t('status_label') ?></th>
                    <th><?= t('date_label') ?></th>
                    <th style="text-align:right"><?= t('actions') ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($requests as $req): ?>
                <tr>
                    <td><strong><?= Helper::e($req['username']) ?></strong></td>
                    <td class="text-muted"><?= Helper::e($req['email']) ?></td>
                    <td>
                        <?php if ($req['status'] === 'pending'): ?>
                            <span class="badge badge-warning"><?= t('pending_status') ?></span>
                        <?php elseif ($req['status'] === 'approved'): ?>
                            <span class="badge badge-success"><?= t('approved_status') ?></span>
                        <?php elseif ($req['status'] === 'revoked'): ?>
                            <span class="badge badge-secondary"><?= t('revoked_status') ?></span>
                        <?php else: ?>
                            <span class="badge badge-danger"><?= t('denied_status') ?></span>
                        <?php endif; ?>
                    </td>
                    <td class="text-muted" style="font-size:.85rem"><?= date('d.m.Y H:i', strtotime($req['created_at'])) ?></td>
                    <td style="text-align:right">
                        <?php if ($req['status'] === 'pending'): ?>
                        <form method="POST" style="display:inline">
                            <?= Helper::csrf() ?>
                            <input type="hidden" name="request_id" value="<?= $req['id'] ?>">
                            <input type="hidden" name="user_id"    value="<?= $req['user_id'] ?>">
                            <button name="action" value="approve" class="btn btn-success btn-sm"><?= t('approve') ?></button>
                            <button name="action" value="deny"    class="btn btn-danger btn-sm" data-confirm-click="<?= Helper::e(t('confirm_deny')) ?>"><?= t('deny') ?></button>
                        </form>
                        <?php elseif ($req['status'] === 'approved'): ?>
                        <form method="POST" style="display:inline" data-confirm="<?= Helper::e(t('confirm_revoke_permission')) ?>">
                            <?= Helper::csrf() ?>
                            <input type="hidden" name="action"  value="revoke">
                            <input type="hidden" name="user_id" value="<?= $req['user_id'] ?>">
                            <button type="submit" class="btn btn-ghost btn-sm"><?= t('revoke_permission') ?></button>
                        </form>
                        <?php elseif ($req['status'] === 'revoked'): ?>
                        <form method="POST" style="display:inline">
                            <?= Helper::csrf() ?>
                            <input type="hidden" name="request_id" value="<?= $req['id'] ?>">
                            <input type="hidden" name="user_id"    value="<?= $req['user_id'] ?>">
                            <button name="action" value="approve" class="btn btn-ghost btn-sm"><?= t('approve_again') ?></button>
                        </form>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>

<?php if (!empty($usersWithoutRequest)): ?>
<div class="card">
    <div class="card-header">
        <h2 class="card-title"><?= t('grant_permission_direct') ?></h2>
    </div>
    <div class="card-body" style="padding:0">
        <table class="data-table">
            <thead>
                <tr>
                    <th><?= t('username') ?></th>
                    <th><?= t('email') ?></th>
                    <th style="text-align:right"><?= t('actions') ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($usersWithoutRequest as $u): ?>
                <tr>
                    <td><strong><?= Helper::e($u['username']) ?></strong></td>
                    <td class="text-muted"><?= Helper::e($u['email']) ?></td>
                    <td style="text-align:right">
                        <form method="POST" style="display:inline">
                            <?= Helper::csrf() ?>
                            <input type="hidden" name="action"  value="grant">
                            <input type="hidden" name="user_id" value="<?= $u['id'] ?>">
                            <button type="submit" class="btn btn-ghost btn-sm"><?= t('grant_permission') ?></button>
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>
