<template>
    <div class="row" @keyup.enter="submitForm">
        <div class="col">
            <div class="row">
                <div class="col">
                    <div class="row m-b-20">
                        <div class="col">
                            <h3 class="all-caps m-b-5 bold no-margin">Edit Bank Account</h3>
                        </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">
                        <div class="col">
                            <validation-wrapper-component :validator="v$.parameters.reference">
                                <label>Reference</label>
                                <input type="text" class="form-control" v-model="parameters.reference">
                            </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">
                            </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">
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row" v-if="parameters.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">
                                    </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">
                                    </validation-wrapper-component>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row m-b-20" v-if="parameters.type !== 2">
                        <div class="col">
                            <validation-wrapper-component selectable :validator="v$.parameters.bank_name">
                                <label>Bank Name</label>
                                <selectable-component :endpoint="asset('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="modal">Cancel</div>
                        </div>
                        <div class="col p-l-5">
                            <div class="btn btn-sm btn-success btn-block b-rad-none" @click="submitForm">Update</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { watch } 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 {
    section: string;
    data: Record<string, any>;
    company_id: number;
}

const props = defineProps<Props>();

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

const asset = (path: string) => (window as any).Vapor.asset(path);

const hydrateParameters = () => {
    parameters.value = {
        ...(props.data || {}),
        company_id: props.company_id,
        account_type: props.data?.type
    };
};

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

const rules = {
    parameters: {
        holder_name: { required },
        account_no: { required },
        bank_branch: {},
        swift: {},
        snap: {},
        bank_name: { required },
        reference: {}
    }
};

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

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

    submit(route('api.bank.update', props.data.id), 'put', props.section, true, true, parameters.value);
};
</script>
