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

import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";
import { useDataLinkCenter } from "@/modules/04_registryPerson/stores/LinkCenter";
import { useProfileDataStore } from "@/modules/04_registryPerson/stores/profile";

import type { FormPerson } from "@/modules/04_registryPerson/interface/index/family";

import Header from "@/components/DialogHeader.vue"; //ส่วนหัว popup

const $q = useQuasar();
const mixin = useCounterMixin();
const storeLinkCenter = useDataLinkCenter();
const storeProfile = useProfileDataStore();
const {
  showLoader,
  hideLoader,
  messageError,
  dialogConfirm,
  modalError,
  success,
} = mixin;

const props = defineProps({
  fetchData: Function,
  requestId: String,
});

const modal = defineModel<boolean>("modal", { required: true }); // ตัวแปร popup
const profileId = defineModel<string>("profileId", { required: true }); //id บุคคล

const motherData = reactive<FormPerson>({
  isLive: 1,
  citizenId: "",
  prefix: "",
  firstName: "",
  lastName: "",
  job: "",
});

/** ปิด popup */
async function closeDialog() {
  modal.value = false;

  motherData.isLive = 1;
  motherData.citizenId = "";
  motherData.prefix = "";
  motherData.firstName = "";
  motherData.lastName = "";
  motherData.job = "";
}

async function onSubmit() {
  dialogConfirm($q, async () => {
    const body = {
      profileId: profileId.value,
      motherCitizenId: motherData.citizenId,
      motherPrefix: motherData.prefix,
      motherFirstName: motherData.firstName,
      motherLastName: motherData.lastName,
      motherCareer: motherData.job,
      motherLive: motherData.isLive === 1 ? true : false,
    };
    http
      .post(config.API.profileFamily("", "mother"), body)
      .then(async (res) => {
        await upDateStatus();
        success($q, "บันทึกข้อมูลสำเร็จ");
        closeDialog();
        await props.fetchData?.();
      })
      .catch((e) => {
        messageError($q, e);
        hideLoader();
      })
      .finally(() => {});
  });
}

/** ส่ง สถานะ ดำเนินการเเล้ว */
async function upDateStatus() {
  await http
    .patch(config.API.requestEdit + `${props.requestId}`, {
      status: "COMPLETE",
      remark: "",
    })
    .then(async () => {
      await props.fetchData?.();
      await success($q, "บันทึกข้อมูลสำเร็จ");
      closeDialog();
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      hideLoader();
    });
}
</script>
<template>
  <q-dialog v-model="modal" persistent>
    <q-card class="col-12" style="width: 80vw">
      <q-form greedy @submit.prevent @validation-success="onSubmit">
        <Header tittle="ขอปรับปรุงข้อมูลมารดา" :close="closeDialog" />
        <q-separator />

        <q-card-section class="scroll" style="max-height: 80vh">
          <div class="row q-col-gutter-sm">
            <div class="col-12">
              <div class="row col-12 q-col-gutter-x-xs q-col-gutter-y-xs">
                <div class="col-xs-6 col-sm-6 col-md-3">
                  <q-input
                    outlined
                    hide-bottom-space
                    bg-color="white"
                    dense
                    v-model="motherData.citizenId"
                    :label="`${'เลขประจำตัวประชาชน'}`"
                    maxlength="13"
                    :rules="[
                          (val:string) => !!val || 'กรุณากรอกเลขบัตรประชาชน',
                          (val:string) =>
                            val.length === 13 ||
                            'กรุณากรอกเลขบัตรประชาชนให้ครบ 13 หลัก',
                        ]
                  "
                    class="inputgreen"
                    mask="#############"
                  />
                </div>
                <div class="col-xs-6 col-sm-6 col-md-2">
                  <q-select
                    outlined
                    bg-color="white"
                    lazy-rules
                    hidden-space
                    hide-bottom-space
                    dense
                    class="inputgreen"
                    :rules="
[(val:string) => !!val || 'กรุณาเลือกคำนำหน้าชื่อ']

                  "
                    label="คำนำหน้าชื่อ"
                    v-model="motherData.prefix"
                    use-input
                    input-debounce="0"
                    emit-value
                    map-options
                    :options="storeLinkCenter.OpsPerson.prefixOps"
                    option-label="name"
                    option-value="name"
                    @filter="(inputValue:string,
                    doneFn:Function) => storeProfile.filterSelector(inputValue, doneFn,'prefixOps'
                    ) "
                  />
                </div>
                <div class="col-xs-6 col-sm-6 col-md-2">
                  <q-input
                    outlined
                    dense
                    hide-bottom-space
                    bg-color="white"
                    class="inputgreen"
                    lazy-rules
                    v-model="motherData.firstName"
                    :rules="
[(val:string) => !!val || 'กรุณากรอกชื่อ']

                  "
                    label="ชื่อ"
                    hidden-space
                  />
                </div>
                <div class="col-xs-6 col-sm-6 col-md-2">
                  <q-input
                    outlined
                    dense
                    class="inputgreen"
                    bg-color="white"
                    hide-bottom-space
                    lazy-rules
                    v-model="motherData.lastName"
                    :rules="
[(val:string) => !!val || 'กรุณากรอกนามสกุล']

                  "
                    label="นามสกุล"
                  />
                </div>
                <div class="col-xs-6 col-sm-6 col-md-3">
                  <q-input
                    outlined
                    dense
                    class="inputgreen"
                    bg-color="white"
                    v-model="motherData.job"
                    label="อาชีพ"
                  />
                </div>
                <div class="col-12">
                  <div class="row q-my-xs">
                    <div class="q-mr-sm">สถานภาพการมีชีวิต</div>
                    <div>
                      <q-radio
                        class="q-mr-sm"
                        v-model="motherData.isLive"
                        checked-icon="task_alt"
                        unchecked-icon="panorama_fish_eye"
                        :val="1"
                        label="ยังมีชีวิตอยู่"
                        dense
                        :color="!motherData.isLive ? 'grey' : 'primary'"
                      />
                      <q-radio
                        v-model="motherData.isLive"
                        checked-icon="task_alt"
                        unchecked-icon="panorama_fish_eye"
                        :val="0"
                        :color="!motherData.isLive ? 'grey' : 'red'"
                        label="ถึงแก่กรรม"
                        dense
                      />
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </q-card-section>
        <q-separator />

        <q-card-actions align="right">
          <q-btn
            label="บันทึกลงทะเบียนประวัติ"
            color="secondary"
            type="submit"
          />
          <q-btn label="ยกเลิก" color="orange" @click="modal = false"
            ><q-tooltip>ยกเลิก</q-tooltip></q-btn
          >
        </q-card-actions>
      </q-form>
    </q-card>
  </q-dialog>
</template>
