<template>
  <q-dialog ref="dialogRef" @hide="onDialogHide">
    <div style="width: 100%; max-width: 600px">
      <q-card class="overflow-hidden" style="width: 100%">
        <DefaultDialogHeader title="Responder Suporte" @close="onDialogCancel" />

        <q-form ref="formRef" @submit="onOKClick">
          <q-card-section class="q-pt-sm">
            <DefaultInput
              v-model="reply"
              label="Descreva a resposta"
              type="textarea"
              class="col-12"
            />
          </q-card-section>

          <q-card-actions align="right" class="q-px-md q-pb-md">
            <q-btn
              outline
              color="primary"
              label="Cancelar"
              @click="onDialogCancel"
            />
            <q-btn
              color="primary"
              label="Responder"
              type="submit"
              :loading="loading"
            />
          </q-card-actions>
        </q-form>
      </q-card>
    </div>
  </q-dialog>
</template>

<script setup>
import { ref } from "vue";
import { useDialogPluginComponent } from "quasar";

import DefaultDialogHeader from "src/components/defaults/DefaultDialogHeader.vue";
import DefaultInput from "src/components/defaults/DefaultInput.vue";
import { createSupportReply } from "src/api/support_reply";

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

const { ticketId } = defineProps({
  ticketId: {
    type: Number,
    required: true,
  },
});

const { dialogRef, onDialogHide, onDialogOK, onDialogCancel } =
  useDialogPluginComponent();

const loading = ref(false);
const reply = ref(null);

const onOKClick = async () => {
  loading.value = true;
  try {
    await createSupportReply(ticketId, { reply: reply.value });
    onDialogOK(true);
  } finally {
    loading.value = false;
  }
};
</script>
