<script setup lang="ts">
import { useRouter } from "vue-router";
import { useQuasar } from "quasar";

import config from "@/app.config";
import http from "@/plugins/http";
import { useCounterMixin } from "@/stores/mixin";

import Form from "@/modules/11_discipline/components/1_Complaint/Form.vue";

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

/** บันทึกข้อมูล */
async function onSubmit(data: any) {
  /** post */
  showLoader();
  http
    .post(config.API.complaintAdd(), data)
    .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>
