<script setup lang="ts">
import { ref, watch, reactive } from "vue";

import { useAppealComplainStore } from "@/modules/11_discipline/store/AppealComplainStore";

import type { HistoryStatusType } from "@/modules/11_discipline/interface/response/AppealComplain";
import type { QTableProps } from "quasar";

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

const mixin = useCounterMixin();
const { date2Thai } = mixin;
const dataStore = useAppealComplainStore();

const rows = ref<HistoryStatusType[]>([]);

/** หัวข้อที่เเสดงในตารางผู้ถูกร้องเรียน */
const visibleColumns = ref<string[]>([
  "no",
  "status",
  "createdFullName",
  "createdAt",
]);

/** หัวตารางผู้ถูกร้องเรียน */
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "center",
    label: "ลำดับ",
    sortable: false,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "status",
    align: "left",
    label: "สถานะ",
    sortable: false,
    field: "status",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "createdFullName",
    align: "left",
    label: "ผู้ดำเนินการ",
    sortable: false,
    field: "createdFullName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "createdAt",
    align: "left",
    label: "วันที่แก้ไข",
    sortable: false,
    field: "createdAt",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);

const props = defineProps({
  modal: {
    type: Boolean,
    default: false,
  },
  data: {
    type: Object,
    default: {},
  },
  close: {
    type: Function,
    default: () => "",
  },
});

function clickClose() {
  props.close();
}

watch(
  () => props.data,
  () => {
    rows.value = props.data.map((item: HistoryStatusType) => ({
      createdFullName: item.createdFullName,
      status: dataStore.statusTothai(item.status),
      createdAt: date2Thai(item.createdAt, false, true),
    }));
  }
);
</script>

<template>
  <q-dialog v-model="props.modal" persistent>
    <q-card style="min-width: 30vw">
      <q-toolbar>
        <q-toolbar-title class="text-subtitle2 text-bold">
          ประวัติแก้ไขสถานะ
        </q-toolbar-title>
        <q-btn
          icon="close"
          unelevated
          round
          dense
          @click="clickClose"
          style="color: #ff8080; background-color: #ffdede"
        />
      </q-toolbar>
      <q-separator />
      <q-card-section class="q-pa-md bg-grey-1">
        <q-table
          ref="table"
          :columns="columns"
          :rows="rows"
          row-key="idcard"
          flat
          bordered
          dense
          hide-bottom
          class="custom-header-table"
          :visible-columns="visibleColumns"
          :rows-per-page-options="[100]"
        >
          <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-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-tr>
          </template>
        </q-table>
      </q-card-section>
    </q-card>
  </q-dialog>
</template>
