<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { useQuasar } from "quasar";
import { useRouter, useRoute } from "vue-router";
import http from "@/plugins/http";
import config from "@/app.config";

import { useCounterMixin } from "@/stores/mixin";

import type { FormDataRole } from "@/modules/01_metadataNew/interface/request/Main";
import type { DataOption } from "@/modules/01_metadataNew/interface/index/Main";

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

const heightSize = ref<string>("224");
const id = ref<string>(route.params.id ? route.params.id.toLocaleString() : "");

const form = reactive<FormDataRole>({
  position: "", //ตำแหน่งในสายงาน
  year: null, //ปีงบประมาณ
  round: "", //รอบการประเมิน
  org: "", //หน่วยงาน/ส่วนราชการ
  including: "", //รหัสตัวชี้วัด
  includingName: "", //ชื่อตัวชี้วัด
  target: "", //ค่าเป้าหมาย
  unit: "", //หน่วยนับ
  weight: "", //น้ำหนัก
  meaning: "", //นิยามหรือความหมาย
  formula: "", //สูตรคำนวณ
  documentInfoEvidence: "", //ข้อมูลเอกสารหลักฐาน

  node: null,
  nodeId: null,
  orgRevisionId: null,
  date: [null, null],
});

const fieldLabels = {
  score5: "5",
  score4: "4",
  score3: "3",
  score2: "2",
  score1: "1",
};

const formScore = reactive<any>({
  score5: "",
  score4: "",
  score3: "",
  score2: "",
  score1: "",
});

/** Option  รอบการประเมิน*/
const roundOp = ref<DataOption[]>([
  { id: "APR", name: "รอบเมษายน" },
  { id: "OCT", name: "รอบตุลาคม" },
]);

function fetchspecialByid(id: string) {
  showLoader();
  http
    .get(config.API.kpiSpecial + `/${id}`)
    .then((res) => {
      const data = res.data.result;
      form.year = data.year;
      form.round = data.period;
      form.including = data.including;
      form.includingName = data.includingName;
      form.target = data.target;
      form.unit = data.unit;
      form.weight = data.weight;
      formScore.score1 = data.achievement1;
      formScore.score2 = data.achievement2;
      formScore.score3 = data.achievement3;
      formScore.score4 = data.achievement4;
      formScore.score5 = data.achievement5;
      form.formula = data.formula;
      form.meaning = data.meaning;
      form.documentInfoEvidence = data.documentInfoEvidence;
      if (data.startDate && data.endDate) {
        form.date = [data.startDate, data.endDate];
      }
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      hideLoader();
    });
}

function onResize(size: any) {
  heightSize.value = `${size.height - 99}`;
}

