lrj
2 天以前 c61d4fe27c97d2ecc907756aa571a4ef14a7b9b6
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
<template>
  <div class="rating-scheme-form">
    <!-- 页面标题 -->
    <div class="page-header">
      <h2>{{ isEdit ? '编辑评分模板' : '新增评分模板' }}</h2>
      <el-button @click="handleBack">返回列表</el-button>
    </div>
 
    <!-- 表单内容 -->
    <div class="form-container">
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="120px"
        v-loading="loading"
      >
        <!-- 基本信息 -->
        <el-card class="form-card" header="基本信息">
          <el-form-item label="模板名称" prop="name">
            <el-input
              v-model="form.name"
              placeholder="请输入模板名称"
              maxlength="30"
              show-word-limit
            />
          </el-form-item>
          
          <el-form-item label="模板描述" prop="description">
            <el-input
              v-model="form.description"
              type="textarea"
              :rows="3"
              placeholder="请输入模板描述"
              maxlength="500"
              show-word-limit
            />
          </el-form-item>
        </el-card>
 
        <!-- 评分条目 -->
        <el-card class="form-card" header="评分条目">
          <div class="items-header">
            <span>评分条目配置</span>
            <el-button type="primary" size="small" @click="handleAddItem">
              <el-icon><Plus /></el-icon>
              添加条目
            </el-button>
          </div>
 
          <div class="items-list" v-if="form.items && form.items.length > 0">
            <div
              v-for="(item, index) in form.items"
              :key="index"
              class="item-row"
            >
              <el-row :gutter="20" align="middle">
                <el-col :span="1">
                  <span class="item-index">{{ index + 1 }}</span>
                </el-col>
                <el-col :span="8">
                  <el-form-item
                    :prop="`items.${index}.name`"
                    :rules="itemRules.name"
                    label-width="0"
                  >
                    <el-input
                      v-model="item.name"
                      placeholder="条目名称"
                      maxlength="30"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item
                    :prop="`items.${index}.maxScore`"
                    :rules="itemRules.maxScore"
                    label-width="0"
                  >
                    <el-input-number
                      v-model="item.maxScore"
                      :min="1"
                      :max="100"
                      placeholder="最大分值"
                      style="width: 100%"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <div class="item-actions">
                    <el-button
                      type="text"
                      size="small"
                      @click="handleMoveUp(index)"
                      :disabled="index === 0"
                    >
                      <el-icon><ArrowUp /></el-icon>
                    </el-button>
                    <el-button
                      type="text"
                      size="small"
                      @click="handleMoveDown(index)"
                      :disabled="index === form.items.length - 1"
                    >
                      <el-icon><ArrowDown /></el-icon>
                    </el-button>
                    <el-button
                      type="text"
                      size="small"
                      @click="handleRemoveItem(index)"
                      style="color: #f56c6c"
                    >
                      <el-icon><Delete /></el-icon>
                    </el-button>
                  </div>
                </el-col>
                <el-col :span="3">
                  <div class="item-score">
                    <el-tag type="success">{{ item.maxScore || 0 }}分</el-tag>
                  </div>
                </el-col>
              </el-row>
            </div>
          </div>
 
          <div v-else class="empty-items">
            <el-empty description="暂无评分条目,请添加条目" />
          </div>
 
          <!-- 总分显示 -->
          <div class="total-score">
            <span>模板总分:</span>
            <el-tag type="primary" size="large">{{ totalScore }}分</el-tag>
          </div>
        </el-card>
 
        <!-- 操作按钮 -->
        <div class="form-actions">
          <el-button type="primary" @click="handleSubmit" :loading="submitting">
            {{ isEdit ? '更新' : '保存' }}
          </el-button>
          <el-button @click="handleBack">取消</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>
 
<script>
import { ref, reactive, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Plus, ArrowUp, ArrowDown, Delete } from '@element-plus/icons-vue'
import { getRatingScheme, saveRatingScheme } from '@/api/rating'
 
