<template>
  <q-dialog ref="dialogRef" @hide="onDialogHide">
    <q-card class="q-dialog-plugin overflow-hidden" style="width: 700px; max-width: 95vw">
      <DefaultDialogHeader :title="title" @close="onDialogCancel" />
      <q-form ref="formRef" @submit="onOKClick">
        <q-card-section class="row q-col-gutter-sm q-pt-none">
          <DefaultInput
            v-model="form.title"
            v-model:error="validationErrors.title"
            :label="$t('company_settings.benefit.title')"
            :rules="[inputRules.required]"
            class="col-12"
          />
          <div class="col-12">
            <MdiIconPicker
              v-model="form.icon"
              :label="$t('company_settings.benefit.icon')"
              :rules="[inputRules.required]"
            />
          </div>
          <DefaultInput
            v-model="form.description"
            v-model:error="validationErrors.description"
            :label="$t('company_settings.benefit.description')"
            type="textarea"
            :rows="3"
            class="col-12"
          />
        </q-card-section>
        <q-card-actions>
          <q-space />
          <q-btn
            outline
            color="negative"
            :label="$t('common.actions.cancel')"
            @click="onDialogCancel"
          />
          <q-btn
            color="primary"
            :label="benefit ? $t('common.actions.save') : $t('common.actions.add')"
            type="submit"
            :loading="loading"
            :disable="!hasUpdatedFields"
          />
        </q-card-actions>
      </q-form>
    </q-card>
  </q-dialog>
</template>

<script setup>
import { useTemplateRef } from "vue";
import { useDialogPluginComponent } from "quasar";
import { useI18n } from "vue-i18n";
import { useInputRules } from "src/composables/useInputRules";
import { useFormUpdateTracker } from "src/composables/useFormUpdateTracker";
import { useSubmitHandler } from "src/composables/useSubmitHandler";
import { createCompanyBenefit, updateCompanyBenefit } from "src/api/companySettings";

import DefaultDialogHeader from "src/components/defaults/DefaultDialogHeader.vue";
import DefaultInput from "src/components/defaults/DefaultInput.vue";
import MdiIconPicker from "src/components/defaults/MdiIconPicker.vue";

defineEmits([...useDialogPluginComponent.emits]);

const { benefit, title } = defineProps({
  benefit: {
    type: Object,
    default: null,
  },
  title: {
    type: Function,
    default: () => useI18n().t("company_settings.benefit.dialog_title"),
  },
});

const { inputRules } = useInputRules();
const { dialogRef, onDialogHide, onDialogOK, onDialogCancel } = useDialogPluginComponent();
const formRef = useTemplateRef("formRef");

const { form, getUpdatedFields, hasUpdatedFields } = useFormUpdateTracker({
  title: benefit?.title ?? "",
  icon: benefit?.icon ?? null,
  description: benefit?.description ?? "",
});

const { loading, validationErrors, execute: submitForm } = useSubmitHandler({
  onSuccess: () => onDialogOK(true),
  formRef,
});

const onOKClick = async () => {
  if (benefit) {
    await submitForm(() => updateCompanyBenefit(benefit.id, getUpdatedFields.value));
  } else {
    await submitForm(() => createCompanyBenefit({ ...form }));
  }
};
</script>
