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

import type { QTableProps } from "quasar";

import { useSalaryEmployeeListSDataStore } from "@/modules/13_salary/store/SalaryEmployeeListsStore";

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

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

const props = defineProps({
  year: Number,
  snapShot: String,
  roundFilter: Object,
});

/** itemsCard*/
const itemsCardAPR = ref([
  {
    lable: "จำนวนคนทั้งหมด",
    name: "group1",
    color: "secondary",
    total: 0,
  },
  {
    lable: "15% ของจำนวนคน",
    name: "group2",
    color: "light-blue-4",
    total: 0,
  },
  {
    lable: "เลือกไปแล้ว",
    name: "group2",
    color: "primary",
    total: 0,
  },
  {
    lable: "คงเหลือโควตา",
    name: "group2",
    color: "indigo-6",
    total: 0,
  },
  {
    lable: "สำรอง",
    name: "group2",
    color: "red-6",
    total: 0,
  },
]);
const itemsCardOCT = ref([
  {
    lable: "จำนวนเงินคนครองปัจจุบัน",
    name: "group1",
    color: "secondary",
    total: 0,
  },

  {
    lable: "วงเงิน 6%",
    name: "group2",
    color: "light-blue-4",
    total: 0,
  },
  {
    lable: "ยอดเงินที่ใช้ไป",
    name: "group2",
    color: "primary",
    total: 0,
  },
  {
    lable: "วงเงิน 6%-ยอดเงินที่ใช้ไป",
    name: "group2",
    color: "indigo-6",
    total: 0,
  },
  {
    lable: "ใช้ไปเท่าไหร่",
    name: "group2",
    color: "blue-6",
    total: 0,
  },
  {
    lable: "เหลือเท่าไหร่",
    name: "group2",
    color: "green-6",
    total: 0,
  },
  {
    lable: "สำรอง",
    name: "group2",
    color: "red-6",
    total: 0,
  },
]);

const itemsCardSpeciel = ref([
  {
    lable: "จำนวนคนทั้งหมด",
    name: "group1",
    color: "secondary",
    total: 0,
  },
  {
    lable: "เลือกไปแล้ว",
    name: "group2",
    color: "primary",
    total: 0,
  },
  {
    lable: "สำรอง",
    name: "group2",
    color: "red-6",
    total: 0,
  },
]);

const itemsCard = computed(() => {
  const items =
    store.roundMainCode === "APR"
      ? itemsCardAPR.value
      : store.roundMainCode === "OCT"
      ? itemsCardOCT.value
      : itemsCardSpeciel.value;
  return items;
});

const rows = ref<any>([]);
const columnsAPR = ref<QTableProps["columns"]>([
  {
    name: "org",
    align: "left",
    label: "หน่วยงาน",
    sortable: true,
    field: "org",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "total",
    align: "left",
    label: "จำนวนคนทั้งหมด",
    sortable: true,
    field: "total",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => Number(v).toLocaleString(),
  },

  {
    name: "fifteenPercent",
    align: "left",
    label: "15% ของจำนวนคน",
    sortable: true,
    field: "fifteenPercent",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) =>
      Number(v).toLocaleString("en", {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2,
      }),
  },
  {
    name: "chosen",
    align: "left",
    label: "เลือกไปแล้ว",
    sortable: true,
    field: "chosen",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => Number(v).toLocaleString(),
  },
  {
    name: "remaining",
    align: "left",
    label: "คงเหลือโควตา",
    sortable: true,
    field: "remaining",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => Number(v).toLocaleString(),
  },
  {
    name: "totalBackup",
    align: "left",
    label: "สำรอง",
    sortable: true,
    field: "totalBackup",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => Number(v).toLocaleString(),
  },
]);
const columnsOCT = ref<QTableProps["columns"]>([
  {
    name: "org",
    align: "left",
    label: "หน่วยงาน",
    sortable: true,
    field: "org",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "currentAmount",
    align: "left",
    label: "จำนวนเงินคนครองปัจจุบัน",
    sortable: true,
    field: "currentAmount",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) =>
      Number(v).toLocaleString("en", {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2,
      }),
  },

  {
    name: "sixPercentAmount",
    align: "left",
    label: "วงเงิน 6%",
    sortable: true,
    field: "sixPercentAmount",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) =>
      Number(v).toLocaleString("en", {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2,
      }),
  },
  {
    name: "spentAmount",
    align: "left",
    label: "ยอดเงินที่ใช้ไป",
    sortable: true,
    field: "spentAmount",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) =>
      Number(v).toLocaleString("en", {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2,
      }),
  },
  {
    name: "sixPercentSpentAmount",
    align: "left",
    label: "วงเงิน 6%-ยอดเงินที่ใช้ไป",
    sortable: true,
    field: "sixPercentSpentAmount",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) =>
      Number(v).toLocaleString("en", {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2,
      }),
  },

  {
    name: "useAmount",
    align: "left",
    label: "ใช้ไปเท่าไหร่",
    sortable: true,
    field: "useAmount",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) =>
      Number(v).toLocaleString("en", {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2,
      }),
  },
  {
    name: "remainingAmount",
    align: "left",
    label: "เหลือเท่าไหร่",
    sortable: true,
    field: "remainingAmount",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) =>
      Number(v).toLocaleString("en", {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2,
      }),
  },
  {
    name: "totalBackup",
    align: "left",
    label: "สำรอง",
    sortable: true,
    field: "totalBackup",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => Number(v).toLocaleString(),
  },
]);

