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

import { useCounterMixin } from "@/stores/mixin";
import { useComplainstDataStore } from "@/modules/11_discipline/store/ComplaintsStore";

const mixin = useCounterMixin();
const $q = useQuasar();

const { dialogConfirm } = mixin;

const props = defineProps({
  title: {
    type: String,
    default: "ส่งไปสืบสวน",
  },
  modal: {
    type: Boolean,
    require: true,
  },
  close: {
    type: Function,
    require: true,
  },
  sentApprove: {
    type: Function,
    default: () => "",
  },
  rows: {
    type: Array,
    default: [],
  },
  columns: {
    type: Array,
    default: [],
  },
  visibleColumns: {
    type: Array,
    default: [],
  },
});

const emit = defineEmits(["returnPerson"]);
/** หัวตาราง */


const selected = ref<any>([]);
const inspectionResults = ref<string>("");
const filter = ref<string>("");
// const inputRef = ref<any>(null);

const initialPagination = ref<any>({
  descending: false,
  rowsPerPage: 25,
});

function onclickSend() {
    dialogConfirm(
      $q,
      async () => {
        // success($q, `ส่งข้อมูล${props.title}สำเร็จ`);
        emit("returnPerson", selected.value);
        props.close?.();
      },
      `ยืนยันการส่ง${props.title}`,
      `ต้องการยืนยันการส่ง${props.title}หรือไม่`
    );
  
}

function onClickClose() {
  props.close?.();
}

watch([() => props.modal], () => {
  inspectionResults.value = props.modal ? "" : "";
  selected.value = props.modal ? [] : [];
  if (props.modal === true) {
    selected.value = 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"
                />
              </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-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"
                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>
                  {{ col.value }}
                </div>
              </q-td>
            </q-tr>
          </template>
        </d-table>
        <!-- <div class="col-xs-12 col-sm-12 q-mt-sm">
          <q-input
            for="inputInspectionResults"
            ref="inputRef"
            dense
            outlined
            hide-bottom-space
            v-model="inspectionResults"
            :rules="[(val) => !!val || 'กรุณากรอกผลการตรวจสอบเรื่องร้องเรียน']"
            lazy-rules
            label="ผลการตรวจสอบเรื่องร้องเรียน"
            type="textarea"
            rows="5"
          />
        </div> -->
      </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>
