<div class="row mb-3">
  <div class="col mb-2 mb-xl-0">
    <div class="form-inline d-flex align-items-center">
      <label class="text-muted me-2 mb-0" i18n>Filter by:</label>
      <input class="form-control form-control-sm flex-fill w-auto" type="text" autofocus [(ngModel)]="nameFilter" (keyup)="onNameFilterKeyUp($event)" placeholder="Name" i18n-placeholder>
    </div>
  </div>

  <div class="col-auto mb-2 mb-xl-0">
    <div class="form-inline d-flex align-items-center">
      <div class="input-group input-group-sm w-auto d-none d-md-flex">
        <span class="input-group-text border-0" i18n>Show:</span>
      </div>
      <div class="input-group input-group-sm w-auto me-3">
        <select class="form-select form-select-sm small" [(ngModel)]="pageSize">
          <option [ngValue]="25">25</option>
          <option [ngValue]="50">50</option>
          <option [ngValue]="100">100</option>
        </select>
        <span class="input-group-text text-muted d-none d-md-flex" i18n>per page</span>
      </div>
      <ngb-pagination [pageSize]="pageSize" [collectionSize]="collectionSize" [(page)]="page" [maxSize]="5" (pageChange)="reloadData()" size="sm" aria-label="Pagination"></ngb-pagination>
    </div>
  </div>

</div>

<div class="card border table-responsive mb-3">
  <table class="table table-striped align-middle shadow-sm mb-0">
    <thead>
      <tr>
        <th>
          <div class="form-check m-0 ms-2 me-n2">
            <input type="checkbox" class="form-check-input" id="all-objects" [(ngModel)]="togggleAll" [disabled]="data.length === 0" (change)="$event.target.checked ? selectPage() : clearSelection(); $event.stopPropagation();">
            <label class="form-check-label" for="all-objects"></label>
          </div>
        </th>
        <th class="fw-normal" pngxSortable="name" [currentSortField]="sortField" [currentSortReverse]="sortReverse" (sort)="onSort($event)" i18n>Name</th>
        <th class="fw-normal d-none d-sm-table-cell" pngxSortable="matching_algorithm" [currentSortField]="sortField" [currentSortReverse]="sortReverse" (sort)="onSort($event)" i18n>Matching</th>
        <th class="fw-normal" pngxSortable="document_count" [currentSortField]="sortField" [currentSortReverse]="sortReverse" (sort)="onSort($event)" i18n>Document count</th>
        @for (column of extraColumns; track column) {
          <th class="fw-normal" [ngClass]="{ 'd-none d-sm-table-cell' : column.hideOnMobile }" pngxSortable="{{column.key}}" [currentSortField]="sortField" [currentSortReverse]="sortReverse" (sort)="onSort($event)">{{column.name}}</th>
        }
        <th class="fw-normal" i18n>Actions</th>
      </tr>
    </thead>
    <tbody>
      @if (loading && data.length === 0) {
        <tr>
          <td colspan="5">
            <div class="spinner-border spinner-border-sm me-2" role="status"></div>
            <ng-container i18n>Loading...</ng-container>
          </td>
        </tr>
      }
      @for (object of data; track object) {
        <ng-container [ngTemplateOutlet]="objectRow" [ngTemplateOutletContext]="{ object: object, depth: 0 }"></ng-container>
      }
    </tbody>
  </table>
</div>

@if (!loading || data.length > 0) {
  <div class="d-flex mb-2">
    @if (displayCollectionSize > 0) {
      <div>
        <ng-container i18n>{displayCollectionSize, plural, =1 {One {{typeName}}} other {{{displayCollectionSize || 0}} total {{typeNamePlural}}}}</ng-container>
        @if (selectedObjects.size > 0) {
          &nbsp;({{selectedObjects.size}} selected)
        }
      </div>
    }
    @if (collectionSize > 20) {
      <ngb-pagination class="ms-auto" [pageSize]="pageSize" [collectionSize]="collectionSize" [(page)]="page" [maxSize]="5" (pageChange)="reloadData()" size="sm" aria-label="Pagination"></ngb-pagination>
    }
  </div>
}