const columnsSpeciel = ref<QTableProps["columns"]>([
  {
    name: "org",
    align: "left",
    label: "หน่วยงาน",
    sortable: true,
    field: "org",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "total",
    align: "left",
    label: "จำนวนคนทั้งหมด",
    sortable: true,
    field: "total",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => Number(v).toLocaleString(),
  },

  {
    name: "chosen",
    align: "left",
    label: "เลือกไปแล้ว",
    sortable: true,
    field: "chosen",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => Number(v).toLocaleString(),
  },

  {
    name: "totalBackup",
    align: "left",
    label: "สำรอง",
    sortable: true,
    field: "totalBackup",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => Number(v).toLocaleString(),
  },
]);
const columns = computed(() => {
  const columnsss =
    store.roundMainCode === "APR"
      ? columnsAPR.value
      : store.roundMainCode === "OCT"
      ? columnsOCT.value
      : columnsSpeciel.value;
  return columnsss;
});
const visibleColumns = ref<string[]>(
  store.roundMainCode === "APR"
    ? ["org", "total", "fifteenPercent", "chosen", "remaining", "totalBackup"]
    : store.roundMainCode === "OCT"
    ? [
        "org",
        "currentAmount",
        "sixPercentAmount",
        "spentAmount",
        "sixPercentSpentAmount",
        "useAmount",
        "remainingAmount",
        "totalBackup",
      ]
    : []
);

function fetchDataDashboard() {
  showLoader();
  const formData = {
    year: props?.year,
    group: "GROUP1",
    period: props?.roundFilter?.id,
    snapshot: props?.snapShot,
  };
  http
    .post(config.API.salaryDashboardEmp, formData)
    .then((res) => {
      const quota = res.data.result.dashboard;
      itemsCardAPR.value[0].total = quota.total;
      itemsCardAPR.value[1].total = quota.fifteenPercent.toLocaleString("en", {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2,
      });
      itemsCardAPR.value[2].total = quota.chosen;
      itemsCardAPR.value[3].total = quota.remaining;
      itemsCardAPR.value[4].total = quota.totalBackup;

      itemsCardOCT.value[0].total = quota.currentAmount;
      itemsCardOCT.value[1].total = quota.sixPercentAmount;
      itemsCardOCT.value[2].total = quota.spentAmount;
      itemsCardOCT.value[3].total = quota.sixPercentSpentAmount;
      itemsCardOCT.value[4].total = quota.useAmount;
      itemsCardOCT.value[5].total = quota.remainingAmount;
      itemsCardOCT.value[6].total = quota.totalBackup;

      itemsCardSpeciel.value[0].total = quota.total;
      itemsCardSpeciel.value[1].total = quota.chosen;
      itemsCardSpeciel.value[2].total = quota.totalBackup;

      rows.value = res.data.result.salaryOrg;
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      hideLoader();
    });
}
onMounted(() => {
  fetchDataDashboard();
});

watch([() => props?.snapShot, () => props.roundFilter], () => {
  fetchDataDashboard();
});
</script>

<template>
  <!-- Card โควต้า -->

  <div class="row col-12 q-pa-md bg-grey-2">
    <div class="row col-12 q-col-gutter-sm">
      <div
        v-for="(item, index) in itemsCard"
        :key="index"
        :class="
          store.roundMainCode === 'APR'
            ? 'col-6 col-sm-4 col-md-3 col-lg-2'
            : 'col-3'
        "
      >
        <q-card>
          <q-card-section>
            <div class="row items-center no-wrap">
              <div class="col">
                <div class="">{{ item.lable }}</div>
              </div>

              <div :class="`text-${item.color} text-bold`">
                {{
                  item.total
                    ? store.roundMainCode === "OCT"
                      ? item.total.toLocaleString("en", {
                          minimumFractionDigits: 2,
                          maximumFractionDigits: 2,
                        })
                      : item.total
                    : 0
                }}
              </div>
            </div>
          </q-card-section>
        </q-card>
      </div>
      <!-- <div class="row col justify-end self-center">
          <q-btn-dropdown color="blue-5" label="ดาวน์โหลด">
            <q-list>
              <q-item
                v-for="(item, index) in store.roundMainCode === 'APR'
                  ? store.itemDownloadApr
                  : store.roundMainCode === 'OCT'
                  ? store.itemDownloadOct
                  : []"
                :key="index"
                clickable
                v-close-popup
                @click="onClickDownload(item)"
              >
                <q-item-section>
                  <q-item-label>{{ item.name }}</q-item-label>
                </q-item-section>
              </q-item>
            </q-list>
          </q-btn-dropdown>
        </div> -->
    </div>
  </div>
  <q-separator />
  <div class="col-12 q-pa-md">
    <d-table
      for="table"
      ref="table"
      :columns="columns"
      :rows="rows"
      row-key="id"
      flat
      bordered
      dense
      class="custom-header-table"
    >
      <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 v-for="col in props.cols" :key="col.name" :props="props">
            <div class="table_ellipsis">
              {{ col.value }}
            </div>
          </q-td>
        </q-tr>
      </template>
    </d-table>
  </div>
</template>

<style scoped></style>
