<script setup lang="ts">
import { onMounted, ref } from "vue";
import type { QTableProps } from "quasar";
import { useQuasar } from "quasar";
import type {
  RowListForm,
  ListMenu,
} from "@/modules/01_metadataNew/interface/request/position";

import http from "@/plugins/http";
import config from "@/app.config";

import DialogAdd from "@/modules/01_metadataNew/components/position/DialogAddExecutive.vue";
import { useCounterMixin } from "@/stores/mixin";

const $q = useQuasar();
const mixin = useCounterMixin();
const {
  dialogConfirm,
  showLoader,
  hideLoader,
  messageError,
  success,
  dialogRemove,
} = mixin;

const dataEdit = ref<any>();
const isEdit = ref<boolean>(false);
const modalAdd = ref<boolean>(false);
const filterKeyword = ref<string>("");
const rows = ref<RowListForm[]>([]);

const listMenu = ref<ListMenu[]>([
  {
    label: "แก้ไข",
    icon: "mdi-pencil",
    type: "copy",
    color: "teal-6",
  },
  {
    label: "ลบ",
    icon: "delete",
    type: "remove",
    color: "red",
  },
]);

const visibleColumns = ref<string[]>([
  "no",
  "posExecutiveName",
  "posExecutivePriority",
]);
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: false,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "posExecutiveName",
    align: "left",
    label: "ชื่อตำแหน่งทางการบริหาร",
    sortable: true,
    field: "posExecutiveName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "posExecutivePriority",
    align: "left",
    label: "ลำดับความสำคัญ",
    sortable: true,
    field: "posExecutivePriority",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);

function getData() {
  showLoader();
  http
    .get(config.API.orgPosExecutive)
    .then((res) => {
      const dataList = res.data.result;
      rows.value = dataList;
    })
    .catch((e) => {
      messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
}
function popUpAdd() {
  modalAdd.value = true;
}

function editPopUp(data: RowListForm[]) {
  dataEdit.value = data;
  modalAdd.value = true;
  isEdit.value = true;
}

function deletePos(id: string) {
  dialogRemove($q, () => {
    showLoader();
    http
      .delete(config.API.orgPosExecutiveById(id))
      .then(() => {
        success($q, "ลบข้อมูลสำเร็จ");
        getData();
      })
      .catch((err) => {
        messageError($q, err);
      })
      .finally(() => {
        hideLoader();
      });
  });
}

onMounted(() => {
  getData();
});
</script>

<template>
  <div class="row col-12 q-col-gutter-x-sm">
    <div class="col-2">
      <q-btn
        size="12px"
        flat
        round
        color="primary"
        icon="mdi-plus"
        @click="popUpAdd()"
      >
        <q-tooltip>เพิ่มตำแหน่งทางการบริหาร</q-tooltip>
      </q-btn>
    </div>

    <q-space />
    <q-input outlined dense v-model="filterKeyword" label="ค้นหา"></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"
    />
  </div>
  <div class="col-12 q-mt-sm">
    <d-table
      ref="table"
      :columns="columns"
      :rows="rows"
      row-key="id"
      flat
      bordered
      :paging="true"
      dense
      class="custom-header-table"
      :visible-columns="visibleColumns"
    >
      <template v-slot:header="props">
        <q-tr :props="props">
          <q-th
            v-for="col in props.cols"
            :key="col.name"
            :props="props"
            style="color: #000000; font-weight: 500"
          >
            <span class="text-weight-medium">{{ col.label }}</span>
          </q-th>
          <q-th auto-width></q-th>
        </q-tr>
      </template>
      <template v-slot:body="props">
        <q-tr :props="props" class="cursor-pointer">
          <q-td v-for="col in props.cols" :key="col.name" :props="props">
            <div v-if="col.name == 'no'">
              {{ props.rowIndex + 1 }}
            </div>
            <div v-else>
              {{ col.value }}
            </div>
          </q-td>
          <q-td auto-width>
            <q-btn
              color="edit"
              flat
              dense
              round
              class="q-mr-xs"
              size="12px"
              icon="edit"
              clickable
              @click.stop="
                () => {
                  editPopUp(props.row);
                }
              "
            >
              <q-tooltip>แก้ไขข้อมูล</q-tooltip>
            </q-btn>
            <q-btn
              color="red"
              flat
              dense
              round
              size="12px"
              icon="mdi-delete"
              clickable
              @click.stop="deletePos(props.row.id)"
              v-close-popup
            >
              <q-tooltip>ลบข้อมูล</q-tooltip>
            </q-btn>
          </q-td>
        </q-tr>
      </template>
    </d-table>
  </div>

  <DialogAdd
    v-model:executive="modalAdd"
    v-model:form-data="dataEdit"
    v-model:edit="isEdit"
    :get-data="getData"
  />
</template>
