<script setup lang="ts">
import { onMounted, ref, useAttrs, reactive, watch } from "vue";
import type { QTableProps } from "quasar";
import type {
  FormMainProbation,
  FormMainProbation2,
} from "@/modules/05_placement/interface/request/Main";
import { useQuasar } from "quasar";
import { useRouter } from "vue-router";
import http from "@/plugins/http";
import config from "@/app.config";
import DialogHeader from "@/modules/07_insignia/components/DialogHeader.vue";
import { useCounterMixin } from "@/stores/mixin";
import type { resMain } from "@/modules/06_retirement/interface/response/Main";
const yearOptions = reactive<any[]>([]);
import cardTop from "@/modules/07_insignia/components/2_Manage/StatCard.vue";
import tab1 from "@/modules/07_insignia/components/2_Manage/Tab1.vue";
import tab2 from "@/modules/07_insignia/components/2_Manage/Tab2.vue";
import tab3 from "@/modules/07_insignia/components/2_Manage/Tab3.vue";
import tab4 from "@/modules/07_insignia/components/2_Manage/Tab4.vue";

const Note = ref<string>("");
const addNote = ref<boolean>(false);
const saveWriteNote = ref<any[]>([]);
const mixin = useCounterMixin();
const { messageError, date2Thai, showLoader, hideLoader, dialogMessage } =
  mixin;
const router = useRouter();
const $q = useQuasar(); //ใช้ noti quasar
const modal = ref<boolean>(false);
const pagination = ref({
  sortBy: "desc",
  descending: false,
  page: 1,
  rowsPerPage: 10,
});
const round = ref<string>("รอบการเสนอพระราชทานเครื่องราชปี 2556");
const optionRound = [
  "รอบการเสนอพระราชทานเครื่องราชปี 2557",
  "รอบการเสนอพระราชทานเครื่องราชปี 2558",
  "รอบการเสนอพระราชทานเครื่องราชปี 2556",
];
const visibleColumns = ref<string[]>([
  "no",
  "fullname",
  "requestType",

]); //ค้นหา คอลัมน์ คอลัมน์ที่แสดง

// หัวตาราง
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: true,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "fullname",
    align: "left",
    label: "ชื่อ-นามสกุล",
    sortable: true,
    field: "fullname",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "requestType",
    align: "left",
    label: "ประเภทคำร้องขอเเก้ไข",
    sortable: true,
    field: "requestType",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  
]);

// ข้อมูลตาราง (จำลอง)
const rows = ref<any[]>([
  {
    no: "1",
    fullname: "นายใจดี ยอดใจ ",
    requestType: "xxxxxx",
  },
  {
    no: "2",
    fullname: "นายใจดี ยอดใจ ",
    requestType: "xxxxxx",
  },
  {
    no: "3",
    fullname: "นายใจดี ยอดใจ ",
    requestType: "xxxxxx",
  },
]);


const clickDelete = (id: string) => {
  $q.dialog({
    title: "ยืนยันการลบข้อมูล",
    message: "ต้องการลบข้อมูลนี้ใช่หรือไม่?",
    cancel: {
      flat: true,
      color: "negative",
    },
    persistent: true,
  })
    .onOk(async () => {})
    .onCancel(() => {})
    .onDismiss(() => {});
};
const clickNote = () => {
  addNote.value = true;
};
const clickAdd = () => {
  //   modal.value = true;
  $q.dialog({
    title: "เพิ่มประกาศ",
    message: "เลือกประกาศที่ต้องการเพิ่มข้อมูล",
    options: {
      type: "radio",
      model: "opt1",
      // inline: true
      items: [
        { label: "ประกาศเพิ่มผู้เกษียณ", value: "ADD" },
        { label: "ประกาศแก้ไขข้อมูลผู้เกษียณ", value: "EDIT" },
        { label: "ประกาศยกเลิกผู้เกษียณ", value: "REMOVE" },
      ],
    },
    cancel: {
      flat: true,
      color: "negative",
    },
    persistent: true,
  })
    .onOk((data) => {
      console.log("option===>", data);
      router.push(`/retirement/list/${type.value}/${currentYear}`);
    })
    .onCancel(() => {})
    .onDismiss(() => {});
  /* $q.dialog({
      title: "ยืนยันการเพิ่มข้อมูลประกาศเกษียณ",
      message: "ต้องการเพิ่มข้อมูลประกาศเกษียณใช่หรือไม่?",
      cancel: {
        flat: true,
        color: "negative",
      },
      persistent: true,
    })
      .onOk(async () => {
        router.push(`/retirement/list/${type.value}/${currentYear}`);
      })
      .onCancel(() => {})
      .onDismiss(() => {}); */
};
const clickClose = async () => {
  addNote.value = false;
};
const organization = ref<string>();
const organizationOptions = ref<any>([{ id: 1, name: "ทั้งหมด" }]);
const stat = ref<any>({
  total: 0,
  sendName: 0,
  nonSend: 0,
  personSend: 0,
  disclaim: 0,
});

const saveNote = async () => {
  if (saveWriteNote.value.length == 0) {
    dialogMessage(
      $q,
      "ไม่สามารถบันทึกข้อมูลได้",
      "กรุณาเพิ่มหมายเหตุ",
      "warning",
      undefined,
      "orange",
      undefined,
      undefined,
      true
    );
    return;
  } else {
    await saveTextNote();
  }
};

const saveTextNote = async () => {};

// ค้นหาในตาราง
const filterKeyword = ref<string>("");
const filterRef = ref<any>(null);
const resetFilter = () => {
  filterKeyword.value = "";
  filterRef.value.focus();
};

const filterKeyword2 = ref<string>("");
const filterRef2 = ref<any>(null);
const resetFilter2 = () => {
  filterKeyword2.value = "";
  filterRef2.value.focus();
};

const attrs = ref<any>(useAttrs());

const paging = ref<boolean>(true);
const paginationLabel = (start: string, end: string, total: string) => {
  if (paging.value == true) return " " + start + "-" + end + " ใน " + total;
  else return start + "-" + end + " ใน " + total;
};
const nextPage = (prop: any) => {
  // console.log(prop.id);

  router.push(`/retirement/listretire/${prop.id}/${type.value}`);
};
</script>
<template>
  <div class="toptitle text-dark col-12 row items-center">
    รายการที่ยื่นคำร้องขอแก้ไขข้อมูลการขอพรราชทานเหรียนจักรพรรดิมาลา
  </div>
  <q-card flat bordered class="col-12 q-mt-sm q-pa-md">
    <div class="row q-col-gutter-sm">
      <div class="row col-12 q-col-gutter-sm">
        <div>
          <div class=" col-12 row items-center">
            <div class="q-pl-sm q-mr-md text-weight-bold text-grey-6">รอบ</div>

            <q-select borderless v-model="round" :options="optionRound" />
          </div>
        </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">
        <d-table
          ref="table"
          :columns="columns"
          :rows="rows"
          :filter="filterKeyword"
          row-key="Order"
          flat
          bordered
          :paging="true"
          dense
          class="custom-header-table"
          v-bind="attrs"
          :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-tr>
          </template>
          <template v-slot:body="props">
            <q-tr :props="props" class="cursor-pointer">
              <q-td key="no" :props="props" >
                {{ props.row.no }}
              </q-td>
              <q-td key="fullname" :props="props">
                {{ props.row.fullname }}
              </q-td>
              <q-td
                key="requestType"
                :props="props"
                @click="clickEdit(props.row)"
              >
                {{ props.row.requestType }}
              </q-td>

            </q-tr>
          </template>
          <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>
        </d-table>
      </div>
    </div>
  </q-card>
</template>
