<script setup>
import { ref } from 'vue';
import { useI18n } from 'vue-i18n';

const emit = defineEmits(['onSubmit']);

defineProps({
    title: {
        type: String,
        default: '',
    },
    subtitle: {
        type: String,
        default: '',
    },
    defaultSubmitButton: {
        type: Boolean,
        default: true,
    },
    defaultCancelButton: {
        type: Boolean,
        default: true,
    },
    customSubmitButtonLabel: {
        type: String,
        default: '',
    },
});

const { t } = useI18n();

const closeButton = ref(null);
const isLoading = ref(false);

const onSubmit = () => {
    emit('onSubmit');
    closeForm();
};

const closeForm = () => {
    if (closeButton.value) closeButton.value.click();
};
</script>

<template>
    <QForm
        @submit="onSubmit($event)"
        class="all-pointer-events full-width"
        style="max-width: 800px"
    >
        <QCard class="q-pa-lg">
            <span ref="closeButton" class="close-icon" v-close-popup>
                <QIcon name="close" size="sm" />
            </span>
            <h1 class="title">{{ title }}</h1>
            <p>{{ subtitle }}</p>
            <slot name="form-inputs" />
            <div class="q-mt-lg row justify-end">
                <QBtn
                    v-if="defaultCancelButton"
                    :label="t('globals.cancel')"
                    color="primary"
                    flat
                    class="q-ml-sm"
                    :disabled="isLoading"
                    :loading="isLoading"
                    v-close-popup
                />
                <QBtn
                    v-if="defaultSubmitButton"
                    :label="customSubmitButtonLabel || t('globals.save')"
                    type="submit"
                    color="primary"
                    :disabled="isLoading"
                    :loading="isLoading"
                />
                <slot name="custom-buttons" />
            </div>
        </QCard>
    </QForm>
</template>

<style lang="scss" scoped>
.title {
    font-size: 17px;
    font-weight: bold;
    line-height: 20px;
}

.close-icon {
    position: absolute;
    top: 20px;
    right: 20px;
    cursor: pointer;
}
</style>
