<ol class="breadcrumb breadcrumb-arrow font-trirong mm-breadcrumb">
  <li><a href="javascript://" [routerLink]="['/cns-portal/dashboard/order']"><clr-icon shape="home"></clr-icon></a></li>
  <li><a href="javascript://"><clr-icon shape="lock"></clr-icon>{{ 'SECURITY' | translate }}</a></li>
  <li><a href="javascript://">{{'USER_MAINTENANCE'| translate}}</a></li>
</ol>

<section class="um-hero">
  <div class="um-hero__content">
    <div class="um-hero__icon"><clr-icon shape="user"></clr-icon></div>
    <div>
      <h2 class="um-hero__title">{{'USER_MAINTENANCE'| translate}}</h2>
      <p class="um-hero__subtitle">{{ 'SECURITY' | translate }}</p>
    </div>
  </div>
  <div class="um-hero__actions">
    <button class="um-btn um-btn-secondary" (click)="downloadFiles()">
      <clr-icon shape="download"></clr-icon>
      <span class="um-btn-text">{{ 'DOWNLOAD_TEMPLATE' | translate }}</span>
    </button>
    <button class="um-btn um-btn-secondary" (click)="csvImport()">
      <clr-icon shape="import"></clr-icon>
      <span class="um-btn-text">{{ 'IMPORT' | translate }}</span>
    </button>
    <button class="um-btn um-btn-secondary" (click)="onExport()">
      <clr-icon shape="export"></clr-icon>
      <span class="um-btn-text">{{ 'EXPORT_XLSX' | translate }}</span>
    </button>
    <button id="add" class="um-btn um-btn-primary" (click)="goToAdd()">
      <clr-icon shape="plus"></clr-icon>
      <span class="um-btn-text">{{ 'ADD' | translate }}</span>
    </button>
    <!-- <button id="add" class="um-btn um-btn-primary" *ngIf="mcreate == 'true' || mcreate == true" (click)="goToAdd()">
      <clr-icon shape="plus"></clr-icon>
      <span class="um-btn-text">{{ 'ADD' | translate }}</span>
    </button> -->
  </div>
</section>

<div class="um-toolbar">
  <div class="um-toolbar__left">
    <div class="um-search">
      <clr-icon shape="search"></clr-icon>
      <input class="um-search__input" type="text" [placeholder]="'SEARCH' | translate" [(ngModel)]="filterText" />
    </div>
  </div>
  <div class="um-toolbar__right">
    <div class="um-view-toggle">
      <button class="um-btn um-btn-sm" [class.um-btn-primary]="viewMode==='cards'" (click)="setViewMode('cards')">
        <clr-icon shape="view-cards"></clr-icon>
      </button>
      <button class="um-btn um-btn-sm" [class.um-btn-primary]="viewMode==='table'" (click)="setViewMode('table')">
        <clr-icon shape="table"></clr-icon>
      </button>
    </div>
  </div>
</div>

<clr-datagrid class="um-grid" [clrDgLoading]="loading" *ngIf="viewMode==='table'">
  <clr-dg-placeholder>
    <ng-template #loadingSpinner>
      <clr-spinner>{{'LOADING' | translate}} ... </clr-spinner>
    </ng-template>
    <div *ngIf="error;else loadingSpinner">{{error}}</div>
  </clr-dg-placeholder>

  <clr-dg-column [clrDgField]="'userId'">
    <ng-container *clrDgHideableColumn="{hidden: false}">
      {{'USER_ID' | translate}}
    </ng-container>
  </clr-dg-column>
  <clr-dg-column [clrDgField]="'fullName'" style="width: 200px;">
    <ng-container *clrDgHideableColumn="{hidden: false}">
      {{'FULL_NAME' | translate}}
    </ng-container>
  </clr-dg-column>
  <clr-dg-column [clrDgField]="'email'">
    <ng-container *clrDgHideableColumn="{hidden: false}">
      {{'EMAIL' | translate}}
    </ng-container>
  </clr-dg-column>
  <clr-dg-column [clrDgField]="'usrGrpName'">
    <ng-container *clrDgHideableColumn="{hidden: false}">
      {{'USER_GROUP_NAME' | translate}}
    </ng-container>
  </clr-dg-column>

  <clr-dg-row *clrDgItems="let user of filteredUsers;let i = index" [clrDgItem]="user">
    <clr-dg-cell>{{user.userId}}</clr-dg-cell>
    <clr-dg-cell>{{user.fullName}}</clr-dg-cell>
    <clr-dg-cell>{{user.email}}</clr-dg-cell>
    <clr-dg-cell>{{user.usrGrpName}}</clr-dg-cell>

    <clr-dg-action-overflow>
      <!-- <button class="um-action-item" *ngIf="medit == 'true' || medit == true" (click)="goToEdit(user.userId)">
        <clr-icon shape="edit"></clr-icon>
        {{'EDIT' | translate}}
      </button> -->
      <button class="um-action-item" (click)="goToEdit(user.userId)">
        <clr-icon shape="edit"></clr-icon>
        {{'EDIT' | translate}}
      </button>
      <button class="um-action-item um-btn-error" (click)="onDelete(user)">
        <clr-icon shape="trash"></clr-icon>
        {{'DELETE'|translate}}
      </button>
    </clr-dg-action-overflow>

    <clr-dg-row-detail *clrIfExpanded>
      <table class="table">
        <tr>
          <td class="td-title">username</td>
          <td class="td-content">{{user.username}}</td>
        </tr>
      </table>
    </clr-dg-row-detail>
  </clr-dg-row>

  <clr-dg-footer class="um-grid-footer">
    <clr-dg-pagination #pagination [clrDgPageSize]="10">
      <clr-dg-page-size [clrPageSizeOptions]="[10,20,50,100]">Record per page</clr-dg-page-size>
      {{pagination.firstItem + 1}} - {{pagination.lastItem + 1}}
      of {{pagination.totalItems}} Records
    </clr-dg-pagination>
  </clr-dg-footer>
