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

import { useCounterMixin } from "@/stores/mixin";
import { useSalaryEmployeeListSDataStore } from "@/modules/13_salary/store/SalaryEmployeeListsStore";
import http from "@/plugins/http";
import config from "@/app.config";

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

/** use*/
const $q = useQuasar();
const store = useSalaryEmployeeListSDataStore();
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 isReadonly = ref<boolean>(false); // อ่านได้อย่างเดียว

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

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

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

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

<template>
  <q-dialog v-model="modal" persistent>
    <q-card class="col-12" style="width: 30%">
      <q-form greedy @submit.prevent @validation-success="onSubmit">
        <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:string) => !!val || `${'กรุณากลุ่ม'}`]"
              lazy-rules
              hide-bottom-space
            />
          </div>
        </q-card-section>
        <q-separator />

        <q-card-actions align="right">
          <q-btn
            type="submit"
            for="#submitForm"
            color="secondary"
            label="บันทึก"
          />
        </q-card-actions>
      </q-form>
    </q-card>
  </q-dialog>
</template>

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