<ng-template #objectRow let-object="object" let-depth="depth">
  <tr (click)="toggleSelected(object); $event.stopPropagation();" class="data-row fade" [class.show]="show">
    <td>
      <div class="form-check m-0 ms-2 me-n2">
        <input type="checkbox" class="form-check-input" id="{{typeName}}{{object.id}}" [checked]="selectedObjects.has(object.id)" (click)="toggleSelected(object); $event.stopPropagation();">
        <label class="form-check-label" for="{{typeName}}{{object.id}}"></label>
      </div>
    </td>
    <td class="name-cell" style="--depth: {{depth}}">
      @if (depth > 0) {
        <div class="indicator"></div>
      }
      <button class="btn btn-link ms-0 ps-0 text-start" (click)="userCanEdit(object) ? openEditDialog(object) : null; $event.stopPropagation()">{{ object.name }}</button>
    </td>
    <td class="d-none d-sm-table-cell">{{ getMatching(object) }}</td>
    <td>{{ getDocumentCount(object) }}</td>
    @for (column of extraColumns; track column) {
      <td [ngClass]="{ 'd-none d-sm-table-cell' : column.hideOnMobile }">
        @if (column.badgeFn) {
          <span
            class="badge"
            [style.color]="column.badgeFn.call(null, object)?.textColor"
            [style.backgroundColor]="column.badgeFn.call(null, object)?.backgroundColor"
          >
            {{ column.badgeFn.call(null, object)?.text }}
          </span>
        } @else if (column.monospace) {
          <span class="font-monospace">{{ column.valueFn.call(null, object) }}</span>
        } @else {
          {{ column.valueFn.call(null, object) }}
        }
      </td>
    }
    <td>
      <div class="btn-toolbar gap-2">
        <div class="btn-group d-block d-sm-none">
          <div ngbDropdown container="body" class="d-inline-block">
            <button type="button" class="btn btn-link" id="actionsMenuMobile" (click)="$event.stopPropagation()" ngbDropdownToggle>
              <i-bs name="three-dots-vertical"></i-bs>
            </button>
            <div ngbDropdownMenu aria-labelledby="actionsMenuMobile">
              <button (click)="openEditDialog(object)" *pngxIfPermissions="{ action: PermissionAction.Change, type: permissionType }" ngbDropdownItem i18n>Edit</button>
              <button class="text-danger" (click)="openDeleteDialog(object)" *pngxIfPermissions="{ action: PermissionAction.Delete, type: permissionType }" ngbDropdownItem i18n>Delete</button>
              @if (getDocumentCount(object) > 0) {
                  <a
                    *pngxIfPermissions="{ action: PermissionAction.View, type: PermissionType.Document }"
                    ngbDropdownItem
                    [routerLink]="getDocumentFilterUrl(object)"
                    (click)="$event?.stopPropagation()"
                    i18n
                    >Filter Documents ({{ getDocumentCount(object) }})</a
                  >
              }
            </div>
          </div>
        </div>
        <div class="btn-group d-none d-sm-inline-block">
          <button class="btn btn-sm btn-outline-secondary" (click)="openEditDialog(object); $event.stopPropagation();" *pngxIfPermissions="{ action: PermissionAction.Change, type: permissionType }" [disabled]="!userCanEdit(object)">
            <i-bs width="1em" height="1em" name="pencil" class="me-1"></i-bs><ng-container i18n>Edit</ng-container>
          </button>
          <button class="btn btn-sm btn-outline-danger" (click)="openDeleteDialog(object); $event.stopPropagation();" *pngxIfPermissions="{ action: PermissionAction.Delete, type: permissionType }" [disabled]="!userCanDelete(object)">
            <i-bs width="1em" height="1em" name="trash" class="me-1"></i-bs><ng-container i18n>Delete</ng-container>
          </button>
        </div>
        @if (getDocumentCount(object) > 0) {
          <div class="btn-group d-none d-sm-inline-block">
            <a
              class="btn btn-sm btn-outline-secondary"
              *pngxIfPermissions="{ action: PermissionAction.View, type: PermissionType.Document }"
              [routerLink]="getDocumentFilterUrl(object)"
              (click)="$event?.stopPropagation()"
            >
              <i-bs width="1em" height="1em" name="filter" class="me-1"></i-bs><ng-container i18n>Documents</ng-container
              ><span class="badge bg-light text-secondary ms-2">{{ getDocumentCount(object) }}</span>
            </a>
          </div>
        }
      </div>
    </td>
  </tr>

  @if (object.children && object.children.length > 0) {
    @for (child of object.children; track child) {
      <ng-container [ngTemplateOutlet]="objectRow" [ngTemplateOutletContext]="{ object: getOriginalObject(child), depth: depth + 1 }"></ng-container>
    }
  }
</ng-template>
