zhouwx
2025-05-19 457f9c817adef8b003ee6379f493798bae5cbb69
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
<template>
  <a-modal
      :title="title"
      :visible="visible"
      centered
      :confirm-loading="confirmLoading"
      width="50%"
      cancelText="取消"
      okText="确认"
      @ok="onSubmit"
      @cancel="handleCancel"
      :afterClose="clearMod"
  >
    <a-form-model ref="ruleForm" :rules="rules" :model="form" :label-col="labelCol" :wrapper-col="wrapperCol" :colon="false">
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="姓名" prop="name">
            <a-input v-model.trim="form.name" :disabled="isView"/>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="手机号码" prop="phone">
            <a-input v-model.trim="form.phone" :disabled="isView"/>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="所属单位" prop="company">
            <a-input v-model.trim="form.company" :disabled="isView"/>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="职务" prop="post">
            <a-input v-model.trim="form.post" :disabled="isView"/>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="行政区划">
            <a-tree-select
                v-model="areaId"
                style="width: 100%"
                :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                :tree-data="areaData"
                placeholder="行政区划"
                :replaceFields="replaceFields"
                @change="changeArea"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="组织架构">
            <a-select
                v-model.trim="form.orgStructureId"
                placeholder="选择组织架构"
                style="width: 100%"
                allowClear
                show-search
            >
              <a-select-option v-for="item in orgData" :value="item.id" :key="item.id">{{item.orgName}}</a-select-option>
            </a-select>
<!--            <a-input v-model="orgName" />-->
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="排位顺序">
            <a-input type="number" v-model.number="form.orderNum" :disabled="isView"/>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="24">
<!--          <template v-for="(item,index) in options">-->
            <a-form-model-item label="责任类别"  :label-col="{span: 3}" :wrapper-col="{span: 14}" prop="typeList">
              <a-checkbox-group
                  v-model="form.typeList"
                  :options="options"
                  @change="onTypeChange"
                  :disabled="isView"
                  style="width: 100%;"
              />
            </a-form-model-item>
<!--          </template>-->
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="24" v-for="(item,index) in options">
          <a-form-model-item v-if="item.children && item.children.length>0 && form.typeList.find(i=>i == item.label)" :label="item.label" :label-col="{span: 3}" :wrapper-col="{span: 14}" :prop="'tagList.' + item.label">
            <a-checkbox-group
                v-model="form.tagList[item.label]"
                :options="item.children"
                @change="onTagChange"
                :disabled="isView"
            />
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </a-modal>
</template>
 
