<script setup lang="ts">
import { onMounted, ref, watch, reactive } from "vue";
import { useQuasar } from "quasar";
import type { QTableProps } from "quasar";
import { useInsigniaDataStore } from "@/modules/07_insignia/store";
import { useRouter } from "vue-router";
import DialogInformation from "@/components/Dialogs/Information.vue";
import { useroleUserDataStore } from "@/stores/roleUser";

const roleDataStore = useroleUserDataStore();
const { adminRole } = roleDataStore;

const router = useRouter();
const DataStore = useInsigniaDataStore();
const props = defineProps({
  tab: {
    type: String,
  },
  roundId: {
    type: String,
  },
  fecthInsigniaAll: {
    type: Function,
  },
  fecthInsigniaByOc: {
    type: Function,
  },
});

const organization = ref<string>("1");
const organizationOptions = ref<any>([{ id: "1", name: "ทั้งหมด" }]);
const visibleColumns = ref<string[]>([
  "no",
  "citizenId",
  "name",
  "employeeType",
  "position",
  "level",
  "salary",
  "insigniaType",
  "insigniaSend",
  "insigniaLevel",
]);
// หัวตาราง
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: true,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "citizenId",
    align: "left",
    label: "เลขประจำตัวประชาชน",
    sortable: true,
    field: "citizenId",
    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: "employeeType",
    align: "left",
    label: "สถานภาพ",
    sortable: true,
    field: "employeeType",
    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: "level",
    align: "left",
    label: "อันดับ/ระดับ",
    sortable: true,
    field: "level",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "salary",
    align: "left",
    label: "เงินเดือน",
    sortable: true,
    field: "salary",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "insigniaType",
    align: "left",
    label: "ประเภทเครื่องราชฯ ปัจจุบัน",
    sortable: true,
    field: "insigniaType",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "insigniaSend",
    align: "left",
    label: "ประเภทเครื่องราชฯ ที่ยื่นขอ",
    sortable: true,
    field: "insigniaSend",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "insigniaLevel",
    align: "left",
    label: "ชั้นเครื่องราชฯ",
    sortable: true,
    field: "insigniaLevel",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "dateSend",
    align: "left",
    label: "วันที่ยื่นขอ",
    sortable: true,
    field: "dateSend",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);
onMounted(async () => {
  organizationOptions.value = DataStore.optionsTypeOc;
  organization.value = await (DataStore.agency != null
    ? DataStore.agency
    : DataStore.typeOc);

  if (organization.value !== "" || organization.value !== undefined) {
    if (props.fecthInsigniaByOc) {
      await props.fecthInsigniaByOc(
        props.roundId,
        organization.value,
        "officer",
        props.tab
      );
      DataStore.mainTab = props.tab;
    }
  }
});
const changtypeOc = async () => {
  if (props.fecthInsigniaByOc) {
    await props.fecthInsigniaByOc(
      props.roundId,
      organization.value,
      "officer",
      props.tab
    );
  }
  DataStore.typeOc = organization.value;
};
const nextPage = (id: string) => {
  router.push(`/registry/${id}`);
};

const filterKeyword = ref<string>("");
const filterRef = ref<any>(null);
const resetFilter = () => {
  filterKeyword.value = "";
  filterRef.value.focus();
};
const pagination = ref({
  sortBy: "name",
  descending: false,
  page: 1,
  rowsPerPage: 10,
});
const paging = ref<boolean>(true);
const paginationLabel = (start: number, end: number, total: number) => {
  if (paging.value == true) return " " + start + "-" + end + " ใน " + total;
  else return start + "-" + end + " ใน " + total;
};