export default {
  name: 'RatingSchemeForm',
  components: {
    Plus,
    ArrowUp,
    ArrowDown,
    Delete
  },
  setup() {
    const router = useRouter()
    const route = useRoute()
    
    // 响应式数据
    const formRef = ref()
    const loading = ref(false)
    const submitting = ref(false)
    
    const form = reactive({
      id: null,
      name: '',
      description: '',
      items: []
    })
 
    // 表单验证规则
    const rules = {
      name: [
        { required: true, message: '请输入模板名称', trigger: 'blur' },
        { min: 1, max: 30, message: '模板名称长度在1到30个字符', trigger: 'blur' }
      ]
    }
 
    const itemRules = {
      name: [
        { required: true, message: '请输入条目名称', trigger: 'blur' },
        { min: 1, max: 30, message: '条目名称长度在1到30个字符', trigger: 'blur' }
      ],
      maxScore: [
        { required: true, message: '请输入最大分值', trigger: 'blur' },
        { type: 'number', min: 1, max: 100, message: '分值范围在1到100之间', trigger: 'blur' }
      ]
    }
 
    // 计算属性
    const isEdit = computed(() => !!route.params.id)
    
    const totalScore = computed(() => {
      return form.items.reduce((sum, item) => sum + (item.maxScore || 0), 0)
    })
 
    // 加载数据
    const loadData = async () => {
      if (!isEdit.value) return
      
      try {
        loading.value = true
        const data = await getRatingScheme(route.params.id)
        
        form.id = data.id
        form.name = data.name
        form.description = data.description
        form.items = data.items || []
      } catch (error) {
        console.error('加载评分模板失败:', error)
        ElMessage.error('加载数据失败')
        handleBack()
      } finally {
        loading.value = false
      }
    }
 
    // 添加条目
    const handleAddItem = () => {
      form.items.push({
        id: null,
        name: '',
        maxScore: 10,
        orderNo: form.items.length + 1
      })
    }
 
    // 删除条目
    const handleRemoveItem = (index) => {
      form.items.splice(index, 1)
      // 重新设置排序号
      form.items.forEach((item, idx) => {
        item.orderNo = idx + 1
      })
    }
 
    // 上移条目
    const handleMoveUp = (index) => {
      if (index > 0) {
        const temp = form.items[index]
        form.items[index] = form.items[index - 1]
        form.items[index - 1] = temp
        
        // 重新设置排序号
        form.items.forEach((item, idx) => {
          item.orderNo = idx + 1
        })
      }
    }
 
    // 下移条目
    const handleMoveDown = (index) => {
      if (index < form.items.length - 1) {
        const temp = form.items[index]
        form.items[index] = form.items[index + 1]
        form.items[index + 1] = temp
        
        // 重新设置排序号
        form.items.forEach((item, idx) => {
          item.orderNo = idx + 1
        })
      }
    }
 
    // 提交表单
    const handleSubmit = async () => {
      try {
        // 验证表单
        await formRef.value.validate()
        
        // 验证至少有一个条目
        if (!form.items || form.items.length === 0) {
          ElMessage.error('请至少添加一个评分条目')
          return
        }
 
        submitting.value = true
        
        // 准备提交数据
        const submitData = {
          id: form.id,
          name: form.name,
          description: form.description,
          items: form.items.map((item, index) => ({
            id: item.id,
            name: item.name,
            maxScore: item.maxScore,
            orderNo: index + 1
          }))
        }
        
        await saveRatingScheme(submitData)
        
        ElMessage.success(isEdit.value ? '更新成功' : '保存成功')
        handleBack()
      } catch (error) {
        console.error('保存评分模板失败:', error)
        ElMessage.error(error.message || '保存失败')
      } finally {
        submitting.value = false
      }
    }
 
    // 返回列表
    const handleBack = () => {
      router.push('/rating-scheme')
    }
 
    // 初始化
    onMounted(() => {
      loadData()
    })
 
    return {
      formRef,
      loading,
      submitting,
      form,
      rules,
      itemRules,
      isEdit,
      totalScore,
      handleAddItem,
      handleRemoveItem,
      handleMoveUp,
      handleMoveDown,
      handleSubmit,
      handleBack
    }
  }
}
</script>
 
<style scoped>
.rating-scheme-form {
  padding: 20px;
}
 
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
 
.page-header h2 {
  margin: 0;
  color: #303133;
}
 
.form-container {
  max-width: 1000px;
}
 
.form-card {
  margin-bottom: 20px;
}
 
.items-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  font-weight: bold;
}
 
.items-list {
  margin-bottom: 20px;
}
 
.item-row {
  margin-bottom: 15px;
  padding: 15px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  background: #fafafa;
}
 
.item-index {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  background: #409eff;
  color: white;
  border-radius: 50%;
  font-size: 12px;
  font-weight: bold;
}
 
.item-actions {
  display: flex;
  gap: 5px;
  justify-content: center;
}
 
.item-score {
  text-align: center;
}
 
.empty-items {
  margin: 40px 0;
}
 
.total-score {
  text-align: right;
  padding: 15px 0;
  border-top: 1px solid #ebeef5;
  font-size: 16px;
  font-weight: bold;
}
 
.form-actions {
  text-align: center;
  padding: 20px 0;
}
 
.form-actions .el-button {
  margin: 0 10px;
  min-width: 100px;
}
</style>