editMaintainanceItem.vue
5.19 KB
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
<template>
<el-dialog :title="$t('maintainanceItem.edit.title')" :visible.sync="visible" width="50%" @close="handleClose">
<el-form ref="form" :model="form" :rules="rules" label-width="120px">
<el-form-item :label="$t('maintainanceItem.form.titleType')" prop="titleType">
<el-select v-model="form.titleType" :placeholder="$t('maintainanceItem.form.titleTypePlaceholder')"
style="width:100%" @change="handleTitleTypeChange">
<el-option v-for="item in titleTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
</el-form-item>
<el-form-item :label="$t('maintainanceItem.form.itemTitle')" prop="itemTitle">
<el-input v-model="form.itemTitle" :placeholder="$t('maintainanceItem.form.itemTitlePlaceholder')" />
</el-form-item>
<template v-if="form.titleType && form.titleType !== '3003'">
<el-form-item v-for="(item, index) in form.titleValues" :key="index"
:label="`${$t('maintainanceItem.form.option')} ${index + 1}`" :prop="'titleValues.' + index + '.itemValue'"
:rules="{
required: true,
message: $t('maintainanceItem.form.optionRequired'),
trigger: 'blur'
}">
<el-row :gutter="10">
<el-col :span="20">
<el-input v-model="item.itemValue" :placeholder="$t('maintainanceItem.form.optionPlaceholder')" />
</el-col>
<el-col :span="3">
<el-button v-if="index === form.titleValues.length - 1" type="text" @click="handleAddOption">
{{ $t('common.add') }}
</el-button>
<el-button v-else type="text" @click="handleRemoveOption(index)">
{{ $t('common.delete') }}
</el-button>
</el-col>
</el-row>
</el-form-item>
</template>
<el-form-item :label="$t('maintainanceItem.form.seq')" prop="seq">
<el-input v-model.number="form.seq" :placeholder="$t('maintainanceItem.form.seqPlaceholder')" />
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="visible = false">
{{ $t('common.cancel') }}
</el-button>
<el-button type="primary" @click="handleSubmit">
{{ $t('common.confirm') }}
</el-button>
</span>
</el-dialog>
</template>
<script>
import { updateMaintainanceItem } from '@/api/inspection/maintainanceItemApi'
export default {
name: 'EditMaintainanceItem',
data() {
return {
visible: false,
form: {
titleId: '',
titleType: '',
itemTitle: '',
seq: '',
titleValues: [],
communityId: ''
},
rules: {
itemTitle: [
{ required: true, message: this.$t('maintainanceItem.form.itemTitleRequired'), trigger: 'blur' }
],
titleType: [
{ required: true, message: this.$t('maintainanceItem.form.titleTypeRequired'), trigger: 'change' }
],
seq: [
{ required: true, message: this.$t('maintainanceItem.form.seqRequired'), trigger: 'blur' },
]
},
titleTypeOptions: [
{ value: '1001', label: this.$t('maintainanceItem.titleType.single') },
{ value: '2002', label: this.$t('maintainanceItem.titleType.multiple') },
{ value: '3003', label: this.$t('maintainanceItem.titleType.shortAnswer') }
]
}
},
methods: {
open(data) {
//this.resetForm()
if (data) {
this.form = { ...data }
this.form.titleValues = data.titleValues || []
console.log(this.form, data)
}
this.visible = true
},
resetForm() {
this.form = {
titleId: '',
titleType: '',
itemTitle: '',
seq: '',
titleValues: [],
communityId: ''
}
this.$nextTick(() => {
this.$refs.form && this.$refs.form.resetFields()
})
},
handleClose() {
this.resetForm()
},
handleTitleTypeChange(value) {
if (value === '3003') {
this.form.titleValues = []
} else if (value === '1001' && (!this.form.titleValues || this.form.titleValues.length === 0)) {
this.form.titleValues = [{ itemValue: '', seq: 1 }]
} else if (value === '2002' && (!this.form.titleValues || this.form.titleValues.length === 0)) {
this.form.titleValues = [
{ itemValue: '', seq: 1 },
{ itemValue: '', seq: 2 }
]
}
},
handleAddOption() {
this.form.titleValues.push({
itemValue: '',
seq: this.form.titleValues.length + 1
})
},
handleRemoveOption(index) {
this.form.titleValues.splice(index, 1)
// 重新排序
this.form.titleValues.forEach((item, i) => {
item.seq = i + 1
})
},
handleSubmit() {
this.$refs.form.validate(async valid => {
if (valid) {
try {
await updateMaintainanceItem(this.form)
this.$message.success(this.$t('common.operationSuccess'))
this.$emit('success')
this.visible = false
} catch (error) {
this.$message.error(error.message || this.$t('common.updateFailed'))
}
}
})
}
}
}
</script>