<script setup lang="ts">
import { ref, watchEffect, watch, type PropType } from "vue";

import { useCounterMixin } from "@/stores/mixin";
/** importType*/
import type { QTableProps } from "quasar";
import type { Meeting } from "@/modules/12_evaluatePersonal/interface/index/Main";
import type { Meetings } from "@/modules/12_evaluatePersonal/interface/response/Main";

/** importComponents*/
import DialogHeader from "@/components/DialogHeader.vue";

const mixin = useCounterMixin();
const { onSearchDataTable } = mixin;
const filterKeyword = defineModel<string>("filterKeyword", { required: true });
const rows = defineModel<Meetings[]>("rows", { required: true });
const rowsData = defineModel<Meetings[]>("rowsData", { required: true });

/** รับ props มาจากหน้าหลัก */
const props = defineProps({
  Modal: Boolean,
  clickClose: Function,
  getData: Function,
  filterTable: {
    type: String,
    default: "",
  },
  maxPage: {
    type: Number,
    require: true,
  },
  rowsPerPage: {
    type: Number,
    require: true,
  },
  page: {
    type: Number,
    require: true,
  },
  getList: {
    type: Function,
    default: () => "",
  },
  selectedRow: {
    type: Array as PropType<Meeting[]>,
    required: true,
  },
});

/** emit*/
const emit = defineEmits([
  "update:filterKeyword2",
  "update:selected",
  "update:pagination",
  "returnDirector",
]);

const selected = ref<Meeting[]>([]);
const currentPage = ref<number>(1);

/**ข้อมูลหัว ตาราง*/
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: false,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "date",
    align: "left",
    label: "วันเวลาในการประชุม",
    sortable: true,
    field: "date",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "title",
    align: "left",
    label: "หัวข้อการประชุม",
    sortable: true,
    field: "title",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "round",
    align: "left",
    label: "ครั้งที่",
    sortable: true,
    field: "round",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "result",
    align: "left",
    label: "ผลการพิจารณาของคณะกรรมการประเมินผลงานแต่ละคณะ",
    sortable: true,
    field: "result",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },

  {
    name: "timePeriod",
    align: "left",
    label: "ระยะเวลาในการแก้ไขผลงาน",
    sortable: true,
    field: "timePeriod",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
]);

/** แสดงจำนวนในตาราง */
const pagination = ref({
  descending: true,
  page: Number(props.page),
  rowsPerPage: props.rowsPerPage,
});

/** เลือกกรรมการ */
async function onClickAddMeet() {
  emit("returnDirector", selected.value);
}

function updateInput(value: any) {
  emit("update:filterKeyword2", value);
}

/**รีเซ็ตค่าในช่องค้นหา */
function Reset() {
  emit("update:filterKeyword2", "");
}

function updateProp(newPagination: number, page: number) {
  // ส่ง event ไปยัง parent component เพื่ออัพเดทค่า props
  emit("update:pagination", newPagination, page);
}

function onSearch() {
  rows.value = onSearchDataTable(
    filterKeyword.value,
    rowsData.value,
    columns.value ? columns.value : []
  );
}

/** เช็คค่า props.Modal === true */
watchEffect(() => {
  if (props.Modal === true) {
    selected.value = props.selectedRow;
    filterKeyword.value = "";
  }
});

watch(
  () => currentPage.value,
  () => {
    if (pagination.value.rowsPerPage) {
      updateProp(pagination.value.rowsPerPage, currentPage.value);
    }
  }
);

watch(
  () => pagination.value.rowsPerPage,
  () => {
    currentPage.value = 1;
    if (pagination.value.rowsPerPage) {
      updateProp(pagination.value.rowsPerPage, currentPage.value);
    }
  }
);
</script>

<template>
  <q-dialog v-model="props.Modal" persistent>
    <q-card style="width: 1200px; max-width: 80vw">
      <DialogHeader :tittle="`เลือกการประชุม`" :close="clickClose" />
      <q-separator />
      <q-card-section>
        <q-input
          borderless
          outlined
          dense
          class="col-12 q-mb-sm"
          v-model="filterKeyword"
          placeholder="ค้นหารายการประชุม"
          style="max-width: 100%"
          @keydown.enter="onSearch"
        >
          <template v-slot:append>
            <q-icon name="search" />
          </template>
        </q-input>

        <d-table
          :columns="columns"
          :rows="rows"
          row-key="id"
          selection="multiple"
          v-model:selected="selected"
          :rows-per-page-options="[10, 25, 50, 100]"
          v-model:pagination="pagination"
        >
          <template v-slot:header-selection="scope">
            <q-checkbox
              keep-color
              color="primary"
              dense
              v-model="scope.selected"
            />
          </template>
          <template v-slot:body="props">
            <q-tr :props="props" class="cursor-pointer">
              <q-td>
                <q-checkbox
                  keep-color
                  color="primary"
                  dense
                  v-model="props.selected"
                />
              </q-td>
              <q-td v-for="col in props.cols" :key="col.name" :props="props">
                <div v-if="col.name == 'no'">
                  {{ props.rowIndex + 1 }}
                </div>
                <div v-else>
                  {{ col.value }}
                </div>
              </q-td>
            </q-tr>
          </template>
        </d-table>
      </q-card-section>

      <q-separator />

      <q-card-actions align="right" class="bg-white text-teal">
        <q-btn
          label="เพิ่มการประชุม"
          @click="onClickAddMeet"
          :disable="selected.length === 0"
          color="public"
        />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>
