<template>
    <div class="row">
        <div class="col">
            <loading-component style="height: 50px; top: 0;" key="1" color="success" v-show="isLoading"></loading-component>
            <div class="row" v-show="!isLoading">
                <div class="col">
                    <div class="row m-b-20">
                        <div class="col">
                            <h6 class="all-caps m-b-5 bold no-margiPsn">Edit Service Charge</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 m-r-0">
                        <div class="col p-r-0">
                            <validation-wrapper-component :validator="v$.parameters.detail.amount.value">
                                <label>Service Charge (MYR)</label>
                                <input type="text" class="form-control" v-model.lazy="parameters.detail.amount.value" v-money3="parameters.detail.amount.type === 'percentage' ? threeDecimalPercentage : productPrice">
                            </validation-wrapper-component>
                        </div>
                        <div class="col-2 text-white pointer text-center" :class="[{ 'bg-master-lightest': parameters.detail.amount.type === 'percentage' }, { 'bg-info': parameters.detail.amount.type === 'fix_amount' }]" @click="parameters.detail.amount.type = 'fix_amount'">
                            <div class="row h-100 align-items-center">
                                <div class="col p-r-20 fs-20" :class="[{ 'text-master': parameters.detail.amount.type === 'percentage' }]">
                                    MYR
                                </div>
                            </div>
                        </div>
                        <div class="col-2 text-white pointer text-center" :class="[{ 'bg-master-lightest': parameters.detail.amount.type === 'fix_amount' }, { 'bg-info': parameters.detail.amount.type === 'percentage' }]" @click="parameters.detail.amount.type = 'percentage'">
                            <div class="row h-100 align-items-center">
                                <div class="col p-r-20 fs-20" :class="[{ 'text-master': parameters.detail.amount.type === 'fix_amount' }]">
                                    %
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row m-b-10 m-r-0">
                        <div class="col p-r-0">
                            <validation-wrapper-component :validator="v$.parameters.detail.transferFee.value">
                                <label>Transfer Fee (FEC)</label>
                                <input type="text" class="form-control" v-model.lazy="parameters.detail.transferFee.value" v-money3="parameters.detail.transferFee.type === 'percentage' ? threeDecimalPercentage : productPrice">
                            </validation-wrapper-component>
                        </div>
                        <div class="col-2 text-white pointer" :class="[{ 'bg-master-lightest': parameters.detail.transferFee.type === 'percentage' }, { 'bg-info': parameters.detail.transferFee.type === 'fix_amount' }]" @click="parameters.detail.transferFee.type = 'fix_amount'">
                            <div class="row h-100 align-items-center text-center">
                                <div class="col p-r-20 p-l-20 fs-20" :class="[{ 'text-master': parameters.detail.transferFee.type === 'percentage' }]">
                                    FEC
                                </div>
                            </div>
                        </div>
                        <div class="col-2 text-white pointer" :class="[{ 'bg-master-lightest': parameters.detail.transferFee.type === 'fix_amount' }, { 'bg-info': parameters.detail.transferFee.type === 'percentage' }]" @click="parameters.detail.transferFee.type = 'percentage'">
                            <div class="row h-100 align-items-center text-center">
                                <div class="col p-r-20 fs-20" :class="[{ 'text-master': parameters.detail.transferFee.type === 'fix_amount' }]">
                                    %
                                </div>
                            </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="modal">Cancel</div>
                        </div>
                        <div class="col p-l-5">
                            <div class="btn btn-sm btn-success btn-block b-rad-none" @click="submitForm">Confirm</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
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> | null;
    supplierId: number;
}

const props = defineProps<Props>();

const productPrice = { decimal: '.', thousands: ',', precision: 3 };
const threeDecimalPercentage = { decimal: '.', thousands: '', precision: 3, suffix: '%' };
const { parameters, submit, error, isLoading } = useFormHandler({
    section: props.section,
    data: {
        name: 'Service Charge',
        reference: 'SERVICE_CHARGE',
        detail: {
            id: props.supplierId,
            amount: {
                type: props.data ? props.data.detail.amount.type : 'percentage',
                value: props.data ? parseFloat(props.data.detail.amount.value).toFixed(3) : 0
            },
            transferFee: {
                type: props.data ? props.data.detail.transferFee.type : 'percentage',
                value: props.data ? parseFloat(props.data.detail.transferFee.value).toFixed(3) : 0
            },
            transferCompanyId: props.data ? props.data.detail.transferCompanyId : props.supplierId
        },
        segment_id: 1
    }
});

const rules = {
    parameters: {
        detail: {
            amount: {
                value: { required }
            },
            transferFee: {
                value: { required }
            },
            transferCompanyId: { required }
        }
    }
};

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

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

    submit(
        props.data ? route('api.segment_constant.update', props.data.id) : route('api.segment_constant.create'),
        props.data ? 'put' : 'post',
        props.section,
        true,
        true,
        parameters.value
    );
};
</script>
