<template>
    <div class="row">
        <div class="col">
            <loading-component style="height: 50px; top: 0;" key="1" color="success" v-show="isLoading" />
            <div class="row" v-show="!isLoading">
                <div class="col">
                    <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">
                        <div class="col">
                            <validation-wrapper-component :validator="v$.parameters.account_no">
                                <label>ALIPAY 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>ALIPAY Account Holder Name</label>
                                <input type="text" class="form-control" v-model="parameters.holder_name" @keyup="onlyChinese" :disabled="disabled">
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col" :class="[{'p-r-15': disabled}, {'p-r-5': !disabled}]">
                            <div class="btn btn-sm btn-default bg-master-lightest btn-block b-rad-none" @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">Add Account</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { watch, ref } from 'vue';
import { useFormHandler } from '@/composables/useFormHandler';
import { useVuelidate } from '@vuelidate/core';
import { required } from '@vuelidate/validators';
import route from '@/general/functions/router';

interface Props {
    company_id?: number;
    type?: number;
    closable?: boolean;
    disabled?: boolean;
    country_id?: number;
    serviceType?: any;
    section: string;
    data?: any;
}

const props = withDefaults(defineProps<Props>(), {
    company_id: 1,
    type: 1,
    closable: true,
    disabled: false,
    country_id: 1,
    serviceType: null
});

const emit = defineEmits(['close', 'createdBank']);

const { parameters, submit, error, isLoading, closeModal, formHandler} = useFormHandler(props);

const defaults = {
    company_id: props.company_id,
    account_type: 4,
    reference: '',
    bank_name: '-',
    holder_name: '',
    account_no: '',
    bank_branch: '',
    country_id: props.country_id,
};

const syncFromProps = () => {
    if ((!props.data || props.data === undefined) && !props.data.account_no) {
        Object.assign(parameters.value, defaults);
    }
};
// syncFromProps();

const rules = {
    parameters: {
        holder_name: { required },
        account_no: { required }
    }
};

const v$ = useVuelidate(rules, { parameters });

const englishTextWarning = ref(false);

const onlyChinese = (event: Event) => {
    const target = event.target as HTMLInputElement;
    const value = target.value;
    const regex = /^[^~`!@#$%^&*()_+=[\]\{}|;':",.\/<>?a-zA-Z0-9-]+$/;

    englishTextWarning.value = !regex.test(value);
};

const resetForm = () => {
    Object.assign(parameters.value, defaults);
    v$.value.$reset();
};

watch(() => props.data, syncFromProps, { immediate: true });

const submitForm = async () => {
    const isValid = await v$.value.$validate();
    if (!isValid) return;

    parameters.value.account_type = 4;
    parameters.value.bank_name = '-';

    submit(route('api.bank.create'), 'post', props.section, true, false, parameters.value, {
        successHandler: (response: any) => {
            if (props.type !== 2) {
                closeModal();
            } else {
                emit('createdBank', response.payload.data);
            }
            formHandler();
            resetForm();
        },
        errorHandler: (response: any) => {
            formHandler(response.message);
        }
    });
};
</script>
