<template>
    <div class="row ">
        <div class="col bg-white padding-15">
            <div class="row">
                <div class="col-12">
                    <div class="form-group">
                        <label>Date: {{ item.posting_date }}</label>
                    </div>
                    <div class="form-group">
                        <label>Transaction Description 1: {{ item.transaction_description_1 }}</label>
                    </div>
                    <div class="form-group">
                        <label>Transaction Description 2: {{ item.transaction_description_2 }}</label>
                    </div>
                    <div class="form-group">
                        <label>Transaction Description 3: {{ item.transaction_description_3 }}</label>
                    </div>
                    <div class="form-group">
                        <label>Transaction Description 4: {{ item.transaction_description_4 }}</label>
                    </div>
                    <div class="form-group">
                        <label>Transaction Description 5: {{ item.transaction_description_5 }}</label>
                    </div>
                </div>
            </div>
            <div class="row m-b-15">
                <div class="col">
                    <div class="row">
                        <div class="col">
                            <validation-wrapper-component selectable :validator="v$.pay_for_value">
                                <label>Transaction Type</label>
                                <select-component :options="['Sales','Top Up', 'Internal Bank Transfer', 'Others']" v-model="pay_for_value"></select-component>
                            </validation-wrapper-component>
                        </div>
                    </div>

                    <div class="row m-t-15" v-if="['Sales', 'Top Up'].includes(pay_for_value)">
                        <div class="col">
                            <validation-wrapper-component selectable :key="custom_options_key" :validator="v$.system_references">
                                <label>Service</label>
                                <select-component :options="serviceOptions" v-model="system_references"></select-component>
                            </validation-wrapper-component>
                        </div>
                    </div>

                    <div class="row m-t-15" v-if="['Sales', 'Top Up', 'Others'].includes(pay_for_value)">
                        <div class="col">
                            <validation-wrapper-component :validator="v$.transaction_reference">
                                <label>Transaction ID / Reference</label>
                                <input type="text" class="form-control" v-model.lazy="transaction_reference">
                            </validation-wrapper-component>
                        </div>
                    </div>
                </div>
            </div>
            <p class="text-danger fs-9">{{ error }}</p>
            <div class="row">
                <div class="col">
                    <div class="row">
                        <div class="col p-r-5">
                            <div data-dismiss="modal" class="btn btn-sm btn-default bg-master-lighter btn-block b-rad-none">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 { ref, watch, onMounted, computed } from 'vue';
    import { useComponentHandler } from '@/composables/useComponentHandler';
    import { useFormHandler } from '@/composables/useFormHandler';
    import { useVuelidate } from '@vuelidate/core';
    import { required } from "@vuelidate/validators";

    const props = withDefaults(defineProps<{
        data: any;
        section: string;
        editMapped?: boolean;
    }>(), {
        editMapped: false
    });

    const { item } = useComponentHandler(props);
    const { submit } = useFormHandler(props);

    const error = ref('');
    const system_references = ref("");
    const transaction_reference = ref("");
    const pay_for_value = ref("");
    const custom_options_key = ref(1);
    const pay_for = ref({
        status: false,
        options: ['sales', 'top_up', 'internal_bank_transfer', 'others'],
    });

    const rules = {
        system_references: { },
        transaction_reference: { },
        pay_for_value: { required },
    };

    const v$ = useVuelidate(rules, { system_references, transaction_reference, pay_for_value });

    onMounted(() => {
        pay_for.value = item.value.pay_for;
        system_references.value = item.value.system_references;
    });

    watch(() => item.value, () => {
        pay_for.value = item.value.pay_for;
        system_references.value = item.value.system_references;
    }, { deep: true });

    watch(pay_for_value, () => {
        custom_options_key.value++;
        transaction_reference.value = '';
    });

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

        const parameters: Record<string, any> = {
            pay_for: pay_for_value.value.replace(/\s/g, "_").toLowerCase(),
            system_references: system_references.value ? system_references.value.toLowerCase() : '',
            transaction_reference: transaction_reference.value
        };

        if (props.editMapped) parameters['editMapped'] = true;

        submit(
            (window as any).route('api.accounting.statement.details.update', props.data.id),
            'put',
            props.section,
            true,
            true,
            parameters,
            {
                errorHandler: (err: any) => {
                    error.value = err.message;
                }
            }
        );
    };

    const serviceOptions = computed(() => {
        const rawOptions = (() => {
            switch (pay_for_value.value) {
                case 'Sales':
                    return ['Exchange', 'Izyim', 'Lite', 'Cntr', 'Probashi', 'Pets'];
                case 'Top Up':
                    return ['Exchange', 'Izyim'];
                default:
                    return [];
            }
        })();
        return rawOptions.map(option => ({ id: option, text: option }));
    });

    // Re-register legacy methods if needed (though they are now local functions)
</script>

