<template>
    <div class="row">
        <div class="col">
            <loading-component style="height: 50px; top: 0;" key="1" color="success" v-show="$store.getters.isLoading(section)"></loading-component>
            <div class="row" v-show="!$store.getters.isLoading(section)">
                <div class="col">
                    <div class="row m-b-20" v-if="type !== 2">
                        <div class="col">
                            <h6 class="all-caps m-b-5 bold no-margin">Add Bank Account</h6>
                        </div>
                    </div>
                    <div class="row m-b-5 animate__animated animate__fadeInUpBig animate__fast" v-if="error">
                        <div class="col">
                            <small class="bold fs-10 text-danger">{{error}}</small>
                        </div>
                    </div>
                    <div class="row" v-if="currency !== 'USD'">
                        <div class="col">
                            <div class="row m-b-10" v-if="type === 2">
                                <div class="col">
                                    <validation-wrapper-component :validator="$v.parameters.reference">
                                        <label>Reference</label>
                                        <input type="text" class="form-control" v-model="parameters.reference" :disabled="disabled">
                                    </validation-wrapper-component>
                                </div>
                            </div>
                            <div class="row m-b-10">
                                <div class="col">
                                    <validation-wrapper-component :validator="$v.parameters.holder_name">
                                        <label>Account Holder Name / Company Name</label>
                                        <input type="text" class="form-control" v-model="parameters.holder_name" :disabled="disabled">
                                    </validation-wrapper-component>
                                </div>
                            </div>
                            <div class="row m-b-10">
                                <div class="col">
                                    <validation-wrapper-component :validator="$v.parameters.account_no">
                                        <label>{{ serviceType ? serviceType.id === 4 ? 'Alipay recipient Email / Phone' : 'Account No.' : 'Account No.'}}</label>
                                        <input type="text" class="form-control" v-model="parameters.account_no" :disabled="disabled">
                                    </validation-wrapper-component>
                                </div>
                            </div>
                            <div class="row" v-if="type === 2">
                                <div class="col">
                                    <div class="row m-b-10">
                                        <div class="col">
                                            <validation-wrapper-component :validator="$v.parameters.bank_name">
                                                <label>Bank Name</label>
                                                <input type="text" class="form-control" v-model="parameters.bank_name" :disabled="disabled">
                                            </validation-wrapper-component>
                                        </div>
                                    </div>
                                    <div class="row m-b-10">
                                        <div class="col">
                                            <validation-wrapper-component :validator="$v.parameters.bank_branch">
                                                <label>Bank Branch / 所在地</label>
                                                <input type="text" class="form-control" v-model="parameters.bank_branch" :disabled="disabled">
                                            </validation-wrapper-component>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row" v-if="currency === 'USD'">
                        <div class="col">
                            <div class="row m-b-10">
                                <div class="col">
                                    <validation-wrapper-component :validator="$v.parameters.holder_name">
                                        <label>Account Holder Name</label>
                                        <input type="text" class="form-control" v-model="parameters.holder_name" :disabled="disabled">
                                    </validation-wrapper-component>
                                </div>
                            </div>
                            <div class="row m-b-10">
                                <div class="col">
                                    <validation-wrapper-component :validator="$v.parameters.reference">
                                        <label>Account Holder Address</label>
                                        <input type="text" class="form-control" v-model="parameters.reference" :disabled="disabled">
                                    </validation-wrapper-component>
                                </div>
                            </div>
                            <div class="row m-b-10">
                                <div class="col">
                                    <validation-wrapper-component :validator="$v.parameters.account_no">
                                        <label>Account No</label>
                                        <input type="text" class="form-control" v-model="parameters.account_no" :disabled="disabled">
                                    </validation-wrapper-component>
                                </div>
                            </div>
                            <div class="row" v-if="type === 2">
                                <div class="col">
                                    <div class="row m-b-10">
                                        <div class="col">
                                            <validation-wrapper-component :validator="$v.parameters.bank_name">
                                                <label>Bank Name</label>
                                                <input type="text" class="form-control" v-model="parameters.bank_name" :disabled="disabled">
                                            </validation-wrapper-component>
                                        </div>
                                    </div>
                                    <div class="row m-b-10">
                                        <div class="col">
                                            <validation-wrapper-component :validator="$v.parameters.bank_branch">
                                                <label>Bank Address</label>
                                                <input type="text" class="form-control" v-model="parameters.bank_branch" :disabled="disabled">
                                            </validation-wrapper-component>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row m-b-10" >
                                <div class="col p-r-5">
                                    <validation-wrapper-component :validator="$v.parameters.swift">
                                        <label>Swift</label>
                                        <input type="text" class="form-control" v-model="parameters.swift" :disabled="disabled">
                                    </validation-wrapper-component>
                                </div>
                                <div class="col p-l-5 hide">
                                    <validation-wrapper-component :validator="$v.parameters.snap">
                                        <label>Snap</label>
                                        <input type="text" class="form-control" v-model="parameters.snap" :disabled="disabled">
                                    </validation-wrapper-component>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row m-b-20" v-if="type !== 2">
                        <div class="col">
                            <validation-wrapper-component selectable :validator="$v.parameters.bank_name">
                                <label>Bank Name</label>
                                <selectable-component endpoint="malaysian_banks.json" section="banksListSection" valueColumn="name" :labelColumn="['name']" v-model="parameters.bank_name"></selectable-component>
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col p-r-5">
                            <div class="btn btn-sm btn-default bg-master-lightest btn-block b-rad-none" :data-dismiss="closable ? 'modal' : ''" @click="$emit('close')">{{disabled ? 'Change Recipient Account' : 'Cancel'}}</div>
                        </div>
                        <div class="col p-l-5" v-if="!disabled">
                            <div class="btn btn-sm btn-success btn-block b-rad-none" @click="submitForm()">Add Account</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
    import FormHandler from '../../../general/mixins/formHandler';
    import { required, requiredIf } from "vuelidate/lib/validators";
    export default {
        props : {
            company_id: {
                type: Number,
                default: 1
            },
            type: {
                type: Number,
                default: 1
            },
            closable: {
                type: Boolean,
                default: true
            },
            disabled: {
                type: Boolean,
                default: false
            },
            country_id: {
                type: Number,
                default: 1
            },
            serviceType: {
                type: Object,
                required: false,
                default: null
            },
            currency: {
                type: String,
                required: false,
                default: 'RMB'
            }
        },
        data(){
            return {
                parameters: {
                    company_id: this.company_id,
                    account_type: this.type,
                    reference: '',
                    bank_name: '',
                    holder_name: '',
                    account_no: '',
                    bank_branch: '',
                    swift: '',
                    snap: '',
                    country_id: this.country_id,
                }
            }
        },
        validations: {
            parameters: {
                holder_name: {
                    required
                },
                account_no: {
                    required
                },
                bank_branch: {
                    // required
                },
                swift: {
                    // required
                },
                snap: {
                    // required
                },
                bank_name: {
                    required
                },
                reference: {
                    required: requiredIf(function () { return this.parameters.type === 2 })
                }
            }
        },
        methods: {
            submitForm(){
                this.submit(route('api.bank.create'), 'post', this.section, true, false);
            },
            successHandler(response){
                this.type !== 2 ? this.closeModal() : this.$emit('createdBank', response.payload.data);
                this.formHandler();
                this.resetForm();
            },
            errorHandler(error){
                this.formHandler(error.message);
            }
        },
        mixins: [FormHandler]

    }
</script>
