wlzboy
4 天以前 06a17c236d4cb9b8da75fce43af938cb7ea510bf
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<template>
  <scroll-view class="edit-welfare-task-container" scroll-y="true">
    <view class="form-header">
      <view class="back-btn" @click="goBack">
        <uni-icons type="arrowleft" size="20"></uni-icons>
      </view>
      <view class="title">编辑福祉车任务</view>
    </view>
    
    <view class="form-section" v-if="taskDetail">
      <!-- 使用车辆选择器组件 -->
      <VehicleSelector
        label="任务车辆"
        v-model="selectedVehicleId"
        :dept-id="currentUser.deptId"
        vehicle-type="WELFARE"
        :auto-select-bound="false"
        @change="onVehicleChange"
      />
      
      <view class="form-item">
        <view class="form-label">执行任务人员</view>
        <view class="staff-list">
          <view class="staff-item">
            <text>{{ currentUser.name }} ({{ currentUser.position }})</text>
            <uni-icons type="checkmarkempty" size="20" color="#007AFF"></uni-icons>
          </view>
          <view 
            class="staff-item" 
            v-for="(staff, index) in additionalStaff" 
            :key="index"
            @click="removeStaff(index)"
          >
            <text>{{ staff.name }} ({{ staff.position }})</text>
            <uni-icons type="closeempty" size="20" color="#ff4d4f"></uni-icons>
          </view>
          <view class="add-staff" @click="addStaff">
            <uni-icons type="plusempty" size="20" color="#007AFF"></uni-icons>
            <text>添加人员</text>
          </view>
        </view>
      </view>
      
      <view class="form-item">
        <view class="form-label">归属机构</view>
        <picker mode="selector" :range="organizations" @change="onOrganizationChange">
          <view class="form-input picker-input">
            {{ selectedOrganization || '请选择归属机构' }}
            <uni-icons type="arrowright" size="16" color="#999"></uni-icons>
          </view>
        </picker>
      </view>
      
      <view class="form-item">
        <view class="form-label">服务时间</view>
        <uni-datetime-picker 
          v-model="taskForm.serviceTime" 
          type="datetime" 
          :placeholder="'请选择服务时间'"
          class="form-input"
        />
      </view>
      
      <view class="form-section-title">乘客信息</view>
      <view class="form-item">
        <view class="form-label">联系人</view>
        <input 
          class="form-input" 
          placeholder="请输入联系人" 
          v-model="taskForm.passenger.contact"
        />
      </view>
      
      <view class="form-item">
        <view class="form-label">联系电话</view>
        <input 
          class="form-input" 
          type="number" 
          placeholder="请输入联系电话" 
          v-model="taskForm.passenger.phone"
        />
      </view>
      
      <view class="form-item">
        <view class="form-label">出发地址</view>
        <view class="form-input picker-input" @click="selectStartAddress">
          {{ taskForm.startAddress || '请选择出发地址' }}
          <uni-icons type="arrowright" size="16" color="#999"></uni-icons>
        </view>
      </view>
      
      <view class="form-item">
        <view class="form-label">目的地址</view>
        <view class="form-input picker-input" @click="selectEndAddress">
          {{ taskForm.endAddress || '请选择目的地址' }}
          <uni-icons type="arrowright" size="16" color="#999"></uni-icons>
        </view>
      </view>
      
      <view class="form-item">
        <view class="form-label">公里数</view>
        <input 
          class="form-input" 
          type="digit" 
          placeholder="请输入公里数" 
          v-model="taskForm.distance"
        />
      </view>
      
      <view class="form-item">
        <view class="form-label">成交价</view>
        <input 
          class="form-input" 
          type="digit" 
          placeholder="请输入成交价" 
          v-model="taskForm.price"
        />
      </view>
      
      <view class="form-actions">
        <button class="submit-btn" @click="submitTask" :disabled="loading">
          {{ loading ? '保存中...' : '保存' }}
        </button>
      </view>
    </view>
    
    <view class="loading" v-else>
      <uni-icons type="spinner-cycle" size="40" color="#007AFF"></uni-icons>
      <text>加载中...</text>
    </view>
    
    <!-- 地图选择器弹窗 -->
    <uni-popup ref="mapPopup" type="bottom" :mask-click="false">
      <view class="map-popup-container">
        <view class="popup-header">
          <view class="popup-title">选择地址</view>
          <view class="close-btn" @click="closeMapSelector">
            <uni-icons type="closeempty" size="20" color="#999"></uni-icons>
          </view>
        </view>
        <map-selector 
          :initial-address="getInitialAddress()" 
          @addressSelected="onAddressSelected"
        ></map-selector>
      </view>
    </uni-popup>
  </scroll-view>
