hrms-checkin/src/components/FormTime.vue

349 lines
10 KiB
Vue
Raw Normal View History

2023-11-14 17:47:43 +07:00
<script setup lang="ts">
2025-05-14 16:51:58 +07:00
import { ref, watch, onMounted, reactive } from 'vue'
2024-09-02 17:37:08 +07:00
import { PropType } from 'vue'
2023-11-14 17:47:43 +07:00
import { useQuasar } from 'quasar'
import moment from 'moment'
2024-09-02 17:37:08 +07:00
import http from '@/plugins/http'
import config from '@/app.config'
2024-09-02 17:37:08 +07:00
import { useCounterMixin } from '@/stores/mixin'
2024-09-02 17:37:08 +07:00
import type { FormRef, DataCheckIn } from '@/interface/index/Main'
import type { FormTimeStemp } from '@/interface/response/checkin'
2023-11-14 17:47:43 +07:00
2025-05-14 16:51:58 +07:00
import MapCheck from '@/components/AscGISMapTime.vue'
2023-11-14 17:47:43 +07:00
const $q = useQuasar()
2024-09-02 17:37:08 +07:00
const {
date2Thai,
success,
dialogConfirm,
showLoader,
hideLoader,
messageError,
2025-03-11 10:10:35 +07:00
convertDateToAPI,
2024-09-02 17:37:08 +07:00
} = useCounterMixin()
2023-11-14 17:47:43 +07:00
2024-09-02 17:37:08 +07:00
/**
* props จาก components Popup
*/
2023-11-14 17:47:43 +07:00
const props = defineProps({
dataById: {
2024-09-02 17:37:08 +07:00
type: Object as PropType<DataCheckIn>,
2023-11-14 17:47:43 +07:00
default: null,
},
closePopup: {
type: Function,
required: true,
},
fetchData: {
type: Function,
require: true,
default: () => {
console.log('fetchData func')
},
},
action: {
type: String,
default: '',
},
2023-11-14 17:47:43 +07:00
})
2024-09-02 17:37:08 +07:00
const dataByIdVal = ref<DataCheckIn>() //ข้อมูลประวัติการลงเวลา
2025-03-11 10:10:35 +07:00
const date = ref<Date | string | null>(new Date()) //วันที่ข้อแก้ไข
2024-09-02 17:37:08 +07:00
const checkboxIn = ref<boolean>(false) //ขอแก้ไขเวลาเข้างาน
const checkboxOut = ref<boolean>(false) //ขอแก้ไขเวลาออกงาน
const reason = ref<string>('') // เหตุผล
const statusAction = ref<boolean>(false) //สถานะ เพิ่ม,แก้ไข
const dateNow = ref<Date>(new Date()) // วันปัจจุบัน
const timeNoew = ref<string>('') //เวลาปัจจุบัน
const dateRef = ref<object | null>(null) //ref วันที่ข้อแก้ไข
const reasonRef = ref<object | null>(null) //ref เหตุผล
const objectRef: FormRef = {
date: dateRef,
reason: reasonRef,
}
const checkstatusBox = ref<boolean>(false) //สถานะการเลือกเวลาเข้า,ออก
2025-05-14 16:51:58 +07:00
const formLocation = reactive({
lat: 0, //พิกัดละติจูด
lng: 0, //พิกัดลองจิจูด
POI: '', //ชื่อสถานที่
})
2023-11-14 17:47:43 +07:00
2024-09-02 17:37:08 +07:00
/**
* งกนอปเดตเวลา
*/
2023-11-14 17:47:43 +07:00
function updateClock() {
const date = Date.now()
const hh = moment(date).format('HH')
const mm = moment(date).format('mm')
timeNoew.value = `${hh}:${mm} น.`
}
2024-09-02 17:37:08 +07:00
/**
* function checkValidate
*/
2023-11-14 17:47:43 +07:00
function onCkickSave() {
const hasError = []
for (const key in objectRef) {
if (Object.prototype.hasOwnProperty.call(objectRef, key)) {
const property = objectRef[key]
if (property.value && typeof property.value.validate === 'function') {
const isValid = property.value.validate()
hasError.push(isValid)
}
}
}
2025-03-19 17:19:09 +07:00
// ลงเวลาพิเศษบังคับให้แก้ทั้งเข้าและออก
if (props.action === 'special') {
checkboxIn.value = true
checkboxOut.value = true
}
2023-11-14 17:47:43 +07:00
if (checkboxIn.value === false && checkboxOut.value === false) {
checkstatusBox.value = true
}
if (
hasError.every(
(result) => result === true && checkstatusBox.value === false
)
) {
dialogConfirm($q, async () => {
2024-09-02 17:37:08 +07:00
const data: FormTimeStemp = {
2025-03-19 17:19:09 +07:00
checkDate: convertDateToAPI(date.value as Date),
checkInEdit: checkboxIn.value,
checkOutEdit: checkboxOut.value,
description: reason.value,
2025-05-14 16:51:58 +07:00
latitude: formLocation.lat.toString(),
longitude: formLocation.lng.toString(),
POI: formLocation.POI,
}
2024-09-02 17:37:08 +07:00
createListTime(data)
2023-11-14 17:47:43 +07:00
})
}
}
/**
* function เพมลงเวลากรณเศษ
* @param data body Request
*/
async function createListTime(data: FormTimeStemp) {
2024-01-18 16:23:27 +07:00
showLoader()
await http
.post(config.API.createTimeStamp(), data)
2024-09-02 17:37:08 +07:00
.then(async () => {
// เรียก function fetch รายการประวัติการลงเวลา
await props.fetchData()
await success($q, 'บันทึกข้อมูลสำเร็จ')
props.closePopup?.()
})
.catch((err) => {
2024-09-02 17:37:08 +07:00
messageError($q, err)
})
.finally(() => {
2024-01-18 16:23:27 +07:00
hideLoader()
})
}
2025-05-14 16:51:58 +07:00
/**
* funciton เรยกพดละต ดลองต
* @param location ดละต ดลองต
* @param namePOI อสถานท ไดมาจากระบบ ArcGis ของกองสารสนเทศภศาสตร
*/
async function updateLocation(
latitude: number,
longitude: number,
namePOI: string
) {
formLocation.lat = latitude
formLocation.lng = longitude
formLocation.POI = namePOI
}
2024-09-02 17:37:08 +07:00
/**
* watch การเปลยนแปลงของ checkboxIn และ checkboxOut
*/
watch(
[() => checkboxIn.value, () => checkboxOut.value],
([newCheckboxIn, newCheckboxOut]) => {
if (checkstatusBox.value) {
if (newCheckboxIn || newCheckboxOut) {
checkstatusBox.value = false
}
}
}
)
/**
* Hook
*/
onMounted(() => {
updateClock()
dataByIdVal.value = props.dataById
if (dataByIdVal.value == null) {
statusAction.value = true
} else {
2025-03-19 17:19:09 +07:00
date.value = convertDateToAPI(new Date(dataByIdVal.value.checkInDateTime))
}
})
2023-11-14 17:47:43 +07:00
</script>
2025-03-19 16:05:25 +07:00
2023-11-14 17:47:43 +07:00
<template>
2025-05-15 09:57:13 +07:00
<q-card-section
:horizontal="$q.screen.gt.md"
style="max-height: 65vh; padding: 0px"
class="scroll"
>
2025-05-14 16:51:58 +07:00
<div class="col-12">
<div class="row q-col-q-gutter-sm">
<div class="q-pa-md col-md-5 col-xs-12">
<q-card
flat
bordered
:class="$q.screen.gt.xs ? 'col-12 bg-grey-1' : 'col-12 '"
>
<q-card-section class="bg-primary text-white q-pa-sm">
2025-05-15 09:57:13 +07:00
<div class="text-center text-bold">เวลาปจasdasdasจ</div>
2025-05-14 16:51:58 +07:00
</q-card-section>
2024-09-02 17:37:08 +07:00
2025-05-14 16:51:58 +07:00
<q-card-section class="text-center q-pa-sm">
<div class="row q-gutter-md">
<div class="col">{{ date2Thai(dateNow) }}</div>
<div class="col">{{ timeNoew }}</div>
</div>
</q-card-section>
</q-card>
2023-11-14 17:47:43 +07:00
2025-05-14 16:51:58 +07:00
<q-card flat bordered class="col-12 q-mt-sm">
<div class="q-pa-sm">
<datepicker
2024-09-02 13:40:15 +07:00
:readonly="!statusAction"
2025-05-14 16:51:58 +07:00
v-model="date"
:locale="'th'"
:enableTimePicker="false"
week-start="0"
autoApply
2024-09-02 13:40:15 +07:00
outlined
lazy-rules
2025-05-14 16:51:58 +07:00
:max-date="dateNow"
2024-09-02 13:40:15 +07:00
>
2025-05-14 16:51:58 +07:00
<template #year="{ year }">
{{ year + 543 }}
2024-09-02 13:40:15 +07:00
</template>
2025-05-14 16:51:58 +07:00
<template #year-overlay-value="{ value }">
{{ parseInt(value + 543) }}
</template>
<template #trigger>
<q-input
:readonly="!statusAction"
ref="dateRef"
outlined
dense
:model-value="
date !== null ? date2Thai(new Date(date)) : null
"
:label="`${
action === 'edit'
? 'วันที่ขอแก้ไข'
: 'วันที่ขอลงเวลากรณีพิเศษ'
}`"
format-header="YYYY-MM-DD"
lazy-rules
:rules="[(val:string) => !!val || 'กรุณาเลือกวันที่']"
hide-bottom-space
>
<template v-slot:prepend>
<q-icon
name="event"
class="cursor-pointer"
style="color: var(--q-primary)"
>
</q-icon>
</template>
</q-input> </template
></datepicker>
</div>
</q-card>
2023-11-14 17:47:43 +07:00
2025-05-14 16:51:58 +07:00
<q-card
v-if="action === 'edit'"
flat
bordered
class="q-pa-sm col-12 q-mt-sm"
:style="{
borderColor: checkstatusBox ? '#B22222' : '',
borderWidth: checkstatusBox ? '2px' : '',
}"
>
<div class="row q-gutter-xs">
<div class="col-12">
<q-checkbox
keep-color
color="primary"
v-model="checkboxIn"
label="ขอแก้ไขเวลาเข้างาน"
/>
</div>
<div class="col-12">
<q-checkbox
keep-color
v-model="checkboxOut"
color="primary"
label="ขอแก้ไขเวลาออกงาน"
/>
</div>
</div>
</q-card>
<div
v-if="checkstatusBox"
class="text-red-9 q-pa-sm"
style="font-size: 10px"
>
กรณาเลอก
2024-09-02 13:40:15 +07:00
</div>
2025-05-14 16:51:58 +07:00
<!-- :style="action == 'special' ? 'margin-bottom: 150px' : ''" -->
<q-card flat bordered class="q-pa-sm col-12 q-mt-sm">
<q-input
ref="reasonRef"
dense
outlined
v-model="reason"
label="เหตุผล"
type="textarea"
:rows="4"
label-color="grey-5"
:rules="[(val:string) => !!val || 'กรุณากรอกเหตุผล']"
lazy-rules
hide-bottom-space
class="custom-aqua-border"
2024-09-02 13:40:15 +07:00
/>
2025-05-14 16:51:58 +07:00
</q-card>
</div>
<q-separator vertical />
2025-05-15 09:57:13 +07:00
<div
class="q-pa-md col"
:style="!$q.screen.gt.sm ? 'padding-top: 0;' : ''"
>
<q-card flat bordered class="col-12">
<MapCheck @update:location="updateLocation" />
</q-card>
2024-01-19 15:34:09 +07:00
</div>
2024-09-02 13:40:15 +07:00
2025-05-14 16:51:58 +07:00
<!-- :style="action == 'special' ? 'margin-bottom: 120px' : ''" -->
2023-11-14 17:47:43 +07:00
</div>
2024-09-02 13:40:15 +07:00
</div>
2025-05-14 16:51:58 +07:00
</q-card-section>
2025-05-15 09:57:13 +07:00
<q-separator />
<q-card-actions align="right">
<q-btn color="secondary" label="บันทึก" @click="onCkickSave" />
</q-card-actions>
2023-11-14 17:47:43 +07:00
</template>
<style scoped></style>