/* tslint:disable:component-selector */
import { Component, Input } from '@angular/core';
import { RoleFormComponent } from './role-form.component';
import * as i0 from "@angular/core";
import * as i1 from "../core";
import * as i2 from "@angular/router";
import * as i3 from "../user-config";
import * as i4 from "@angular/material/dialog";
import * as i5 from "../user";
import * as i6 from "@angular/common";
import * as i7 from "@angular/material/button";
import * as i8 from "@angular/material/icon";
import * as i9 from "@angular/material/table";
import * as i10 from "@angular/material/card";
import * as i11 from "@angular/material/progress-bar";
import * as i12 from "@angular/flex-layout/flex";
import * as i13 from "@angular/flex-layout/extended";
import * as i14 from "ngx-infinite-scroll";
import * as i15 from "./role-form-button.component";
/**
 * Component used to view a set of roles
 *
 * @author Joshua Kissoon
 * @since 20160818
 */
export class RolesViewAllComponent {
    constructor(http, route, credentialsService, userConfigService, dialog, currentUserService) {
        this.http = http;
        this.route = route;
        this.credentialsService = credentialsService;
        this.userConfigService = userConfigService;
        this.dialog = dialog;
        this.currentUserService = currentUserService;
        this.roles = [];
        this.displayedColumns = [
            'index',
            'label',
            'description',
            'module',
            'users',
            'permissions',
            'actions'
        ];
        this.totalItems = 0;
        this.currentPage = 1;
        this.isWorking = false;
        this.moduleCode = null;
        this.config = this.userConfigService.getConfig();
    }
    get isAdminUser() {
        return (this.credentialsService.credentials?.user?.user_type_id === 1);
    }
    ngOnInit() {
        this.route.params.subscribe(params => {
            if (params.module_code !== undefined) {
                this.moduleCode = params.module_code;
            }
            this.loadData();
        });
        if (this.isAdminUser) {
            this.displayedColumns = [
                'index',
                'label',
                'description',
                'module',
                'editable',
                'is_global',
                'users',
                'actions',
                'permissions'
            ];
        }
        if (!(this.config?.roleConfig?.showModuleSelectorOnForm)) {
            this.displayedColumns = this.displayedColumns.filter(x => x !== 'module');
        }
    }
    loadData() {
        this.isWorking = true;
        const filters = {
            withCount: 'permissions',
            sort_by: 'label|asc'
        };
        if (this.moduleCode) {
            filters.module_code = this.moduleCode;
        }
        if (this.clientId) {
            filters.client_id = this.clientId;
        }
        this.http.get('api/roles', { params: filters }).toPromise().then((res) => {
            this.isWorking = false;
            this.totalItems = res.total;
            this.roles = this.roles.concat(res.data);
        }, () => {
            this.isWorking = false;
        });
    }
    reload() {
        this.roles = [];
        this.currentPage = 1;
        if (!this.isWorking) {
            this.loadData();
        }
    }
    onScroll() {
        if (!this.isWorking && this.totalItems > this.roles.length) {
            this.currentPage += 1;
            this.loadData();
        }
    }
    createOrModifyRole(role) {
        const dialogRef = this.dialog.open(RoleFormComponent, {
            width: '500px',
            data: {
                role,
                client_id: this.clientId,
                module_code: this.moduleCode
            }
        });
        dialogRef.afterClosed().subscribe(result => {
            if (result) {
                this.reload();
            }
        });
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: RolesViewAllComponent, deps: [{ token: i1.HttpService }, { token: i2.ActivatedRoute }, { token: i1.CredentialsService }, { token: i3.UserConfigService }, { token: i4.MatDialog }, { token: i5.CurrentUserService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: RolesViewAllComponent, selector: "roles-view-all", inputs: { clientId: "clientId" }, ngImport: i0, template: "<div fxLayout=\"column\" class=\"m-2\">\n  <mat-card appearance=\"outlined\">\n    <mat-card-content class=\"d-flex justify-content-start align-items-center gap-2\">\n      <h3 class=\"m-0\">\n        Manage Roles\n      </h3>\n      <app-role-form-button (success)=\"reload()\" class=\"mt-2\"\n                            *ngIf=\"(!config.roleConfig.disableAddRole) && currentUserService?.hasPermission('role_create')\"\n                            [clientId]=\"clientId\"></app-role-form-button>\n      <button mat-raised-button type=\"button\" [routerLink]=\"['../users']\" *ngIf=\"!config.roleConfig.disableUsers\"\n              class=\"ml-1\">\n        <mat-icon>people</mat-icon>\n        Users\n      </button>\n      <button mat-raised-button type=\"button\" [routerLink]=\"['../role-permissions']\"\n              *ngIf=\"!config.roleConfig.disablePermissions\" class=\"ml-1\">\n        <mat-icon>lock</mat-icon>\n        Role Permissions\n      </button>\n    </mat-card-content>\n  </mat-card>\n\n  <mat-card appearance=\"outlined\" class=\"mt-2 p-0\">\n    <mat-card-content>\n      <table mat-table [dataSource]=\"roles\" class=\"w-100\" infiniteScroll [infiniteScrollDistance]=\"2\"\n        [infiniteScrollThrottle]=\"50\" (scrolled)=\"onScroll()\" aria-describedby=\"Roles\">\n\n        <!-- Index Column -->\n        <ng-container matColumnDef=\"index\">\n          <th mat-header-cell *matHeaderCellDef> # </th>\n          <td mat-cell *matCellDef=\"let element; let i = index\"> {{ i+1 }} </td>\n        </ng-container>\n\n        <!-- Label Column -->\n        <ng-container matColumnDef=\"label\">\n          <th mat-header-cell *matHeaderCellDef> Label </th>\n          <td mat-cell *matCellDef=\"let element\">\n            <p class=\"mb-0\">{{element?.label}}</p>\n            <span class=\"badge\"\n              [ngClass]=\"{'badge-primary':element?.is_global, 'badge-secondary': !element?.is_global}\">\n              {{element?.is_global ? 'App role' : 'Custom role'}}\n            </span>\n          </td>\n        </ng-container>\n\n        <!-- Description Column -->\n        <ng-container matColumnDef=\"description\">\n          <th mat-header-cell *matHeaderCellDef> Description </th>\n          <td mat-cell *matCellDef=\"let element\"> {{element?.description }} </td>\n        </ng-container>\n\n        <!-- Module Column -->\n        <ng-container matColumnDef=\"module\">\n          <th mat-header-cell *matHeaderCellDef> Module </th>\n          <td mat-cell *matCellDef=\"let element\"> {{element?.module?.name}} </td>\n        </ng-container>\n\n        <!-- Editable Column -->\n        <ng-container matColumnDef=\"editable\">\n          <th mat-header-cell *matHeaderCellDef> Editable </th>\n          <td mat-cell *matCellDef=\"let element\">\n            <span *ngIf=\"element?.is_editable\" class=\"btn btn-success btn-xs\">Yes</span>\n            <span *ngIf=\"!element?.is_editable\" class=\"btn btn-danger btn-xs\">No</span>\n          </td>\n        </ng-container>\n\n        <!-- GLobal Column -->\n        <ng-container matColumnDef=\"is_global\">\n          <th mat-header-cell *matHeaderCellDef> Global </th>\n          <td mat-cell *matCellDef=\"let element\">\n            <span *ngIf=\"element?.is_global\" class=\"btn btn-success btn-xs\">Yes</span>\n            <span *ngIf=\"!element?.is_global\" class=\"btn btn-danger btn-xs\">No</span>\n          </td>\n        </ng-container>\n\n        <!-- Users Column -->\n        <ng-container matColumnDef=\"users\">\n          <th mat-header-cell *matHeaderCellDef> # Users </th>\n          <td mat-cell *matCellDef=\"let element\"> {{element?.role_users_count}} </td>\n        </ng-container>\n\n        <!-- Permissions Column -->\n        <ng-container matColumnDef=\"permissions\">\n          <th mat-header-cell *matHeaderCellDef> Permissions </th>\n          <td mat-cell *matCellDef=\"let element\"> {{element?.permissions_count}} </td>\n        </ng-container>\n\n        <!-- Actions Column -->\n        <ng-container matColumnDef=\"actions\">\n          <th mat-header-cell *matHeaderCellDef> Actions </th>\n          <td mat-cell *matCellDef=\"let role\" class=\"text-muted\">\n            <button mat-icon-button type=\"button\" [routerLink]=\"['../roles/', role?.id]\">\n              <mat-icon>remove_red_eye</mat-icon>\n            </button>\n            <app-role-form-button (success)=\"reload()\" [role]=\"role\" [isUpdate]=\"true\"\n              *ngIf=\"(isAdminUser || role.is_editable) && currentUserService.hasPermission('role_update')\"\n              [clientId]=\"clientId\"></app-role-form-button>\n\n          </td>\n        </ng-container>\n\n        <tr mat-header-row *matHeaderRowDef=\"displayedColumns\"></tr>\n        <tr mat-row *matRowDef=\"let row; columns: displayedColumns;\"></tr>\n      </table>\n      <mat-progress-bar *ngIf=\"isWorking\" mode=\"indeterminate\"></mat-progress-bar>\n    </mat-card-content>\n  </mat-card>\n\n</div>\n", dependencies: [{ kind: "directive", type: i6.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i6.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i2.RouterLink, selector: "[routerLink]", inputs: ["target", "queryParams", "fragment", "queryParamsHandling", "state", "info", "relativeTo", "preserveFragment", "skipLocationChange", "replaceUrl", "routerLink"] }, { kind: "component", type: i7.MatButton, selector: "    button[mat-button], button[mat-raised-button], button[mat-flat-button],    button[mat-stroked-button]  ", exportAs: ["matButton"] }, { kind: "component", type: i7.MatIconButton, selector: "button[mat-icon-button]", exportAs: ["matButton"] }, { kind: "component", type: i8.MatIcon, selector: "mat-icon", inputs: ["color", "inline", "svgIcon", "fontSet", "fontIcon"], exportAs: ["matIcon"] }, { kind: "component", type: i9.MatTable, selector: "mat-table, table[mat-table]", exportAs: ["matTable"] }, { kind: "directive", type: i9.MatHeaderCellDef, selector: "[matHeaderCellDef]" }, { kind: "directive", type: i9.MatHeaderRowDef, selector: "[matHeaderRowDef]", inputs: ["matHeaderRowDef", "matHeaderRowDefSticky"] }, { kind: "directive", type: i9.MatColumnDef, selector: "[matColumnDef]", inputs: ["matColumnDef"] }, { kind: "directive", type: i9.MatCellDef, selector: "[matCellDef]" }, { kind: "directive", type: i9.MatRowDef, selector: "[matRowDef]", inputs: ["matRowDefColumns", "matRowDefWhen"] }, { kind: "directive", type: i9.MatHeaderCell, selector: "mat-header-cell, th[mat-header-cell]" }, { kind: "directive", type: i9.MatCell, selector: "mat-cell, td[mat-cell]" }, { kind: "component", type: i9.MatHeaderRow, selector: "mat-header-row, tr[mat-header-row]", exportAs: ["matHeaderRow"] }, { kind: "component", type: i9.MatRow, selector: "mat-row, tr[mat-row]", exportAs: ["matRow"] }, { kind: "component", type: i10.MatCard, selector: "mat-card", inputs: ["appearance"], exportAs: ["matCard"] }, { kind: "directive", type: i10.MatCardContent, selector: "mat-card-content" }, { kind: "component", type: i11.MatProgressBar, selector: "mat-progress-bar", inputs: ["color", "value", "bufferValue", "mode"], outputs: ["animationEnd"], exportAs: ["matProgressBar"] }, { kind: "directive", type: i12.DefaultLayoutDirective, selector: "  [fxLayout], [fxLayout.xs], [fxLayout.sm], [fxLayout.md],  [fxLayout.lg], [fxLayout.xl], [fxLayout.lt-sm], [fxLayout.lt-md],  [fxLayout.lt-lg], [fxLayout.lt-xl], [fxLayout.gt-xs], [fxLayout.gt-sm],  [fxLayout.gt-md], [fxLayout.gt-lg]", inputs: ["fxLayout", "fxLayout.xs", "fxLayout.sm", "fxLayout.md", "fxLayout.lg", "fxLayout.xl", "fxLayout.lt-sm", "fxLayout.lt-md", "fxLayout.lt-lg", "fxLayout.lt-xl", "fxLayout.gt-xs", "fxLayout.gt-sm", "fxLayout.gt-md", "fxLayout.gt-lg"] }, { kind: "directive", type: i13.DefaultClassDirective, selector: "  [ngClass], [ngClass.xs], [ngClass.sm], [ngClass.md], [ngClass.lg], [ngClass.xl],  [ngClass.lt-sm], [ngClass.lt-md], [ngClass.lt-lg], [ngClass.lt-xl],  [ngClass.gt-xs], [ngClass.gt-sm], [ngClass.gt-md], [ngClass.gt-lg]", inputs: ["ngClass", "ngClass.xs", "ngClass.sm", "ngClass.md", "ngClass.lg", "ngClass.xl", "ngClass.lt-sm", "ngClass.lt-md", "ngClass.lt-lg", "ngClass.lt-xl", "ngClass.gt-xs", "ngClass.gt-sm", "ngClass.gt-md", "ngClass.gt-lg"] }, { kind: "directive", type: i14.InfiniteScrollDirective, selector: "[infiniteScroll], [infinite-scroll], [data-infinite-scroll]", inputs: ["infiniteScrollDistance", "infiniteScrollUpDistance", "infiniteScrollThrottle", "infiniteScrollDisabled", "infiniteScrollContainer", "scrollWindow", "immediateCheck", "horizontal", "alwaysCallback", "fromRoot"], outputs: ["scrolled", "scrolledUp"] }, { kind: "component", type: i15.RoleFormButtonComponent, selector: "app-role-form-button", inputs: ["config", "clientId", "isUpdate", "role"], outputs: ["success"] }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: RolesViewAllComponent, decorators: [{
            type: Component,
            args: [{ selector: 'roles-view-all', template: "<div fxLayout=\"column\" class=\"m-2\">\n  <mat-card appearance=\"outlined\">\n    <mat-card-content class=\"d-flex justify-content-start align-items-center gap-2\">\n      <h3 class=\"m-0\">\n        Manage Roles\n      </h3>\n      <app-role-form-button (success)=\"reload()\" class=\"mt-2\"\n                            *ngIf=\"(!config.roleConfig.disableAddRole) && currentUserService?.hasPermission('role_create')\"\n                            [clientId]=\"clientId\"></app-role-form-button>\n      <button mat-raised-button type=\"button\" [routerLink]=\"['../users']\" *ngIf=\"!config.roleConfig.disableUsers\"\n              class=\"ml-1\">\n        <mat-icon>people</mat-icon>\n        Users\n      </button>\n      <button mat-raised-button type=\"button\" [routerLink]=\"['../role-permissions']\"\n              *ngIf=\"!config.roleConfig.disablePermissions\" class=\"ml-1\">\n        <mat-icon>lock</mat-icon>\n        Role Permissions\n      </button>\n    </mat-card-content>\n  </mat-card>\n\n  <mat-card appearance=\"outlined\" class=\"mt-2 p-0\">\n    <mat-card-content>\n      <table mat-table [dataSource]=\"roles\" class=\"w-100\" infiniteScroll [infiniteScrollDistance]=\"2\"\n        [infiniteScrollThrottle]=\"50\" (scrolled)=\"onScroll()\" aria-describedby=\"Roles\">\n\n        <!-- Index Column -->\n        <ng-container matColumnDef=\"index\">\n          <th mat-header-cell *matHeaderCellDef> # </th>\n          <td mat-cell *matCellDef=\"let element; let i = index\"> {{ i+1 }} </td>\n        </ng-container>\n\n        <!-- Label Column -->\n        <ng-container matColumnDef=\"label\">\n          <th mat-header-cell *matHeaderCellDef> Label </th>\n          <td mat-cell *matCellDef=\"let element\">\n            <p class=\"mb-0\">{{element?.label}}</p>\n            <span class=\"badge\"\n              [ngClass]=\"{'badge-primary':element?.is_global, 'badge-secondary': !element?.is_global}\">\n              {{element?.is_global ? 'App role' : 'Custom role'}}\n            </span>\n          </td>\n        </ng-container>\n\n        <!-- Description Column -->\n        <ng-container matColumnDef=\"description\">\n          <th mat-header-cell *matHeaderCellDef> Description </th>\n          <td mat-cell *matCellDef=\"let element\"> {{element?.description }} </td>\n        </ng-container>\n\n        <!-- Module Column -->\n        <ng-container matColumnDef=\"module\">\n          <th mat-header-cell *matHeaderCellDef> Module </th>\n          <td mat-cell *matCellDef=\"let element\"> {{element?.module?.name}} </td>\n        </ng-container>\n\n        <!-- Editable Column -->\n        <ng-container matColumnDef=\"editable\">\n          <th mat-header-cell *matHeaderCellDef> Editable </th>\n          <td mat-cell *matCellDef=\"let element\">\n            <span *ngIf=\"element?.is_editable\" class=\"btn btn-success btn-xs\">Yes</span>\n            <span *ngIf=\"!element?.is_editable\" class=\"btn btn-danger btn-xs\">No</span>\n          </td>\n        </ng-container>\n\n        <!-- GLobal Column -->\n        <ng-container matColumnDef=\"is_global\">\n          <th mat-header-cell *matHeaderCellDef> Global </th>\n          <td mat-cell *matCellDef=\"let element\">\n            <span *ngIf=\"element?.is_global\" class=\"btn btn-success btn-xs\">Yes</span>\n            <span *ngIf=\"!element?.is_global\" class=\"btn btn-danger btn-xs\">No</span>\n          </td>\n        </ng-container>\n\n        <!-- Users Column -->\n        <ng-container matColumnDef=\"users\">\n          <th mat-header-cell *matHeaderCellDef> # Users </th>\n          <td mat-cell *matCellDef=\"let element\"> {{element?.role_users_count}} </td>\n        </ng-container>\n\n        <!-- Permissions Column -->\n        <ng-container matColumnDef=\"permissions\">\n          <th mat-header-cell *matHeaderCellDef> Permissions </th>\n          <td mat-cell *matCellDef=\"let element\"> {{element?.permissions_count}} </td>\n        </ng-container>\n\n        <!-- Actions Column -->\n        <ng-container matColumnDef=\"actions\">\n          <th mat-header-cell *matHeaderCellDef> Actions </th>\n          <td mat-cell *matCellDef=\"let role\" class=\"text-muted\">\n            <button mat-icon-button type=\"button\" [routerLink]=\"['../roles/', role?.id]\">\n              <mat-icon>remove_red_eye</mat-icon>\n            </button>\n            <app-role-form-button (success)=\"reload()\" [role]=\"role\" [isUpdate]=\"true\"\n              *ngIf=\"(isAdminUser || role.is_editable) && currentUserService.hasPermission('role_update')\"\n              [clientId]=\"clientId\"></app-role-form-button>\n\n          </td>\n        </ng-container>\n\n        <tr mat-header-row *matHeaderRowDef=\"displayedColumns\"></tr>\n        <tr mat-row *matRowDef=\"let row; columns: displayedColumns;\"></tr>\n      </table>\n      <mat-progress-bar *ngIf=\"isWorking\" mode=\"indeterminate\"></mat-progress-bar>\n    </mat-card-content>\n  </mat-card>\n\n</div>\n" }]
        }], ctorParameters: () => [{ type: i1.HttpService }, { type: i2.ActivatedRoute }, { type: i1.CredentialsService }, { type: i3.UserConfigService }, { type: i4.MatDialog }, { type: i5.CurrentUserService }], propDecorators: { clientId: [{
                type: Input
            }] } });
//# sourceMappingURL=data:application/json;base64, 