<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 ? 'Update' : 'Create' }} Announcement</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$.parameters.title">
                                <label>Title</label>
                                <input type="text" class="form-control" v-model="parameters.title">
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col">
                            <validation-wrapper-component :validator="v$.parameters.description">
                                <label>Message</label>
                                <textarea class="form-control h-75" v-model="parameters.description" rows="20"></textarea>
                            </validation-wrapper-component>
                        </div>
                    </div>

                    <div class="row m-b-20">
                        <div class="col-6 p-r-0">
                            <validation-wrapper-component :validator="v$.parameters.starting_on">
                                <label>Start Date</label>
                                <date-picker-component v-model="parameters.starting_on"></date-picker-component>
                            </validation-wrapper-component>
                        </div>
                        <div class="col-6 p-l-0">
                            <validation-wrapper-component :validator="v$.parameters.ending_on">
                                <label>End Date</label>
                                <date-picker-component v-model="parameters.ending_on"></date-picker-component>
                            </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="closable ? 'modal' : ''" @click="$emit('close')">Cancel</div>
                        </div>
                        <div class="col p-l-5">
                            <div class="btn btn-sm btn-success btn-block b-rad-none" @click="submitForm">{{ data ? 'Update' : 'Create' }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script setup lang="ts">
import { useFormHandler } from '@/composables/useFormHandler';
import route from '@/general/functions/router';
import { useVuelidate } from '@vuelidate/core';
import { required } from '@vuelidate/validators';

interface Props {
    section: string;
    data?: any;
    closable?: boolean;
    country_id?: number;
}

const props = withDefaults(defineProps<Props>(), {
    closable: true,
    country_id: 1
});

defineEmits(['close']);

const { parameters, submit, error, isLoading } = useFormHandler({
    section: props.section,
    data: props.data || {
        starting_on: '',
        ending_on: '',
        title: '',
        description: ''
    }
});

const rules = {
    parameters: {
        title: { required },
        description: { required },
        starting_on: { required },
        ending_on: { required }
    }
};

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

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

    const endpoint = props.data
        ? route('api.announcement.update', props.data.id)
        : route('api.announcement.create');
    const method = props.data ? 'put' : 'post';

    await submit(endpoint, method, props.section, true, false, parameters.value);
};
</script>
