<script setup lang="ts">
import { ref, defineModel, watch } from "vue";
import { useQuasar } from "quasar";
import http from "@/plugins/http";
import config from "@/app.config";

/** importComponents*/
import Header from "@/components/DialogHeader.vue";

/** importStore*/
import { useCounterMixin } from "@/stores/mixin";
import { useSalaryListSDataStore } from "@/modules/13_salary/store/SalaryListsStore";

/** use*/
const $q = useQuasar();
const store = useSalaryListSDataStore();
const { dialogConfirm, success, messageError, showLoader, hideLoader } =
  useCounterMixin();

/** props*/
const modal = defineModel<boolean>("modal", { required: true });
const profileId = defineModel<string>("profileId", { required: true });
const props = defineProps({
  group: { type: String },
  fetchData: {
    type: Function,
  },
});

/** ตัวแปร*/
const group = ref<string>("");
const groupRef = ref<any>(null);
const isReadonly = ref<boolean>(false); // อ่านได้อย่างเดียว

/*** ฟังก์ชั่นสำหรับ validate ฟอร์ม */
function validateForm() {
  if (groupRef.value.validate()) {
    onSubmit();
  }
}

/** function ปืด Popup */
function close() {
  modal.value = false;
  group.value = "";
}

/** function ยืนยันการบันทึกข้อมูล*/
function onSubmit() {
  dialogConfirm($q, () => {
    showLoader();
    const body = {
      profileId: profileId.value,
      groupId: group.value,
    };
    http
      .post(config.API.salaryPeriod() + `/change/group`, body)
      .then(() => {
        success($q, "บันทึกข้อมูลสำเร็จ");
        props.fetchData?.();
      })
      .catch((e) => {
        messageError($q, e);
      })
      .finally(() => {
        hideLoader();
        close();
      });
  });
}

watch(
  () => modal.value,
  () => {
    if (modal.value) {
      group.value =
        props.group === "กลุ่ม1" ? store.groupOp[1].id : store.groupOp[0].id;
    }
  }
);

function inputEdit(val: boolean) {
  return {
    "full-width cursor-pointer inputgreen ": val,
    "full-width cursor-pointer inputgreen": !val,
  };
}
</script>

<template>
  <q-dialog v-model="modal" persistent>
    <q-card class="col-12" style="width: 30%">
      <Header :tittle="`ย้ายกลุ่ม`" :close="close" />
      <q-separator />

      <q-card-section class="scroll" style="max-height: 70vh">
        <div class="q-gutter-y-sm">
          <q-select
            ref="groupRef"
            :class="inputEdit(isReadonly)"
            v-model="group"
            label="กลุ่ม"
            dense
            outlined
            emit-value
            map-options
            option-label="name"
            option-value="id"
            :options="store.groupOp.filter((e) => e.name !== props.group)"
            :rules="[(val) => !!val || `${'กรุณากลุ่ม'}`]"
            lazy-rules
            hide-bottom-space
          />
        </div>
      </q-card-section>
      <q-separator />
      <form @submit.prevent="validateForm">
        <q-card-actions align="right" class="bg-white text-teal">
          <!-- <q-btn flat label="OK" v-close-popup /> -->
          <q-btn
            type="submit"
            for="#submitForm"
            unelevated
            dense
            class="q-px-md items-center"
            color="light-blue-10"
            label="บันทึก"
          />
        </q-card-actions>
      </form>
    </q-card>
  </q-dialog>
</template>

<style lang="scss" scoped></style>