<script>
import {addContactors, getStructure, updateContactors} from '@/api/contactBook'
import {verifySimplePhone} from "@/util/validate";
import {getDistrictInfo} from "@/api/login";
import subMenu from "@/views/Admin/contactBook/subMenu.vue";
import {getOrgData} from "@/api/user";
export default {
  name: 'addressUserMod',
  components: {subMenu},
  props: ['groupList'],
  data () {
    let validatePhone = (rule, value, callback)=>{
      if(value === ''){
        callback(new Error('请输入手机号'))
      }else{
        if(!verifySimplePhone(value)){
          callback(new Error('手机号格式有误'))
        }else{
          callback()
        }
      }
    }
 
    return {
      title: '新增责任人',
      visible: false,
      isView: false,
      confirmLoading: false,
      areaData: [],
      labelCol: { span: 6 },
      wrapperCol: { span: 14 },
      areaId: null,
      orgName: '',
      areaVal: [],
      orgData: [],
      fieldNames:{
        label: 'name',
        value: 'id',
        children: 'children'
      },
      form: {
        id: null,
        name: '',
        phone: '',
        company: '',
        post: '',
        orgStructureId: null,
        districtId: null,
        districtCode: '',
        orderNum: 0,
        typeList: [],
        tagList: {}
      },
      tagList: [],
      rules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur'}],
        phone: [{ required: true, validator: validatePhone, trigger: 'blur'}],
        company: [{ required: true, message: '请输入单位名称', trigger: 'blur'}],
        post: [{ required: true, message: '请输入职务', trigger: 'change'}],
        typeList: [{ required: true, message: '请选择责任类别', trigger: 'change'}],
        tagList: []
      },
      options: [
        {
          value: '党委政府',
          label: '党委政府',
          children: [
            {
              value: '主要领导',
              label: '主要领导',
            },
            {
              value: '分管领导',
              label: '分管领导',
            }
          ]
        },
        {
          value: '两委三部',
          label: '两委三部',
          children: [
            {
              value: '安委会成员单位',
              label: '安委会成员单位'
            },
            {
              value: '减灾委成员单位',
              label: '减灾委成员单位'
            },
            {
              value: '防汛抗旱指挥部',
              label: '防汛抗旱指挥部'
            },
            {
              value: '抗震救灾指挥部',
              label: '抗震救灾指挥部'
            },
            {
              value: '森林草原防灭火指挥部',
              label: '森林草原防灭火指挥部'
            }
          ]
        },
        {
          value: '应急部门',
          label: '应急部门',
          children: [
            {
              value: '应急部门',
              label: '应急部门'
            }
          ]
        },
        {
          value: '救援队伍',
          label: '救援队伍',
          children: [
            {
              value: '救援队伍',
              label: '救援队伍'
            }
          ]
        },
        {
          value: '灾害信息员',
          label: '灾害信息员',
          children: [
            {
              value: '灾害信息员',
              label: '灾害信息员'
            }
          ]
        },
        {
          value: '企业',
          label: '企业',
          children: [
            {
              value: '企业',
              label: '企业'
            }
          ]
        }
      ],
      replaceFields: {
        children:'children',
        title:'name',
        key:'id',
        value: 'id'
      },
      currentKey: [],
      openKeys: [],
 
    }
  },
  created() {
    const t = this
    // t.getDistrictInfo()
    t.getStructure()
  },
 
 
  methods:{
    openDialog(type,data,areaId,orgName,orgId) {
      const t = this
      t.changeArea(areaId)
      t.createRules()
      t.areaId = Number(areaId)
      t.orgName = orgName
 
      if(type == 'add'){
        t.title = '新增责任人'
        t.isView = false
        t.form = {
          id: null,
          name: '',
          phone: '',
          company: '',
          post: '',
          orgStructureId: Number(orgId),
          districtId: t.areaId,
          districtCode: t.findCodeById(t.areaData,t.areaId),
          orderNum: 0,
          typeList: [],
          tagList: {}
        }
      }else{
        for(let i in data){
          if(t.isValidKey(i,t.form)){
            t.form[i] = data[i]
          }
        }
        t.form.typeList = []
        t.form.tagList = {}
        t.form.typeList = data.orgUserTypes.map(i=>i.typeName)
        // for(let i of data.orgUserTypes){
        //   t.form.tagList[i.typeName] = i.orgUserTags.map(j=>j.tagName)
        // }
        // Vue.set方法添加新属性确保响应式
        for (let i of data.orgUserTypes) {
          t.$set(t.form.tagList, i.typeName, i.orgUserTags.map(j => j.tagName));
        }
        if(type == 'edit'){
          t.title = '编辑责任人'
          t.isView = false
        }else{
          t.title = '责任人详情'
          t.isView = true
        }
      }
      t.visible = true
    },
    onChange(value) {
      const t = this
 
    },
    isValidKey(key, object){
      return key in object;
    },
    createRules(){
      for(let i of this.options){
        this.rules.tagList[i.label] = (
            {
              required: true,
              validator: (rule, value, callback) => {
                if (!value || value.length === 0) {
                  callback(new Error('请选择标签类别'));
                } else {
                  callback();
                }
              },
              trigger: 'change'
            }
        )
      }
    },
    onTypeChange(checkedValue) {
      console.log(checkedValue,this.form.typeList,666);
    },
 
    onTagChange(checkedValue) {
      console.log(checkedValue,this.form.tagList,777);
    },
 
    clearMod(){
      this.$refs.ruleForm.clearValidate()
      this.$refs.ruleForm.resetFields()
      this.rules.tagList = []
    },
 
    onSubmit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          if(this.title == '新增责任人'){
            const {id,tagList,...data} = this.form
            data.typeList = this.form.typeList.map(item=>{
              const newTagList = this.form.tagList[item].map(i=>{
                return {
                  tagName: i
                }
              })
              return {
                typeName: item,
                tagList: newTagList
              }
            })
            addContactors(data).then((res)=>{
              if(res.data.code == 100){
                this.$message.success('新增责任人成功')
                this.$emit('refresh')
                this.visible = false
              }else{
                this.$message.error(res.data.msg)
              }
            })
          }else if(this.title == '编辑责任人'){
            const {tagList,...data} = this.form
            data.typeList = this.form.typeList.map(item=>{
              const newTagList = this.form.tagList[item].map(i=>{
                return {
                  tagName: i
                }
              })
              return {
                typeName: item,
                tagList: newTagList
              }
            })
            updateContactors(data).then((res)=>{
              if(res.data.code == 100){
                this.$message.success('修改责任人成功')
                this.$emit('refresh')
                this.visible = false
              }else{
                this.$message.error(res.data.msg)
              }
            })
          }else{
            this.visible = false
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
 
    async getStructure(){
      const t = this
      const res = await getStructure()
      if(res.data.code == 100){
        t.areaData = t.filterBranches(res.data.data,['自治区直辖县级行政区划'])
        // const firstItem = this.firstIdWithOrgStructures(res.data.data[0])
        // if(firstItem){
        //   t.openKeys =  [firstItem.id +'-'+  firstItem.name]
        //   t.currentId = firstItem.id
        //   t.currentKey = [firstItem.orgStructures[0].id +'-'+  firstItem.orgStructures[0].orgName]
        // }else{
        //   t.$message.warning('暂无可操作的单位,请先进入“组织架构维护页面”新建相应的本级单位')
        // }
      }else{
        t.$message.warning(res.data.msg)
      }
    },
    filterBranches(branches, targetNames) {
      return branches.filter(branch => {
        if (targetNames.includes(branch.name)) {
          return false; // 过滤掉当前分支
        }
        if (branch.children && branch.children.length > 0) {
          branch.children = this.filterBranches(branch.children, targetNames);
        }
        return true; // 保留当前分支
      });
    },
    async getDistrictInfo(){
      let res = await getStructure()
      if(res.data.code == 100){
        this.areaData = res.data.data
      } else {
        this.$message.warning(res.data.msg);
      }
    },
 
    findCodeById(data, id) {
      for (let item of data) {
        if (item.id === id) {
          return item.code;
        }
        if (item.children && item.children.length > 0) {
          const code = this.findCodeById(item.children, id);
          if (code) {
            return code;
          }
        }
      }
      return null; // 如果未找到,返回 null
    },
 
    handleCancel(e) {
      const t = this
      t.visible = false;
    },
    changeArea(val){
      const t = this
      t.form.districtId = val;
      t.form.districtCode = t.findCodeById(t.areaData,val)
      t.form.orgStructureId = null;
      getOrgData(val).then(res => {
        if(res.data.code === 100) {
          console.log("res",res)
          t.orgData = res.data.data
        }else {
          t.$message.warning(res.data.msg)
        }
      })
    }
  }
}
</script>
 
<style lang="less" scoped>
 
</style>