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

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

import DialogHeader from "@/components/DialogHeader.vue";
import DialogCreateCommand from "@/modules/18_command/components/DialogCreateCommand.vue";
import { title } from "process";

const modal = defineModel<boolean>("modal", { required: true });
const mixin = useCounterMixin();
const $q = useQuasar();
const { dialogConfirm, dialogMessageNotify } = mixin;

const dataMapToSend = computed(() => {
  return selected.value.map((i: any) => ({
    id: i.id,
    profileId: i.personId,
    prefix: i.prefix,
    firstName: i.firstName,
    lastName: i.lastName,
    citizenId: i.idcard,
  }));
});

const rows = ref<any[]>([]);
const props = defineProps({
  title: {
    type: String,
    default: "ส่งไปสืบสวน",
  },
  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 modalCommand = ref<boolean>(false);
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 (
    props.title == "ส่งไปพักราชการ" ||
    props.title == "ส่งไปสืบสวน" ||
    props.title == "ส่งไปสอบสวน"
  ) {
    if (selected.value.length === 0) {
      dialogMessageNotify($q, "กรุณาเลือกอย่างน้อย 1 บุคคล");
    } else {
      dialogConfirm(
        $q,
        async () => {
          emit("returnPerson", selected.value);
          modal.value = false;
        },
        `ยืนยันการ${props.title}`,
        `ต้องการยืนยันการ${props.title}หรือไม่`
      );
    }
  } else {
    if (selected.value.length === 0) {
      dialogMessageNotify($q, "กรุณาเลือกอย่างน้อย 1 บุคคล");
    } else {
      dialogConfirm(
        $q,
        async () => {
          modalCommand.value = true;
          modal.value = false;
        },
        "ยืนยันส่งไปออกคำสั่ง",
        "ต้องการยืนยันส่งไปออกคำสั่งใช่หรือไม่?"
      );
    }
  }
}

/** ปิด pop up */
function onClickClose() {
  props.close?.();
  filter.value = "";
  selected.value = [];
}

watch(
  () => modal.value,
  () => {
    if (modal.value) {
      inspectionResults.value = modal ? "" : "";
      selected.value = [];
    }
  }
);
</script>
<template>
  <q-dialog v-model="modal" persistent>
    <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 name="search" />
              </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: 140px"
              class="gt-xs q-ml-sm"
            />
          </div>
        </div>
        <d-table
          ref="table"
          :columns="props.columns"
          :rows="props.rows"
          :filter="filter.trim()"
          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-selection="scope">
            <q-checkbox
              keep-color
              color="primary"
              dense
              v-model="scope.selected"
            />
          </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"
          :disable="selected.length == 0"
        />
      </q-card-actions>
    </q-card>
  </q-dialog>

  <!-- dialog สร้างคำสั่ง -->
  <DialogCreateCommand
    v-model:modal="modalCommand"
    :command-type-code="'C-PM-32'"
    :persons="selected ? dataMapToSend : []"
  />
</template>

<style scoped></style>