</clr-datagrid>

<div class="um-cards" *ngIf="viewMode==='cards'">
  <div class="um-card-item" *ngFor="let user of filteredUsers; let i = index">
    <div class="um-card-item__header">
      <div class="um-card-item__icon"><clr-icon shape="user"></clr-icon></div>
      <div class="um-card-item__title">{{user.fullName}}</div>
      <div class="um-card-item__badge">ID: {{user.userId}}</div>
    </div>
    <div class="um-card-item__body">
      <div class="um-kv"><span>{{'EMAIL' | translate}}</span><strong>{{user.email}}</strong></div>
      <div class="um-kv"><span>{{'GROUP' | translate}}</span><strong>{{user.usrGrpName}}</strong></div>
    </div>
    <div class="um-card-item__footer">
      <!-- <button class="um-btn um-btn-sm um-btn-outline" *ngIf="medit == 'true' || medit == true"
        (click)="goToEdit(user.userId)">
        <clr-icon shape="edit"></clr-icon> {{'EDIT' | translate}}
      </button> -->

      <button class="um-btn um-btn-sm um-btn-outline" (click)="goToEdit(user.userId)">
        <clr-icon shape="edit"></clr-icon> {{'EDIT' | translate}}
      </button>
      <button class="um-btn um-btn-sm um-btn-error" (click)="onDelete(user)">
        <clr-icon shape="trash"></clr-icon> {{'DELETE' | translate}}
      </button>
    </div>
  </div>
</div>

<clr-modal [(clrModalOpen)]="modaldelete" [clrModalSize]="'lg'" [clrModalStaticBackdrop]="true">
  <div class="modal-body" *ngIf="rowSelected.userId">
    <div class="um-delete-header">
      <div class="um-delete-icon">
        <clr-icon shape="exclamation-triangle" size="48"></clr-icon>
      </div>
      <h1 class="um-delete-title">{{'ARE_YOU_SURE_WANT_TO_DELETE' | translate}}?</h1>
      <p class="um-delete-subtitle">{{'THIS_ACTION_CANNOT_BE_UNDONE' | translate}}</p>
    </div>

    <div class="um-delete-details">
      <div class="um-delete-detail-item">
        <span class="um-delete-detail-label">{{'USER_ID' | translate}}:</span>
        <span class="um-delete-detail-value">{{rowSelected.userId}}</span>
      </div>
      <div class="um-delete-detail-item">
        <span class="um-delete-detail-label">{{'FULL_NAME' | translate}}:</span>
        <span class="um-delete-detail-value">{{rowSelected.fullName}}</span>
      </div>
      <div class="um-delete-detail-item">
        <span class="um-delete-detail-label">{{'EMAIL' | translate}}:</span>
        <span class="um-delete-detail-value">{{rowSelected.email}}</span>
      </div>
    </div>

    <div class="modal-footer">
      <button type="button" class="um-btn um-btn-outline" (click)="modaldelete = false">{{'CANCEL' |
        translate}}</button>
      <button type="submit" (click)="delete(rowSelected.userId)" class="um-btn um-btn-error">{{'DELETE' |
        translate}}</button>
    </div>
  </div>
</clr-modal>

<clr-modal [(clrModalOpen)]="modalCsv" [clrModalSize]="'sm'" [clrModalStaticBackdrop]="false">
  <h3 class="modal-title">{{'IMPORT_FILE' | translate}}</h3>
  <div class="modal-body">
    <div class="um-form-group">
      <label class="um-form-label">{{'SELECT_CSV_FILE' | translate}}</label>
      <input type="file" name="file" class="um-file-input" (change)="selectFile($event)"
        accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel">
    </div>
  </div>
  <div class="modal-footer">
    <button class="um-btn um-btn-outline" type="button" (click)="modalCsv = false">{{'CANCEL' | translate}}</button>
    <button class="um-btn um-btn-primary" type="button" [disabled]="!selectedFiles" (click)="saveCsv()">{{'IMPORT' |
      translate}}</button>
  </div>
</clr-modal>