<script setup lang="ts">
import { ref } from "vue";
import { useQuasar, type QTableProps } from "quasar";

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

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

const $q = useQuasar();
const { dialogConfirm } = useCounterMixin();

const modal = defineModel<boolean>("modal", { required: true });

const selected = ref<any[]>([]);
const rows = ref<any[]>([
  {
    idCard: "6796519798790",
    fullName: "ว่าที่ร้อยตรีวรรนิมมามานา วงศ์วโรทัย",
    salaryAmount: 0,
  },
  {
    idCard: "7707218775803",
    fullName: "นายศรัณย์ ศิลาดี",
    salaryAmount: 1,
  },
]);
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    field: (row) => rows.value.indexOf(row) + 1,
    sortable: false,
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "idCard",
    align: "left",
    label: "เลขประจำตัวประชาชน",
    field: "idCard",
    sortable: true,
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "fullName",
    align: "left",
    label: "ชื่อ-นามสกุล",
    field: "fullName",
    sortable: true,
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);

function onSubmit() {
  dialogConfirm($q, () => {
    onClose();
  });
}

function onClose() {
  modal.value = false;
}
</script>

<template>
  <q-dialog v-model="modal" persistent>
    <q-card style="width: 50vw; max-width: 50vw">
      <DialogHeader tittle="รายชื่อในการออกคำสั่ง" :close="onClose" />
      <q-separator />
      <q-card-section style="max-height: 60vh">
        <d-table
          :rows="rows"
          :columns="columns"
          row-key="name"
          :selection="'multiple'"
          v-model:selected="selected"
        >
          <template v-slot:header="props">
            <q-tr :props="props">
              <q-th auto-width>
                <q-checkbox
                  keep-color
                  color="primary"
                  dense
                  v-model="props.selected"
                />
              </q-th>
              <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>
                <q-checkbox
                  keep-color
                  color="primary"
                  dense
                  v-model="props.selected"
                />
              </q-td>
              <q-td v-for="col in props.cols" :key="col.name" :props="props">{{
                col.name == "no" ? props.rowIndex + 1 : col.value
              }}</q-td>
            </q-tr>
          </template>
        </d-table>
      </q-card-section>
      <q-separator />
      <q-card-actions align="right">
        <q-btn label="บันทึก" color="public" @click="onSubmit"> </q-btn>
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<style scoped></style>
