<script setup lang="ts">
import { onMounted, ref } from "vue";
import { useQuasar } from "quasar";
import { useRoute, useRouter } from "vue-router";
import { useCounterMixin } from "@/stores/mixin";

import http from "@/plugins/http";
import config from "@/app.config";
import keycloak from "@/plugins/keycloak";

import type { QTableProps, QForm } from "quasar";
// import type {
//   TypeFile,
//   ResponseDataDetail,
// } from "@/modules/05_placement/interface/response/Transfer";

const $q = useQuasar();
const route = useRoute();
const router = useRouter();
const mixin = useCounterMixin();
const edit = ref<boolean>(false);
const personId = route.params.id as string;
const {
  date2Thai,
  dialogMessage,
  messageError,
  showLoader,
  hideLoader,
  success,
  dialogConfirm,
} = mixin;

const roleAdmin = ref<boolean>(false);
const myForm = ref<QForm | null>(null);
const name = ref<string>("นายสมคิด ยอดใจ");
const level = ref<string>("ชำนาญการพิเศษ");
const institution = ref<string>("ฝ่ายบริหารงานทั่วไป");
const fullname = ref<string>("");
const id = ref<string>("");
const prefix = ref<string>("");
const prefixId = ref<string>("");
const firstname = ref<string>("");
const citizenId = ref<string>("");
const lastname = ref<string>("");
const position = ref<string>("");
const posNo = ref<string>("");
const positionLevel = ref<string>("");
const educationOld = ref<string>("");
const createdAt = ref<string>("");
const organization = ref<string>("");
const reason = ref<string>("");
const status = ref<string>("");
const date = ref<Date | null>(null);
const amount = ref<number>();
const amountOld = ref<number>();
const positionTypeOld = ref<string>("");
const positionLevelOld = ref<string>("");
const dateOfBirth = ref<Date>();
const genderId = ref<string>("");
const nationality = ref<string>("");
const race = ref<string>("");
const religionId = ref<string>("");
const bloodGroupId = ref<string>("");
const relationshipId = ref<string>("");
const telephoneNumber = ref<string>("");
const positionNumberOld = ref<string>("");
const organizationPositionOld = ref<string>("");
const isActive = ref<string>("");
// const responseData = ref<ResponseDataDetail>({
//   createdAt: new Date(),
//   date: new Date(),
//   id: "",
//   organization: "",
//   organizationPositionOld: "",
//   positionLevelOld: "",
//   positionNumberOld: "",
//   positionTypeOld: "",
//   reason: "",
//   salary: 0,
//   status: "",
// });

// const rows = ref<TypeFile[]>([]);
// const columns = ref<QTableProps["columns"]>([
//   {
//     name: "no",
//     align: "left",
//     label: "ลำดับ",
//     sortable: true,
//     field: "no",
//     headerStyle: "font-size: 14px",
//     style: "font-size: 14px",
//   },
//   {
//     name: "fileName",
//     align: "left",
//     label: "ชื่อไฟล์",
//     sortable: true,
//     field: "fileName",
//     headerStyle: "font-size: 14px",
//     style: "font-size: 14px",
//   },
//   {
//     name: "btnMicrosoft",
//     align: "right",
//     label: "ปุ่ม",
//     sortable: true,
//     field: "btnMicrosoft",
//     headerStyle: "font-size: 14px",
//     style: "font-size: 14px",
//   },
// ]);

