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

import { useRoute, useRouter } from "vue-router";
import { useCounterMixin } from "@/stores/mixin";
import http from "@/plugins/http";
import config from "@/app.config";

const $q = useQuasar();
const route = useRoute();
const { messageError, showLoader, hideLoader } = useCounterMixin();

const Header = defineAsyncComponent(
  () =>
    import(
      "@/modules/05_placement/components/probation/FormEvaluation/Header.vue"
    )
);
const SummaryScore = defineAsyncComponent(
  () => import("@/modules/05_placement/components/probation/SummaryScore.vue")
);

const assignId = ref<string>(route.params.form.toString());
const tab = ref<string>("save1");
const tabs = ref<any[]>([]);
const dataArrayNumber = ref<number>(1);

const changeTab = (tabVal: string) => {
  tab.value = tabVal;
  dataArrayNumber.value = Number(tabVal.charAt(4));
};

/**
 * @param id assignId
 */
async function fecthAssign(id: string) {
  showLoader();
  await http
    .get(config.API.evaluateReportcreate(id))
    .then((res) => {
      tabs.value = res.data.result.result.map((e: any, index: number) => {
        return { no: index + 1, ...e };
      });
    })
    .catch((e) => {
      messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
}

onMounted(() => {
  fecthAssign(assignId.value);
});
</script>

<template>
  <Header
    v-if="tabs.length > 0"
    :change-tab="changeTab"
    :add-data="() => {}"
    :loop="tabs.length"
    :FileDownload="() => {}"
    :checkPermission="false"
  />

  <q-tab-panels
    v-model="tab"
    animated
    swipeable
    vertical
    transition-prev="jump-up"
    transition-next="jump-up"
    v-if="tabs.length > 0"
    class="mt"
  >
    <q-tab-panel v-for="item in tabs" :name="`save${item.no}`" :key="item.no">
      <SummaryScore :tab="tab" />
    </q-tab-panel>
  </q-tab-panels>

  <div class="q-gutter-md" v-else>
    <div class="flex justify-center items-center q-my-md q-gutter-md">
      ยังไม่ได้รายงานผลการประเมินฯ
    </div>
  </div>
</template>

<style lang="scss" scoped>
.mt {
  margin-top: 25px;
}
</style>
