<script setup lang="ts">
import { ref, onMounted } from "vue";
// import { useRouter } from "vue-router";
import { useCounterMixin } from "@/stores/mixin";

import DialogForm from "@/modules/07_insignia/components/3_result/DialogForm.vue";
import type { OptionData } from "@/modules/07_insignia/interface/index/Main";
import type { QTableProps, QInput } from "quasar";
import { useQuasar } from "quasar";

// const router = useRouter();
const mixin = useCounterMixin();
const { date2Thai, dialogConfirm } = mixin;
const $q = useQuasar();
const tab = ref<string>("hight");

const fileUpload = ref<any>("");
const reason = ref<string>("");
const documentTitle = ref<string>("");

const selectType = ref<string>("1");
const selectTypeOption = ref<OptionData[]>([{id: "1", name: "เบญจมาภรณ์มงกุฎไทย"}]);

const selectRound = ref<string>("1");
const selectRoundOption = ref<OptionData[]>([
  { id: "1", name: "รอบการเสนอขอพระราชทานเครื่องราชฯ ปี 2566" },
]);

const visibleColumns = ref<String[]>([
  "no",
  "status",
  "name",
  "type",
  "page",
  "number",
  "vatnumber",
  "datepay",
  "typepay",
  "address",
  "action"
]);
const filterRef = ref<QInput>();
const filter = ref<string>("");
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    field: "no",
    sortable: true,
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "status",
    align: "left",
    label: "สถานะ",
    field: "status",
    sortable: true,
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "name",
    align: "left",
    label: "ชื่อ - สกุล",
    field: "name",
    sortable: true,
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "type",
    align: "left",
    label: "ประเภทเครื่องราชฯ",
    field: "type",
    sortable: true,
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "page",
    align: "left",
    label: "หน้าในราชกิจนุเบกษา",
    field: "page",
    sortable: true,
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "number",
    align: "left",
    label: "ลำดับที่ในราชกิจจานุเบกษา",
    field: "number",
    sortable: true,
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "vatnumber",
    align: "left",
    label: "หมายเลขใบกำกับ",
    field: "vatnumber",
    sortable: true,
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "datepay",
    align: "left",
    label: "วันที่จ่ายใบกำกับฯ",
    field: "datepay",
    sortable: true,
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "typepay",
    align: "left",
    label: "รูปแบบการจ่าย",
    field: "typepay",
    sortable: true,
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "address",
    align: "left",
    label: "ที่อยู่ที่จ่าย",
    field: "address",
    sortable: true,
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "action",
    align: "left",
    label: "",
    field: "",
  },
]);
const rows = ref<any>([
  {
    no: "1",
    status: "รอบันทึกข้อมูล",
    name: "นางสาวรัชภรณ์ ภักดี",
    type: "ทั้งหมด",
    page: "8",
    number: "11",
    vatnumber: "5647833",
    datepay: null,
    typepay: null,
    address: "เลขที่ 2/3 หมู่ที่ 14 ถนนบ...",
  },
  {
    no: "2",
    status: "บันทึกลง ก.พ. 7 แล้ว",
    name: "นางสาวภาพรรณ ลออ",
    type: "ทั้งหมด",
    page: "12",
    number: "11",
    vatnumber: "1122345",
    datepay: null,
    typepay: null,
    address: "เลขที่ 2/3 หมู่ที่ 14 ถนนบ...",
  },
]);

const modal = ref<boolean>(false);

const close = () => {
  modal.value = false;
}

const addData = () => {
  modal.value = true;
  console.log("addData")
};

const editData = (id: string) => {
  modal.value = true;
  console.log("editData", id)
};

const save = () => {
  console.log("save function")
}

const resetFilter = () => {
  // reset ค่าที่ค้นหาเมื่อกดปุ่ม X ในกล่องค้นหา
  filter.value = "";
  filterRef.value!.focus();
};

