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

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

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

const total = defineModel<number>("total", { required: true });
const totalList = defineModel<number>("totalList", { required: true });
const pagination = defineModel<any>("pagination", { required: true });
const filterKeyword2 = defineModel<string>("filterKeyword2", {
  required: true,
});
const selected = ref<directorType[]>([]);
/** ค้นหาคอลัม */
const visibleColumns2 = ref<string[]>([
  "no",
  "name",
  "position",
  "duty",
  "email",
  "phone",
  "role",
]);

/**ข้อมูลหัว ตาราง*/
const columns2 = 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: "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: "position",
    align: "left",
    label: "ตำแหน่ง",
    sortable: true,
    field: "position",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },

  {
    name: "email",
    align: "left",
    label: "อีเมล",
    sortable: true,
    field: "email",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "phone",
    align: "left",
    label: "เบอร์โทรศัพท์",
    sortable: true,
    field: "phone",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
]);

/** รับ props มาจากหน้าหลัก */
const props = defineProps({
  Modal: Boolean,
  clickClose: Function,
  getSearch: Function,
  rows2: Array,
  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 checkSelected = computed(() => {
  if (selected.value.length === 0) {
    return true;
  }
});

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

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

/**
 * ส่งค่า input กลับไปหน้าหลัก
 * @param value ค่าจาก input ฟิลเตอร์
 */
function updateInput() {
  props.getSearch?.();
}

/**รีเซ็ตค่าในช่องค้นหา */
function Reset() {
  filterKeyword2.value = "";
  props.getSearch?.();
}

function updatePagination(newPagination: any) {
  pagination.value.page = 1;
  pagination.value.rowsPerPage = newPagination.rowsPerPage;
}

/** เช็คค่า props.Modal === true */
watch(
  () => props.Modal,
  () => {
    if (props.Modal === true) {
      selected.value = props.selectedRow;
      props.getList();
    }
  }
);
</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>
        <div class="row q-col-gutter-sm">
          <div class="row col-12">
            <q-space />
            <div class="row q-col-gutter-sm">
              <q-input
                borderless
                outlined
                dense
                v-model="filterKeyword2"
                @keydown.enter.prevent="updateInput"
                placeholder="ค้นหารายชื่อ"
                style="max-width: 100%"
              >
                <template v-slot:append>
                  <q-icon name="search" />
                </template>
              </q-input>

              <q-select
                v-model="visibleColumns2"
                multiple
                outlined
                dense
                options-dense
                :display-value="$q.lang.table.columns"
                emit-value
                map-options
                :options="columns2"
                option-value="name"
                style="min-width: 140px"
              />
            </div>
          </div>
          <div class="col-12">
            <d-table
              :columns="columns2"
              :rows="rows2"
              row-key="id"
              :visible-columns="visibleColumns2"
              selection="multiple"
              v-model:selected="selected"
              :rows-per-page-options="[10, 25, 50, 100]"
              @update:pagination="updatePagination"
            >
              <template v-slot:pagination="scope">
                ทั้งหมด {{ total }} รายการ
                <q-pagination
                  v-model="pagination.page"
                  active-color="primary"
                  color="dark"
                  :max="Number(totalList)"
                  size="sm"
                  boundary-links
                  direction-links
                  :max-pages="5"
                  @update:model-value="props.getList?.()"
                ></q-pagination>
              </template>

              <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'">
                      {{
                        (pagination.page - 1) * pagination.rowsPerPage +
                        props.rowIndex +
                        1
                      }}
                    </div>
                    <div v-else>
                      {{ col.value }}
                    </div>
                  </q-td>
                </q-tr>
              </template>
            </d-table>
          </div>
        </div>
      </q-card-section>

      <q-separator />

      <q-card-actions align="right" class="bg-white text-teal">
        <q-btn
          label="เพิ่มรายชื่อกรรมการ"
          @click="directorSave"
          :disable="checkSelected"
          color="public"
        />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>
