<script setup lang="ts">
import { ref, useAttrs } from "vue";
import { QTooltip, useQuasar } from "quasar";
import { checkPermission } from "@/utils/permissions";

import type {
  PersonData,
  optionData,
} from "@/modules/05_placement/interface/index/Main";
const $q = useQuasar();
const attrs = ref<any>(useAttrs());
const paging = ref<boolean>(true);
const table = ref<any>(null);
const filterRef = ref<any>(null);

const bmaOfficer = ref<string>("");
const bmaOfficerOption = ref<optionData[]>([
  { id: "", name: "ทั้งหมด" },
  { id: "OFFICER", name: "ขรก.กทม. สามัญ" },
  { id: "EXTERNAL", name: "บุคคลภายนอก" },
]);
const props = defineProps({
  inputfilter: String,
  inputvisible: Array,
  inputvisibleFilter: String,
  editvisible: Boolean,
  titleText: String,
  modaladdlist: Boolean,
  displayAdd: Boolean,
  optionsFilter: {
    type: Array,
    defualt: [],
  },
  boss: {
    type: Boolean,
    defualt: false,
  },
  saveNoDraft: {
    type: Boolean,
    defualt: false,
  },
  history: {
    type: Boolean,
    defualt: false,
  },
  paging: {
    type: Boolean,
    defualt: false,
  },
  nornmalData: {
    type: Boolean,
    defualt: false,
  },
  nextPageVisible: {
    type: Boolean,
    defualt: false,
  },
  publicData: {
    type: Boolean,
    defualt: true,
    required: false,
  },
  updateData: {
    type: Boolean,
    defualt: true,
    required: false,
  },
  publicNoBtn: {
    type: Boolean,
    defualt: false,
  },
  refreshBtn: {
    type: Boolean,
    defualt: false,
  },
  add: {
    type: Function,
    default: () => console.log("not function"),
  },
  onSearch: {
    type: Function,
    default: () => console.log("not function"),
  },
  edit: {
    type: Function,
    default: () => console.log("not function"),
  },
  save: {
    type: Function,
    default: () => console.log("not function"),
  },
  deleted: {
    type: Function,
    default: () => console.log("not function"),
  },
  cancel: {
    type: Function,
    default: () => console.log("not function"),
  },
  publish: {
    type: Function,
    default: () => console.log("not function"),
  },
  validate: {
    type: Function,
    default: () => console.log("not function"),
  },
  refresh: {
    type: Function,
    default: () => console.log("not function"),
  },
  containStatus: Boolean,
  roleAdmin: Boolean,
});

const pagination = ref({
  sortBy: "examNumber",
  descending: false,
  page: 1,
  rowsPerPage: 10,
});

/** ส่งค่า*/
const emit = defineEmits([
  "update:inputfilter",
  "update:inputvisible",
  "update:editvisible",
  "update:titleText",
  "update:inputvisibleFilter",
  "update:containfilter",
  "update-modaladdlist",
  "update-modaladdSendPerson",
  "update-newRows",
]);

function paginationLabel(start: string, end: string, total: string) {
  if (paging.value == true) return " " + start + "-" + end + " ใน " + total;
  else return start + "-" + end + " ใน " + total;
}

/**
 * update ค่าที่ค้นหา
 * @param value string
 */
function updateInput(value: any) {
  emit("update:inputfilter", value);
}

/** update ข้อมูลที่ เเสดง */
function updateVisible(value: any) {
  emit("update:inputvisible", value);
}

/**
 * แสดงสถานะบรรจุแล้ว
 * @param value true/false
 */
function updateContain(value: any) {
  emit("update:containfilter", value);
}

/** reset ค่าที่ค้นหาเมื่อกดปุ่ม X ในกล่องค้นหา */
function resetFilter() {
  emit("update:inputfilter", "");
  filterRef.value.focus();
}

/** ส่งรายชื่อไปยังหน่วยงาน */
function updateModaladdlist() {
  emit("update-modaladdlist", true);
}

