<mg-ajax path="/client/api/Clients/{{patch.params.id}}" options="vnPatch"></mg-ajax>
<vn-watcher
    vn-id="watcher"
    data="$ctrl.client"
    form="form"
    save="patch">
</vn-watcher>
<form name="form" ng-submit="$ctrl.onSubmit()" compact>
    <vn-card class="vn-pa-lg">
        <vn-horizontal>
            <vn-autocomplete
                vn-one
                label="Billing data"
                vn-acl="salesAssistant"
                field="$ctrl.client.payMethodFk"
                url="/client/api/PayMethods"
                fields="['ibanRequired']"
                initial-data="$ctrl.client.payMethod">
            </vn-autocomplete>
            <vn-input-number
                vn-one
                min="0"
                step="1"
                label="Due day"
                field="$ctrl.client.dueDay"
                vn-acl="salesAssistant"
                rule>
            </vn-input-number>
        </vn-horizontal>
        <vn-horizontal>
            <vn-textfield
                vn-one
                label="IBAN"
                field="$ctrl.client.iban"
                on-change="$ctrl.autofillBic()"
                vn-acl="salesAssistant">
            </vn-textfield>
            <vn-autocomplete
                vn-one
                label="Swift / BIC"
                url="/client/api/BankEntities"
                field="$ctrl.client.bankEntityFk"
                fields="['name']"
                initial-data="$ctrl.client.bankEntityFk"
                on-change="$ctrl.autofillBic()"
                search-function="{or: [{bic: {like: $search +'%'}}, {name: {like: '%'+ $search +'%'}}]}"
                value-field="id"
                show-field="bic"
                vn-acl="salesAssistant"
                disabled="$ctrl.ibanCountry == 'ES'">
                <tpl-item>
                    <vn-horizontal>
                        <vn-one>{{bic}}</vn-one>
                        <vn-one>
                            <div class="ellipsize" style="max-width: 10em">{{name}}</div>
                        </vn-one>
                    </vn-horizontal>
                </tpl-item>
                <append>
                    <vn-icon-button
                        vn-auto
                        class="vn-my-md"
                        icon="add_circle"
                        vn-tooltip="New bank entity"
                        vn-dialog="bankEntityDialog"
                        vn-acl="salesAssistant">
                    </vn-icon-button>
                </append>
            </vn-autocomplete>
        </vn-horizontal>
        <vn-horizontal class="vn-py-sm">
            <vn-one>
                <vn-check
                    label="Received LCR"
                    field="$ctrl.client.hasLcr"
                    vn-acl="salesAssistant">
                </vn-check>
            </vn-one>
            <vn-one>
                <vn-check
                    label="Received core VNL"
                    field="$ctrl.client.hasCoreVnl"
                    vn-acl="salesAssistant">
                </vn-check>
            </vn-one>
            <vn-one>
                <vn-check
                    label="Received B2B VNL"
                    field="$ctrl.client.hasSepaVnl"
                    vn-acl="salesAssistant">
                </vn-check>
            </vn-one>
        </vn-horizontal>
    </vn-card>
    <vn-button-bar>
        <vn-submit label="Save" vn-acl="salesAssistant"></vn-submit>
        <vn-button label="Undo changes" ng-if="watcher.dataChanged()" ng-click="watcher.loadOriginalData()"></vn-button>
    </vn-button-bar>
</form>

<!-- Create bank entity dialog -->
<vn-dialog class="edit" 
    vn-id="bankEntityDialog"
    on-open="$ctrl.onBankEntityOpen()"
    on-response="$ctrl.onBankEntityResponse(response)">
    <tpl-body>
        <h5 class="vn-py-sm" translate>New bank entity</h5>
        <vn-horizontal>
            <vn-textfield vn-one
                label="Name"
                model="$ctrl.newBankEntity.name"
                required="true">
            </vn-textfield>
        </vn-horizontal>
        <vn-horizontal>
            <vn-autocomplete vn-id="country" vn-one
                label="Country"
                field="$ctrl.newBankEntity.countryFk"
                fields="['id', 'country', 'code']"
                url="/client/api/Countries"
                value-field="id"
                show-field="country"
                required="true">
            </vn-autocomplete>
        </vn-horizontal>
        <vn-horizontal>
            <vn-textfield vn-one
                label="Entity Code"
                model="$ctrl.newBankEntity.id"
                ng-show="country.selection.code === 'ES'">
            </vn-textfield>
        </vn-horizontal>
        <vn-horizontal>
            <vn-textfield vn-one
                label="Swift / BIC"
                model="$ctrl.newBankEntity.bic"
                required="true">
            </vn-textfield>
        </vn-horizontal>
    </tpl-body>
    <tpl-buttons>
        <input type="button" response="CANCEL" translate-attr="{value: 'Cancel'}"/>
        <button response="ACCEPT" translate>Create</button>
    </tpl-buttons>
</vn-dialog>