hrms-checkin/src/components/FormTime.vue

307 lines
8.5 KiB
Vue
Raw Normal View History

2023-11-14 17:47:43 +07:00
<script setup lang="ts">
import { ref, watch, onMounted } 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
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) //สถานะการเลือกเวลาเข้า,ออก
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,
}
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()
})
}
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>
2024-09-02 13:40:15 +07:00
<div class="col-12 row">
<div class="row q-col-q-gutter-sm q-pa-md">
<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">
<div class="text-center text-bold">เวลาปจจ</div>
</q-card-section>
2024-09-02 17:37:08 +07:00
2024-09-02 13:40:15 +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
2024-09-02 13:40:15 +07:00
<q-card flat bordered class="col-12 q-mt-sm">
<div class="q-pa-sm">
<datepicker
:readonly="!statusAction"
v-model="date"
:locale="'th'"
:enableTimePicker="false"
week-start="0"
autoApply
outlined
lazy-rules
:max-date="dateNow"
>
<template #year="{ year }">
{{ year + 543 }}
</template>
<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'
? 'วันที่ขอแก้ไข'
: 'วันที่ขอลงเวลากรณีพิเศษ'
}`"
2024-09-02 13:40:15 +07:00
format-header="YYYY-MM-DD"
lazy-rules
2024-09-02 17:37:08 +07:00
:rules="[(val:string) => !!val || 'กรุณาเลือกวันที่']"
2024-09-02 13:40:15 +07:00
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
2024-09-02 13:40:15 +07:00
<q-card
v-if="action === 'edit'"
2024-09-02 13:40:15 +07:00
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>
2024-01-19 15:34:09 +07:00
</div>
2024-09-02 13:40:15 +07:00
</q-card>
<div
v-if="checkstatusBox"
class="text-red-9 q-pa-sm"
style="font-size: 10px"
>
กรณาเลอก
2023-11-14 17:47:43 +07:00
</div>
2025-03-19 16:05:25 +07:00
<q-card
flat
bordered
class="q-pa-sm col-12 q-mt-sm"
:style="action == 'special' ? 'margin-bottom: 150px' : ''"
>
2024-09-02 13:40:15 +07:00
<q-input
ref="reasonRef"
dense
outlined
v-model="reason"
label="เหตุผล"
type="textarea"
:rows="4"
label-color="grey-5"
2024-09-02 17:37:08 +07:00
:rules="[(val:string) => !!val || 'กรุณากรอกเหตุผล']"
2024-09-02 13:40:15 +07:00
lazy-rules
hide-bottom-space
class="custom-aqua-border"
/>
</q-card>
</div>
<div class="col-12"><q-separator /></div>
<div class="row col-12 q-pa-sm">
<q-space />
<q-btn color="secondary" label="บันทึก" @click="onCkickSave" />
2023-11-14 17:47:43 +07:00
</div>
2024-01-19 15:34:09 +07:00
</div>
2023-11-14 17:47:43 +07:00
</template>
<style scoped></style>