| | |
| | | import { $t } from '#/locales'; |
| | | |
| | | import { useFormSchema } from '../data'; |
| | | import LocationMapPicker from './location-map-picker.vue'; |
| | | |
| | | const emit = defineEmits(['success']); |
| | | const formType = ref<FormType>('create'); |
| | | const locationRequired = ref(false); |
| | | const longitude = ref<number>(); |
| | | const latitude = ref<number>(); |
| | | const radiusMeters = ref(10); |
| | | |
| | | const isDetail = computed(() => formType.value === 'detail'); |
| | | const getTitle = computed(() => { |
| | |
| | | } |
| | | return '新增考勤规则'; |
| | | }); |
| | | |
| | | function getFormSchema(type: FormType) { |
| | | return useFormSchema(type, { |
| | | onLatitudeChange: (value) => { |
| | | latitude.value = value == null ? undefined : Number(value); |
| | | }, |
| | | onLocationRequiredChange: (value) => { |
| | | locationRequired.value = value; |
| | | }, |
| | | onLongitudeChange: (value) => { |
| | | longitude.value = value == null ? undefined : Number(value); |
| | | }, |
| | | onRadiusChange: (value) => { |
| | | radiusMeters.value = value == null ? 10 : Number(value); |
| | | }, |
| | | }); |
| | | } |
| | | |
| | | async function handleMapSelect(location: { |
| | | address?: string; |
| | | latitude: number; |
| | | locationName?: string; |
| | | longitude: number; |
| | | }) { |
| | | const currentValues = await formApi.getValues(); |
| | | longitude.value = location.longitude; |
| | | latitude.value = location.latitude; |
| | | await formApi.setFieldValue('longitude', location.longitude); |
| | | await formApi.setFieldValue('latitude', location.latitude); |
| | | await formApi.setFieldValue('coordinateSystem', 'WGS84'); |
| | | if (location.address) { |
| | | await formApi.setFieldValue('locationAddress', location.address); |
| | | } |
| | | if (!currentValues.locationName && location.locationName) { |
| | | await formApi.setFieldValue('locationName', location.locationName); |
| | | } |
| | | } |
| | | |
| | | const [Form, formApi] = useVbenForm({ |
| | | commonConfig: { |
| | |
| | | } |
| | | const data = modalApi.getData<{ formType: FormType; id?: number }>(); |
| | | formType.value = data.formType; |
| | | formApi.setState({ schema: useFormSchema(data.formType) }); |
| | | locationRequired.value = false; |
| | | longitude.value = undefined; |
| | | latitude.value = undefined; |
| | | radiusMeters.value = 10; |
| | | formApi.setState({ schema: getFormSchema(data.formType) }); |
| | | formApi.setDisabled(data.formType === 'detail'); |
| | | modalApi.setState({ showConfirmButton: data.formType !== 'detail' }); |
| | | if (!data?.id) { |
| | | await formApi.setValues({ |
| | | coordinateSystem: 'WGS84', |
| | | locationRequired: false, |
| | | radiusMeters: 10, |
| | | }); |
| | | return; |
| | | } |
| | | modalApi.lock(); |
| | | try { |
| | | await formApi.setValues(await getAttendanceRule(data.id)); |
| | | const rule = await getAttendanceRule(data.id); |
| | | await formApi.setValues(rule); |
| | | locationRequired.value = Boolean(rule.locationRequired); |
| | | longitude.value = rule.longitude == null ? undefined : Number(rule.longitude); |
| | | latitude.value = rule.latitude == null ? undefined : Number(rule.latitude); |
| | | radiusMeters.value = rule.radiusMeters ?? 10; |
| | | } finally { |
| | | modalApi.unlock(); |
| | | } |
| | |
| | | </script> |
| | | |
| | | <template> |
| | | <Modal :title="getTitle" class="w-[800px]"> |
| | | <Modal :title="getTitle" class="w-[1000px]"> |
| | | <Form class="mx-4" /> |
| | | <section v-if="locationRequired" class="mx-4 mt-2"> |
| | | <div class="mb-2 text-sm font-medium">地图选择打卡范围</div> |
| | | <LocationMapPicker |
| | | :disabled="isDetail" |
| | | :latitude="latitude" |
| | | :longitude="longitude" |
| | | :radius-meters="radiusMeters" |
| | | @select="handleMapSelect" |
| | | /> |
| | | </section> |
| | | </Modal> |
| | | </template> |
| | | </template> |