<script setup lang="ts">
import { useRoute, useRouter } from "vue-router";
import Form from "@/modules/11_discipline/components/1_Complaint/Form.vue";
import { useCounterMixin } from "@/stores/mixin";
import { useQuasar } from "quasar";

import config from "@/app.config";
import http from "@/plugins/http";

const $q = useQuasar();
const mixin = useCounterMixin();

const { showLoader, hideLoader, success, messageError } = mixin;
const router = useRouter();

/** บันทึกข้อมูล */
async function onSubmit(data: any) {
  /** post */
  showLoader();
  http
    .post(config.API.complaintAdd(), {
      id: data.id,
      respondentType: data.respondentType,
      organizationId: data.organizationId,
      consideredAgency: data.consideredAgency,
      title: data.title,
      description: data.description,
      dateReceived: data.dateReceived,
      dateConsideration: data.dateConsideration,
      offenseDetails: data.offenseDetails,
      levelConsideration: data.levelConsideration,
      dateNotification: data.dateNotification,
      complaintFrom: data.complaintFrom,
      appellant: data.appellant,
      documentFile: data.documentFile,
      status: data.status,
      result: data.result,
      persons: data.persons.map((item: any) => ({
        personId: item.personId,
        idcard: item.idcard,
        prefix: item.prefix,
        firstName: item.firstName,
        lastName: item.lastName,
        name: item.name,
        posNo: item.posNo === "-" ? null : item.posNo,
        position: item.position,
        positionLevel: item.positionLevel,
        salary: item.salary === "-" ? null : item.salary,
        organization: item.organization,
        phone: item.phone === "-" ? null : item.phone,
        email: item.email,
      })),
      disciplineComplaintDocs: data.disciplineComplaintDocs.map(
        (file: any) => ({
          id: file.id,
          fileName: file.fileName,
          pathName: file.pathName,
        })
      ),
    })
    .then((res) => {
      success($q, "บันทึกข้อมูลสำเร็จ");
      router.push(`/discipline/complaints/${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>

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