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

import { useRoute } from "vue-router";
import { checkPermission } from "@/utils/permissions";
import { useCounterMixin } from "@/stores/mixin";
import { useResultsPerformDataStore } from "@/modules/04_registryPerson/stores/ResultsPerformance";
import http from "@/plugins/http";
import config from "@/app.config";

import type { QTableProps } from "quasar";
import type { RequestItemsObject } from "@/modules/04_registryPerson/interface/request/ResultsPerformance";
import type { ResponseObject } from "@/modules/04_registryPerson/interface/response/ResultsPerformance";

import DialogHeader from "@/components/DialogHeader.vue";
import DialogDevelop from "@/modules/04_registryPerson/components/detail/Achievement/DialogDevelopmance.vue";

const $q = useQuasar();
const route = useRoute();
const store = useResultsPerformDataStore();
const { textRangePoint, textPoint } = store;
const mixin = useCounterMixin();
const {
  date2Thai,
  success,
  messageError,
  showLoader,
  hideLoader,
  dialogConfirm,
  pathRegistryEmp,
} = mixin;

const profileId = ref<string>(
  route.params.id ? route.params.id.toString() : ""
);
const empType = ref<string>(pathRegistryEmp(route.name?.toString() ?? ""));

const id = ref<string>(""); //id ที่ต้องการแก้ไข

const isEdit = ref<boolean>(false); //สถานะการแก้ไขข้อมูล
const modal = ref<boolean>(false); //แสดง popup ผลการประเมินการปฏิบัติราชการ
const modalDevelop = ref<boolean>(false); //แสดง popup การพัฒนารายบุคคล
const modeView = ref<string>("table"); //การแสดงผล Table,Card ผลการประเมินการปฏิบัติราชการ
const modeViewPlan = ref<string>("table"); //การแสดงผล Table,Card การพัฒนารายบุคคล
const modalHistory = ref<boolean>(false); //แสดง popup ประวัติแก้ไข
const kpiDevelopmentId = ref<string>(""); // id การพัฒนารายบุคคล

/**
 * props
 */
const isLeave = defineModel<boolean>("isLeave", {
  required: true,
});

const totalIdp = ref<number>(0);
const totalListIdp = ref<number>(1);
const paginationIdp = ref({
  sortBy: "createdAt",
  descending: true,
  page: 1,
  rowsPerPage: 10,
});

const resPerformForm = reactive<RequestItemsObject>({
  name: "",
  point1Total: 0, //ส่วนที่1 (น้ำหนัก)
  point1: 0, //ผลประเมินส่วนที่1 (คะแนน)
  point2Total: 0, // ส่วนที่2 (น้ำหนัก)
  point2: 0, //ส่วนที่2 (น้ำหนัก)
  pointSumTotal: 0, //ผลรวม (น้ำหนัก)
  pointSum: 0, //ผลประเมินรวม (คะแนน)
  date: null, //วันที่ได้รับ
});

