zjk
2026-06-26 18e856f0d5bde820e0b65d8749b5f26fb03bc7ec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<script setup lang="ts">
import noDataImg from '@/assets/element-img/no-data.png'
import { onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { addCheckLocation, getCheckLocationList } from '@/service/api/attendance'
import AddressFormModal from '@/views/operations/attendance/address/components/AddressFormModal.vue'
 
const props = defineProps<{
  show: boolean
  editPoint?: {
    id: string
    pointId: number
    timeLimit: string
    name?: string
    range?: string
  } | null
}>()
 
const emit = defineEmits<{
  'update:show': [value: boolean]
  'confirm': [data: any]
}>()
 
const show = computed({
  get: () => props.show,
  set: value => emit('update:show', value),
})
 
const pointId = ref<number | null>(null)
const startTime = ref<string | null>(null)
const endTime = ref<string | null>(null)
const isPointSelectOpen = ref(false)
const showAddressFormModal = ref(false)
 
const pointOptions = ref<Array<{ label: string, value: number, range: string }>>([])
 
async function getPointOptions() {
  const res = await getCheckLocationList({
    pageNum: 1,
    pageSize: 99999,
    type: 2,
  })
  if (res.code === 200) {
    const records = Array.isArray(res.data?.records) ? res.data.records : []
    pointOptions.value = records.map((item: any) => ({
      label: item.locationAddress,
      value: item.id,
      range: item.locationRange,
    }))
  }
}
 
function handleAddPoint() {
  showAddressFormModal.value = true
}
 
async function handleConfirmCreatePoint(payload: any) {
  try {
    const res = await addCheckLocation({
      ...payload,
      type: 2,
    })
    if (res.code === 200) {
      window.$message.success('新增打卡点成功')
      await getPointOptions()
 
      // 创建后自动回填到下拉:优先用返回ID,其次按地址/范围匹配
      const createdId = Number(res.data?.id ?? res.data?.locationId ?? 0)
      if (createdId) {
        pointId.value = createdId
      }
      else {
        const createdAddress = String(payload?.locationAddress || '')
        const createdRange = Number(payload?.locationRange ?? Number.NaN)
        const matched = pointOptions.value
          .find(item =>
            item.label === createdAddress
            && Number(item.range) === createdRange,
          )
        if (matched) {
          pointId.value = matched.value
        }
      }
      return
    }
    window.$message.warning(res.msg || '新增打卡点失败')
  }
  catch (error) {
    console.error('新增打卡点失败:', error)
    // window.$message.error('新增打卡点失败')
  }
}
 
function parseTimeToMinute(time: string) {
  const [hour, minute] = time.split(':').map(Number)
  return hour * 60 + minute
}
 
// 禁止选择小于开始时间的结束时间(结束时间面板直接置灰)
function isEndHourDisabled(hour: number) {
  if (!startTime.value)
    return false
  const [startHour] = startTime.value.split(':').map(Number)
  return hour < startHour
}
 
function isEndMinuteDisabled(minute: number, hour: number | null) {
  if (!startTime.value || hour === null)
    return false
  const [startHour, startMinute] = startTime.value.split(':').map(Number)
  return hour === startHour && minute < startMinute
}
 
function handleCancel() {
  show.value = false
  pointId.value = null
  startTime.value = null
  endTime.value = null
}
 
watch([startTime, endTime], ([start, end]) => {
  if (!start || !end)
    return
  if (parseTimeToMinute(end) < parseTimeToMinute(start)) {
    endTime.value = null
    window.$message.warning('结束时间不能小于开始时间')
  }
})
 
watch(show, async (visible) => {
  if (!visible)
    return
  // 每次打开都刷新选项,避免编辑时 value 已有但 options 尚未加载导致不回显
  await getPointOptions()
  // 编辑场景:打开弹窗时回填点位和时间段
  if (props.editPoint) {
    const editPointId = props.editPoint.pointId || null
    pointId.value = editPointId
    // 当前点位不在下拉选项中时,补一个临时选项用于回显名称
    if (editPointId && !pointOptions.value.some(item => item.value === editPointId)) {
      pointOptions.value.unshift({
        label: props.editPoint.name || `点位${editPointId}`,
        value: editPointId,
        range: String(props.editPoint.range || '-'),
      })
    }
    const timeRangeMatch = String(props.editPoint.timeLimit || '').match(/^(\d{2}:\d{2})\s*~\s*(\d{2}:\d{2})$/)
    startTime.value = timeRangeMatch?.[1] ?? null
    endTime.value = timeRangeMatch?.[2] ?? null
    return
  }
  // 新增场景:每次打开重置表单
  pointId.value = null
  startTime.value = null
  endTime.value = null
})
 
function handleConfirm() {
  if (!pointId.value) {
    window.$message.warning('请选择打卡点')
    return
  }
 
  const selectedPoint = pointOptions.value.find(p => p.value === pointId.value)
  let timeStr = '-'
  if (startTime.value && endTime.value) {
    // 结束时间不能早于开始时间
    if (endTime.value < startTime.value) {
      window.$message.warning('结束时间不能小于开始时间')
      return
    }
    timeStr = `${startTime.value}~${endTime.value}`
  }
  else if (startTime.value || endTime.value) {
    window.$message.warning('请选择完整的打卡时间段')
    return
  }
 
  emit('confirm', {
    id: props.editPoint?.id,
    pointId: pointId.value,
    name: selectedPoint?.label,
    range: selectedPoint?.range,
    timeLimit: timeStr,
  })
  show.value = false
  pointId.value = null
  startTime.value = null
  endTime.value = null
}
 
const lastTimePickerPanelUnit = ref<'hour' | 'minute' | 'second' | 'period' | null>(null)
 
function handleTimePickerPanelClick(event: Event) {
  const target = event.target as HTMLElement | null
  const panelItem = target?.closest('.n-time-picker-col__item')
  if (!panelItem) {
    lastTimePickerPanelUnit.value = null
    return
  }
 
  const columnElement = panelItem.closest('.n-time-picker-col') as HTMLElement | null
  const columnsContainer = columnElement?.parentElement
  if (!columnElement || !columnsContainer) {
    lastTimePickerPanelUnit.value = null
    return
  }
 
  const columnElements = Array.from(columnsContainer.children).filter(element =>
    (element as HTMLElement).classList.contains('n-time-picker-col'),
  )
  const columnIndex = columnElements.indexOf(columnElement)
  const units: Array<'hour' | 'minute' | 'second' | 'period'> = ['hour', 'minute', 'second', 'period']
  lastTimePickerPanelUnit.value = units[columnIndex] || null
}
 
function closeTimePicker() {
  if (lastTimePickerPanelUnit.value !== 'minute')
    return
  lastTimePickerPanelUnit.value = null
  if (document.activeElement) {
    (document.activeElement as HTMLElement).blur()
  }
}
 
onMounted(() => {
  document.addEventListener('click', handleTimePickerPanelClick, true)
  getPointOptions()
})
 
onBeforeUnmount(() => {
  document.removeEventListener('click', handleTimePickerPanelClick, true)
})
</script>
 
<template>
  <n-modal
    v-model:show="show"
    :mask-closable="false"
    preset="card"
    title="选择打卡点"
    class="w-400px"
    :auto-focus="false"
    :segmented="{
      content: true,
      action: true,
    }"
  >
    <div class="flex flex-col gap-16px pt-2">
      <div>
        <div class="mb-2">
          <span class="text-red-500">*</span>打卡点
        </div>
        <div>
          <n-select
            v-model:value="pointId"
            :options="pointOptions"
            placeholder="请选择"
            clearable
            class="flex-1"
            @update:show="isPointSelectOpen = $event"
          >
            <template #arrow>
              <img
                src="https://huishou-1323682843.cos.ap-nanjing.myqcloud.com/images/e120f81c-9f15-4e1a-9938-22252898edf9.png"
                class="arrow-icon"
                :class="{ 'arrow-icon--open': isPointSelectOpen }"
                style="width: 16px; height: 16px"
                alt="arrow"
              >
            </template>
            <template #empty>
              <div class="flex-center">
                <img
                  :src="noDataImg"
                  style="width: 50%"
                  alt="no-data"
                >
              </div>
            </template>
          </n-select>
          <n-button type="primary" class="w-full mt-8px" dashed @click="handleAddPoint">
            新增打卡点
          </n-button>
        </div>
      </div>
 
      <div>
        <div class="mb-2">
          打卡限时 <span class="text1">(不设置即代表不限时)</span>
        </div>
        <div class="flex items-center gap-2 time-range">
          <n-time-picker
            v-model:formatted-value="startTime"
            format="HH:mm"
            value-format="HH:mm"
            placeholder="开始时间"
            clearable
            :actions="['now']"
            class="custom-time-picker flex-1"
            @update:value="closeTimePicker"
          />
          <span style="color: #d9d9d9;">→</span>
          <n-time-picker
            v-model:formatted-value="endTime"
            format="HH:mm"
            value-format="HH:mm"
            placeholder="结束时间"
            clearable
            :actions="['now']"
            :is-hour-disabled="isEndHourDisabled"
            :is-minute-disabled="isEndMinuteDisabled"
            class="custom-time-picker flex-1"
            @update:value="closeTimePicker"
          />
        </div>
      </div>
    </div>
 
    <template #action>
      <n-space justify="end">
        <n-button @click="handleCancel">
          取消
        </n-button>
        <n-button type="primary" @click="handleConfirm">
          保存
        </n-button>
      </n-space>
    </template>
  </n-modal>
 
  <AddressFormModal
    v-model:show="showAddressFormModal"
    mode="add"
    title="巡游打卡地"
    :type="2"
    @confirm="handleConfirmCreatePoint"
  />
</template>
 
<style scoped>
.arrow-icon {
  transition: transform 0.2s;
  user-select: none;
}
 
.arrow-icon--open {
  transform: rotate(180deg);
}
 
.time-range {
  width: 100%;
  border: 1px solid #d9d9d9;
  border-radius: 3px;
  padding: 0;
  transition: border-color 0.3s;
}
 
.time-range:hover {
  border-color: #0068ff;
}
 
:deep(.custom-time-picker .n-input) {
  border: none !important;
  box-shadow: none !important;
  background-color: transparent;
}
 
:deep(.custom-time-picker .n-input:hover) {
  border: none !important;
  box-shadow: none !important;
}
 
:deep(.custom-time-picker .n-input--focus) {
  border: none !important;
  box-shadow: none !important;
}
 
:deep(.custom-time-picker .n-input .n-input__state-border) {
  display: none !important;
}
 
:deep(.custom-time-picker .n-input .n-input__border) {
  display: none !important;
}
 
.text1 {
  color: rgba(23, 31, 38, 0.6)
}
</style>