const dialogNote = ref<boolean>(false);
const note = ref<string>("");
const noteTitle = ref<string>("");
const showNote = (row: any) => {
  note.value = row.requestNote;
  noteTitle.value = "เหตุผลกรลบออกของ" + " " + row.name;
  dialogNote.value = true;
};
const closeReson = () => {
  dialogNote.value = false;
};
</script>
<template>
  <div class="col-12 row q-pa-md">
    <div class="row col-12">
      <div class="row col-12 q-col-gutter-sm">
        <q-select
          v-if="roleDataStore.adminRole"
          v-model="organization"
          label="หน่วยงาน"
          dense
          emit-value
          map-options
          :options="organizationOptions"
          option-value="id"
          option-label="name"
          lazy-rules
          hide-bottom-space
          :readonly="false"
          :borderless="false"
          :outlined="true"
          :hide-dropdown-icon="false"
          style="min-width: 150px"
          @update:model-value="changtypeOc"
        />
        <q-select
          v-model="DataStore.typeinsignia"
          label="ปรเภทเครื่องราชฯ"
          dense
          emit-value
          map-options
          :options="DataStore.typeinsigniaOptions"
          option-value="id"
          option-label="name"
          lazy-rules
          hide-bottom-space
          :readonly="false"
          :borderless="false"
          :outlined="true"
          :hide-dropdown-icon="false"
          style="min-width: 150px"
          @update:model-value="
            DataStore.searchDataTable(
              DataStore.typeinsignia,
              DataStore.employeeClass
            )
          "
        />
        <div>
          <q-select
            v-model="DataStore.employeeClass"
            dense
            outlined
            lazy-rules
            hide-bottom-space
            :label="`${'สถานภาพ'}`"
            emit-value
            map-options
            option-label="name"
            :options="DataStore.employeeClassOps"
            option-value="id"
            :readonly="false"
            :borderless="false"
            style="min-width: 150px"
            @update:model-value="
              DataStore.searchDataTable(
                DataStore.typeinsignia,
                DataStore.employeeClass
              )
            "
          />
        </div>
        <q-space />

        <q-input
          class="col-xs-12 col-sm-3 col-md-2"
          standout
          dense
          v-model="filterKeyword"
          ref="filterRef"
          outlined
          debounce="300"
          placeholder="ค้นหา"
        >
          <template v-slot:append>
            <q-icon v-if="filterKeyword == ''" name="search" />
            <q-icon
              v-if="filterKeyword !== ''"
              name="clear"
              class="cursor-pointer"
              @click="resetFilter"
            />
          </template>
        </q-input>

        <q-select
          v-model="visibleColumns"
          multiple
          outlined
          dense
          options-dense
          :display-value="$q.lang.table.columns"
          emit-value
          map-options
          :options="columns"
          option-value="name"
          options-cover
          style="min-width: 150px"
          class="col-xs-12 col-sm-3 col-md-2"
        />
      </div>
      <div class="col-12 q-pt-sm">
        <q-table
          ref="table"
          :columns="columns"
          :rows="DataStore.rows"
          :filter="filterKeyword"
          row-key="name"
          flat
          bordered
          :paging="true"
          dense
          class="custom-header-table"
          :visible-columns="visibleColumns"
          :pagination-label="paginationLabel"
          v-model:pagination="pagination"
        >
          <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-th auto-width />
            </q-tr>
          </template>
          <template v-slot:body="props">
            <q-tr
              :props="props"
              class="cursor-pointer"
              @click="nextPage(props.row.profileId)"
            >
              <q-td key="no" :props="props">
                {{ props.rowIndex + 1 }}
              </q-td>
              <q-td key="citizenId" :props="props">
                {{ props.row.citizenId }}
              </q-td>
              <q-td key="name" :props="props">
                {{ props.row.name }}
              </q-td>
              <q-td key="employeeType" :props="props">
                {{ props.row.employeeType }}
              </q-td>
              <q-td key="position" :props="props">
                {{ props.row.position }}
              </q-td>
              <q-td key="level" :props="props">
                {{ props.row.level }}
              </q-td>
              <q-td key="salary" :props="props">
                {{ Number(props.row.salary).toLocaleString() }}
              </q-td>

              <q-td key="insigniaType" :props="props">
                {{ props.row.insigniaType }}
              </q-td>
              <q-td key="insigniaSend" :props="props">
                {{ props.row.insigniaSend }}
              </q-td>
              <q-td key="insigniaLevel" :props="props">
                {{ props.row.insigniaLevel }}
              </q-td>
              <q-td v-if="props.row.requestNote != null" auto-width>
                <q-btn
                  dense
                  size="12px"
                  flat
                  round
                  color="blue"
                  @click.stop="showNote(props.row)"
                  icon="mdi-information-outline"
                >
                  <q-tooltip>เหตุผลการลบ</q-tooltip>
                </q-btn>
              </q-td>
            </q-tr>
          </template>
          <template v-slot:pagination="scope">
            <q-pagination
              v-model="pagination.page"
              active-color="primary"
              color="primary"
              :max="scope.pagesNumber"
              :max-pages="5"
              size="sm"
              boundary-links
              direction-links
            ></q-pagination>
          </template>
        </q-table>
      </div>
    </div>
  </div>
  <DialogInformation
    :modal="dialogNote"
    :title="noteTitle"
    :desc="note"
    :click-close="closeReson"
  />
</template>
