<script setup lang="ts">
import config from "@/app.config";
import http from "@/plugins/http";

import { useRouter } from "vue-router";
import { useQuasar } from "quasar";
import { useCounterMixin } from "@/stores/mixin";

import Form from "@/modules/11_discipline/components/8_AppealComplain/Form.vue";

const mixin = useCounterMixin();

const { showLoader, messageError, hideLoader, dialogConfirm, success } = mixin;
const $q = useQuasar();
const router = useRouter();

/**
 * บันทึกข้อมูล อุทธรณ์/ร้องทุกข์
 * @param data ชุดข้อมูล
 */
async function onSubmit(data: any) {
  const formData = new FormData();
  formData.append("profileId", data.profileId);
  formData.append("citizenId", data.citizenId);
  formData.append("fullname", data.fullname);
  formData.append("title", data.title);
  formData.append("description", data.description);
  formData.append("type", data.type);
  formData.append("year", data.year);
  formData.append("caseType", data.caseType);
  formData.append("oc", data.oc);
  formData.append("profileType", data.profileType);
  formData.append("caseNumber", data.caseNumber);
  formData.append("position", data.position);
  formData.append("file", data.file);
  showLoader();
  http
    .post(config.API.appealAdd() + "/admin", formData)
    .then((res) => {
      success($q, "บันทึกข้อมูลสำเร็จ");
      //เปลี่ยนหน้าไปหน้ารายละเอียด
      router.push(`/discipline-appealcomplain/${res.data.result}`);
    })
    .catch((e) => {
      messageError($q, e);
    })
    .finally(async () => {
      hideLoader();
    });
}
</script>

<template>
  <div class="col-xs-12 col-sm-12 col-md-11">
    <div class="toptitle text-dark col-12 row items-center">
      <q-btn
        for="#backMaininvestigate"
        icon="mdi-arrow-left"
        unelevated
        round
        dense
        flat
        color="primary"
        class="q-mr-sm"
        @click="router.go(-1)"
      />
      เพิ่มการอุทธรณ์/ร้องทุกข์
    </div>

    <Form :on-submit="onSubmit" />
  </div>
</template>
