<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">{{ parameters.id ? 'Update Reward' : 'Create Reward' }}</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">
                            <input type="hidden" class="form-control" v-if="parameters.id" v-model="parameters.id">
                            <validation-wrapper-component :validator="v$.parameters.name">
                                <label>Name</label>
                                <input type="text" class="form-control" v-model="parameters.name">
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <validation-wrapper-component :validator="v$.parameters.description">
                                <label>Description</label>
                                <input type="text" class="form-control" v-model="parameters.description">
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <validation-wrapper-component selectable :validator="v$.parameters.is_active">
                                <label>Is Active</label>
                                <selectable-hard-coded-component section="rewardisActiveOptions" :selectable-options="optionsIsActive" v-model="parameters.is_active"></selectable-hard-coded-component >
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <validation-wrapper-component selectable :validator="v$.parameters.type">
                                <label>Type</label>
                                <selectable-hard-coded-component section="rewardTypeOptions" :selectable-options="optionsType" v-model="parameters.type"></selectable-hard-coded-component>
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <validation-wrapper-component :validator="v$.parameters.value">
                                <label>Value</label>
                                <input class="form-control" name="value" v-model.lazy="parameters.value">
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <validation-wrapper-component :validator="v$.parameters.order">
                                <label>Order</label>
                                <input class="form-control" name="value" v-model.lazy="parameters.order">
                            </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">{{ parameters.id ? 'Update Reward' : 'Create Reward' }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script setup lang="ts">
import { useFormHandler } from '@/composables/useFormHandler';
import { useQueueStore } from '@/stores/queue';
import route from '@/general/functions/router';
import { useVuelidate } from '@vuelidate/core';
import { required } from '@vuelidate/validators';

interface Props {
    section: string;
    data?: any;
    optionsIsActive: any[];
    optionsType: any[];
}

const props = defineProps<Props>();
const queueStore = useQueueStore();

const { parameters, submit, error, isLoading, closeModal, formHandler } = useFormHandler({
    section: props.section,
    data: props.data || {
        id: 0,
        name: '',
        description: '',
        value: '',
        type: '',
        is_active: '',
        order: 9999
    }
});

const rules = {
    parameters: {
        id: { required },
        name: { required },
        description: { required },
        value: { required },
        type: { required },
        is_active: { required },
        order: {}
    }
};

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

const reset = () => {
    parameters.value = {
        id: 0,
        name: '',
        description: '',
        value: '',
        type: '',
        is_active: '',
        order: 9999
    };
    v$.value.$reset();
};

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

    const endpoint = parameters.value.id
        ? route('api.reward.update', parameters.value.id)
        : route('api.reward.create');
    const method = parameters.value.id ? 'put' : 'post';

    await submit(endpoint, method, props.section, true, false, parameters.value, {
        successHandler: () => {
            closeModal();
            formHandler();
            reset();
            queueStore.reloadList({ name: 'rewardListSection' });
        }
    });
};
</script>
