<script setup lang="ts">
import { ref, onMounted, computed } from "vue";
import http from "@/plugins/http";
import config from "@/app.config";
import { useQuasar } from "quasar";

/**
 * importType
 */
import type { QTableProps } from "quasar";
import type { DataPosSalary } from "@/modules/13_salary/interface/response/Main";

/**
 * importStore
 */
import { useCounterMixin } from "@/stores/mixin";

const $q = useQuasar();
const { date2Thai, showLoader, hideLoader, messageError } = useCounterMixin();

/**
 *  props
 */

const profileId = defineModel<string>("profileId", { required: true });
const employeeClass = defineModel<string>("employeeClass", { required: true });

/**
 * Table
 */
const keyword = ref<string>("");
const rows = ref<DataPosSalary[]>([]);
const baseColumns = ref<QTableProps["columns"]>([
  {
    name: "date",
    align: "left",
    label: "วัน เดือน ปี",
    sortable: true,
    field: "date",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => date2Thai(v),
  },
  {
    name: "amount",
    align: "left",
    label:
      employeeClass.value === "-employee" ? "ค่าตอบแทนรายเดือน" : "เงินเดือน",
    sortable: true,
    field: "amount",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => Number(v).toLocaleString(),
  },
  {
    name: "positionSalaryAmount",
    align: "left",
    label: "เงินประจำตำแหน่ง",
    sortable: true,
    field: "positionSalaryAmount",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => Number(v).toLocaleString(),
  },
  {
    name: "mouthSalaryAmount",
    align: "left",
    label: "เงินค่าตอบแทนรายเดือน",
    sortable: true,
    field: "mouthSalaryAmount",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => Number(v).toLocaleString(),
  },

  {
    name: "posNo",
    align: "left",
    label: "ตำแหน่งเลขที่",
    sortable: true,
    field: "posNo",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },

  {
    name: "positionType",
    align: "left",
    label: "ประเภทตำแหน่ง",
    sortable: true,
    field: "positionType",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "positionLevel",
    align: "left",
    label: "ระดับ",
    sortable: true,
    field: "positionLevel",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },

  {
    name: "templateDoc",
    align: "left",
    label: "เอกสารอ้างอิง",
    sortable: true,
    field: "templateDoc",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "refCommandNo",
    align: "left",
    label: "เลขที่คำสั่ง",
    sortable: true,
    field: "refCommandNo",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);
const columns = computed(() => {
  if (employeeClass.value === "-employee") {
    if (baseColumns.value) {
      return baseColumns.value.filter(
        (column) =>
          column.name !== "positionSalaryAmount" &&
          column.name !== "mouthSalaryAmount"
      );
    }
  }
  return baseColumns.value;
});
const visibleColumns = ref<string[]>([
  "date",
  "amount",
  "positionSalaryAmount",
  "mouthSalaryAmount",
  "posNo",
  "positionType",
  "positionLevel",
  "templateDoc",
  "refCommandNo",
]);

/**
 * function fetch ข้อมูลเงินเดือน / ค่าจ้าง
 */
function fetchListSalary() {
  showLoader();
  http
    .get(config.API.salaryListCard(profileId.value, employeeClass.value))
    .then((res) => {
      rows.value = res.data.result;
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      hideLoader();
    });
}

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

<template>
  <q-card-section class="q-pt-none">
    <div class="row items-center q-gutter-x-sm q-pb-sm">
      <q-space />
      <q-input dense outlined v-model="keyword" label="ค้นหา" class="q-mr-sm">
        <template v-slot:append>
          <q-icon v-if="keyword == ''" name="search" />
          <q-icon
            v-else
            name="clear"
            class="cursor-pointer"
            @click="keyword = ''"
          />
        </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"
        
        style="min-width: 140px"
      />
    </div>
    <div class="col-12">
      <d-table
        ref="table"
        row-key="id"
        flat
        bordered
        dense
        :filter="keyword"
        :paging="true"
        :rows-per-page-options="[20, 50, 100]"
        :visible-columns="visibleColumns"
        :rows="rows"
        :columns="columns"
      >
        <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">
            <q-td v-for="col in props.cols" :key="col.id">
              <div class="table_ellipsis">
                {{ col.value ? col.value : "-" }}
              </div>
            </q-td>
          </q-tr>
        </template>
      </d-table>
    </div>
  </q-card-section>
</template>

<style scoped></style>
