<div class="col p-2 h-100 fade" [class.show]="show()">
  <div class="card h-100 shadow-sm document-card" [class.placeholder-glow]="!document()" [class.card-selected]="selected()" (mouseleave)="mouseLeaveCard()">
    <div class="border-bottom doc-img-container rounded-top" (click)="this.toggleSelected.emit($event)" (dblclick)="dblClickDocument.emit(this)">
       @if (document()) {
        @if (priority()) {
          <img class="card-img doc-img" [class.inverted]="getIsThumbInverted()" [ngSrc]="getThumbUrl()" fill priority alt="{{ document().title | documentTitle }} thumbnail" i18n-alt>
        } @else {
          <img class="card-img doc-img" [class.inverted]="getIsThumbInverted()" [ngSrc]="getThumbUrl()" fill alt="{{ document().title | documentTitle }} thumbnail" i18n-alt>
        }

        <div class="border-end border-bottom bg-light py-1 px-2 document-card-check">
          <div class="form-check">
            <input type="checkbox" class="form-check-input" id="smallCardCheck{{document().id}}" [checked]="selected()" (click)="this.toggleSelected.emit($event)">
            <label class="form-check-label" for="smallCardCheck{{document().id}}"></label>
          </div>
        </div>
      } @else {
        <div class="placeholder bg-secondary w-100 card-img doc-img"></div>
      }

      @if (document() && displayFields().includes(DisplayField.TAGS)) {
        <div class="tags d-flex flex-column text-end position-absolute me-1 fs-6" [class.tags-no-wrap]="document().tags.length > 3">
          @for (tagID of tagIDs; track tagID) {
            <pngx-tag [tagID]="tagID" (click)="clickTag.emit(tagID);$event.stopPropagation()" [clickable]="true" linkTitle="Toggle tag filter" i18n-linkTitle></pngx-tag>
          }
          @if (moreTags) {
            <div>
              <span class="badge text-dark">+ {{moreTags}}</span>
            </div>
          }
        </div>
      }
    </div>

    @if (document() && displayFields().includes(DisplayField.NOTES) && notesEnabled && document().notes.length) {
      <a routerLink="/documents/{{document().id}}/notes" class="document-card-notes py-2 px-1">
        <span class="badge rounded-pill bg-light border text-primary">
          <i-bs width="1.2em" height="1.2em" class="ms-1 me-1" name="chat-left-text"></i-bs>
        {{document().notes.length}}</span>
      </a>
    }

    <div class="card-body bg-light p-2">
      <p class="card-text">
         @if (document()) {
          @if (displayFields().includes(DisplayField.CORRESPONDENT) && document().correspondent) {
            <a title="Toggle correspondent filter" i18n-title (click)="clickCorrespondent.emit(document().correspondent);$event.stopPropagation()" class="fw-bold btn-link">{{document().correspondent | correspondentName | async}}</a>
            @if (displayFields().includes(DisplayField.TITLE)) {:}
          }
          @if (displayFields().includes(DisplayField.TITLE)) {
            {{document().title | documentTitle}}
          }
        } @else {
          <div class="placeholder bg-secondary w-100"></div>
          <div class="placeholder bg-secondary w-50"></div>
        }
      </p>
    </div>
    <div class="card-footer pt-0 pb-2 px-2">
      <div class="list-group list-group-flush border-0 pt-1 pb-2 card-info">
         @if (document()) {
          @if (displayFields().includes(DisplayField.DOCUMENT_TYPE) && document().document_type) {
            <button type="button" class="list-group-item list-group-item-action bg-transparent ps-0 p-1 border-0 d-flex align-items-start" title="Toggle document type filter" i18n-title
              (click)="clickDocumentType.emit(document().document_type);$event.stopPropagation()">
              <i-bs width="1em" height="1em" class="me-2 text-muted flex-shrink-0" name="file-earmark"></i-bs>
              <small>{{document().document_type | documentTypeName | async}}</small>
            </button>
          }
          @if (displayFields().includes(DisplayField.STORAGE_PATH) && document().storage_path) {
            <button type="button" class="list-group-item list-group-item-action bg-transparent ps-0 p-1 border-0 d-flex align-items-start" title="Toggle storage path filter" i18n-title
              (click)="clickStoragePath.emit(document().storage_path);$event.stopPropagation()">
              <i-bs width="1em" height="1em" class="me-2 text-muted flex-shrink-0" name="folder"></i-bs>
              <small>{{document().storage_path | storagePathName | async}}</small>
            </button>
          }
          @if (displayFields().includes(DisplayField.CREATED)) {
            <div class="list-group-item bg-transparent p-0 border-0 d-flex flex-wrap-reverse justify-content-between">
              <ng-template #dateCreatedTooltip>
                <div class="d-flex flex-column text-light">
                  <span i18n>Created: {{ document().created | customDate }}</span>
                  <span i18n>Added: {{ document().added | customDate }}</span>
                  <span i18n>Modified: {{ document().modified | customDate }}</span>
                </div>
              </ng-template>
              <div class="ps-0 p-1" placement="top" [ngbTooltip]="dateCreatedTooltip">
                <i-bs width="1em" height="1em" class="me-2 text-muted" name="calendar-event"></i-bs>
                <small>{{document().created | customDate:'mediumDate'}}</small>
              </div>
            </div>
          }
          @if (displayFields().includes(DisplayField.ADDED)) {
            <div class="list-group-item bg-transparent p-0 border-0 d-flex flex-wrap-reverse justify-content-between">
              <ng-template #dateAddedTooltip>
                <div class="d-flex flex-column text-light">
                  <span i18n>Created: {{ document().created | customDate }}</span>
                  <span i18n>Added: {{ document().added | customDate }}</span>
                  <span i18n>Modified: {{ document().modified | customDate }}</span>
                </div>
              </ng-template>
              <div class="ps-0 p-1" placement="top" [ngbTooltip]="dateAddedTooltip">
                <i-bs width="1em" height="1em" class="me-2 text-muted" name="calendar-event"></i-bs>
                <small>{{document().added | customDate:'mediumDate'}}</small>
              </div>
            </div>
          }
          @if (displayFields().includes(DisplayField.PAGE_COUNT) && document().page_count) {
            <div class="list-group-item bg-transparent p-0 border-0 d-flex flex-wrap-reverse justify-content-between">
              <div class="ps-0 p-1" placement="top">
                <i-bs width="1em" height="1em" class="me-2 text-muted" name="files"></i-bs>
                <small i18n>{document().page_count, plural, =1 {1 page} other {{{document().page_count}} pages}}</small>
              </div>
            </div>
          }
          @if (displayFields().includes(DisplayField.ASN) && document().archive_serial_number | isNumber) {
            <div class="ps-0 p-1">
              <i-bs width="1em" height="1em" class="me-2 text-muted" name="upc-scan"></i-bs>
              <small>#{{document().archive_serial_number}}</small>
            </div>
          }
          @if (displayFields().includes(DisplayField.OWNER) && document().owner && document().owner !== settingsService.currentUser().id) {
            <div class="ps-0 p-1">
              <i-bs width="1em" height="1em" class="me-2 text-muted" name="person-fill-lock"></i-bs>
              <small>{{document().owner | username | async}}</small>
            </div>
          }
          @if (displayFields().includes(DisplayField.SHARED) && document().is_shared_by_requester) {
            <div class="ps-0 p-1">
              <i-bs width="1em" height="1em" class="me-2 text-muted" name="people-fill"></i-bs>
              <small i18n>Shared</small>
            </div>
          }
          @for (field of document().custom_fields; track field.field) {
            @if (displayFields().includes(DisplayField.CUSTOM_FIELD + field.field)) {
              <div class="ps-0 p-1 d-flex align-items-center overflow-hidden">
                <i-bs width="1em" height="1em" class="me-2 text-muted" name="ui-radios"></i-bs>
                <small><pngx-custom-field-display [document]="document()" [fieldId]="field.field" showNameIfEmpty="true"></pngx-custom-field-display></small>
              </div>
            }
          }
        } @else {
          <div class="placeholder bg-secondary w-100"></div>
        }
      </div>
      <div class="d-flex justify-content-between align-items-center">
        <div class="btn-group w-100">
           @if (document()) {
            <a routerLink="/documents/{{document().id}}" class="btn btn-sm btn-outline-secondary" title="Open" i18n-title *pngxIfPermissions="{ action: PermissionAction.Change, type: PermissionType.Document }" i18n-title>
              <i-bs name="file-earmark-richtext"></i-bs>
            </a>
            <pngx-preview-popup [document]="document()" #popupPreview>
              <i-bs name="eye"></i-bs>
            </pngx-preview-popup>
            <a [href]="getDownloadUrl()" class="btn btn-sm btn-outline-secondary" title="Download" i18n-title (click)="$event.stopPropagation()">
              <i-bs name="download"></i-bs>
            </a>
          } @else {
            <span class="btn btn-sm btn-outline-secondary placeholder bg-secondary" aria-hidden="true"></span>
            <span class="btn btn-sm btn-outline-secondary placeholder bg-secondary" aria-hidden="true"></span>
            <span class="btn btn-sm btn-outline-secondary placeholder bg-secondary" aria-hidden="true"></span>
          }
        </div>
      </div>
    </div>
  </div>
</div>
