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

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

<form class="form" [formGroup]="detailForm" *ngIf="entity$ | async as paymentMethod">
    <vdr-form-field [label]="'common.name' | translate" for="name">
        <input
            id="name"
            type="text"
            formControlName="name"
            [readonly]="!(updatePermission | hasPermission)"
            (input)="updateCode(paymentMethod.code, $event.target.value)"
        />
    </vdr-form-field>
    <vdr-form-field
        [label]="'common.code' | translate"
        for="code"
        [readOnlyToggle]="updatePermission | hasPermission"
    >
        <input
            id="code"
            type="text"
            formControlName="code"
            [readonly]="!(updatePermission | hasPermission)"
        />
    </vdr-form-field>
    <vdr-rich-text-editor
        formControlName="description"
        [readonly]="!(updatePermission | hasPermission)"
        [label]="'common.description' | translate"
    ></vdr-rich-text-editor>
    <vdr-form-field [label]="'common.enabled' | translate" for="description">
        <clr-toggle-wrapper>
            <input
                type="checkbox"
                clrToggle
                id="enabled"
                [vdrDisabled]="!(updatePermission | hasPermission)"
                formControlName="enabled"
            />
        </clr-toggle-wrapper>
    </vdr-form-field>
    <section formGroupName="customFields" *ngIf="customFields.length">
        <label>{{ 'common.custom-fields' | translate }}</label>
        <vdr-tabbed-custom-fields
            entityName="PaymentMethod"
            [customFields]="customFields"
            [customFieldsFormGroup]="detailForm.get('customFields')"
            [readonly]="!(updatePermission | hasPermission)"
        ></vdr-tabbed-custom-fields>
    </section>

    <vdr-custom-detail-component-host
        locationId="payment-method-detail"
        [entity$]="entity$"
        [detailForm]="detailForm"
    ></vdr-custom-detail-component-host>

    <div class="clr-row mt-4">
        <div class="clr-col">
            <label class="clr-control-label">{{ 'settings.payment-eligibility-checker' | translate }}</label>
            <vdr-configurable-input
                *ngIf="selectedChecker && selectedCheckerDefinition"
                [operation]="selectedChecker"
                [operationDefinition]="selectedCheckerDefinition"
                [readonly]="!(updatePermission | hasPermission)"
                (remove)="removeChecker()"
                formControlName="checker"
            ></vdr-configurable-input>
            <div *ngIf="!selectedChecker || !selectedCheckerDefinition">
                <vdr-dropdown>
                    <button class="btn btn-outline" vdrDropdownTrigger>
                        <clr-icon shape="plus"></clr-icon>
                        {{ 'common.select' | translate }}
                    </button>
                    <vdr-dropdown-menu vdrPosition="bottom-left">
                        <button
                            *ngFor="let checker of checkers"
                            type="button"
                            vdrDropdownItem
                            (click)="selectChecker(checker)"
                        >
                            {{ checker.description }}
                        </button>
                    </vdr-dropdown-menu>
                </vdr-dropdown>
            </div>
        </div>
        <div class="clr-col">
            <label class="clr-control-label">{{ 'settings.payment-handler' | translate }}</label>
            <vdr-configurable-input
                *ngIf="selectedHandler && selectedHandlerDefinition"
                [operation]="selectedHandler"
                [operationDefinition]="selectedHandlerDefinition"
                [readonly]="!(updatePermission | hasPermission)"
                (remove)="removeHandler()"
                formControlName="handler"
            ></vdr-configurable-input>
            <div *ngIf="!selectedHandler || !selectedHandlerDefinition">
                <vdr-dropdown>
                    <button class="btn btn-outline" vdrDropdownTrigger>
                        <clr-icon shape="plus"></clr-icon>
                        {{ 'common.select' | translate }}
                    </button>
                    <vdr-dropdown-menu vdrPosition="bottom-left">
                        <button
                            *ngFor="let handler of handlers"
                            type="button"
                            vdrDropdownItem
                            (click)="selectHandler(handler)"
                        >
                            {{ handler.description }}
                        </button>
                    </vdr-dropdown-menu>
                </vdr-dropdown>
            </div>
        </div>
    </div>
</form>
