<script setup lang="ts">
import { ref, onMounted, computed } from "vue"
import { useQuasar } from "quasar"
import { useRouter, useRoute } from "vue-router"

import FormPart2 from "@/modules/05_leave/componenst/Forms/Form.vue"
import SickForm from "@/modules/05_leave/componenst/Forms/01_SickForm.vue"
import FormBirth from "@/modules/05_leave/componenst/Forms/03_Birth.vue"
import HelpWifeBirthForm from "@/modules/05_leave/componenst/Forms/04_HelpWifeBirthForm.vue"
import VacationForm from "@/modules/05_leave/componenst/Forms/05_VacationForm.vue"
import OrdinationForm from "@/modules/05_leave/componenst/Forms/06_OrdinationForm.vue"
import HajjForm from "@/modules/05_leave/componenst/Forms/07_HajjForm.vue"
import MilitaryForm from "@/modules/05_leave/componenst/Forms/08_MilitaryForm.vue"
import StudyForm from "@/modules/05_leave/componenst/Forms/09_StudyForm.vue"
import TrainForm from "@/modules/05_leave/componenst/Forms/10_TrainForm.vue"
import WorkInternationalForm from "@/modules/05_leave/componenst/Forms/11_WorkInternationalForm.vue"
import FollowSpouseForm from "@/modules/05_leave/componenst/Forms/12_FollowSpouseForm.vue"
import RehabilitationForm from "@/modules/05_leave/componenst/Forms/13_RehabilitationForm.vue"
import { useLeaveStore } from "@/modules/05_leave/store"
import http from "@/plugins/http"
import config from "@/app.config"
import type { QForm } from "quasar"
import { useCounterMixin } from "@/stores/mixin"

const dataStore = useLeaveStore()
const { fetchLeaveType } = dataStore
const mixin = useCounterMixin()
const { date2Thai, dialogConfirm, success, messageError } = mixin
const router = useRouter()
const route = useRoute()
const myform = ref<QForm | null>(null)
const $q = useQuasar()

const model = ref<string>("")
const modelSpecific = ref<string>("")

onMounted(async () => {
	await fectOptionType()
})

const saveAbsence = () => {
	$q.dialog({
		title: "ยืนยันการยื่นข้อมูลลา",
		message: "ต้องการยื่นข้อมูลลานี้ใช่หรือไม่?",
		cancel: {
			flat: true,
			color: "negative",
		},
		persistent: true,
	})
		.onOk(() => {
			// createFormresign()
			if (model.value !== "LV-007") {
				console.log(1)
			} else if (model.value === "LV-007") {
				console.log(2)
			}
			router.push(`/leave`)
		})
		.onCancel(() => {})
		.onDismiss(() => {})
}
/** ฟังก์ชั่น บันทึก */
const onSubmit = async () => {
	dialogConfirm(
		$q,
		async () => {
			await saveformdata()
		},
		"ยืนยันการยื่นใบลา",
		"ต้องการยืนยันการยื่นใบลานี้ใช่หรือไม่ ?"
	)

	console.log("save")
}

async function fectOptionType() {
	await http
		.get(config.API.leaveType())
		.then(async res => {
			const data = res.data.result
			await fetchLeaveType(data)
		})
		.catch(err => {
			messageError($q, err)
		})
}

async function FetchCheck(formData: any) {
	await http
		.post(config.API.leaveCheck(), { type: dataStore.typeId ?? null, leaveStartDate: formData.leaveStartDate ?? null, leaveEndDate: formData.leaveEndDate ?? null })
		.then((res: any) => {
			const data = res.data.result
			formData.dateStart = data.isLeave
			formData.subject = data.sumDateWork
			formData.leaveabsentDaySummon = data.totalDate
		})
		.catch((e: any) => {
			messageError($q, e)
		})
}
</script>

<template>
	<div class="col-12 row justify-center">
		<div class="col-xs-12 col-sm-12 col-md-11">
			<div class="toptitle text-white col-12 row items-center">
				<q-btn to="/leave" icon="mdi-arrow-left" unelevated round dense flat color="primary" class="q-mr-sm" />
				<div>ยื่นใบลา</div>
			</div>

			<q-form ref="myform" class="col-12">
				<q-card bordered>
					<div class="col-12 row q-col-gutter-md q-pa-md">
						<div class="col-xs-12 col-sm-12">
							<div style="display: flex; align-items: center">
								<q-icon name="mdi-numeric-1-circle" size="20px" color="primary" />
								<div class="q-pl-sm text-weight-bold text-dark">เลือกประเภทการลา</div>
							</div>
							<div class="q-py-sm q-px-lg">
								<div class="row">
									<q-select
										dense
										class="col-12 col-sm-6 col-md-4"
										outlined
										v-model="model"
										:options="dataStore.options"
										option-value="code"
										option-label="name"
										emit-value
										map-options
										prefix="ประเภทใบลา :"
										@update:model-value="dataStore.typeConvert(model, null), (modelSpecific = ''), dataStore.fetchProfile()"
									/>
								</div>
								<div class="row q-mt-sm">
									<div class="col-12 col-sm-6 col-md-3" v-if="model === 'LV-006' || model === 'LV-008'">
										<q-select
											dense
											outlined
											v-model="modelSpecific"
											:options="model === 'LV-006' ? dataStore.optionsOrdination : dataStore.optionsSpecific"
											option-value="code"
											option-label="name"
											emit-value
											map-options
											prefix="ประเภทการลา :"
											@update:model-value="dataStore.typeConvert(model, modelSpecific)"
										/>
									</div>
								</div>

								<!-- v-if (ลาอุปสมบทหรือลาประกอบพิธีฮัจย์ฯ||ลาไปศึกษา ฝึกอบรม ปฎิบัติการวิจัย หรือดูงาน) -->
							</div>

							<div>
								<div style="display: flex; align-items: center">
									<q-icon name="mdi-numeric-2-circle" size="20px" color="primary" />
									<div class="q-pl-sm text-weight-bold text-dark">ข้อมูลการลา</div>
								</div>
								<FormPart2 :model="model" />
							</div>
						</div>
						<div v-if="model" class="col-12">
							<SickForm :on-submit="onSubmit" v-if="model === 'LV-001' || model === 'LV-002'" />
							<FormBirth :on-submit="onSubmit" v-if="model === 'LV-003'" />
							<HelpWifeBirthForm :on-submit="onSubmit" v-if="model === 'LV-004'" />
							<VacationForm :on-submit="onSubmit" v-if="model === 'LV-005'" />
							<OrdinationForm :on-submit="onSubmit" v-if="model === 'LV-006' && modelSpecific === '0'" />
							<HajjForm :on-submit="onSubmit" v-if="model === 'LV-006' && modelSpecific === '1'" />
							<MilitaryForm :on-submit="onSubmit" v-if="model === 'LV-007'" style="width: 100%" />
							<StudyForm :on-submit="onSubmit" v-if="model === 'LV-008' && modelSpecific === 's0'" style="width: 100%" />
							<TrainForm :on-submit="onSubmit" v-if="model === 'LV-008' && modelSpecific != 's0' && modelSpecific != ''" />
							<WorkInternationalForm :on-submit="onSubmit" v-if="model === 'LV-009'" />
							<FollowSpouseForm :on-submit="onSubmit" v-if="model === 'LV-010'" />
							<RehabilitationForm :on-submit="onSubmit" v-if="model === 'LV-011'" />
						</div>
					</div>
				</q-card>
			</q-form>
		</div>
	</div>
</template>