</template>
 
<script>
import { mapState } from 'vuex'
import uniDatetimePicker from '@/uni_modules/uni-datetime-picker/components/uni-datetime-picker/uni-datetime-picker.vue'
import uniPopup from '@/uni_modules/uni-popup/components/uni-popup/uni-popup.vue'
import { getTask, updateTask } from "@/api/task"
import MapSelector from './components/map-selector.vue'
import VehicleSelector from './components/VehicleSelector.vue'
import distanceCalculator from '@/mixins/distanceCalculator.js'
 
export default {
  components: {
    uniDatetimePicker,
    uniPopup,
    MapSelector,
    VehicleSelector
  },
  mixins: [distanceCalculator],
  data() {
    return {
      taskId: null,
      taskDetail: null,
      selectedVehicleId: null,
      selectedOrganization: '',
      mapSelectorType: '',
      // 扩展 addressCoordinates 支持多种键名
      addressCoordinates: {
        start: null,
        end: null,
        startAddress: null,
        endAddress: null
      },
      taskForm: {
        serviceTime: '',
        passenger: {
          contact: '',
          phone: ''
        },
        startAddress: '',
        endAddress: '',
        distance: '',
        price: ''
      },
      organizations: ['广州分公司', '深圳分公司', '珠海分公司', '佛山分公司'],
      additionalStaff: [],
      loading: false
    }
  },
  computed: {
    ...mapState({
      currentUser: state => ({
        name: state.user.nickName || '张三',
        position: '司机',
        deptId: state.user.deptId || 100
      })
    })
  },
  onLoad(options) {
    if (options.id) {
      this.taskId = options.id
      this.loadTaskDetail()
    } else {
      this.$modal.showToast('任务ID不能为空')
      setTimeout(() => {
        uni.navigateBack()
      }, 1500)
    }
  },
  methods: {
    // 加载任务详情
    loadTaskDetail() {
      if (!this.taskId) {
        this.$modal.showToast('任务ID不能为空')
        return
      }
      
      getTask(this.taskId).then(response => {
        this.taskDetail = response.data || response
        
        // 填充表单数据 - 福祉车任务特有字段
        if (this.taskDetail.welfareInfo) {
          this.taskForm.serviceTime = this.taskDetail.plannedStartTime || ''
          this.taskForm.passenger.contact = this.taskDetail.welfareInfo.passengerContact || ''
          this.taskForm.passenger.phone = this.taskDetail.welfareInfo.passengerPhone || ''
          this.taskForm.startAddress = this.taskDetail.welfareInfo.pickupAddress || ''
          this.taskForm.endAddress = this.taskDetail.welfareInfo.destinationAddress || ''
          this.taskForm.distance = this.taskDetail.welfareInfo.serviceDistance || ''
          this.taskForm.price = this.taskDetail.welfareInfo.servicePrice || ''
        }
        
        // 设置车辆信息
        if (this.taskDetail.assignedVehicles && this.taskDetail.assignedVehicles.length > 0) {
          const firstVehicle = this.taskDetail.assignedVehicles[0]
          this.selectedVehicleId = firstVehicle.vehicleId
        }
        
        // 设置地址坐标(使用mixin中的方法)
        if (this.taskDetail.departureLongitude && this.taskDetail.departureLatitude) {
          const startLocation = {
            lng: this.taskDetail.departureLongitude,
            lat: this.taskDetail.departureLatitude
          }
          this.setStartLocation(startLocation)
          // 同时保存到 startAddress 键
          this.addressCoordinates.startAddress = startLocation
        }
        if (this.taskDetail.destinationLongitude && this.taskDetail.destinationLatitude) {
          const endLocation = {
            lng: this.taskDetail.destinationLongitude,
            lat: this.taskDetail.destinationLatitude
          }
          this.setEndLocation(endLocation)
          // 同时保存到 endAddress 键
          this.addressCoordinates.endAddress = endLocation
        }
      }).catch(error => {
        console.error('加载任务详情失败:', error)
        this.$modal.showToast('加载任务详情失败')
        setTimeout(() => {
          uni.navigateBack()
        }, 1500)
      })
    },
    
    // 车辆选择变化
    onVehicleChange(vehicle) {
      // 组件已经处理了selectedVehicleId的更新
      console.log('选中车辆:', vehicle)
    },
    
    onOrganizationChange(e) {
      this.selectedOrganization = this.organizations[e.detail.value]
    },
    
    selectStartAddress() {
      this.mapSelectorType = 'startAddress'
      this.$refs.mapPopup.open()
    },
    
    selectEndAddress() {
      this.mapSelectorType = 'endAddress'
      this.$refs.mapPopup.open()
    },
    
    getInitialAddress() {
      return this.mapSelectorType === 'startAddress' 
        ? this.taskForm.startAddress 
        : this.taskForm.endAddress
    },
    
    onAddressSelected(address) {
      const formattedAddress = address.title + ' - ' + address.address
      
      if (this.mapSelectorType === 'startAddress') {
        this.taskForm.startAddress = formattedAddress
        const location = this.setLocationByAddress('start', address)
        // 同时保存到 startAddress 键
        this.addressCoordinates.startAddress = location
      } else if (this.mapSelectorType === 'endAddress') {
        this.taskForm.endAddress = formattedAddress
        const location = this.setLocationByAddress('end', address)
        // 同时保存到 endAddress 键
        this.addressCoordinates.endAddress = location
      }
      
      // 监听mixin中的距离计算完成事件
      this.$once('distance-calculated', (distance) => {
        this.taskForm.distance = this.formatDistance(distance)
      })
      
      this.closeMapSelector()
    },
    
    closeMapSelector() {
      this.$refs.mapPopup.close()
      this.mapSelectorType = ''
    },
    
    addStaff() {
      this.$modal.showToast('添加人员功能开发中')
    },
    
    removeStaff(index) {
      this.additionalStaff.splice(index, 1)
    },
    
    validateForm() {
      if (!this.selectedVehicleId) {
        this.$modal.showToast('请选择任务车辆')
        return false
      }
      
      if (!this.taskForm.passenger.contact) {
        this.$modal.showToast('请输入联系人')
        return false
      }
      
      if (!this.taskForm.passenger.phone) {
        this.$modal.showToast('请输入联系电话')
        return false
      }
      
      if (!this.taskForm.startAddress) {
        this.$modal.showToast('请选择出发地址')
        return false
      }
      
      if (!this.taskForm.endAddress) {
        this.$modal.showToast('请选择目的地址')
        return false
      }
      
      return true
    },
    
    buildSubmitData() {
      // 构建提交数据 - 使用与 TaskCreateVO 一致的结构
      const submitData = {
        taskId: this.taskId,
        taskType: 'WELFARE',
        vehicleIds: this.selectedVehicleId ? [this.selectedVehicleId] : [],
        plannedStartTime: this.taskForm.serviceTime,
        
        // 出发地地址和坐标
        departureAddress: this.taskForm.startAddress,
        departureLongitude: this.addressCoordinates.startAddress ? this.addressCoordinates.startAddress.lng : null,
        departureLatitude: this.addressCoordinates.startAddress ? this.addressCoordinates.startAddress.lat : null,
        
        // 目标地地址和坐标
        destinationAddress: this.taskForm.endAddress,
        destinationLongitude: this.addressCoordinates.endAddress ? this.addressCoordinates.endAddress.lng : null,
        destinationLatitude: this.addressCoordinates.endAddress ? this.addressCoordinates.endAddress.lat : null,
        
        // 距离和价格(主任务字段)
        distance: this.taskForm.distance ? parseFloat(this.taskForm.distance) : null,
        price: this.taskForm.price ? parseFloat(this.taskForm.price) : null,
        
        // 乘客信息(嵌套对象)
        passenger: {
          contact: this.taskForm.passenger.contact,
          phone: this.taskForm.passenger.phone
        }
      }
      
      return submitData
    },
    
    submitTask() {
      if (!this.validateForm()) {
        return
      }
      
      this.$modal.confirm('确定要保存修改吗?').then(() => {
        this.loading = true
        const submitData = this.buildSubmitData()
        
        updateTask(submitData).then(response => {
          this.loading = false
          this.$modal.showToast('任务更新成功')
          setTimeout(() => {
            uni.navigateBack()
          }, 1500)
        }).catch(error => {
          this.loading = false
          console.error('任务更新失败:', error)
          this.$modal.showToast('任务更新失败,请重试')
        })
      }).catch(() => {})
    },
    
    goBack() {
      uni.navigateBack()
    }
  }
}
</script>
 