//Table ผลการประเมินการปฏิบัติราชการ
const rows = ref<ResponseObject[]>([]);
const filterSearch = ref<string>("");
const columns = ref<QTableProps["columns"]>([
  {
    name: "date",
    align: "left",
    label: "วันที่ได้รับ",
    sortable: true,
    field: "date",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => date2Thai(v),
  },
  {
    name: "point1Total",
    align: "left",
    label: "ส่วนที่1 (น้ำหนัก)",
    sortable: true,
    field: "point1Total",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "point1",
    align: "left",
    label: "ผลประเมินส่วนที่1 (คะแนน)",
    sortable: true,
    field: "point1",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "point2Total",
    align: "left",
    label: "ส่วนที่2 (น้ำหนัก)",
    sortable: true,
    field: "point2Total",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "point2",
    align: "left",
    label: "ผลประเมินส่วนที่2 (คะแนน)",
    sortable: true,
    field: "point2",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "pointSumTotal",
    align: "left",
    label: "ผลรวม (น้ำหนัก)",
    sortable: true,
    field: "pointSumTotal",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "pointSum",
    align: "left",
    label: "ผลประเมินรวม (คะแนน)",
    sortable: true,
    field: "pointSum",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "name",
    align: "left",
    label: "ผลประเมิน",
    sortable: true,
    field: (v) => `${textPoint(v.pointSum)} ${textRangePoint(v.pointSum)}`,
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
]);
const visibleColumns = ref<String[]>([
  "point1Total",
  "point1",
  "point2Total",
  "point2",
  "pointSumTotal",
  "pointSum",
  "name",
  "date",
]);
const pagination = ref({
  page: 1,
  rowsPerPage: 10,
});

//Table การพัฒนารายบุคคล (Individual Development Plan)
const rowsPlan = ref<any[]>([]);
const filterSearchPlan = ref<string>("");
const columnsPlan = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: true,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => date2Thai(v),
  },
  {
    name: "name",
    align: "left",
    label: "ความรู้/ทักษะ/สมรรถนะที่ต้องได้รับการพัฒนา",
    sortable: true,
    field: "name",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "developmentProjects",
    align: "left",
    label: "วิธีการพัฒนา",
    sortable: true,
    field: "developmentProjects",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "developmentTarget",
    align: "left",
    label: "เป้าหมายการพัฒนา",
    sortable: true,
    field: "developmentTarget",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "developmentResults",
    align: "left",
    label: "วิธีการวัดผลการพัฒนา",
    sortable: true,
    field: "developmentResults",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "developmentReport",
    align: "left",
    label: "รายงานผลการพัฒนา",
    sortable: true,
    field: "developmentReport",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
]);
const visibleColumnsPlan = ref<String[]>([
  "no",
  "name",
  "developmentProjects",
  "developmentTarget",
  "developmentResults",
  "developmentReport",
]);