const getData = async () => {
  showLoader();
  await http
    .get(config.API.relocationDetail(personId))
    .then((res: any) => {
      const data = res.data.result;
      citizenId.value = data.citizenId;
      fullname.value = `${data.prefix}${data.firstname} ${data.lastname}`;
      prefixId.value = data.prefixId;
      prefix.value = data.prefix;
      firstname.value = data.firstname;
      lastname.value = data.lastname;
      educationOld.value = data.educationOld;
      position.value = data.position;
      posNo.value = data.posNo;
      positionLevel.value = data.positionLevel;
      createdAt.value = data.createdAt;
      organization.value = data.organization;
      reason.value = data.reason;
      status.value = data.status;
      date.value = data.date;
      amount.value = data.amount;
      amountOld.value = data.amountOld;
      positionTypeOld.value = data.positionTypeOld;
      positionLevelOld.value = data.positionLevelOld;
      positionNumberOld.value = data.positionNumberOld;
      organizationPositionOld.value = data.organizationPositionOld;
      dateOfBirth.value = data.dateOfBirth;
      genderId.value = data.genderId;
      nationality.value = data.nationality;
      race.value = data.race;
      religionId.value = data.religionId;
      bloodGroupId.value = data.bloodGroupId;
      relationshipId.value = data.relationshipId;
      telephoneNumber.value = data.telephoneNumber;
      isActive.value = data.isActive;
    })
    .catch((e) => {
      //   messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
};
const getClass = (val: boolean) => {
  return {
    "full-width inputgreen cursor-pointer": val,
    "full-width cursor-pointer": !val,
  };
};
//
const conditionSave = async () => {
  if (myForm.value !== null) {
    myForm.value.validate().then((success: any) => {
      if (success) {
        dialogConfirm(
          $q,
          async () => await saveData(),
          "ต้องการแก้ไขข้อมูลหรือไม่?",
          "แก้ไขข้อมูลเพื่อลงบัญชีแนบท้าย"
        );
      }
    });
  }
};
const cancelBtn = () => {
  edit.value = !edit;
  getData();
};
const saveData = async () => {
  const body = {
    educationOld: educationOld.value,
    prefixId: prefixId.value,
    citizenId: citizenId.value,
    firstname: firstname.value,
    lastname: lastname.value,
    reason: reason.value,
    organizationPositionOld: organizationPositionOld.value,
    positionTypeOld: positionTypeOld.value,
    positionLevelOld: positionLevelOld.value,
    positionNumberOld: positionNumberOld.value,
    amount: amount.value,
    amountOld: amountOld.value,
    dateOfBirth: dateOfBirth.value,
    genderId: genderId.value,
    nationality: nationality.value,
    race: race.value,
    religionId: religionId.value,
    bloodGroupId: bloodGroupId.value,
    relationshipId: relationshipId.value,
    telephoneNumber: telephoneNumber.value,
  };
  showLoader();
  await http
    .put(config.API.relocationMainEdit(personId.toString()), body)
    .then((res: any) => {
      //   const data = res.data.result;
      //   console.log(data);
      success($q, "แก้ไขข้อมูลเพื่อลงบัญชีแนบท้ายสำเร็จ");
      edit.value = false;
    })
    .catch((e) => {
      messageError($q, e);
    })
    .finally(async () => {
      await getData();
      hideLoader();
    });
};

// const confirmMessage = async () => {
//     dialogMessage(
//       $q,
//       "ต้องการยืนยันส่งคำร้องไปยัง สกจ หรือไม่?",
//       "ข้อมูลจะถูกนำส่งคำร้องไปยัง สกจ ทันที",
//       "mdi-help-circle-outline",
//       "ยืนยัน",
//       "primary",
//       () => sendConfirm(),
//       undefined
//     );
//   };
// const sendConfirm = async () => {
//     showLoader();
//     await http
//       .get(config.API.transferConfirmId(personId.toString()))
//       .then((res: any) => {
//         //   const data = res.data.result;
//         //   console.log(data);
//         success($q, "ส่งคำร้องข้อมูลสำเร็จ");
//       })
//       .catch((e) => {
//         messageError($q, e);
//       })
//       .finally(async () => {
//         await getData();
//         hideLoader();
//       });
//   };
onMounted(async () => {
  await getData();
});
</script>
<template>
  <div class="toptitle text-dark col-12 row items-center">
    <q-btn
      icon="mdi-arrow-left"
      unelevated
      round
      dense
      flat
      color="primary"
      class="q-mr-sm"
      @click="router.go(-1)"
    />
    รายละเอียดการย้าย {{ fullname }}
  </div>
  <q-card bordered class="row col-12 text-dark">
    <div class="bg-grey-1 q-pa-sm col-12 row items-center text-primary">
      <div class="q-pl-sm text-weight-bold text-subtitle2">{{ fullname }}</div>
      <q-space />
      <q-btn
        outline
        color="blue"
        dense
        icon-right="mdi-open-in-new"
        class="q-px-sm"
        label="ดูข้อมูลทะเบียนประวัติ"
        @click="router.push(`/registry/${personId}`)"
      />
    </div>
    <div class="col-12"><q-separator /></div>
    <div class="row col-12 q-pa-md">
      <div class="col-12 row bg-white q-col-gutter-md">
        <div class="col-xs-3 col-sm-2 col-md-1 row">
          <q-img src="@/assets/avatar_user.jpg" />
        </div>
        <div class="col-xs-6 col-sm-3 row items-center">
          <div class="col-12 q-pl-md">
            <div class="col-12 text-top">ตำแหน่งในสายงาน</div>
            <div class="col-12 text-detail">
              {{ positionTypeOld }}
            </div>
          </div>
        </div>
        <div class="col-xs-6 col-sm-3 row items-center">
          <div class="col-12">
            <div class="col-12 text-top">ระดับ</div>
            <div class="col-12 text-detail">
              {{ positionLevelOld }}
            </div>
          </div>
        </div>
        <div class="col-xs-6 col-sm-3 row items-center">
          <div class="col-12">
            <div class="col-12 text-top">สังกัด</div>
            <div class="col-12 text-detail">
              {{ organizationPositionOld }}
            </div>
          </div>
        </div>
      </div>
    </div>
  </q-card>

  <q-card bordered class="row col-12 text-dark q-mt-sm">
    <div class="bg-grey-1 q-pa-sm col-12 row items-center text-primary">
      <div class="q-pl-sm text-weight-bold text-dark">
        แก้ไขข้อมูลเพื่อลงบัญชีแนบท้าย
      </div>
      <q-space />
      <div class="q-gutter-sm" v-if="!edit">
        <q-btn
          outline
          color="primary"
          dense
          icon-right="mdi-file-edit-outline"
          class="q-px-sm"
          label="แก้ไข"
          style="width: 80px"
          @click="edit = !edit"
        />
      </div>
      <div class="q-gutter-sm" v-else>
        <q-btn
          outline
          color="public"
          dense
          class="q-px-sm"
          label="บันทึก"
          style="width: 80px"
          @click="conditionSave"
        />
        <q-btn
          outline
          color="red"
          dense
          class="q-px-sm"
          label="ยกเลิก"
          style="width: 80px"
          @click="cancelBtn"
        />
      </div>
    </div>
    <div class="col-12"><q-separator /></div>
    <q-form ref="myForm">
      <div class="row col-12 q-pa-md">
        <div class="col-12 row bg-white q-col-gutter-md">
          <div class="col-xs-6 col-sm-3 row items-center">
            <div class="text-weight-bold text-grey">วุฒิการศึกษา</div>
            <div class="col-12">
              <q-input
                :class="getClass(edit)"
                :outlined="edit"
                dense
                lazy-rules
                :readonly="!edit"
                :borderless="!edit"
                v-model="educationOld"
                :rules="[(val) => !!val || `${'กรุณากรอกตำแหน่งประเภท'}`]"
                hide-bottom-space
                :label="`${'วุฒิ/สาขา'}`"
              />
            </div>
          </div>
          <div class="col-xs-12 row items-center">
            <div class="col-12 q-pb-sm"><q-separator /></div>

            <div class="col-12">
              <div class="text-weight-bold text-grey">
                ตำแหน่งและหน่วยงานเดิม
              </div>
            </div>
            <div class="col-12">
              <q-input
                :class="getClass(edit)"
                :outlined="edit"
                dense
                lazy-rules
                :readonly="!edit"
                :borderless="!edit"
                v-model="organizationPositionOld"
                :rules="[(val) => !!val || `${'กรุณากรอกตำแหน่ง/สังกัด'}`]"
                hide-bottom-space
                :label="`${'ตำแหน่ง/สังกัด'}`"
                type="textarea"
              />
            </div>
          </div>
          <div class="col-xs-6 col-sm-3 row items-center">
            <div class="col-12">
              <q-input
                :class="getClass(edit)"
                :outlined="edit"
                dense
                lazy-rules
                :readonly="!edit"
                :borderless="!edit"
                v-model="positionTypeOld"
                :rules="[(val) => !!val || `${'กรุณากรอกตำแหน่งประเภท'}`]"
                hide-bottom-space
                :label="`${'ตำแหน่งประเภท'}`"
              />
            </div>
          </div>
          <div class="col-xs-6 col-sm-3 row items-center">
            <div class="col-12">
              <q-input
                :class="getClass(edit)"
                :outlined="edit"
                dense
                lazy-rules
                :readonly="!edit"
                :borderless="!edit"
                v-model="positionLevelOld"
                :rules="[(val) => !!val || `${'กรุณากรอกระดับ'}`]"
                hide-bottom-space
                :label="`${'ระดับ'}`"
              />
            </div>
          </div>
          <div class="col-xs-6 col-sm-3 row items-center">
            <div class="col-12">
              <q-input
                :class="getClass(edit)"
                :outlined="edit"
                dense
                lazy-rules
                :readonly="!edit"
                :borderless="!edit"
                v-model="positionNumberOld"
                :rules="[(val) => !!val || `${'กรุณากรอกเลขที่'}`]"
                hide-bottom-space
                :label="`${'เลขที่'}`"
              />
            </div>
          </div>
          <div class="col-xs-6 col-sm-3 row items-center">
            <div class="col-12">
              <q-input
                :class="getClass(edit)"
                :outlined="edit"
                dense
                lazy-rules
                :readonly="!edit"
                :borderless="!edit"
                v-model="amountOld"
                :rules="[(val) => !!val || `${'กรุณากรอกเงินเดือน'}`]"
                hide-bottom-space
                :label="`${'เงินเดือน'}`"
                type="number"
              />
            </div>
          </div>
          <div class="col-12"><q-separator /></div>

          <!-- <div class="col-xs-6 col-sm-6 row items-center">
              <div class="col-12">
                <q-input
                  :class="getClass(edit)"
                  :outlined="edit"
                  dense
                  lazy-rules
                  :readonly="!edit"
                  :borderless="!edit"
                  v-model="organization"
                  :rules="[(val) => !!val || `${'กรุณากรอกโอนไปสังกัด'}`]"
                  hide-bottom-space
                  :label="`${'โอนไปสังกัด'}`"
                />
              </div>
            </div> -->
          <div class="col-xs-6 col-sm-3 row items-center">
            <div class="text-weight-bold text-grey col-12">
              เงินเดือนตำแหน่งและหน่วยงานที่รับย้าย
            </div>
            <div class="col-12">
              <q-input
                :class="getClass(edit)"
                :outlined="edit"
                dense
                lazy-rules
                :readonly="!edit"
                :borderless="!edit"
                v-model="amount"
                :rules="[(val) => !!val || `${'กรุณากรอกเงินเดือน'}`]"
                hide-bottom-space
                :label="`${'เงินเดือน'}`"
                type="number"
              />
            </div>
          </div>
          <div class="col-12">
            <q-input
              :class="getClass(edit)"
              :outlined="edit"
              dense
              lazy-rules
              :readonly="!edit"
              :borderless="!edit"
              v-model="reason"
              :rules="[(val) => !!val || `${'กรุณากรอกหมายเหตุ '}`]"
              hide-bottom-space
              :label="`${'หมายเหตุ '}`"
              type="textarea"
            />
          </div>
        </div>
      </div>
    </q-form>
  </q-card>
</template>

<style lang="scss" scope>
.q-img {
  border-radius: 5px;
  height: 70px;
}
.text-top {
  color: gray;
  font-weight: 400;
  padding-bottom: 3px;
}
.text-detail {
  font-weight: 500;
}
</style>
