<template>
    <div class="row">
        <div class="col">
            <div class="row">
                <div class="col">
                    <div class="row m-b-10">
                        <div class="col">
                            <h5 class="all-caps m-b-5 bold no-margin">Edit Transaction Group</h5>
                        </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 selectable :validator="v$.parameters.supplier_id">
                                <label>Supplier</label>
                                <selectable-component :endpoint="route('api.company.list') + '?filters=' + JSON.stringify({'business_type': 3, 'status_in': [2, 1, 0]})" section="supplierListSection" valueColumn="id" :labelColumn="['name']" v-model="parameters.supplier_id"></selectable-component>
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <validation-wrapper-component :validator="v$.parameters.rate">
                                <label class="all-caps">Purchase Rate</label>
                                <input type="text" class="form-control" v-model.lazy="parameters.rate" v-money3="exchangeRate">
                            </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 { useFormHandler } from '@/composables/useFormHandler';
import { useQueueStore } from '@/stores/queue';
import useVuelidate from '@vuelidate/core';

const props = defineProps<{
    currency_rate: number;
    supplier_id: number;
    id: number;
    section: string;
}>();

const { submit, closeModal, error, parameters, formHandler } = useFormHandler({ section: props.section });
const queueStore = useQueueStore();

import route from '@/general/functions/router';

parameters.value = {
    supplier_id: props.supplier_id,
    rate: (Math.round((props.currency_rate + Number.EPSILON) * 10000) / 10000).toFixed(5)
};

const rules = {
    parameters: {
        supplier_id: { },
        rate: { },
    },
};

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

const submitForm = () => {
    submit(route('api.transaction.group.update', props.id), 'put', props.section, true, true, parameters.value, {
        successHandler: () => {
            closeModal();
            formHandler();
        },
        errorHandler: (response: any) => {
            formHandler(response.message);
        }
    });
};

const exchangeRate = {
    precision: 5,
    decimal: '.',
    thousands: ',',
    prefix: '',
    suffix: '',
};
</script>
