<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 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.account_no">
                                <label>{{ serviceType.id === 4 ? 'Alipay recipient Email / Phone' : 'Account No.' }}</label>
                                <input type="text" class="form-control" v-model="parameters.account_no" :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</label>
                                <input type="text" class="form-control" v-model="parameters.holder_name" @keyup="onlyChinese($event)" :disabled="disabled">
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row" v-show="englishTextWarning === true">
                        <div class="col">
                            <div class="fs-11 text-danger m-b-10" v-if="!confirmProceedEnglishText">Warning: We do not encourage to transfer to non-chinese recipient Alipay account ! <span class="text-danger bold pointer text-underline" @click="confirmProceedEnglishText = !confirmProceedEnglishText">Proceed Anyway.</span></div>
                            <div class="row" v-if="confirmProceedEnglishText">
                                <div class="col text-danger bold">
                                    <p>Foreign name alipay may exceed Monthly / Yearly Limit , and may be unable to withdraw your funds out.</p>
                                </div>
                            </div>
                            <div class="text-danger m-b-10" v-if="confirmProceedEnglishText">The risk is too high. <span class="text-danger bold pointer text-underline" @click="confirmProceedEnglishText = !confirmProceedEnglishText">I changed my mind.</span></div>
                        </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">
                            <button class="btn btn-sm btn-success btn-block b-rad-none" @click="submitForm()" :disabled="!confirmProceedEnglishText && englishTextWarning">Add Account</button>
                        </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
            }
        },
        data(){
            return {
                parameters: {
                    company_id: this.company_id,
                    account_type: 3,
                    reference: '',
                    bank_name: '-',
                    holder_name: '',
                    account_no: '',
                    bank_branch: '-',
                    country_id: this.country_id,
                },
                englishTextWarning: false,
                confirmProceedEnglishText: false,
            }
        },
        validations: {
            parameters: {
                holder_name: {
                    required
                },
                account_no: {
                    required
                },
                reference: {
                    required: requiredIf(function () { return this.parameters.type === 2 })
                }
            }
        },
        methods: {
            submitForm(){
                this.parameters.account_type = 3,
                this.parameters.bank_name = '-',
                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);
            },
            onlyChinese(event){
                let value = event.target.value,
                    regex = /^[^~`!@#$%^&*()_+=[\]\{}|;':",.\/<>?a-zA-Z0-9-]+$/;

                event.preventDefault();

                this.englishTextWarning = false;
                if(regex.test(value) === false){
                    this.englishTextWarning = true;
                }
            },
        },
        mixins: [FormHandler]

    }
</script>