Mr.Zhang
2023-08-15 38dff69b2c218ae53b4cc79257368139a2803ebc
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
<template>
  <GlobalWindow
    :title="title"
    :visible.sync="visible"
    :confirm-working="isWorking"
    @confirm="confirm"
  >
    <el-form :model="form" ref="form" :rules="rules">
      <el-form-item label="设备:" prop="deviceId">
        <el-select v-model="form.deviceId" clearable filterable placeholder="请选择">
          <el-option
            v-for="(item, index) in device()"
            :key="index"
            :label="'【' + item.code+' 】'+ item.name"
            :value="item.id">
            <span class="long-title-style" :title="'【' + item.code + '】'+ item.name">【{{ item.code }}】{{item.name}}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属员工:" prop="userIds">
        <el-select v-model="form.userIds" multiple clearable filterable placeholder="请选择" @change="selectUser">
          <el-option
            v-for="(item, index) in select()"
            :key="item.id"
            :label="index==0?'全部':(item.name + '-' + item.dmodel.name + '-' + item.phone)"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </GlobalWindow>
</template>
 
<script>
import BaseOpera from '@/components/base/BaseOpera'
import GlobalWindow from '@/components/common/GlobalWindow'
 
export default {
  name: 'OperaUserDeviceExtWindow',
  extends: BaseOpera,
  components: { GlobalWindow },
  data () {
    return {
      // 表单数据
      form: {
        id: null,
        userIds: [],
        deviceId: null
      },
      // device: [],
      // user: [],
      // 验证规则
      rules: {
        userIds: [
          { required: true, message: '请选择员工', trigger: 'change' }
        ],
        deviceId: [
          { required: true, message: '请选择设备', trigger: 'change' }
        ]
      }
    }
  },
  inject: ['device', 'user'],
  created () {
    this.config({
      api: '/ext/userDeviceExt',
      'field.id': 'id'
    })
  },
  methods: {
    /**
     * 确认新建
     *
     * @private
     */
    __confirmCreate () {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return
        }
        const form = JSON.parse(JSON.stringify(this.form))
        form.userIds = form.userIds.join(',')
        // 调用新建接口
        this.isWorking = true
        this.api.create(form)
          .then(() => {
            this.visible = false
            this.$tip.apiSuccess('新建成功')
            this.$emit('success')
          })
          .catch(e => {
            this.$tip.apiFailed(e)
          })
          .finally(() => {
            this.isWorking = false
          })
      })
    },
    select() {
      if (this.user().length > 0) {
        return [{id:'all'} ,...this.user()]
      }
      return []
    },
    selectUser (v) {
      console.log(v);
      if (v.indexOf('all') != -1) {
        console.log('all');
        let all = this.user().map(item=> item.id)
        this.form.userIds = all.filter(item => item!='all')
      }
    }
    // selectDevice (v) {
    //   console.log(v)
    //   this.userIds = []
    //   getUserDevice({ devmodelId: v })
    //     .then(res => {
    //       this.user = res
    //     })
    //     .catch(err => {
    //       console.log(err)
    //     })
    // }
  }
}
</script>