<script setup lang="ts">
import { useQuasar } from "quasar";
import { ref, defineAsyncComponent, onMounted } from "vue";

import { useProbationStore } from "@/modules/11_probation/store/probation";
import { useRoute, useRouter } from "vue-router";
import { useCounterMixin } from "@/stores/mixin";

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

const mixin = useCounterMixin();
const { showLoader, hideLoader, messageError, dialogConfirm, success } = mixin;
const Evaluacommander = defineAsyncComponent(
  () =>
    import("@/modules/11_probation/component/editPage/02_evaluacommander.vue")
);
const router = useRouter();
const route = useRoute();

const dataArrayNumber = ref<number>();
const fullname = ref<string>("");
const id = ref<string>(route.params.id as string);
const profileId = ref<string>(route.params.profileId as string);

const store = useProbationStore();
const $q = useQuasar();
const mode = ref<any>($q.screen.gt.xs);

function addData() {
  router.push(
    `/probation/detail/addevaluacommander/${profileId.value}/${id.value}`
  );
}

/**
 * get data
 * @param id person id
 */
async function fecthAssign(id: string) {
  showLoader();
  await http
    .get(config.API.formevaluateCommander(id))
    .then(async (res: any) => {
      await store.fecthdataAssign(res.data.data);
      fullname.value = res.data.data.experimentee
        ? res.data.data.experimentee.name
        : "";
      dataArrayNumber.value = 1;
    })
    .catch((e: any) => {
      messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
}

onMounted(async () => {
  await fecthAssign(id.value);
});
</script>
<template>
  <div v-if="!$q.screen.gt.xs" class="col-xs-12 col-sm-12 col-md-11">
    <div class="toptitle text-white 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>แบบบันทึกผล (ผู้บังคับบัญชา)</div>
    </div>
  </div>
  <div :class="`row q-my-sm ${$q.screen.gt.xs ? '' : 'mobileClass'}`">
    <div class="col-12 q-gutter-md" v-if="store.tabs.length == 0">
      <div class="flex justify-center items-center q-my-md q-gutter-md">
        <q-btn
          outline
          color="primary"
          label="สร้างแบบบันทึกผล"
          @click="addData"
        />
      </div>
    </div>
    <div v-else>
      <Evaluacommander />
    </div>
  </div>
</template>
<style scoped>
.mobileClass {
  background-color: #fff;
  border-radius: 10px;
}
</style>