onMounted(() => {
  fetchspecialByid(id.value);
});
</script>
<template>
  <div class="toptitle text-dark col-12 row items-center">
    <q-btn
      flat
      round
      dense
      class="q-mr-sm"
      icon="mdi-arrow-left"
      color="primary"
      @click="router.go(-1)"
    />
    รายละเอียดงานที่ได้รับมอบหมาย
  </div>
  <q-card flat>
    <q-card-section>
      <div class="row q-col-gutter-sm">
        <div class="col-2">
          <datepicker
            menu-class-name="modalfix"
            v-model="form.year"
            :locale="'th'"
            autoApply
            year-picker
            :enableTimePicker="false"
            @update:model-value="form.round = ''"
            readonly
          >
            <template #year="{ year }">{{ year + 543 }}</template>
            <template #year-overlay-value="{ value }">{{
              parseInt(value + 543)
            }}</template>
            <template #trigger>
              <q-input
                dense
                class="inputgreen"
                outlined
                :model-value="form.year === 0 ? null : Number(form.year) + 543"
                :label="`${'ปีงบประมาณ'}`"
                readonly
              >
                <template v-slot:prepend>
                  <q-icon
                    name="event"
                    class="cursor-pointer"
                    style="color: var(--q-primary)"
                  >
                  </q-icon>
                </template>
              </q-input>
            </template>
          </datepicker>
        </div>
        <div class="col-2">
          <q-select
            readonly
            dense
            outlined
            v-model="form.round"
            :options="roundOp"
            option-label="name"
            option-value="id"
            emit-value
            map-options
            input-class="text-red"
            label="รอบการประเมิน"
            class="inputgreen"
          />
        </div>
        <div class="col-2" v-if="id">
          <q-input
            label="ลำดับ/รหัสตัวชี้วัด"
            v-model="form.including"
            outlined
            readonly
            dense
            class="inputgreen"
          />
        </div>
        <div class="col-6">
          <q-input
            readonly
            label="ชื่อตัวชี้วัด"
            v-model="form.includingName"
            outlined
            dense
            class="inputgreen"
            lazy-rules
            :rules="[(val:string) => !!val || `${'กรุณากรอกชื่อตัวชี้วัด'}`,]"
            hide-bottom-space
          />
        </div>
        <div class="col-4">
          <q-input
            readonly
            label="ค่าเป้าหมาย"
            v-model="form.target"
            outlined
            dense
            class="inputgreen"
            lazy-rules
            :rules="[(val:string) => !!val || `${'กรุณากรอกค่าเป้าหมาย'}`,]"
            hide-bottom-space
          />
        </div>
        <div class="col-4">
          <q-input
            readonly
            label="หน่วยนับ"
            v-model="form.unit"
            outlined
            dense
            class="inputgreen"
            lazy-rules
            :rules="[(val:string) => !!val || `${'กรุณากรอกหน่วยนับ'}`,]"
            hide-bottom-space
          />
        </div>
        <div class="col-4">
          <q-input
            readonly
            label="น้ำหนัก"
            v-model="form.weight"
            outlined
            dense
            class="inputgreen"
            lazy-rules
            :rules="[(val:string) => !!val || `${'กรุณากรอกน้ำหนัก'}`,]"
            hide-bottom-space
          />
        </div>

        <div class="col-12">
          <q-card flat bordered>
            <q-resize-observer @resize="onResize" />
            <q-card-section class="bg-grey-3 q-pa-sm">
              <div class="row text-dark text-body2 text-weight-medium">
                <div class="text-center col-4">ระดับคะแนน</div>
                <div class="col-8">ผลสำเร็จของงาน</div>
              </div>
            </q-card-section>
            <q-card-section class="q-pa-none">
              <div
                v-for="(field, index) in Object.keys(fieldLabels)"
                :key="index + 1"
              >
                <div class="row q-pa-sm">
                  <div
                    class="col-4 text-center text-body1 text-weight-bold self-center"
                  >
                    {{ fieldLabels[field as keyof typeof fieldLabels] }}
                  </div>
                  <div class="col-8 text-left">
                    <q-input
                      readonly
                      v-model="formScore[field]"
                      dense
                      outlined
                      class="inputgreen"
                      label="กรอกผลสำเร็จของงาน"
                      :rules="[(val:string) => !!val || `${'กรุณากรอกผลสำเร็จของงาน'}`,]"
                      hide-bottom-space
                    />
                  </div>
                </div>
                <div
                  class="col-12"
                  v-if="index !== Object.keys(fieldLabels).length - 1"
                >
                  <q-separator />
                </div>
              </div>
            </q-card-section>
          </q-card>
        </div>
        <div class="col-12">
          <q-input
            readonly
            v-model="form.meaning"
            label="นิยามหรือความหมาย"
            dense
            outlined
            lazy-rules
            class="inputgreen"
            :rules="[(val:string) => !!val || `${'กรุณากรอกนิยามหรือความหมาย'}`,]"
            hide-bottom-space
            type="textarea"
          />
        </div>
        <div class="col-12">
          <q-input
            readonly
            v-model="form.formula"
            label="สูตรคำนวณ"
            dense
            outlined
            lazy-rules
            class="inputgreen"
            :rules="[(val:string) => !!val || `${'กรุณากรอกสูตรคำนวณ'}`,]"
            hide-bottom-space
            type="textarea"
          />
        </div>
        <div class="col-12">
          <q-input
            readonly
            class="inputgreen"
            v-model="form.documentInfoEvidence"
            label="ข้อมูลเอกสารหลักฐาน"
            outlined
            dense
            type="textarea"
          ></q-input>
        </div>
        <div class="col-3">
          <datepicker
            v-model="form.date"
            :locale="'th'"
            autoApply
            :enableTimePicker="false"
            week-start="0"
            range
            readonly
          >
            <template #year="{ year }">{{ year + 543 }}</template>
            <template #year-overlay-value="{ value }">{{
              parseInt(value + 543)
            }}</template>
            <template #trigger>
              <q-input
                readonly
                dense
                outlined
                class="inputgreen"
                :model-value="
                  form.date
                    ? `${date2Thai(form.date[0])} - ${date2Thai(form.date[1])}`
                    : null
                "
                :label="`${'ช่วงเวลาเริ่มต้น-สิ้นสุด'}`"
                hide-bottom-space
              >
                <template v-slot:prepend>
                  <q-icon
                    name="event"
                    class="cursor-pointer"
                    style="color: var(--q-primary)"
                  >
                  </q-icon>
                </template>
              </q-input>
            </template>
          </datepicker>
        </div>
      </div>
    </q-card-section>
  </q-card>
</template>

<style scoped></style>
