Fixiaobai
2023-10-25 ae87b881865d0b6812aba2a8928cadb9ef6dd62a
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
<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      label-width="80px"
      class="l-mes"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="编号" prop="factoryNo">
            <el-input
              v-model="dataForm.factoryNo"
              placeholder="编号"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="名称" prop="factoryName">
            <el-input
              v-model="dataForm.factoryName"
              placeholder="名称"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="城市" prop="city">
            <template>
              <div id="app">
                <el-cascader
                  size="small"
                  :options="options"
                  v-model="dataForm.city"
                  @change="handleChange"
                  style="width: 100%;"
                >
                </el-cascader>
              </div>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公司" prop="companyId">
            <el-select
              v-model="dataForm.companyId"
              filterable
              placeholder="请选择"
              style="width: 100%;"
            >
              <el-option
                v-for="(item, index) in this.companyOptions"
                :key="index"
                :label="item.companyName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button
        type="primary"
        :disabled="isSubmit"
        v-thinclick="`dataFormSubmit`"
        >确定</el-button
      >
    </span>
  </el-dialog>
</template>
 
<script>
import { regionData, CodeToText } from 'element-china-area-data'
import {
  getObj,
  addObj,
  putObj,
  loadCompany,
  dataFormTransfer
} from '@/api/basic/factory'
 
export default {
  data() {
    return {
      options: regionData,
      visible: false,
      dataForm: {
        id: 0,
        factoryName: '',
        factoryNo: '',
        selectCityOptionsStr: '',
        city: [],
        companyId: ''
      },
      dataRule: {
        factoryName: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        factoryNo: [
          { required: true, message: '编号不能为空', trigger: 'blur' }
        ],
        city: [{ required: true, message: '城市不能为空', trigger: 'blur' }],
        companyId: [
          { required: true, message: '公司不能为空', trigger: 'blur' }
        ]
      },
      companyOptions: [],
      isSubmit: false
    }
  },
  methods: {
    handleChange() {
      let buffer = ''
      this.dataForm.city.forEach((item, i) => {
        buffer += item + ','
      })
      buffer = buffer.substr(0, buffer.length - 1)
      this.dataForm.selectCityOptionsStr = buffer
    },
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
        if (this.dataForm.id) {
          getObj(this.dataForm.id).then((response) => {
            this.dataForm = response.data.data
            this.dataForm.city = response.data.data.city.split(',')
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.isSubmit = true
      const transferData = {
        factoryName: this.dataForm.factoryName,
        factoryNo: this.dataForm.factoryNo,
        city: this.dataForm.selectCityOptionsStr,
        companyId: this.dataForm.companyId,
        id: this.dataForm.id
      }
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          dataFormTransfer(transferData).then((res) => {
            if (
              res.data.data === true &&
              res.data.code === 0 &&
              this.dataForm.id === 0
            ) {
              this.$message.success('添加成功!')
              this.visible = false
              this.$emit('refreshDataList')
            } else if (
              res.data.data === true &&
              res.data.code === 0 &&
              this.dataForm.id != 0
            ) {
              this.$message.success('修改成功!')
              this.visible = false
              this.$emit('refreshDataList')
            }
            this.isSubmit = false
          })
        } else {
          this.isSubmit = false
        }
      })
    },
    initCompanySelect() {
      loadCompany().then((res) => {
        this.companyOptions = res.data
      })
    }
  },
  created() {
    this.initCompanySelect()
  }
}
</script>
<style scoped></style>