<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-margin">{{ data.remarks.length > 0 ? 'Edit' : 'Create' }} Remark</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">
                        <div class="col">
                            <validation-wrapper-component :validator="v$.content">
                                <label>Remark</label>
                                <input type="text" class="form-control" v-model="parameters.content">
                            </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-primary btn-block b-rad-none" @click="submitForm()">{{ data.remarks.length > 0 ? 'Edit' : 'Create' }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue';
import useVuelidate from '@vuelidate/core';
import { required } from '@vuelidate/validators';
import { useFormHandler } from '@/composables/useFormHandler';
import route from '@/general/functions/router';

const props = defineProps<{
    section: string;
    data: any;
    module_type: string;
}>();

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

const parameters = reactive({
    content: props.data.remarks.length > 0 ? props.data.remarks[0].content : '',
    model_type: props.module_type,
});

const rules = {
    content: { required },
};

const v$ = useVuelidate(rules, parameters);

const submitForm = async () => {
    if (!(await v$.value.$validate())) return;
    const url = props.data.remarks.length > 0
        ? route('api.remark.update', props.data.remarks[0].id)
        : route('api.remark.create', props.data.id);
    const method = props.data.remarks.length > 0 ? 'put' : 'post';
    submit(url, method, props.section, true, true, parameters);
};
</script>