<style lang="scss" scoped>
.edit-welfare-task-container {
  padding: 20rpx;
  background-color: #f5f5f5;
  min-height: 100vh;
  
  .form-header {
    display: flex;
    align-items: center;
    padding: 20rpx 0;
    margin-bottom: 30rpx;
    
    .back-btn {
      width: 60rpx;
      height: 60rpx;
      border-radius: 50%;
      background-color: #f0f0f0;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 20rpx;
    }
    
    .title {
      font-size: 36rpx;
      font-weight: bold;
      color: #333;
    }
  }
  
  .form-section {
    background-color: white;
    border-radius: 15rpx;
    padding: 30rpx;
    box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
    
    .form-section-title {
      font-size: 32rpx;
      font-weight: bold;
      margin: 40rpx 0 20rpx 0;
      padding-bottom: 10rpx;
      border-bottom: 1rpx solid #f0f0f0;
    }
    
    .form-item {
      margin-bottom: 40rpx;
      
      .form-label {
        font-size: 28rpx;
        margin-bottom: 15rpx;
        color: #333;
      }
      
      .form-input {
        height: 70rpx;
        padding: 0 20rpx;
        border: 1rpx solid #eee;
        border-radius: 10rpx;
        font-size: 28rpx;
        
        &.picker-input {
          display: flex;
          align-items: center;
          justify-content: space-between;
        }
      }
      
      .form-textarea {
        width: 100%;
        min-height: 150rpx;
        padding: 20rpx;
        border: 1rpx solid #eee;
        border-radius: 10rpx;
        font-size: 28rpx;
      }
      
      .staff-list {
        .staff-item {
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding: 20rpx;
          background-color: #f9f9f9;
          border-radius: 10rpx;
          margin-bottom: 20rpx;
        }
        
        .add-staff {
          display: flex;
          align-items: center;
          justify-content: center;
          padding: 20rpx;
          border: 1rpx dashed #007AFF;
          border-radius: 10rpx;
          color: #007AFF;
        }
      }
    }
    
    .form-actions {
      margin-top: 50rpx;
      text-align: center;
      
      .submit-btn {
        width: 80%;
        height: 80rpx;
        background-color: #007AFF;
        color: white;
        border-radius: 10rpx;
        font-size: 32rpx;
        
        &[disabled] {
          background-color: #ccc;
          color: #999;
        }
      }
    }
  }
  
  .loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 400rpx;
    color: #999;
    
    text {
      margin-top: 20rpx;
      font-size: 28rpx;
    }
  }
  
  .map-popup-container {
    height: 80vh;
    background-color: white;
    border-top-left-radius: 20rpx;
    border-top-right-radius: 20rpx;
    overflow: hidden;
    
    .popup-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 20rpx 30rpx;
      border-bottom: 1rpx solid #f0f0f0;
      
      .popup-title {
        font-size: 32rpx;
        font-weight: bold;
        color: #333;
      }
      
      .close-btn {
        width: 50rpx;
        height: 50rpx;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }
}
</style>