<template>
  <q-dialog ref="dialogRef" @hide="onDialogHide">
    <q-card
      class="q-dialog-plugin dialog-form-card"
      style="width: 480px; max-width: 95vw"
    >
      <DefaultDialogHeader
        title="Adicionar Mídia"
        @close="onDialogCancel"
      />

      <DefaultForm
        ref="formRef"
        @submit="onSubmit"
      >
        <q-scroll-area class="dialog-form-scroll dialog-form-scroll--xs">
          <q-card-section class="q-pt-none">
            <div class="column q-gutter-sm">
              <DefaultInput
                v-model="form.name"
                label="Nome do documento"
                :error="!!validationErrors.name"
                :error-message="validationErrors.name"
                :rules="[inputRules.required]"
              />

              <CustomFileInput
                v-model="selectedFile"
                accept="image/*,video/*,.pdf"
                label="Arquivo"
                outlined
                :error="!!validationErrors.file"
                :error-message="validationErrors.file"
                :rules="[inputRules.required]"
              />
            </div>
          </q-card-section>
        </q-scroll-area>

        <q-card-actions
          align="right"
          class="q-px-md q-pb-md"
        >
          <q-btn
            color="primary"
            label="Cancelar"
            no-caps
            outline
            @click="onDialogCancel"
          />

          <q-btn
            color="primary"
            label="Adicionar"
            no-caps
            type="submit"
            :loading="loading"
          />
        </q-card-actions>
      </DefaultForm>
    </q-card>
  </q-dialog>
</template>

<script setup>
import { createStudentMedia } from "src/api/student_media";
import { ref } from "vue";
import { useDialogPluginComponent } from "quasar";
import { useInputRules } from "src/composables/useInputRules";
import { useSubmitHandler } from "src/composables/useSubmitHandler";

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

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

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

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

const { inputRules } = useInputRules();

const formRef = ref(null);

const form = ref({
  name: "",
});

const selectedFile = ref(null);

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

const onSubmit = async () => {
  await execute(() => {
    const formData = new FormData();

    formData.append("file", selectedFile.value);
    formData.append("name", form.value.name);
    formData.append("student_id", studentId);

    return createStudentMedia(formData);
  });
};
</script>