<script setup lang="ts">
import { ref, onMounted } from "vue";
import router from "@/router";
/** importType */
import type { QTableProps } from "quasar";
/** importStroe */
import { useOrderStore } from "@/modules/11_discipline/store/OrderStore";

const OrderStore = useOrderStore();

/** รับ props มาจากหน้าหลัก */
const props = defineProps({
  filterTable: {
    type: String,
    default: "",
  },
});

/** ข้อมูลที่เเสดงใน คอลัม */
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ ",
    sortable: true,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "orderName",
    align: "left",
    label: "คำสั่ง",
    sortable: true,
    field: "orderName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },

  {
    name: "orderNo",
    align: "left",
    label: "เลขที่คำสั่ง",
    sortable: true,
    field: "orderNo",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "faultLevel",
    align: "left",
    label: "ระดับความผิด",
    sortable: true,
    field: "faultLevel",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "caseFault",
    align: "left",
    label: "กรณีความผิด",
    sortable: true,
    field: "caseFault",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "fullName",
    align: "left",
    label: "รายชื่อผู้ที่ถูกลงโทษทางวินัย",
    sortable: true,
    field: "fullName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "result",
    align: "left",
    label: "ผลดำเนินการพิจารณา",
    sortable: true,
    field: "result",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "refRaw",
    align: "left",
    label: "อ้างอิงมาตราตามกฎหมาย",
    sortable: true,
    field: "refRaw",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },

  {
    name: "orderTypeName",
    align: "left",
    label: "ประเภท",
    sortable: false,
    field: "orderTypeName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "orderDate",
    align: "left",
    label: "สั่ง ณ วันที่/วันที่คำสั่งมีผล",
    sortable: true,
    field: "orderDate",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a, b) => parseInt(a) - parseInt(b),
  },
  {
    name: "orderByOrganization",
    align: "left",
    label: "คำสั่งโดย",
    sortable: true,
    field: "orderByOrganization",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "signatoryBy",
    align: "left",
    label: "ผู้ลงนาม",
    sortable: true,
    field: "signatoryBy",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "orderStatusName",
    align: "center",
    label: "สถานะคำสั่ง",
    sortable: true,
    field: "orderStatusName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
]);
/** หัวตาราง */
const visibleColumns = ref<string[]>([
  "no",
  "orderName",
  "orderNo",
  "faultLevel",
  "caseFault",
  "fullName",
  "result",
  "refRaw",
  "orderTypeName",
  "orderDate",
  "orderByOrganization",
  "signatoryBy",
  "orderStatusName",
]);

/** โหลดข้อมูลเมื่อเข้าหน้านี้ */
onMounted(() => {
  OrderStore.columns = columns.value;
  OrderStore.visibleColumns = visibleColumns.value;
});

/** pagination */
const pagination = ref({
  descending: true,
  page: 1,
  rowsPerPage: 10,
});

// redirect ไปยัง step ปัจจุบัน
const redirectToPage = (id?: string, status?: string) => {
  let step = 1;
  switch (status) {
    case "จัดทำร่างคำสั่ง":
      step = 1;
      break;
    case "บัญชีแนบท้าย":
      step = 2;
      break;
    case "เลือกผู้ได้รับสำเนาคำสั่ง":
      step = 3;
      break;
    default:
      step = 4;
      break;
  }
  router.push(`/discipline-order/detail/${id}?step=${step}`);
};
</script>

<template>
  <d-table
    for="table"
    ref="table"
    :columns="OrderStore.columns"
    :rows="OrderStore.rows"
    :filter="props.filterTable"
    row-key="subject"
    flat
    bordered
    dense
    class="custom-header-table"
    :visible-columns="OrderStore.visibleColumns"
    v-model:pagination="pagination"
  >
    <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-td
        v-for="col in props.cols"
        :key="col.name"
        :props="props"
        @click="redirectToPage(props.row.orderId, props.row.OrderStatus)"
      >
        <div v-if="col.name === 'no'">
          {{ props.rowIndex + 1 }}
        </div>
        <div>
          {{ col.value }}
        </div>
      </q-td>
    </template>
  </d-table>
</template>

<style scoped></style>
