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

import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";

import DialogHeader from "@/components/DialogHeader.vue";
import CurruncyInput from "@/components/CurruncyInput.vue";

const $q = useQuasar();
const { dialogConfirm, messageError, success } = useCounterMixin();

const props = defineProps({
  getData: Function,
  selectPersonData: Object,
});
const modal = defineModel<boolean>("modal", { required: true });
const titleName = defineModel<string>("titleName", { required: true });
const commandEditRecive = defineModel<string>("commandEditRecive", {
  required: true,
});

const formData = reactive({
  salaryAmount: 0,
  positionSalaryAmount: 0,
  monthSalaryAmount: 0,
  remarkVertical: null,
  remarkHorizontal: null,
});

function onSubmit() {
  dialogConfirm($q, () => {
    http
      .put(config.API.commandEditRecive("tab2", commandEditRecive.value), {
        mouthSalaryAmount: formData.salaryAmount, //เงินเดือน
        positionSalaryAmount: formData.positionSalaryAmount, //เงินประจำตำแหน่ง
        amount: formData.monthSalaryAmount, //เงินค่าตอบแทนรายเดือน
        remarkVertical: formData.remarkVertical, //หมายเหตุแนวตั้ง
        remarkHorizontal: formData.remarkHorizontal, //หมายเหตุแนวนอน
      })
      .then((res) => {
        success($q, "บันทึกข้อมูลสำเร็จ");
        onClose();
        props.getData?.();
      })
      .catch((e) => {
        messageError($q, e);
      })
      .finally(() => {});
  });
}

function onClose() {
  modal.value = false;
  formData.salaryAmount = 0;
  formData.positionSalaryAmount = 0;
  formData.monthSalaryAmount = 0;
  formData.remarkVertical = null;
  formData.remarkHorizontal = null;
}

watch(
  () => modal.value,
  () => {
    if (modal.value) {
      if (props.selectPersonData) {
        const list = props.selectPersonData;
        formData.salaryAmount = list.mouthSalaryAmount;
        formData.positionSalaryAmount = list.positionSalaryAmount;
        formData.monthSalaryAmount = list.amount;
        formData.remarkVertical = list.remarkVertical;
        formData.remarkHorizontal = list.remarkHorizontal;
      }
    }
  }
);
</script>

<template>
  <q-dialog v-model="modal" persistent>
    <q-card style="width: 50vw; max-width: 50vw">
      <q-form q-form greedy @submit.prevent @validation-success="onSubmit">
        <DialogHeader :tittle="titleName" :close="onClose" />
        <q-separator />
        <q-card-section>
          <div class="row col-12 q-col-gutter-sm">
            <div class="col-xs-6 col-sm-6 col-md-6">
              <CurruncyInput
                :edit="true"
                :dense="true"
                v-model="formData.salaryAmount"
                :label="`${'เงินเดือน'}`"
                :rules="[(val: any) => !!val || `${'กรุณากรอกเงินเดือน'}`]"
                hide-bottom-space
              />
            </div>

            <div class="col-xs-6 col-sm-6 col-md-6">
              <CurruncyInput
                :edit="true"
                :dense="true"
                v-model="formData.positionSalaryAmount"
                :label="`${'เงินประจำตำแหน่ง'}`"
                hide-bottom-space
              />
            </div>

            <div class="col-xs-6 col-sm-6 col-md-6">
              <CurruncyInput
                :edit="true"
                :dense="true"
                v-model="formData.monthSalaryAmount"
                :label="`${'เงินค่าตอบแทนรายเดือน'}`"
                hide-bottom-space
              />
            </div>
            <div class="col-xs-6 col-sm-6 col-md-12">
              <q-input
                dense
                outlined
                v-model="formData.remarkVertical"
                label="หมายเหตุแนวตั้ง"
                type="textarea"
                rows="2"
                class="inputgreen"
              />
            </div>
            <div class="col-xs-6 col-sm-6 col-md-12">
              <q-input
                dense
                outlined
                v-model="formData.remarkHorizontal"
                type="textarea"
                label="หมายเหตุแนวนอน"
                rows="2"
                class="inputgreen"
              />
            </div>
          </div>
        </q-card-section>
        <q-separator />
        <q-card-actions align="right">
          <q-btn label="บันทึก" color="public" type="submit">
            <q-tooltip>บันทึก</q-tooltip>
          </q-btn>
        </q-card-actions>
      </q-form>
    </q-card>
  </q-dialog>
</template>

<style scoped></style>
