<script setup lang="ts">
import { useRouter } from "vue-router";
import { useCounterMixin } from "@/stores/mixin";
import http from "@/plugins/http";
import config from "@/app.config";
import { useQuasar } from "quasar";

import type { DataForm } from "@/modules/12_evaluatePersonal/interface/index/director";

import Form from "@/modules/12_evaluatePersonal/components/Director/Form.vue";

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

/**
 * บันทึกข้อมูลที่เเก้ไข
 * @param id ระบุ บุคคล
 */
function onSubmit(formData: DataForm) {
  dialogConfirm($q, () => {
    showLoader();
    http
      .post(config.API.evaluateDirectorMain(), {
        prefix: formData.prefix,
        firstName: formData.firstname,
        lastName: formData.lastname,
        position: formData.position,
        email: formData.email,
        phone: formData.phone,
      })
      .then(() => {
        router.push(`/evaluate/director`);
        success($q, "บันทึกข้อมูลสำเร็จ");
      })
      .catch((e) => {
        messageError($q, e);
      })
      .finally(() => {
        hideLoader();
      });
  });
}
</script>
<template>
  <div class="col-xs-12 col-sm-12 col-md-11">
    <div class="toptitle col-12 row items-center">
      <q-btn
        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>
