<div class="h-full flex flex-col gap-4 cardShadow">
  <p-table
    [columns]="columns"
    [scrollable]="true"
    [value]="items"
    [loading]="loading"
    columnResizeMode="fit"
    stripedRows
    [showFirstLastIcon]="false"
    [expandedRowKeys]="expandedRows"
    [dataKey]="dataKey"
    class="max-sm:hidden! grow flex! flex-col overflow-hidden"
    [tableStyleClass]="!items.length && !loading ? 'h-full' : ''"
  >
    @if (captionBox) {
      <ng-template #caption>
        <ng-container [ngTemplateOutlet]="captionBox"> </ng-container>
      </ng-template>
    }

    <ng-template #header let-columns>
      <tr>
        @if (expandable) {
          <th style="width: 3rem"></th>
        }
        @if (showIndex) {
          <th [style]="{ width: '3rem', minWidth: '3rem' }"></th>
        }
        @for (col of columns; track col.header) {
          <th
            [style]="
              col.type === 'index' ? { width: '3rem', minWidth: '3rem' } : { width: col.width, minWidth: col.minWidth }
            "
          >
            {{ col.header }}
          </th>
        }
        @if (actionsCount()) {
          <th type="th" [style]="{ width: `${actionsCount() * 2 + (actionsCount() - 1) * 0.25}rem` }"></th>
        }
      </tr>
    </ng-template>

    <ng-template #body let-item let-i="rowIndex" let-expanded="expanded">
      <tr>
        @if (expandable) {
          <td>
            <p-button
              type="button"
              pRipple
              [pRowToggler]="item"
              [text]="true"
              [rounded]="true"
              [plain]="true"
              [icon]="expanded ? 'pi pi-chevron-down' : 'pi pi-chevron-left'"
            />
          </td>
        }
        @if (showIndex) {
          <td>
            {{ i + 1 + (currentPage && perPage ? (currentPage - 1) * perPage : 0) }}
          </td>
        }
        @for (col of columns; track col.field) {
          <td>
            <app-page-data-value [item]="item" [column]="col" (thumbnailClick)="openThumbnailModal($event)" />
          </td>
        }
        @if (actionsCount()) {
          <td
            table-action-row
            type="td"
            [showDetails]="showDetails"
            [showDelete]="showDelete"
            [showEdit]="showEdit"
            (edit)="edit(item)"
            (delete)="remove(item)"
            (details)="details(item)"
          ></td>
        }
      </tr>
    </ng-template>

    @if (expandable && expandColumns && expandableItemKey) {
      <ng-template #expandedrow let-item>
        <tr>
          <td [attr.colspan]="columns.length + 1 + (showIndex ? 1 : 0) + (actionsCount() ? 1 : 0)">
            <div class="px-4">
              @if (item[expandableItemKey]?.length) {
                <p-table
                  [columns]="expandColumns || []"
                  [scrollable]="true"
                  [value]="item[expandableItemKey]"
                  [loading]="loading"
                  columnResizeMode="fit"
                  stripedRows
                  [showFirstLastIcon]="false"
                  [expandedRowKeys]="expandedRows"
                  class="grow flex! flex-col overflow-hidden"
                  [tableStyleClass]="!item.items?.length && !loading ? 'h-full' : ''"
                >
                  <ng-template #header let-columns>
                    <tr>
                      @for (col of expandColumns; track col.header) {
                        <th
                          [style]="
                            col.type === 'index'
                              ? { width: '3rem', minWidth: '3rem' }
                              : { width: col.width, minWidth: col.minWidth }
                          "
                        >
                          {{ col.header }}
                        </th>
                      }
                    </tr>
                  </ng-template>

                  <ng-template #body let-item let-i="rowIndex">
                    <tr>
                      @for (col of expandColumns; track col.field) {
                        <td>
                          {{ item.name }}
                          @if (col.type === "index") {
                            {{ i * (currentPage || 1) + 1 }}
                          } @else {
                            <app-page-data-value
                              [item]="item"
                              [column]="col"
                              (thumbnailClick)="openThumbnailModal($event)"
                            />
                          }
                        </td>
                      }
                    </tr>
                  </ng-template>

                  <ng-template #emptymessage>
                    <tr class="">
                      <td [colSpan]="(expandColumns.length + 1).toString()" class="w-full">
                        <uikit-empty-state
                          [title]="emptyPlaceholderTitle"
                          [description]="emptyPlaceholderDescription"
                          [ctaLabel]="emptyPlaceholderCtaLabel || addNewCtaLabel"
                          [showCTA]="showAdd"
                          (ctaClick)="openAddForm()"
                        ></uikit-empty-state>
                      </td>
                    </tr>
                  </ng-template>
                </p-table>
              } @else {
                <div class="grid grid-cols-3 gap-4 py-2">
                  @for (col of expandColumns; track $index) {
                    <app-key-value
                      [label]="col.header"
                      [value]="item[expandableItemKey][col.field]"
                      [type]="col.type || 'simple'"
                      [variant]="col.variant"
                    />
                  }
                </div>
              }
            </div>
          </td>
        </tr>
      </ng-template>
    }

    <ng-template #emptymessage>
      <tr class="">
        <td [colSpan]="(columns.length + (showIndex ? 1 : 0) + (actionsCount() > 0 ? 1 : 0)).toString()" class="w-full">
          <ng-container [ngTemplateOutlet]="emptyMessageCard"></ng-container>
        </td>
      </tr>
    </ng-template>

    <ng-template #loadingbody let-columns>
      @for (i of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; track i) {
        <tr style="height: 46px">
          @if (showIndex) {
            <td>
              <p-skeleton></p-skeleton>
            </td>
          }
          @for (col of columns; track col) {
            <td>
              <p-skeleton></p-skeleton>
            </td>
          }
          @if (actionsCount()) {
            <td>
              <p-skeleton></p-skeleton>
            </td>
          }
        </tr>
      }
    </ng-template>
  </p-table>

  @if (showPaginator) {
    <ng-container [ngTemplateOutlet]="paginator"> </ng-container>
  }
</div>
