<vdr-action-bar>
    <vdr-ab-left>
        <vdr-language-selector
            [disabled]="isNew$ | async"
            [availableLanguageCodes]="availableLanguages$ | async"
            [currentLanguageCode]="languageCode$ | async"
            (languageCodeChange)="setLanguage($event)"
        ></vdr-language-selector>
    </vdr-ab-left>

    <vdr-ab-right>
        <button
            class="btn btn-primary"
            *ngIf="(isNew$ | async); else: updateButton"
            (click)="create()"
            [disabled]="detailForm.invalid || detailForm.pristine"
        >
            {{ 'common.create' | translate }}
        </button>
        <ng-template #updateButton>
            <button
                class="btn btn-primary"
                (click)="save()"
                [disabled]="(detailForm.invalid || detailForm.pristine) && !assetsChanged()"
            >
                {{ 'common.update' | translate }}
            </button>
        </ng-template>
    </vdr-ab-right>
</vdr-action-bar>

<form class="form" [formGroup]="detailForm" *ngIf="(entity$ | async) as category">
    <div class="clr-row">
        <div class="clr-col">
            <section class="form-block">
                <vdr-form-field [label]="'common.name' | translate" for="name">
                    <input id="name" type="text" formControlName="name" />
                </vdr-form-field>
                <vdr-rich-text-editor
                    formControlName="description"
                    [label]="'common.description' | translate"
                ></vdr-rich-text-editor>

                <section formGroupName="customFields" *ngIf="customFields.length">
                    <label>{{ 'catalog.custom-fields' }}</label>
                    <ng-container *ngFor="let customField of customFields">
                        <vdr-custom-field-control
                            *ngIf="customFieldIsSet(customField.name)"
                            [customFieldsFormGroup]="detailForm.get(['customFields'])"
                            [customField]="customField"
                        ></vdr-custom-field-control>
                    </ng-container>
                </section>
            </section>
        </div>
        <div class="clr-col-md-auto">
            <vdr-product-assets
                [assets]="category.assets"
                [featuredAsset]="category.featuredAsset"
                (change)="assetChanges = $event"
            ></vdr-product-assets>
        </div>
    </div>
    <div class="clr-row" formArrayName="filters">
        <div class="clr-col">
            <label>{{ 'catalog.filters' | translate }}</label>
            <ng-container *ngFor="let filter of filters; index as i">
                <vdr-configurable-input
                    (remove)="removeFilter($event)"
                    [facets]="facets$ | async"
                    [operation]="filter"
                    [formControlName]="i"
                ></vdr-configurable-input>
            </ng-container>

            <div>
                <clr-dropdown>
                    <div clrDropdownTrigger>
                        <button class="btn btn-outline">
                            <clr-icon shape="plus"></clr-icon>
                            {{ 'marketing.add-condition' | translate }}
                        </button>
                    </div>
                    <clr-dropdown-menu clrPosition="top-right" *clrIfOpen>
                        <button
                            *ngFor="let filter of allFilters"
                            type="button"
                            clrDropdownItem
                            (click)="addFilter(filter)"
                        >
                            {{ filter.code }}
                        </button>
                    </clr-dropdown-menu>
                </clr-dropdown>
            </div>
        </div>
        <div class="clr-col">
            <vdr-collection-contents [collectionId]="id" #collectionContents>
                <ng-template let-count>
                    <div class="contents-title">
                        {{ 'catalog.collection-contents' | translate }} ({{
                            'common.results-count' | translate: { count: count }
                        }})
                    </div>
                </ng-template>
            </vdr-collection-contents>
        </div>
    </div>
</form>
