<vdr-action-bar>
    <vdr-ab-left [grow]="true">
        <div class="search-form">
            <vdr-product-search-input
                #productSearchInputComponent
                [facetValueResults]="facetValues$ | async"
                (searchTermChange)="setSearchTerm($event)"
                (facetValueChange)="setFacetValueIds($event)"
            ></vdr-product-search-input>
            <vdr-dropdown class="search-settings-menu">
                <button type="button" class="icon-button" vdrDropdownTrigger>
                    <clr-icon shape="cog"></clr-icon>
                </button>
                <vdr-dropdown-menu vdrPosition="bottom-right">
                    <button
                        type="button"
                        vdrDropdownItem
                        (click)="rebuildSearchIndex()"
                        [disabled]="!('UpdateCatalog' | hasPermission)"
                    >
                        {{ 'catalog.rebuild-search-index' | translate }}
                    </button>
                </vdr-dropdown-menu>
            </vdr-dropdown>
        </div>
        <clr-checkbox-wrapper>
            <input type="checkbox" clrCheckbox [(ngModel)]="groupByProduct" />
            <label>{{ 'catalog.group-by-product' | translate }}</label>
        </clr-checkbox-wrapper>
    </vdr-ab-left>
    <vdr-ab-right>
        <vdr-action-bar-items locationId="product-list"></vdr-action-bar-items>
        <a class="btn btn-primary" [routerLink]="['./create']" *vdrIfPermissions="'CreateCatalog'">
            <clr-icon shape="plus"></clr-icon>
            <span class="full-label">{{ 'catalog.create-new-product' | translate }}</span>
        </a>
    </vdr-ab-right>
</vdr-action-bar>

<vdr-data-table
    [items]="items$ | async"
    [itemsPerPage]="itemsPerPage$ | async"
    [totalItems]="totalItems$ | async"
    [currentPage]="currentPage$ | async"
    (pageChange)="setPageNumber($event)"
    (itemsPerPageChange)="setItemsPerPage($event)"
>
    <ng-template let-result="item">
        <td class="left align-middle" [class.disabled]="!result.enabled">
            <div class="image-placeholder">
                <img
                    *ngIf="
                        groupByProduct
                            ? result.productAsset
                            : result.productVariantAsset || result.productAsset as asset;
                        else imagePlaceholder
                    "
                    [src]="asset | assetPreview:'tiny'"
                />
                <ng-template #imagePlaceholder>
                    <div class="placeholder"><clr-icon shape="image" size="48"></clr-icon></div>
                </ng-template>
            </div>
        </td>
        <td class="left align-middle" [class.disabled]="!result.enabled">
            {{ groupByProduct ? result.productName : result.productVariantName }}
        </td>
        <td class="align-middle" [class.disabled]="!result.enabled">
            <vdr-chip *ngIf="!result.enabled">{{ 'common.disabled' | translate }}</vdr-chip>
        </td>
        <td class="right align-middle" [class.disabled]="!result.enabled">
            <vdr-table-row-action
                iconShape="edit"
                [label]="'common.edit' | translate"
                [linkTo]="['./', result.productId]"
            ></vdr-table-row-action>
        </td>
        <td class="right align-middle" [class.disabled]="!result.enabled">
            <vdr-dropdown>
                <button type="button" class="btn btn-link btn-sm" vdrDropdownTrigger>
                    {{ 'common.actions' | translate }}
                    <clr-icon shape="caret down"></clr-icon>
                </button>
                <vdr-dropdown-menu vdrPosition="bottom-right">
                    <button
                        type="button"
                        class="delete-button"
                        (click)="deleteProduct(result.productId)"
                        [disabled]="!('DeleteCatalog' | hasPermission)"
                        vdrDropdownItem
                    >
                        <clr-icon shape="trash" class="is-danger"></clr-icon>
                        {{ 'common.delete' | translate }}
                    </button>
                </vdr-dropdown-menu>
            </vdr-dropdown>
        </td>
    </ng-template>
</vdr-data-table>