/** ส่งรายชื่อไปยังหน่วยงาน */
function openModalOrder() {
  emit("update-modaladdSendPerson", true);
}

/** ส่งรายชื่อไปยังหน่วยงาน */
function onUpdateNewRows() {
  emit("update-newRows", bmaOfficer.value);
}
</script>

<template>
  <div class="q-px-md q-pb-md">
    <div class="col-12 row q-py-sm">
      <q-select
        outlined
        dense
        v-model="bmaOfficer"
        :options="bmaOfficerOption"
        emit-value
        map-options
        option-value="id"
        option-label="name"
        label="สถานภาพ"
        style="width: 200px"
        @update:model-value="onUpdateNewRows"
      >
      </q-select>
      <div>
        <q-btn
          flat
          round
          color="blue"
          icon="mdi-account-arrow-right"
          @click="updateModaladdlist"
          v-if="displayAdd && checkPermission($route)?.attrIsUpdate"
        >
          <q-tooltip>ส่งรายชื่อไปยังหน่วยงาน</q-tooltip>
        </q-btn>
      </div>

      <q-btn
        flat
        round
        color="primary"
        icon="mdi-account-arrow-right"
        @click="openModalOrder()"
      >
        <q-tooltip>ส่งไปออกคำสั่ง</q-tooltip>
      </q-btn>

      <q-space />

      <div class="items-center" style="display: flex">
        <q-toggle
          class="col-xs-12 col-sm-5 col-md-5 toggle-expired-account"
          :model-value="containStatus"
          color="blue"
          label="แสดงสถานะบรรจุแล้ว"
          @update:model-value="updateContain"
        />
        <q-input
          standout
          dense
          :model-value="inputfilter"
          ref="filterRef"
          @update:model-value="updateInput"
          outlined
          placeholder="ค้นหา"
          style="max-width: 200px"
          class="q-ml-sm"
          @keydown.enter="onSearch"
        >
          <template v-slot:append>
            <q-icon name="search" />
            <!-- <q-icon
              v-if="inputfilter !== ''"
              name="clear"
              class="cursor-pointer"
              @click="resetFilter"
            /> -->
          </template>
        </q-input>
        <!-- แสดง table ใน คอลัมน์ -->
        <q-select
          :model-value="inputvisible"
          @update:model-value="updateVisible"
          :display-value="$q.lang.table.columns"
          multiple
          outlined
          dense
          :options="attrs.columns"
          options-dense
          option-value="name"
          map-options
          emit-value
          style="min-width: 140px"
          class="gt-xs q-ml-sm"
        />
      </div>
    </div>

    <q-table
      ref="table"
      flat
      bordered
      class="custom-header-table"
      v-bind="attrs"
      virtual-scroll
      :virtual-scroll-sticky-size-start="48"
      dense
      :pagination-label="paginationLabel"
      v-model:pagination="pagination"
    >
      <template v-slot:pagination="scope">
        <q-pagination
          v-model="pagination.page"
          active-color="primary"
          color="dark"
          :max="scope.pagesNumber"
          :max-pages="5"
          size="sm"
          boundary-links
          direction-links
        ></q-pagination>
      </template>
      <template v-slot:header="props">
        <q-tr :props="props">
          <q-th auto-width v-if="history == true" />
          <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 #body="props">
        <slot v-bind="props" name="columns"></slot>
      </template>
    </q-table>
  </div>
</template>

<style lang="scss">
.icon-color {
  color: #4154b3;
}

.custom-header-table {
  max-height: 64vh;

  .q-table tr:nth-child(odd) td {
    background: white;
  }

  .q-table tr:nth-child(even) td {
    background: #f8f8f8;
  }

  .q-table thead tr {
    background: #ecebeb;
  }

  .q-table thead tr th {
    position: sticky;
    z-index: 1;
  }

  /* this will be the loading indicator */
  .q-table thead tr:last-child th {
    /* height of all previous header rows */
    top: 48px;
  }

  .q-table thead tr:first-child th {
    top: 0;
  }
}
</style>
