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

/** importType*/
import type { QTableProps } from "quasar";
import type { directorType } from "@/modules/11_discipline/interface/index/Main";

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

const selected = ref<directorType[]>([]);

/** รับ props มาจากหน้าหลัก */
const props = defineProps({
  Modal: Boolean,
  clickClose: Function,
  getData: Function,
  rows2: Array,
  filterKeyword2: String,
  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<directorType[]>,
    required: true,
  },
});

const currentPage = ref<number>(1); //หน้าปัจจุบัน

/** ค้นหาคอลัม */
const visibleColumns2 = ref<string[]>([
  "no",
  "name",
  "position",
  "email",
  "phone",
]);
// หัวตาราง
const columns2 = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: false,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "name",
    align: "left",
    label: "ชื่อ-นามสกุล",
    sortable: true,
    field: "name",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "position",
    align: "left",
    label: "ตำแหน่ง",
    sortable: true,
    field: "position",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "email",
    align: "left",
    label: "อีเมล",
    sortable: true,
    field: "email",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "phone",
    align: "left",
    label: "เบอร์โทรศัพท์",
    sortable: true,
    field: "phone",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);

/** แสดงจำนวนในตาราง */
const pagination = ref({
  descending: true,
  page: Number(props.page),
  rowsPerPage: props.rowsPerPage,
});
const emit = defineEmits([
  "update:filterKeyword2",
  "update:selected",
  "update:pagination",
  "returnDirector",
]);

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

/**
 * ฟังก์ชันอัปเดทคำค้นหา
 * @param value  คำค้นหา
 */
function updateInput(value: any) {
  emit("update:filterKeyword2", value);
}

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

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

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

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"
          debounce="300"
          :model-value="filterKeyword2"
          @update:model-value="updateInput"
          placeholder="ค้นหารายชื่อ"
          style="max-width: 100%"
        >
          <template v-slot:append>
            <q-icon name="search" />
          </template>
        </q-input>

        <d-table
          :columns="columns2"
          :rows="rows2"
          :filter="filterKeyword2?.trim()"
          row-key="id"
          :visible-columns="visibleColumns2"
          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="directorSave"
          :disable="selected.length === 0"
          color="public"
        />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>
