<script setup lang="ts">
import { ref, watch } from "vue";
import { useQuasar } from "quasar";
import DialogHeader from "@/components/DialogHeader.vue";

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

const mixin = useCounterMixin();
const $q = useQuasar();
const { dialogConfirm, dialogMessageNotify } = mixin;
const props = defineProps({
  title: {
    type: String,
    default: "ส่งไปสืบสวน",
  },
  modal: {
    type: Boolean,
    require: true,
  },
  close: {
    type: Function,
    require: true,
  },
  sentApprove: {
    type: Function,
    default: () => "",
  },
  fetchData: {
    type: Function,
    default: () => console.log("function fetchData"),
  },
  rows: {
    type: Array,
    default: [],
  },
  columns: {
    type: Array,
    default: [],
  },
  visibleColumns: {
    type: Array,
    default: [],
  },
  checkedVal: {
    type: Boolean,
    default: true,
  },
});

const emit = defineEmits(["returnPerson"]);
const selected = ref<any>([]);
const inspectionResults = ref<string>("");
const filter = ref<string>("");
const initialPagination = ref<any>({
  descending: false,
  rowsPerPage: 25,
});

/** ฟังชั่นส่งคนไปออกคำสั่ง */
function onclickSend() {
  if (selected.value.length === 0) {
    dialogMessageNotify($q, "กรุณาเลือกอย่างน้อย 1 บุคคล");
  } else {
    dialogConfirm(
      $q,
      async () => {
        await emit("returnPerson", selected.value);
        props.fetchData();
        props.close?.();
      },
      `ยืนยันการ${props.title}`,
      `ต้องการยืนยันการ${props.title}หรือไม่`
    );
  }
}

/** ปิด pop up */
function onClickClose() {
  props.close?.();
}

watch([() => props.modal], () => {
  inspectionResults.value = props.modal ? "" : "";
  selected.value = props.modal ? [] : [];
  if (props.modal === true) {
    selected.value = props.checkedVal ? props.rows : [];
  }
});
</script>
<template>
  <q-dialog v-model="props.modal">
    <q-card style="width: 820px; max-width: 80vw">
      <DialogHeader :tittle="props.title" :close="onClickClose" />

      <q-separator />

      <q-card-section class="q-pt-none q-mt-md">
        <div class="col-12 row q-pb-sm items-center">
          <q-space />
          <div class="items-center" style="display: flex">
            <!-- ค้นหาข้อความใน table -->
            <q-input
              for="inputfilterRef"
              standout
              dense
              v-model="filter"
              outlined
              debounce="300"
              placeholder="ค้นหา"
              style="max-width: 200px"
              class="q-ml-sm"
            >
              <template v-slot:append>
                <q-icon v-if="filter == ''" name="search" />
                <q-icon
                  v-if="filter !== ''"
                  name="clear"
                  class="cursor-pointer"
                  @click="filter = ''"
                />
              </template>
            </q-input>
            <!-- แสดงคอลัมน์ใน table -->
            <q-select
              for="selectVisibleColumns"
              v-model="props.visibleColumns"
              :display-value="$q.lang.table.columns"
              multiple
              outlined
              dense
              :options="props.columns"
              options-dense
              option-value="name"
              map-options
              emit-value
              style="min-width: 150px"
              class="gt-xs q-ml-sm"
            />
          </div>
        </div>
        <d-table
          ref="table"
          :columns="props.columns"
          :rows="props.rows"
          :filter="filter"
          row-key="personId"
          flat
          bordered
          :paging="false"
          dense
          class="custom-header-table"
          :visible-columns="props.visibleColumns"
          selection="multiple"
          v-model:selected="selected"
          :pagination="initialPagination"
        >
          <template v-slot:header="props">
            <q-tr :props="props">
              <q-th auto-width> </q-th>
              <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>
                <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">
                <div v-if="col.name == 'no'">
                  {{ props.rowIndex + 1 }}
                </div>
                <div v-else-if="col.name === 'salary'">
                  {{ props.row.salary.toLocaleString() }}
                </div>
                <div v-else>
                  {{ col.value }}
                </div>
              </q-td>
            </q-tr>
          </template>
        </d-table>
      </q-card-section>
      <q-separator />
      <q-card-actions align="right" class="bg-white text-teal">
        <q-btn :label="props.title" color="public" @click="onclickSend" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<style scoped></style>
