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

/**
 * importType
 */
import type { QTableProps } from "quasar";
import type { HistoryPostType } from "@/modules/02_organization/interface/index/Main";

/**
 * importComponrnts
 */
import DialogHeader from "@/components/DialogHeader.vue";

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

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

/**
 * props
 */
const modal = defineModel<boolean>("history", { required: true });
const type = defineModel<number>("type", { required: true });
const orgId = defineModel<string>("orgId", { required: true });

/**
 * ข้อมุล Table
 */
const rows = ref<HistoryPostType[]>([]);
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: false,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "orgRevisionName",
    align: "left",
    label: "ชื่อโครงสร้าง",
    sortable: true,
    field: "orgRevisionName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "name",
    align: "left",
    label: "",
    sortable: true,
    field: "name",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },

  {
    name: "lastUpdatedAt",
    align: "left",
    label: "วันที่แก้ไข",
    sortable: true,
    field: "lastUpdatedAt",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);
const visibleColumns = ref<string[]>([
  "no",
  "name",
  "orgRevisionName",
  "lastUpdatedAt",
]);

/**
 * function เรียกข้อมูล ประวัติหน่วยงาน,ประวัติส่วนราชการ
 */
function postData() {
  showLoader();
  http
    .post(config.API.organizationHistoryPostNew, {
      id: orgId.value,
      type: type.value,
    })
    .then((res) => {
      const dataList = res.data.result;
      const dataMap = dataList.map((item: HistoryPostType) => ({
        id: item.id ? item.id : "-",
        name: item.name ? item.name : "-",
        lastUpdatedAt: item.lastUpdatedAt
          ? date2Thai(item.lastUpdatedAt as Date, false, true)
          : "-",
        orgRevisionName: item.orgRevisionName ? item.orgRevisionName : "-",
      }));
      rows.value = dataMap;
    })
    .catch((e) => {
      messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
}

/**
 * callback function  เมื่อ modal = true เรียกข้อมูล ประวัติหน่วยงาน,ประวัติส่วนราชการ
 */
watch(
  () => modal.value,
  () => {
    if (modal.value == true) {
      postData();
    }
  }
);
</script>
<template>
  <template>
    <q-dialog v-model="modal" persistent>
      <q-card style="min-width: 40vw">
        <DialogHeader
          :tittle="type == 0 ? `ประวัติหน่วยงาน` : `ประวัติส่วนราชการ`"
          :close="() => (modal = false)"
        />
        <q-separator />

        <q-card-section>
          <div class="row q-col-gutter-sm">
            <div class="col-12">
              <d-table
                ref="table"
                :columns="columns"
                :rows="rows"
                row-key="orgRevisionId"
                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
                        v-if="col.name === 'name'"
                        class="text-weight-medium"
                      >
                        {{ type === 0 ? "ชื่อหน่วยงาน" : "ส่วนราชการ" }}
                      </span>

                      <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 v-if="col.name == 'no'">
                        {{ props.rowIndex + 1 }}
                      </div>

                      <div v-else-if="col.name == 'orgRevisionIsCurrent'">
                        <q-icon
                          :name="
                            props.row.orgRevisionIsCurrent == true
                              ? 'mdi-check'
                              : ''
                          "
                          :color="
                            props.row.orgRevisionIsCurrent == true
                              ? 'positive'
                              : ''
                          "
                          class="text-h5"
                        />
                      </div>
                      <div v-else-if="col.name == 'orgRevisionIsDraft'">
                        <q-icon
                          :name="
                            props.row.orgRevisionIsDraft == true
                              ? 'mdi-check'
                              : ''
                          "
                          :color="
                            props.row.orgRevisionIsDraft == true
                              ? 'positive'
                              : ''
                          "
                          class="text-h5"
                        />
                      </div>

                      <div v-else>
                        {{ col.value }}
                      </div>
                    </q-td>
                  </q-tr>
                </template>
              </d-table>
            </div>
          </div>
        </q-card-section>
      </q-card>
    </q-dialog>
  </template>
</template>
