<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useQuasar } from "quasar";
import { useRouter, useRoute } from "vue-router";
import { useCounterMixin } from "@/stores/mixin";
import http from "@/plugins/http";
import config from "@/app.config";
import Form from '@/modules/11_discipline/components/6_BasicInformation/Channel/Form.vue'

/**
 * รวมตัวแปร
 */
const $q = useQuasar()
const mixin = useCounterMixin();
const {
  messageError,
  showLoader,
  dialogConfirm,
  success,
} = mixin;

const router = useRouter();
const route = useRoute();
const edit = ref<boolean>(false);

/* บันทึกข้อมูล**/
function onSubmit(channelReturn:string){
  dialogConfirm($q,()=>saveData(channelReturn))
}

function saveData(channelReturn:string){
  showLoader()
  http
  .post(config.API.complaintChannel(),{
    name:channelReturn
  })
  .then((res) => {
      success($q, "บันทึกข้อมูลสำเร็จ");
    })
    .catch((e) => {
      messageError($q, e);
    })
    .finally(async () => {
      router.push(`/discipline/channel`);
    });
};

/**
 * ย้อนกลับหน้ารายการ
 */
function clickBack(){
  router.push(`/discipline/channel`);
};
</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="clickBack"
      />
      {{ edit ? "ช่องทางการร้องเรียน" : "เพิ่มช่องทางการร้องเรียน" }}
    </div>
    <Form :on-submit="onSubmit" />
  </div>
</template>
