<template>
    <div class="row">
        <div class="col">
            <div class="row justify-content-center">
                <div class="col-auto text-center">
                    <div class="row">
                        <div class="col text-center">
                            <div class="row">
                                <div class="col">
                                    <h5 class="all-caps">Reject Document</h5>
                                    <div class="fs-11">Are you sure you want to reject this customer's identification?</div>
                                </div>
                            </div>
                            <div class="row text-left margin-auto m-t-10 m-b-10">
                                <div class="col">
                                    <span class="text-danger fs-9">{{ error }}</span>
                                    <div class="fs-11">Reason: </div>
                                    <div class="row">
                                        <div class="col fs-11">
                                            <div class="b-a padding-5 w-100 m-b-5 pointer b-grey muted" :class="{'b-primary': rejectRemark === rejectRemarkItem, 'text-primary': rejectRemark === rejectRemarkItem}" v-for="rejectRemarkItem in rejectRemarkArray" @click="chooseRejectRemark(rejectRemarkItem)">{{ rejectRemarkItem }}</div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <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-danger btn-block b-rad-none" @click="approveDocument('reject')">Reject</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useFormHandler } from '@/composables/useFormHandler';
import { useVuelidate } from '@vuelidate/core';
import { required } from '@vuelidate/validators';
import route from '@/general/functions/router';
import { useComponentHandler } from '@/composables/useComponentHandler';

interface Props {
    data: any;
    section: string;
}

const props = defineProps<Props>();

const rejectRemark = ref<string | null>(null);
const rejectRemarkArray = ref<string[]>([]);
const documentType = ref(props.data.document_type === 'IDENTITY_CARD' ? 'IC' : 'SSM');

const { item } = useComponentHandler(props);
const { submit, error, isLoading } = useFormHandler({
    section: props.section
});

const rules = {
    rejectRemark: { required }
};

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

onMounted(() => {
    rejectRemarkArray.value = [
        documentType.value + ' not clear',
        documentType.value + ' name different with registration name',
        'Wrong Document uploaded',
        'Non-Malaysian ' + documentType.value + ' Uploaded', 
        documentType.value + ' not Genuine'
    ];
});

const approveDocument = async (status: string) => {
    const isValid = await v$.value.$validate();
    if (!isValid) {
        // Validation handled by setting error manually if needed, or just let v$ show it
        return;
    }

    submit(route('api.company.identification.approval', item.value.owner.id, item.value.id, status), 'put', 'identificationVerificationSection', true, true, {
        remark: rejectRemark.value
    });
};

const chooseRejectRemark = (remark: string) => {
    rejectRemark.value = remark;
};
</script>