<?php
use BLR\Backline\Modules\Users\Pages as UsersPages;

if (!defined('ABSPATH')) {
    exit;
}

$__blr_ba_data = isset($ctx['users_list']) && is_array($ctx['users_list']) ? $ctx['users_list'] : UsersPages::list_data($ctx);
foreach ($__blr_ba_data as $__blr_ba_key => $__blr_ba_value) {
    ${$__blr_ba_key} = $__blr_ba_value;
}
unset($__blr_ba_data, $__blr_ba_key, $__blr_ba_value);
?>
<div class="container-fluid p-0">
    <div class="d-flex flex-column flex-md-row align-items-md-center justify-content-between gap-3 mb-3">
        <div>
            <h1 class="h3 mb-1">
                <?php echo esc_html__('Users', 'blr-backline'); ?>
                <span class="text-muted small">
                    (<?php echo esc_html(number_format_i18n((int) $users_summary['total'])); ?>)
                </span>
            </h1>
            <div class="text-muted"><?php echo esc_html__('Users with access to this workspace.', 'blr-backline'); ?></div>
        </div>

        <div class="btn-group" role="group" aria-label="<?php echo esc_attr__('Users view mode', 'blr-backline'); ?>">
            <a
                class="btn btn-outline-secondary<?php echo esc_attr($view_mode === 'list' ? ' active' : ''); ?>"
                href="<?php echo esc_url($list_url); ?>"
                aria-pressed="<?php echo esc_attr($view_mode === 'list' ? 'true' : 'false'); ?>"
            >
                <?php echo esc_html__('List', 'blr-backline'); ?>
            </a>
            <a
                class="btn btn-outline-secondary<?php echo esc_attr($view_mode === 'grid' ? ' active' : ''); ?>"
                href="<?php echo esc_url($grid_url); ?>"
                aria-pressed="<?php echo esc_attr($view_mode === 'grid' ? 'true' : 'false'); ?>"
            >
                <?php echo esc_html__('Grid', 'blr-backline'); ?>
            </a>
        </div>
    </div>

    <div class="card flex-fill mb-3">
        <div class="card-body py-3">
            <form method="get" class="row g-2 align-items-end">
                <input type="hidden" name="view" value="<?php echo esc_attr($view_mode); ?>">

                <div class="col-12 col-md-6 col-lg-4">
                    <label class="form-label mb-1" for="blr-ba-users-search"><?php echo esc_html__('Search', 'blr-backline'); ?></label>
                    <input
                        class="form-control"
                        id="blr-ba-users-search"
                        type="search"
                        name="s"
                        value="<?php echo esc_attr($search_term); ?>"
                        placeholder="<?php echo esc_attr__('Name, username, or email', 'blr-backline'); ?>"
                    >
                </div>

                <div class="col-12 col-lg-4">
                    <button class="btn btn-outline-secondary" type="submit"><?php echo esc_html__('Filter', 'blr-backline'); ?></button>
                    <a class="btn btn-link text-decoration-none" href="<?php echo esc_url($base . 'users/'); ?>"><?php echo esc_html__('Reset', 'blr-backline'); ?></a>
                </div>
            </form>
        </div>
    </div>

    <div class="card flex-fill">
        <?php if ($view_mode === 'grid') : ?>
            <div class="card-body">
                <?php if (empty($user_items)) : ?>
                    <p class="text-muted mb-0"><?php echo esc_html__('No Backline users found.', 'blr-backline'); ?></p>
                <?php else : ?>
                    <div class="row g-3">
                        <?php foreach ($user_items as $item) : ?>
                            <div class="col-12 col-md-6 col-xl-4">
                                <div class="card h-100 shadow-sm border-0">
                                    <div class="card-body d-flex flex-column">
                                        <div class="d-flex align-items-center gap-3 mb-3">
                                            <img class="rounded-circle" src="<?php echo esc_url((string) $item['avatar_url']); ?>" alt="" width="56" height="56">
                                            <div class="min-w-0">
                                                <h2 class="h5 mb-1 text-truncate">
                                                    <a class="text-decoration-none" href="<?php echo esc_url((string) $item['profile_url']); ?>">
                                                        <?php echo esc_html((string) $item['display_name']); ?>
                                                    </a>
                                                </h2>
                                                <div class="text-muted small text-truncate"><?php echo esc_html((string) $item['user_email']); ?></div>
                                            </div>
                                        </div>

                                        <div class="mb-3">
                                            <span class="badge <?php echo esc_attr(!empty($item['is_admin']) ? 'bg-primary' : 'bg-secondary'); ?>">
                                                <?php echo esc_html((string) $item['access_label']); ?>
                                            </span>
                                        </div>

                                        <div class="mt-auto">
                                            <a class="btn btn-outline-primary btn-sm" href="<?php echo esc_url((string) $item['profile_url']); ?>">
                                                <?php echo esc_html__('View user', 'blr-backline'); ?>
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        <?php else : ?>
            <div class="table-responsive">
                <table class="table table-hover my-0 align-middle">
                    <thead>
                        <tr>
                            <th><?php echo esc_html__('User', 'blr-backline'); ?></th>
                            <th><?php echo esc_html__('Access', 'blr-backline'); ?></th>
                            <th><?php echo esc_html__('Modules', 'blr-backline'); ?></th>
                            <th><?php echo esc_html__('Joined', 'blr-backline'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($user_items)) : ?>
                            <tr><td colspan="4" class="text-muted"><?php echo esc_html__('No Backline users found.', 'blr-backline'); ?></td></tr>
                        <?php else : ?>
                            <?php foreach ($user_items as $item) : ?>
                                <tr>
                                    <td>
                                        <div class="d-flex align-items-center gap-3">
                                            <img class="rounded-circle" src="<?php echo esc_url((string) $item['avatar_url']); ?>" alt="" width="40" height="40">
                                            <div class="min-w-0">
                                                <a class="fw-semibold text-decoration-none" href="<?php echo esc_url((string) $item['profile_url']); ?>">
                                                    <?php echo esc_html((string) $item['display_name']); ?>
                                                </a>
                                                <div class="text-muted small text-truncate"><?php echo esc_html((string) $item['user_email']); ?></div>
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <span class="badge <?php echo esc_attr(!empty($item['is_admin']) ? 'bg-primary' : 'bg-secondary'); ?>">
                                            <?php echo esc_html((string) $item['access_label']); ?>
                                        </span>
                                    </td>
                                    <td>
                                        <?php if (empty($item['module_labels'])) : ?>
                                            <span class="text-muted">—</span>
                                        <?php else : ?>
                                            <?php foreach ((array) $item['module_labels'] as $module_label) : ?>
                                                <span class="badge bg-light text-dark border me-1 mb-1"><?php echo esc_html((string) $module_label); ?></span>
                                            <?php endforeach; ?>
                                        <?php endif; ?>
                                    </td>
                                    <td><?php echo esc_html((string) ($item['registered_label'] ?: '—')); ?></td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>

    <div class="d-flex flex-column flex-md-row align-items-md-center justify-content-between gap-2 mt-3">
        <div class="text-muted small">
            <?php
            printf(
                /* translators: 1: first visible user number, 2: last visible user number, 3: total user count. */
                esc_html__('Viewing %1$s–%2$s of %3$s users', 'blr-backline'),
                esc_html(number_format_i18n((int) $users_summary['from'])),
                esc_html(number_format_i18n((int) $users_summary['to'])),
                esc_html(number_format_i18n((int) $users_summary['total']))
            );
            ?>
        </div>
        <?php if ($pagination_html !== '') : ?>
            <?php echo wp_kses_post($pagination_html); ?>
        <?php endif; ?>
    </div>
</div>
