<ul class="list-group">

  <li class="list-group-item">
    <div class="row">
      <div class="col" i18n>Name</div>
      <div class="col" i18n>Data Type</div>
      <div class="col" i18n>Actions</div>
    </div>
  </li>

  @if (loading()) {
    <li class="list-group-item">
      <div class="spinner-border spinner-border-sm me-2" role="status"></div>
      <ng-container i18n>Loading...</ng-container>
    </li>
  }

  @for (field of fields(); track field) {
    <li class="list-group-item">
      <div class="row fade" [class.show]="show()">
        <div class="col d-flex align-items-center"><button class="btn btn-link p-0 text-start" type="button" (click)="editField(field)" [disabled]="!permissionsService.currentUserCan(PermissionAction.Change, PermissionType.CustomField)">{{field.name}}</button></div>
        <div class="col d-flex align-items-center">{{getDataType(field)}}</div>
        <div class="col">
          <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 aria-label="Actions" i18n-aria-label>
                  <i-bs name="three-dots-vertical"></i-bs>
                </button>
                <div ngbDropdownMenu aria-labelledby="actionsMenuMobile">
                  <button (click)="editField(field)" *pngxIfPermissions="{ action: PermissionAction.Change, type: PermissionType.CustomField }" ngbDropdownItem i18n>Edit</button>
                  <button class="text-danger" (click)="deleteField(field)" *pngxIfPermissions="{ action: PermissionAction.Delete, type: PermissionType.CustomField }" ngbDropdownItem i18n>Delete</button>
                  @if (field.document_count > 0) {
                    <a
                      *pngxIfPermissions="{ action: PermissionAction.View, type: PermissionType.Document }"
                      ngbDropdownItem
                      [routerLink]="getDocumentFilterUrl(field)"
                      i18n
                      >Filter Documents ({{ field.document_count }})</a
                    >
                  }
                </div>
              </div>
            </div>
            <div class="btn-group d-none d-sm-inline-block">
              <button *pngxIfPermissions="{ action: PermissionAction.Change, type: PermissionType.CustomField }" class="btn btn-sm btn-outline-secondary" type="button" (click)="editField(field)">
                <i-bs width="1em" height="1em" name="pencil" class="me-1"></i-bs><ng-container i18n>Edit</ng-container>
              </button>
              <button *pngxIfPermissions="{ action: PermissionAction.Delete, type: PermissionType.CustomField }" class="btn btn-sm btn-outline-danger" type="button" (click)="deleteField(field)">
                <i-bs width="1em" height="1em" name="trash" class="me-1"></i-bs><ng-container i18n>Delete</ng-container>
              </button>
            </div>
            @if (field.document_count > 0) {
              <div class="btn-group d-none d-sm-inline-block">
                <a
                  class="btn btn-sm btn-outline-secondary"
                  [routerLink]="getDocumentFilterUrl(field)"
                >
                  <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">{{ field.document_count }}</span>
                </a>
              </div>
            }
          </div>
        </div>
      </div>
    </li>
  }
  @if (!loading() && fields().length === 0) {
    <li class="list-group-item" i18n>No fields defined.</li>
  }
</ul>