const filterSelector = (val: any, update: Function, filtername: string) => {
  // switch (filtername) {
  //   case "selectTypeOption":
  //     update(() => {
  //       selectTypeOption.value = selectTypeOptionFilter.value.filter(
  //         (v: DataOption) => v.name!.indexOf(val) > -1
  //       );
  //     });
  //     break;
  //   case "selectStatusOption":
  //     update(() => {
  //       selectStatusOption.value = selectStatusOptionFilter.value.filter(
  //         (v: DataOption) => v.name!.indexOf(val) > -1
  //       );
  //     });
  //     break;

  //   default:
  //     break;
  // }
};
</script>

<template>
  <div class="toptitle text-dark col-12 row items-center">
    บันทึกผลการได้รับพระราชทานเครื่องราชย์อิสริยสภรณ์/การจ่ายใบกำกับ
  </div>
  <q-card flat bordered class="col-12 q-my-md q-mt-sm rounded-borders">
    <div class="q-pa-md row items-center">
      <selector hide-bottom-space borderless dense lazy-rules emit-value map-options options-dense option-label="name"
        option-value="id" v-model="selectRound" :options="selectRoundOption" use-input input-debounce="0"
        input-class="text-bold text-grey">
        <template v-slot:before><label class="text-bold text-grey" style="font-size: 16px">รอบ</label>
        </template>
      </selector>
    </div>
    <q-separator />
    <q-tabs v-model="tab" align="left" class="bg-white text-grey" active-color="primary" indicator-color="primary">
      <q-tab name="hight" label="ขั้นสายสะพาน" />
      <q-tab name="low" label="ขั้นต่ำกว่าสายสะพาน" />
      <q-tab name="medal" label="เหรียญตรา" />
      <q-tab name="doc" label="เอกสารอ้างอิง" />
    </q-tabs>
    <q-separator />
    <div class="q-py-md q-px-md">

      <div v-if="tab=='hight'">
        <div class="row col-12 q-pb-sm">
          <selector outlined dense lazy-rules v-model="selectType" hide-bottom-space :label="`${'เครื่องราชฯ'}`"
            emit-value map-options option-label="name" :options="selectTypeOption" option-value="id" use-input
            input-debounce="0" style="min-width: 150px" class="gt-xs q-ml-sm" @filter="(inputValue: string, doneFn: Function) => filterSelector(inputValue, doneFn, 'selectTypeOption')" />
          <q-btn @click="addData()" size="12px" flat round color="add" icon="mdi-plus">
            <q-tooltip>เพิ่ม</q-tooltip>
          </q-btn>
          <q-btn flat round color="light-blue" icon="upload">
            <!-- <q-tooltip>อัพโหลดไฟล์</q-tooltip> -->
            <q-menu>
              <q-list style="min-width: 150px">
                <q-item clickable v-close-popup>
                  <q-item-section avatar><q-icon color="green-7" name="mdi-file-excel" /></q-item-section>
                  <q-item-section>บันทึกผลการได้รับพระราชทานเครื่องราชย์อิสริยสภรณ์</q-item-section>
                </q-item>
                <q-item clickable v-close-popup>
                  <q-item-section avatar><q-icon color="green-7" name="mdi-file-excel" /></q-item-section>
                  <q-item-section>บันทึกผลการจ่ายใบกำกับ</q-item-section>
                </q-item>
              </q-list>
            </q-menu>
          </q-btn>

          <q-space />

          <div class="items-center" style="display: flex">
            <!-- ค้นหาข้อความใน table -->
            <q-input standout dense v-model="filter" ref="filterRef" outlined debounce="300" placeholder="ค้นหา"
              style="max-width: 200px" class="q-ml-sm">
              <template v-slot:append>
                <q-icon v-if="filter == ''" name="search" />
                <q-icon v-if="filter !== ''" name="clear" class="cursor-pointer" @click="resetFilter" />
              </template>
            </q-input>
            <!-- แสดงคอลัมน์ใน table -->
            <q-select v-model="visibleColumns" :display-value="$q.lang.table.columns" multiple outlined dense
              :options="columns" options-dense option-value="name" map-options emit-value style="min-width: 150px"
              class="gt-xs q-ml-sm" />
          </div>
        </div>
        
        <d-table :rows="rows" :columns="columns" :visible-columns="visibleColumns" :filter="filter" row-key="name">
          <template v-slot:body-cell="props">
            <q-td :props="props">
              <div v-if="props.col.name == 'no'">
                {{ props.rowIndex + 1 }}
              </div>

              <div v-else-if="props.col.name == 'action'">
                <q-btn
                  dense
                  size="12px"
                  flat
                  round
                  color="primary"
                  @click="editData(props.row.no)"
                  icon="mdi-pencil-outline"
                >
                  <q-tooltip>แก้ไขข้อมูล</q-tooltip>
                </q-btn>
              </div>

              <div v-else>
                {{ props.value }}
              </div>
            </q-td>
          </template>
        </d-table>
      </div>

      <div v-else-if="tab=='low'">
        <div class="row col-12 q-pb-sm">
          <selector outlined dense lazy-rules v-model="selectType" hide-bottom-space :label="`${'เครื่องราชฯ'}`"
            emit-value map-options option-label="name" :options="selectTypeOption" option-value="id" use-input
            input-debounce="0" style="min-width: 150px" class="gt-xs q-ml-sm" @filter="(inputValue: string,
              doneFn: Function) => filterSelector(inputValue, doneFn, 'selectTypeOption'
              )" />
          <q-btn @click="addData()" size="12px" flat round color="add" icon="mdi-plus">
            <q-tooltip>เพิ่ม</q-tooltip>
          </q-btn>
          <q-btn flat round color="light-blue" icon="upload">
            <!-- <q-tooltip>อัพโหลดไฟล์</q-tooltip> -->
            <q-menu>
              <q-list style="min-width: 150px">
                <q-item clickable v-close-popup>
                  <q-item-section avatar><q-icon color="green-7" name="mdi-file-excel" /></q-item-section>
                  <q-item-section>บันทึกผลการได้รับพระราชทานเครื่องราชย์อิสริยสภรณ์</q-item-section>
                </q-item>
                <q-item clickable v-close-popup>
                  <q-item-section avatar><q-icon color="green-7" name="mdi-file-excel" /></q-item-section>
                  <q-item-section>บันทึกผลการจ่ายใบกำกับ</q-item-section>
                </q-item>
              </q-list>
            </q-menu>
          </q-btn>

          <q-space />

          <div class="items-center" style="display: flex">
            <!-- ค้นหาข้อความใน table -->
            <q-input standout dense v-model="filter" ref="filterRef" outlined debounce="300" placeholder="ค้นหา"
              style="max-width: 200px" class="q-ml-sm">
              <template v-slot:append>
                <q-icon v-if="filter == ''" name="search" />
                <q-icon v-if="filter !== ''" name="clear" class="cursor-pointer" @click="resetFilter" />
              </template>
            </q-input>
            <!-- แสดงคอลัมน์ใน table -->
            <q-select v-model="visibleColumns" :display-value="$q.lang.table.columns" multiple outlined dense
              :options="columns" options-dense option-value="name" map-options emit-value style="min-width: 150px"
              class="gt-xs q-ml-sm" />
          </div>
        </div>
        
        <d-table :rows="rows" :columns="columns" :visible-columns="visibleColumns" :filter="filter" row-key="name">
          <template v-slot:body-cell="props">
            <q-td :props="props">
              <div v-if="props.col.name == 'no'">
                {{ props.rowIndex + 1 }}
              </div>

              <div v-else-if="props.col.name == 'action'">
                <q-btn
                  dense
                  size="12px"
                  flat
                  round
                  color="primary"
                  @click="editData(props.row.no)"
                  icon="mdi-pencil-outline"
                >
                  <q-tooltip>แก้ไขข้อมูล</q-tooltip>
                </q-btn>
              </div>

              <div v-else>
                {{ props.value }}
              </div>
            </q-td>
          </template>
        </d-table>
      </div>

      <div v-else-if="tab=='medal'">
        <div class="row col-12 q-pb-sm">
          <selector outlined dense lazy-rules v-model="selectType" hide-bottom-space :label="`${'เครื่องราชฯ'}`"
            emit-value map-options option-label="name" :options="selectTypeOption" option-value="id" use-input
            input-debounce="0" style="min-width: 150px" class="gt-xs q-ml-sm" @filter="(inputValue: string,
              doneFn: Function) => filterSelector(inputValue, doneFn, 'selectTypeOption'
              )" />
          <q-btn @click="addData()" size="12px" flat round color="add" icon="mdi-plus">
            <q-tooltip>เพิ่ม</q-tooltip>
          </q-btn>
          <q-btn flat round color="light-blue" icon="upload">
            <!-- <q-tooltip>อัพโหลดไฟล์</q-tooltip> -->
            <q-menu>
              <q-list style="min-width: 150px">
                <q-item clickable v-close-popup>
                  <q-item-section avatar><q-icon color="green-7" name="mdi-file-excel" /></q-item-section>
                  <q-item-section>บันทึกผลการได้รับพระราชทานเครื่องราชย์อิสริยสภรณ์</q-item-section>
                </q-item>
                <q-item clickable v-close-popup>
                  <q-item-section avatar><q-icon color="green-7" name="mdi-file-excel" /></q-item-section>
                  <q-item-section>บันทึกผลการจ่ายใบกำกับ</q-item-section>
                </q-item>
              </q-list>
            </q-menu>
          </q-btn>

          <q-space />

          <div class="items-center" style="display: flex">
            <!-- ค้นหาข้อความใน table -->
            <q-input standout dense v-model="filter" ref="filterRef" outlined debounce="300" placeholder="ค้นหา"
              style="max-width: 200px" class="q-ml-sm">
              <template v-slot:append>
                <q-icon v-if="filter == ''" name="search" />
                <q-icon v-if="filter !== ''" name="clear" class="cursor-pointer" @click="resetFilter" />
              </template>
            </q-input>
            <!-- แสดงคอลัมน์ใน table -->
            <q-select v-model="visibleColumns" :display-value="$q.lang.table.columns" multiple outlined dense
              :options="columns" options-dense option-value="name" map-options emit-value style="min-width: 150px"
              class="gt-xs q-ml-sm" />
          </div>
        </div>
        
        <d-table :rows="rows" :columns="columns" :visible-columns="visibleColumns" :filter="filter" row-key="name">
          <template v-slot:body-cell="props">
            <q-td :props="props">
              <div v-if="props.col.name == 'no'">
                {{ props.rowIndex + 1 }}
              </div>

              <div v-else-if="props.col.name == 'action'">
                <q-btn
                  dense
                  size="12px"
                  flat
                  round
                  color="primary"
                  @click="editData(props.row.no)"
                  icon="mdi-pencil-outline"
                >
                  <q-tooltip>แก้ไขข้อมูล</q-tooltip>
                </q-btn>
              </div>

              <div v-else>
                {{ props.value }}
              </div>
            </q-td>
          </template>
        </d-table>
      </div>

      <div v-else-if="tab=='doc'">
        <Transition>
        <div class="bg-base rounded-borders q-pa-md q-mb-md">
          <div class="row col-12 q-col-gutter-x-lg q-col-gutter-y-md">
            <!-- <div class="col-xs-6 col-md-2">
              <q-input
                class="bg-white"
                outlined
                dense
                lazy-rules
                v-model="issue"
                :label="`${'ฉบับ'}`"
                hide-bottom-space
              />
            </div>
            <div class="col-xs-6 col-md-2">
              <q-input
                class="bg-white"
                outlined
                dense
                lazy-rules
                v-model="book"
                :label="`${'เล่มที่'}`"
                hide-bottom-space
              />
            </div>
            <div class="col-xs-6 col-md-2">
              <q-input
                class="bg-white"
                outlined
                dense
                lazy-rules
                v-model="volume"
                :label="`${'เล่ม'}`"
                hide-bottom-space
              />
            </div>
            <div class="col-xs-6 col-md-2">
              <q-input
                class="bg-white"
                outlined
                dense
                lazy-rules
                v-model="section"
                :label="`${'ตอนที่'}`"
                hide-bottom-space
              />
            </div> -->
            <div class="col-xs-6 col-md-4">
              <q-file
                class="bg-white"
                outlined
                dense
                v-model="fileUpload"
                label="ไฟล์เอกสาร"
                hide-bottom-space
                lazy-rules
              >
                <template v-slot:prepend>
                  <q-icon name="attach_file" color="primary" />
                </template>
              </q-file>
            </div>
            <div class="col-xs-6 col-md-3">
              <q-input
                class="bg-white"
                outlined
                dense
                lazy-rules
                v-model="documentTitle"
                :label="`${'ชื่อเอกสาร'}`"
                hide-bottom-space
              />
            </div>
            <div class="col-xs-12 col-md-5">
              <q-input
                class="bg-white"
                outlined
                dense
                lazy-rules
                v-model="reason"
                :label="`${'หมายเหตุ'}`"
                hide-bottom-space
              />
            </div>
            <!-- <div class="col-xs-6 col-md-2">
              <datepicker
                menu-class-name="modalfix"
                v-model="dateGoverment"
                :locale="'th'"
                autoApply
                borderless
                :enableTimePicker="false"
                week-start="0"
              >
                <template #year="{ year }">
                  {{ year + 543 }}
                </template>
                <template #year-overlay-value="{ value }">
                  {{ parseInt(value + 543) }}
                </template>
                <template #trigger>
                  <q-input
                    outlined
                    dense
                    class="full-width datepicker bg-white"
                    :model-value="
                      dateGoverment != null ? date2Thai(dateGoverment) : null
                    "
                    :label="`${'วันที่ประกาศราชกิจจานุเบกษา'}`"
                  >
                    <template v-slot:prepend>
                      <q-icon
                        name="event"
                        class="cursor-pointer"
                        style="color: var(--q-primary)"
                      >
                      </q-icon>
                    </template>
                  </q-input>
                </template>
              </datepicker>
            </div>
            <div class="col-xs-6 col-md-2">
              <datepicker
                menu-class-name="modalfix"
                v-model="dateReceiver"
                :locale="'th'"
                autoApply
                borderless
                :enableTimePicker="false"
                week-start="0"
              >
                <template #year="{ year }">
                  {{ year + 543 }}
                </template>
                <template #year-overlay-value="{ value }">
                  {{ parseInt(value + 543) }}
                </template>
                <template #trigger>
                  <q-input
                    outlined
                    dense
                    class="full-width datepicker bg-white"
                    :model-value="
                      dateReceiver != null ? date2Thai(dateReceiver) : null
                    "
                    :label="`${'วันที่ได้รับ'}`"
                  >
                    <template v-slot:prepend>
                      <q-icon
                        name="event"
                        class="cursor-pointer"
                        style="color: var(--q-primary)"
                      >
                      </q-icon>
                    </template>
                  </q-input>
                </template>
              </datepicker>
            </div> -->
            <div class="col-xs-12 col-md-9">
              <!-- <p>
                วันที่ประกาศราชกิจจานุเบกษา <b>{{ date2Thai(new Date()) }}</b>
              </p> -->
            </div>
            <!-- <div class="col-xs-6 col-md-3">
              <p>
                วันที่ได้รับ <b>{{ date2Thai(new Date()) }}</b>
              </p>
            </div> -->
            <div class="col-xs-12 col-md-3 flexsave">
              <q-btn label="บันทึก" @click="save" color="public" />
            </div>
          </div>
        </div>
      </Transition>
      </div>

    </div>

    <DialogForm :modal="modal" :save="save" :close="close"/>
  </q-card>
</template>
<style lang="scss" scoped>
.arrow {
  transition: transform 0.5s;
}

.arrow-active {
  transition: transform 0.5s;
  transform: rotate(-90deg);
}

.bg-base {
  background-color: #efefef;
}

.v-enter-active,
.v-leave-active {
  transition: opacity 0.5s ease;
}

.v-enter-from,
.v-leave-to {
  opacity: 0;
}

.flexsave {
  display: flex;
  justify-content: flex-end;
}
</style>
