<div class="card mb-3 shadow-sm" [class.card-selected]="selected" [class.document-card]="selectable" [class.popover-hidden]="popoverHidden" (mouseleave)="mouseLeaveCard()">
  <div class="row g-0">
    <div class="col-md-2 doc-img-background rounded-start" [class.doc-img-background-selected]="selected" (click)="this.toggleSelected.emit($event)" (dblclick)="dblClickDocument.emit()">
      <img [src]="getThumbUrl()" class="card-img doc-img border-end rounded-start" [class.inverted]="getIsThumbInverted()">

      <div class="border-end border-bottom bg-light 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>

    </div>
    <div class="col">
      <div class="card-body bg-light">

        <div class="d-flex justify-content-between align-items-center">
          <h5 class="card-title">
            <ng-container *ngIf="document.correspondent">
              <a *ngIf="clickCorrespondent.observers.length ; else nolink" title="Filter by correspondent" i18n-title (click)="clickCorrespondent.emit(document.correspondent);$event.stopPropagation()" class="fw-bold btn-link">{{(document.correspondent$ | async)?.name}}</a>
              <ng-template #nolink>{{(document.correspondent$ | async)?.name}}</ng-template>:
            </ng-container>
            {{document.title | documentTitle}}
            <pngx-tag [tag]="t" linkTitle="Filter by tag" i18n-linkTitle *ngFor="let t of document.tags$ | async" class="ms-1" (click)="clickTag.emit(t.id);$event.stopPropagation()" [clickable]="clickTag.observers.length"></pngx-tag>
          </h5>
        </div>
        <p class="card-text">
          <span *ngIf="document.__search_hit__ && document.__search_hit__.highlights" [innerHtml]="document.__search_hit__.highlights"></span>
          <span *ngFor="let highlight of searchNoteHighlights" class="d-block">
            <svg width="1em" height="1em" fill="currentColor" class="me-2">
              <use xlink:href="assets/bootstrap-icons.svg#chat-left-text"/>
            </svg>
            <span [innerHtml]="highlight"></span>
          </span>
          <span *ngIf="!document.__search_hit__" class="result-content">{{contentTrimmed}}</span>
        </p>


        <div class="d-flex flex-column flex-md-row align-items-md-center">
          <div class="btn-group">
            <a class="btn btn-sm btn-outline-secondary" (click)="clickMoreLike.emit()">
              <svg class="sidebaricon" fill="currentColor" class="sidebaricon">
                <use xlink:href="assets/bootstrap-icons.svg#diagram-3"/>
              </svg>&nbsp;<span class="d-none d-md-inline" i18n>More like this</span>
            </a>
            <a routerLink="/documents/{{document.id}}" class="btn btn-sm btn-outline-secondary" *pngxIfPermissions="{ action: PermissionAction.Change, type: PermissionType.Document }">
              <svg class="sidebaricon" fill="currentColor" class="sidebaricon">
                <use xlink:href="assets/bootstrap-icons.svg#pencil"/>
              </svg>&nbsp;<span class="d-none d-md-inline" i18n>Edit</span>
            </a>
            <a class="btn btn-sm btn-outline-secondary" target="_blank" [href]="previewUrl"
            [ngbPopover]="previewContent" [popoverTitle]="document.title | documentTitle"
            autoClose="true" popoverClass="shadow popover-preview" (mouseenter)="mouseEnterPreview()" (mouseleave)="mouseLeavePreview()" #popover="ngbPopover">
              <svg class="sidebaricon" fill="currentColor" class="sidebaricon">
                <use xlink:href="assets/bootstrap-icons.svg#eye"/>
              </svg>&nbsp;<span class="d-none d-md-inline" i18n>View</span>
            </a>
            <ng-template #previewContent>
              <object [data]="previewUrl | safeUrl" class="preview" width="100%"></object>
            </ng-template>
            <a class="btn btn-sm btn-outline-secondary" [href]="getDownloadUrl()">
              <svg class="sidebaricon" fill="currentColor" class="sidebaricon">
                <use xlink:href="assets/bootstrap-icons.svg#download"/>
              </svg>&nbsp;<span class="d-none d-md-inline" i18n>Download</span>
            </a>
          </div>


          <div class="list-group list-group-horizontal border-0 card-info ms-md-auto mt-2 mt-md-0">
            <button *ngIf="notesEnabled && document.notes.length" routerLink="/documents/{{document.id}}/notes" class="list-group-item btn btn-sm bg-light text-dark p-1 border-0 me-2" title="View notes" i18n-title>
              <svg class="metadata-icon me-2 text-muted" fill="currentColor">
                <use xlink:href="assets/bootstrap-icons.svg#chat-left-text"/>
              </svg>
              <small i18n>{{document.notes.length}} Notes</small>
            </button>
            <button *ngIf="document.document_type" type="button" class="list-group-item btn btn-sm bg-light text-dark p-1 border-0 me-2" title="Filter by document type" i18n-title
             (click)="clickDocumentType.emit(document.document_type);$event.stopPropagation()">
              <svg class="metadata-icon me-2 text-muted" fill="currentColor">
                <use xlink:href="assets/bootstrap-icons.svg#file-earmark"/>
              </svg>
              <small>{{(document.document_type$ | async)?.name}}</small>
            </button>
            <button *ngIf="document.storage_path" type="button" class="list-group-item btn btn-sm bg-light text-dark p-1 border-0 me-2" title="Filter by storage path" i18n-title
             (click)="clickStoragePath.emit(document.storage_path);$event.stopPropagation()">
              <svg class="metadata-icon me-2 text-muted" fill="currentColor">
                <use xlink:href="assets/bootstrap-icons.svg#archive"/>
              </svg>
              <small>{{(document.storage_path$ | async)?.name}}</small>
            </button>
            <div *ngIf="document.archive_serial_number | isNumber" class="list-group-item me-2 bg-light text-dark p-1 border-0">
              <svg class="metadata-icon me-2 text-muted" fill="currentColor">
                <use xlink:href="assets/bootstrap-icons.svg#upc-scan"/>
              </svg>
              <small>#{{document.archive_serial_number}}</small>
            </div>
            <ng-template #dateTooltip>
              <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="list-group-item bg-light text-dark p-1 border-0" [ngbTooltip]="dateTooltip">
              <svg class="metadata-icon me-2 text-muted" fill="currentColor">
                <use xlink:href="assets/bootstrap-icons.svg#calendar-event"/>
              </svg>
              <small>{{document.created_date | customDate:'mediumDate'}}</small>
            </div>
            <div *ngIf="document.owner && document.owner !== settingsService.currentUser.id" class="list-group-item bg-light text-dark p-1 border-0">
              <svg class="metadata-icon me-2 text-muted" fill="currentColor">
                <use xlink:href="assets/bootstrap-icons.svg#person-fill-lock"/>
              </svg>
              <small>{{document.owner | username}}</small>
            </div>
            <div *ngIf="document.__search_hit__?.score" class="list-group-item bg-light text-dark border-0 d-flex p-0 ps-4 search-score">
              <small class="text-muted" i18n>Score:</small>
              <ngb-progressbar [type]="searchScoreClass" [value]="document.__search_hit__.score" class="search-score-bar mx-2 mt-1" [max]="1"></ngb-progressbar>
            </div>
          </div>
        </div>

      </div>
    </div>
  </div>
</div>