//Table ประวัติแก้ไข
const rowsHistory = ref<ResponseObject[]>([]);
const filterHistory = ref<string>("");
const columnsHistory = ref<QTableProps["columns"]>([
  {
    name: "date",
    align: "left",
    label: "วันที่ได้รับ",
    sortable: true,
    field: "date",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => date2Thai(v),
  },
  {
    name: "point1Total",
    align: "left",
    label: "ส่วนที่1 (น้ำหนัก)",
    sortable: true,
    field: "point1Total",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "point1",
    align: "left",
    label: "ผลประเมินส่วนที่1 (คะแนน)",
    sortable: true,
    field: "point1",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "point2Total",
    align: "left",
    label: "ส่วนที่2 (น้ำหนัก)",
    sortable: true,
    field: "point2Total",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "point2",
    align: "left",
    label: "ผลประเมินส่วนที่2 (คะแนน)",
    sortable: true,
    field: "point2",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "pointSumTotal",
    align: "left",
    label: "ผลรวม (น้ำหนัก)",
    sortable: true,
    field: "pointSumTotal",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "pointSum",
    align: "left",
    label: "ผลประเมินรวม (คะแนน)",
    sortable: true,
    field: "pointSum",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "name",
    align: "left",
    label: "ผลประเมิน",
    sortable: true,
    field: (v) => `${textPoint(v.pointSum)} ${textRangePoint(v.pointSum)}`,
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "lastUpdateFullName",
    align: "left",
    label: "ผู้ดำเนินการ",
    sortable: true,
    field: "lastUpdateFullName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "lastUpdatedAt",
    align: "left",
    label: "วันที่แก้ไข",
    sortable: true,
    field: "lastUpdatedAt",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => date2Thai(v, false, true),
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
]);
const visibleColumnsHistory = ref<String[]>([
  "point1Total",
  "point1",
  "point2Total",
  "point2",
  "pointSumTotal",
  "pointSum",
  "name",
  "date",
  "lastUpdateFullName",
  "lastUpdatedAt",
]);
const historyPagination = ref({
  page: 1,
  rowsPerPage: 10,
});

/**
 * fetch รายการข้อมูลผลการประเมินการปฏิบัติราชการ
 */
async function fetchData() {
  if (!profileId.value) return;
  showLoader();
  try {
    const res = await http.get(
      config.API.profileNewAssessmentsByProfileId(
        profileId.value,
        empType.value
      )
    );
    rows.value = res.data.result;
  } catch (error) {
    messageError($q, error);
    hideLoader();
  } finally {
  }
}

/**
 * fetch รายการการพัฒนารายบุคคล (Individual Development Plan)
 */
async function getDevelop(isLoad?: boolean) {
  if (!profileId.value) return;
  isLoad && showLoader();
  await http
    .get(
      config.API.developMentPlan +
        `/${profileId.value}?page=${paginationIdp.value.page}&pageSize=${
          paginationIdp.value.rowsPerPage
        }&searchKeyword=${filterSearchPlan.value.trim()}
`
    )
    .then((res) => {
      const data = res.data.result.data;
      totalListIdp.value = Math.ceil(
        res.data.result.total / paginationIdp.value.rowsPerPage
      );
      totalIdp.value = res.data.result.total;
      rowsPlan.value = data;

      isLoad && hideLoader();
    })
    .catch((e) => {
      messageError($q, e);
      hideLoader();
    })
    .finally(() => {});
}

/**
 * บันทึกข้อมูลผลการประเมินการปฏิบัติราชการ
 * @param editStatus แก้ไข,เพิ่ม
 */
async function addEditData(editStatus: boolean = false) {
  if (!profileId.value) return;

  showLoader();
  const url = editStatus
    ? config.API.profileNewAssessmentsById(id.value, empType.value)
    : config.API.profileNewAssessments(empType.value);
  const method = editStatus ? "patch" : "post";
  const reqBody: RequestItemsObject = {
    ...resPerformForm,
    profileEmployeeId:
      !editStatus && empType.value !== "" ? profileId.value : undefined,
    profileId:
      !editStatus && empType.value === "" ? profileId.value : undefined,
  };

  try {
    await http[method](url, reqBody);
    await fetchData();
    await success($q, "บันทึกข้อมูลสำเร็จ");
    modal.value = false;
  } catch (e) {
    messageError($q, e);
  } finally {
    hideLoader();
  }
}

/**
 * เปิด form ข้อมูลผลการประเมินการปฏิบัติราชการ
 */
function onClickOpenDialog(editStatus: boolean = false, row?: ResponseObject) {
  modal.value = true;
  isEdit.value = editStatus;
  if (editStatus && row) {
    id.value = row.id;
    resPerformForm.name = row.name;
    resPerformForm.point1Total = row.point1Total;
    resPerformForm.point1 = row.point1;
    resPerformForm.point2Total = row.point2Total;
    resPerformForm.point2 = row.point2;
    resPerformForm.pointSumTotal = row.pointSumTotal;
    resPerformForm.pointSum = row.pointSum;
    resPerformForm.date = row.date;
  } else {
    clearData();
  }
}

/**
 * ปิด form ข้อมูลผลการประเมินการปฏิบัติราชการ
 */
async function clickClose() {
  clearData();
  modal.value = false;
}

/**
 * fetch ช้อมูลประวัติการแก้ไขรายการข้อมูลผลการประเมินการปฏิบัติราชการ
 */
async function clickHistory(row: ResponseObject) {
  modalHistory.value = true;
  filterSearch.value = "";
  showLoader();
  try {
    const res = await http.get(
      config.API.profileNewAssessmentsHisById(row.id, empType.value)
    );

    rowsHistory.value = res.data.result;
  } catch (e) {
    messageError($q, e);
  } finally {
    hideLoader();
  }
}

/**
 * ยืนยีนการบันทึกข้อมูล
 */
function onSubmit() {
  dialogConfirm(
    $q,
    async () => {
      addEditData(isEdit.value);
    },
    "ยืนยันการบันทึกข้อมูล",
    "ต้องการยืนยันการบันทึกข้อมูลนี้หรือไม่ ?"
  );
}

/**
 * เคลียร์ formDฟta
 */
function clearData() {
  (id.value = ""),
    (resPerformForm.name = ""),
    (resPerformForm.point1Total = 0),
    (resPerformForm.point1 = 0),
    (resPerformForm.point2Total = 0),
    (resPerformForm.point2 = 0),
    (resPerformForm.pointSumTotal = 0),
    (resPerformForm.pointSum = 0),
    (resPerformForm.date = null);
}

/**
 * เปิด Dialog การพัฒนารายบุคคล
 * @param data ข้อมูลตาม row
 */
function openDialogDevelop(data: any) {
  modalDevelop.value = true;
  kpiDevelopmentId.value = data.kpiDevelopmentId;
}

function updatePaginationIdp(newPagination: any) {
  paginationIdp.value.page = 1;
  paginationIdp.value.rowsPerPage = newPagination.rowsPerPage;
}

watch(
  () => paginationIdp.value.rowsPerPage,
  async () => {
    await getDevelop(true);
  }
);

onMounted(async () => {
  await fetchData();
  await getDevelop();
  hideLoader();
});
</script>
<template>
  <div class="row q-col-gutter-sm">
    <div class="col-12">
      <span class="text-weight-bold text-body1"
        >ผลการประเมินการปฏิบัติราชการ</span
      >
    </div>
    <div class="col-12">
      <q-card bordered class="q-pa-sm">
        <div class="col-12">
          <div class="row items-center q-gutter-x-sm q-pb-sm">
            <q-btn
              v-if="isLeave == false && checkPermission($route)?.attrIsUpdate"
              dense
              color="primary"
              icon="add"
              flat
              round
              @click="onClickOpenDialog()"
              ><q-tooltip>เพิ่มข้อมูล</q-tooltip></q-btn
            >

            <q-space />
            <q-input
              standout
              dense
              v-model="filterSearch"
              ref="filterRef"
              outlined
              placeholder="ค้นหา"
              debounce="300"
            >
              <template v-slot:append>
                <q-icon name="search" />
              </template>
            </q-input>
            <q-select
              v-if="modeView == 'table'"
              v-model="visibleColumns"
              multiple
              outlined
              dense
              options-dense
              :display-value="$q.lang.table.columns"
              emit-value
              map-options
              :options="columns"
              option-value="name"
              style="min-width: 140px"
            />
            <q-btn-toggle
              v-model="modeView"
              dense
              class="no-shadow toggle-borderd"
              toggle-color="grey-4"
              :options="[
                { value: 'table', slot: 'table' },
                { value: 'card', slot: 'card' },
              ]"
            >
              <template v-slot:table>
                <q-icon
                  name="format_list_bulleted"
                  size="24px"
                  :style="{
                    color: modeView === 'table' ? '#787B7C' : '#C9D3DB',
                  }"
                />
              </template>

              <template v-slot:card>
                <q-icon
                  name="mdi-view-grid-outline"
                  size="24px"
                  :style="{
                    color: modeView === 'card' ? '#787B7C' : '#C9D3DB',
                  }"
                />
              </template>
            </q-btn-toggle>
          </div>
        </div>

        <div class="col-12">
          <d-table
            flat
            dense
            bordered
            :rows="rows"
            :paging="true"
            :columns="columns"
            :filter="filterSearch.trim()"
            v-model:pagination="pagination"
            :grid="modeView === 'card'"
            :visible-columns="visibleColumns"
            :rows-per-page-options="[20, 50, 100]"
            :card-container-class="modeView === 'card' ? 'q-col-gutter-md' : ''"
          >
            <template v-slot:header="props">
              <q-tr :props="props">
                <q-th auto-width />
                <q-th v-for="col in props.cols" :key="col.name" :props="props">
                  <span class="text-weight-medium">{{ col.label }}</span>
                </q-th>
              </q-tr>
            </template>

            <template v-slot:body="props" v-if="modeView === 'table'">
              <q-tr :props="props">
                <q-td auto-width>
                  <q-btn
                    flat
                    dense
                    round
                    color="deep-purple"
                    icon="mdi-history"
                    @click="clickHistory(props.row)"
                  >
                    <q-tooltip
                      >ประวัติแก้ไขผลการประเมินการปฏิบัติราชการ</q-tooltip
                    >
                  </q-btn>
                  <q-btn
                    v-if="
                      isLeave == false && checkPermission($route)?.attrIsUpdate
                    "
                    flat
                    dense
                    round
                    color="edit"
                    icon="edit"
                    @click="onClickOpenDialog(true, props.row)"
                  >
                    <q-tooltip>แก้ไขข้อมูล</q-tooltip>
                  </q-btn>
                </q-td>
                <q-td v-for="col in props.cols" :key="col.name" :props="props">
                  <div class="table_ellipsis">
                    {{ col.value ? col.value : "-" }}
                  </div>
                </q-td>
              </q-tr>
            </template>

            <template v-slot:item="props" v-else>
              <div class="col-xs-12 col-sm-6 col-md-6">
                <q-card bordered>
                  <q-card-actions class="bg-grey-3" align="right">
                    <q-btn
                      flat
                      round
                      color="deep-purple"
                      icon="mdi-history"
                      @click="clickHistory(props.row)"
                    >
                      <q-tooltip
                        >ประวัติแก้ไขผลการประเมินการปฏิบัติราชการ</q-tooltip
                      >
                    </q-btn>
                    <q-btn
                      v-if="
                        isLeave == false &&
                        checkPermission($route)?.attrIsUpdate
                      "
                      flat
                      round
                      color="edit"
                      icon="edit"
                      @click.stop.prevent="onClickOpenDialog(true, props.row)"
                    >
                      <q-tooltip>แก้ไขข้อมุล</q-tooltip>
                    </q-btn>
                  </q-card-actions>
                  <q-separator />
                  <div>
                    <q-item
                      v-for="(col, index) in props.cols.filter(
                      (col:any) => col.name !== 'desc'
                    )"
                      :key="col.name"
                      :class="index % 2 !== 0 ? 'bg-grey-1' : ''"
                    >
                      <q-item-section class="text-grey-6">
                        <q-item-label>{{ col.label }}</q-item-label>
                      </q-item-section>

                      <q-item-section class="text-dark">
                        <q-item-label>
                          {{ col.value ? col.value : "-" }}
                        </q-item-label>
                      </q-item-section>
                    </q-item>
                  </div>
                </q-card>
              </div>
            </template>
          </d-table>
        </div>
      </q-card>
    </div>

    <div class="col-12 q-mt-md">
      <span class="text-weight-bold text-body1 q-pb-sm"
        >การพัฒนารายบุคคล (Individual Development Plan)</span
      >
    </div>
    <div class="col-12">
      <q-card bordered class="q-pa-sm">
        <div class="col-12">
          <div class="row items-center q-gutter-x-sm q-pb-sm">
            <q-space />
            <q-input
              standout
              dense
              v-model="filterSearchPlan"
              ref="filterPlanRef"
              outlined
              placeholder="ค้นหา"
              @keydown.enter.prevent="
                (paginationIdp.page = 1), getDevelop(true)
              "
            >
              <template v-slot:append>
                <q-icon name="search" />
              </template>
            </q-input>
            <q-select
              v-if="modeViewPlan == 'table'"
              v-model="visibleColumnsPlan"
              multiple
              outlined
              dense
              options-dense
              :display-value="$q.lang.table.columns"
              emit-value
              map-options
              :options="columnsPlan"
              option-value="name"
              style="min-width: 140px"
            />
            <q-btn-toggle
              v-model="modeViewPlan"
              dense
              class="no-shadow toggle-borderd"
              toggle-color="grey-4"
              :options="[
                { value: 'table', slot: 'table' },
                { value: 'card', slot: 'card' },
              ]"
            >
              <template v-slot:table>
                <q-icon
                  name="format_list_bulleted"
                  size="24px"
                  :style="{
                    color: modeViewPlan === 'table' ? '#787B7C' : '#C9D3DB',
                  }"
                />
              </template>

              <template v-slot:card>
                <q-icon
                  name="mdi-view-grid-outline"
                  size="24px"
                  :style="{
                    color: modeViewPlan === 'card' ? '#787B7C' : '#C9D3DB',
                  }"
                />
              </template>
            </q-btn-toggle>
          </div>
        </div>

        <div class="col-12">
          <d-table
            flat
            dense
            bordered
            :rows="rowsPlan"
            :paging="true"
            :columns="columnsPlan"
            :grid="modeViewPlan === 'card'"
            :visible-columns="visibleColumnsPlan"
            :card-container-class="
              modeViewPlan === 'card' ? 'q-col-gutter-md' : ''
            "
            :rows-per-page-options="[10, 25, 50, 100]"
            @update:pagination="updatePaginationIdp"
          >
            <template v-slot:pagination="scope">
              ทั้งหมด {{ totalIdp }} รายการ
              <q-pagination
                v-model="paginationIdp.page"
                active-color="primary"
                color="dark"
                :max="Number(totalListIdp)"
                size="sm"
                boundary-links
                direction-links
                :max-pages="5"
                @update:model-value="getDevelop"
              ></q-pagination>
            </template>

            <template v-slot:header="props">
              <q-tr :props="props">
                <q-th auto-width />
                <q-th v-for="col in props.cols" :key="col.name" :props="props">
                  <span class="text-weight-medium">{{ col.label }}</span>
                </q-th>
              </q-tr>
            </template>

            <template v-slot:body="props" v-if="modeViewPlan === 'table'">
              <q-tr :props="props" class="vertical-top">
                <q-td auto-width>
                  <q-btn
                    v-if="checkPermission($route)?.attrIsGet"
                    flat
                    round
                    dense
                    color="info"
                    icon="mdi-eye"
                    @click="openDialogDevelop(props.row)"
                  >
                    <q-tooltip>รายละเอียด</q-tooltip>
                  </q-btn></q-td
                >
                <q-td v-for="col in props.cols" :key="col.name" :props="props">
                  <div v-if="col.name == 'no'">
                    {{
                      (paginationIdp.page - 1) * paginationIdp.rowsPerPage +
                      props.rowIndex +
                      1
                    }}
                  </div>
                  <div v-else-if="col.name == 'developmentProjects'">
                    <div class="column">
                      <q-checkbox
                        size="xs"
                        :model-value="props.row.isDevelopment70"
                        label="70 การลงมือปฏิบัติ (โดยผู้บังคับบัญชามอบหมาย)"
                      />
                      <q-checkbox
                        size="xs"
                        :model-value="props.row.isDevelopment20"
                        label="20 การเรียนรู้จากผู้อื่น (Coach/Mentor/Consulting)"
                      />
                      <q-checkbox
                        size="xs"
                        :model-value="props.row.isDevelopment10"
                        label="10 การฝึกอบรมอื่นๆ"
                      />
                    </div>
                  </div>
                  <div v-else class="table_ellipsis">
                    {{ col.value ? col.value : "-" }}
                  </div>
                </q-td>
              </q-tr>
            </template>

            <template v-slot:item="props" v-else>
              <div class="col-xs-12 col-sm-6 col-md-6">
                <q-card bordered>
                  <q-card-actions class="bg-grey-3" align="right">
                    <q-btn
                      v-if="checkPermission($route)?.attrIsGet"
                      flat
                      round
                      dense
                      color="info"
                      icon="mdi-eye"
                      @click="openDialogDevelop(props.row)"
                    >
                      <q-tooltip>รายละเอียด</q-tooltip>
                    </q-btn>
                  </q-card-actions>
                  <q-separator />
                  <div>
                    <q-item
                      v-for="(col, index) in props.cols.filter(
                (col:any) => col.name !== 'desc'
              )"
                      :key="col.name"
                      :class="index % 2 !== 0 ? 'bg-grey-1' : ''"
                    >
                      <q-item-section class="text-grey-6">
                        <q-item-label>{{ col.label }}</q-item-label>
                      </q-item-section>

                      <div v-if="col.name == 'developmentProjects'">
                        <div class="column">
                          <q-checkbox
                            size="xs"
                            :model-value="props.row.isDevelopment70"
                            label="70 การลงมือปฏิบัติ (โดยผู้บังคับบัญชามอบหมาย)"
                            reaonly
                          />
                          <q-checkbox
                            size="xs"
                            :model-value="props.row.isDevelopment20"
                            label="20 การเรียนรู้จากผู้อื่น (Coach/Mentor/Consulting)"
                          />
                          <q-checkbox
                            size="xs"
                            :model-value="props.row.isDevelopment10"
                            label="10 การฝึกอบรมอื่นๆ"
                          />
                        </div>
                      </div>
                      <div v-else-if="col.name == 'developmentResults'">
                        <div class="column">
                          <span>
                            {{
                              props.row.achievement10
                                ? `- ${props.row.achievement10} (10)`
                                : ""
                            }}
                          </span>
                          <span>
                            {{
                              props.row.achievement5
                                ? `- ${props.row.achievement5} (5)`
                                : ""
                            }}
                          </span>
                          <span>
                            {{
                              props.row.achievement0
                                ? `- ${props.row.achievement10} (0)`
                                : ""
                            }}
                          </span>
                        </div>
                      </div>
                      <q-item-section v-else class="text-dark">
                        <q-item-label>
                          {{ col.value ? col.value : "-" }}
                        </q-item-label>
                      </q-item-section>
                    </q-item>
                  </div>
                </q-card>
              </div>
            </template>
          </d-table>
        </div>
      </q-card>
    </div>
  </div>

  <q-dialog v-model="modal" persistent>
    <q-card>
      <q-form greedy @submit.prevent @validation-success="onSubmit">
        <DialogHeader
          :tittle="
            isEdit
              ? 'แก้ไขผลการประเมินการปฏิบัติราชการ'
              : 'เพิ่มผลการประเมินการปฏิบัติราชการ'
          "
          :close="clickClose"
        />
        <q-separator />
        <q-card-section>
          <div class="row col-12 q-col-gutter-sm">
            <div class="col-12">
              <datepicker
                autoApply
                borderless
                week-start="0"
                menu-class-name="modalfix"
                v-model="resPerformForm.date"
                :locale="'th'"
                :enableTimePicker="false"
              >
                <template #year="{ year }">
                  {{ year + 543 }}
                </template>
                <template #year-overlay-value="{ value }">
                  {{ parseInt(value + 543) }}
                </template>
                <template #trigger>
                  <q-input
                    dense
                    outlined
                    for="inputDatereceive"
                    ref="dateReceivedRef"
                    class="inputgreen"
                    hide-bottom-space
                    :model-value="date2Thai(resPerformForm.date as Date)"
                    :label="`${'วันที่ได้รับ'}`"
                    :rules="[(val:string) => !!val || `${'กรุณาเลือกวันที่ได้รับ'}`]"
                  >
                    <template v-slot:prepend>
                      <q-icon
                        name="event"
                        class="cursor-pointer"
                        color="primary"
                      />
                    </template>
                  </q-input>
                </template>
              </datepicker>
            </div>
            <div class="col-xs-6 col-sm-6 col-md-6">
              <q-input
                dense
                outlined
                lazy-rules
                hide-bottom-space
                class="inputgreen"
                v-model="resPerformForm.point1Total"
                input-class="text-right "
                :label="`${'ส่วนที่1 (น้ำหนัก)'}`"
                :rules="[(val:string) => !!val || `${'กรุณากรอกส่วนที่1 (น้ำหนัก)'}`]"
              />
            </div>
            <div class="col-xs-6 col-sm-6 col-md-6">
              <q-input
                dense
                outlined
                lazy-rules
                hide-bottom-space
                class="inputgreen"
                v-model="resPerformForm.point1"
                input-class="text-right"
                :label="`${'ผลประเมินส่วนที่1 (คะแนน)'}`"
                :rules="[
                  (val:string) => !!val || `${'กรุณากรอกผลประเมินส่วนที่1 (คะแนน)'}`,
                ]"
              />
            </div>
            <div class="col-xs-6 col-sm-6 col-md-6">
              <q-input
                dense
                outlined
                lazy-rules
                hide-bottom-space
                class="inputgreen"
                v-model="resPerformForm.point2Total"
                input-class="text-right"
                :label="`${'ส่วนที่2 (น้ำหนัก)'}`"
                :rules="[(val:string) => !!val || `${'กรุณากรอกส่วนที่2 (น้ำหนัก)'}`]"
              />
            </div>
            <div class="col-xs-6 col-sm-6 col-md-6">
              <q-input
                dense
                outlined
                lazy-rules
                hide-bottom-space
                class="inputgreen"
                v-model="resPerformForm.point2"
                input-class="text-right"
                :label="`${'ผลประเมินส่วนที่2 (คะแนน)'}`"
                :rules="[
                  (val:string) => !!val || `${'กรุณากรอกผลประเมินส่วนที่2 (คะแนน)'}`,
                ]"
              />
            </div>
            <div class="col-xs-6 col-sm-6 col-md-6">
              <q-input
                dense
                outlined
                lazy-rules
                hide-bottom-space
                class="inputgreen"
                v-model="resPerformForm.pointSumTotal"
                input-class="text-right"
                :label="`${'ผลรวม (น้ำหนัก)'}`"
                :rules="[(val:string) => !!val || `${'กรุณากรอกผลรวม (น้ำหนัก)'}`]"
              />
            </div>
            <div class="col-xs-6 col-sm-6 col-md-6">
              <q-input
                dense
                outlined
                lazy-rules
                hide-bottom-space
                v-model="resPerformForm.pointSum"
                class="inputgreen"
                input-class="text-right"
                :label="`${'ผลประเมินรวม (คะแนน)'}`"
                :rules="[
                  (val:string) => !!val || `${'กรุณากรอกผลประเมินรวม (คะแนน)'}`,
                ]"
              />
            </div>
            <div class="col-12 row items-center q-pt-md justify-center">
              ผลการประเมิน:
              <div class="text-bold items-center q-px-sm">
                {{ textPoint(resPerformForm.pointSum) }}
              </div>
              {{ textRangePoint(resPerformForm.pointSum) }}
            </div>
          </div>
        </q-card-section>

        <q-separator />
        <q-card-actions align="right">
          <q-btn label="บันทึก" id="onSubmit" type="submit" color="public">
            <q-tooltip>บันทึกข้อมูล</q-tooltip>
          </q-btn>
        </q-card-actions>
      </q-form>
    </q-card>
  </q-dialog>

  <q-dialog v-model="modalHistory" persistent>
    <q-card style="min-width: 80%">
      <DialogHeader
        tittle="ประวัติแก้ไขผลการประเมินการปฏิบัติราชการ"
        :close="
          () => (
            (modalHistory = false), (rowsHistory = []), (filterHistory = '')
          )
        "
      />
      <q-separator />
      <q-card-section style="max-height: 60vh" class="scroll">
        <div class="row q-gutter-sm q-mb-sm">
          <q-space />
          <q-input
            standout
            dense
            v-model="filterHistory"
            ref="filterRef"
            outlined
            placeholder="ค้นหา"
            debounce="300"
          >
            <template v-slot:append>
              <q-icon name="search" />
            </template>
          </q-input>
          <q-select
            v-model="visibleColumnsHistory"
            multiple
            outlined
            dense
            options-dense
            :display-value="$q.lang.table.columns"
            emit-value
            map-options
            :options="columnsHistory"
            option-value="name"
            style="min-width: 140px"
          />
        </div>
        <d-table
          ref="table"
          flat
          bordered
          dense
          :columns="columnsHistory"
          v-model:pagination="historyPagination"
          :rows="rowsHistory"
          :paging="true"
          :rows-per-page-options="[10, 25, 50, 100]"
          :visible-columns="visibleColumnsHistory"
          :filter="filterHistory.trim()"
        >
          >
          <template v-slot:header="props">
            <q-tr :props="props">
              <q-th v-for="col in props.cols" :key="col.name" :props="props">
                <span class="text-weight-medium">{{ col.label }}</span>
              </q-th>
            </q-tr>
          </template>
          <template v-slot:body="props">
            <q-tr :props="props">
              <q-td v-for="col in props.cols" :key="col.id">
                <div>
                  {{ col.value ? col.value : "-" }}
                </div>
              </q-td>
            </q-tr>
          </template>
        </d-table>
      </q-card-section>
    </q-card>
  </q-dialog>

  <DialogDevelop v-model:modal="modalDevelop" v-model:id="kpiDevelopmentId" />
</template>

<style scoped>
.q-checkbox.cursor-pointer {
  cursor: default !important;
}
</style>
