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
<template>
  <div class="rating-form">
    <div class="form-header">
      <h3>{{ ratingForm.schemeName || '评分表单' }}</h3>
      <div class="total-score">
        总分:{{ totalScore }} / {{ ratingForm.totalMaxScore }}
      </div>
    </div>
    
    <el-form 
      ref="formRef" 
      :model="formData" 
      :rules="rules" 
      label-width="120px"
      class="rating-form-content"
    >
      <!-- 评分项目 -->
      <div v-if="ratingForm.items && ratingForm.items.length > 0" class="rating-items">
        <div 
          v-for="item in ratingForm.items" 
          :key="item.id" 
          class="rating-item"
        >
          <el-form-item 
            :label="item.name" 
            :prop="`scores.${item.id}`"
            class="score-item"
          >
            <div class="score-input-group">
              <el-input-number
                v-model="formData.scores[item.id]"
                :min="0"
                :max="item.maxScore"
                :precision="1"
                :step="0.5"
                placeholder="请输入分数"
                class="score-input"
              />
              <span class="max-score">/ {{ item.maxScore }}</span>
            </div>
            <div v-if="item.description" class="item-description">
              {{ item.description }}
            </div>
          </el-form-item>
        </div>
      </div>
      
      <!-- 评语 -->
      <el-form-item label="评语" prop="comment" class="comment-item">
        <el-input
          v-model="formData.comment"
          type="textarea"
          :rows="4"
          placeholder="请输入评语(选填)"
          maxlength="500"
          show-word-limit
        />
      </el-form-item>
      
      <!-- 提交按钮 -->
      <el-form-item class="submit-item">
        <el-button 
          type="primary" 
          size="large" 
          @click="handleSubmit"
          :loading="submitting"
          class="submit-btn"
        >
          提交评分
        </el-button>
        <el-button 
          size="large" 
          @click="handleReset"
          class="reset-btn"
        >
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
 
<script setup>
import { ref, reactive, computed, watch } from 'vue'
import { ElForm, ElFormItem, ElInputNumber, ElInput, ElButton, ElMessage } from 'element-plus'
 
const props = defineProps({
  ratingForm: {
    type: Object,
    required: true
  },
  activityPlayerId: {
    type: [String, Number],
    required: true
  }
})
 
const emit = defineEmits(['submit'])
 
const formRef = ref()
const submitting = ref(false)
 
// 表单数据
const formData = reactive({
  scores: {},
  comment: ''
})
 
// 初始化分数对象
const initScores = () => {
  if (props.ratingForm.items) {
    props.ratingForm.items.forEach(item => {
      formData.scores[item.id] = null
    })
  }
}
 
// 表单验证规则
const rules = computed(() => {
  const scoreRules = {}
  if (props.ratingForm.items) {
    props.ratingForm.items.forEach(item => {
      scoreRules[`scores.${item.id}`] = [
        { 
          required: true, 
          message: `请输入${item.name}的分数`, 
          trigger: 'blur' 
        },
        {
          type: 'number',
          min: 0,
          max: item.maxScore,
          message: `分数应在0-${item.maxScore}之间`,
          trigger: 'blur'
        }
      ]
    })
  }
  
  return scoreRules
})
 
// 计算总分
const totalScore = computed(() => {
  let total = 0
  Object.values(formData.scores).forEach(score => {
    if (typeof score === 'number' && !isNaN(score)) {
      total += score
    }
  })
  return Math.round(total * 10) / 10 // 保留一位小数
})
 
// 提交评分
const handleSubmit = async () => {
  try {
    await formRef.value.validate()
    
    submitting.value = true
    
    const ratingData = {
      activityPlayerId: props.activityPlayerId,
      scores: formData.scores,
      comment: formData.comment,
      totalScore: totalScore.value
    }
    
    emit('submit', ratingData)
    
  } catch (error) {
    console.error('表单验证失败:', error)
    ElMessage.error('请检查表单填写是否完整')
  } finally {
    submitting.value = false
  }
}
 
// 重置表单
const handleReset = () => {
  formRef.value.resetFields()
  initScores()
}
 
// 监听评分表单变化,重新初始化
watch(() => props.ratingForm, () => {
  initScores()
}, { immediate: true })
</script>
 
<style scoped>
.rating-form {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
 
.form-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 1px solid #EBEEF5;
}
 
.form-header h3 {
  margin: 0;
  color: #303133;
  font-size: 18px;
  font-weight: 600;
}
 
.total-score {
  font-size: 16px;
  font-weight: 600;
  color: #409EFF;
  background: #ECF5FF;
  padding: 8px 16px;
  border-radius: 20px;
}
 
.rating-items {
  margin-bottom: 20px;
}
 
.rating-item {
  margin-bottom: 20px;
  padding: 16px;
  background: #FAFAFA;
  border-radius: 8px;
  border-left: 4px solid #409EFF;
}
 
.score-item :deep(.el-form-item__label) {
  font-weight: 600;
  color: #303133;
}
 
.score-input-group {
  display: flex;
  align-items: center;
  gap: 8px;
}
 
.score-input {
  width: 120px;
}
 
.max-score {
  color: #909399;
  font-size: 14px;
}
 
.item-description {
  margin-top: 8px;
  color: #606266;
  font-size: 13px;
  line-height: 1.4;
}
 
.comment-item {
  margin-bottom: 30px;
}
 
.submit-item {
  text-align: center;
  margin-bottom: 0;
}
 
.submit-btn {
  width: 120px;
  margin-right: 16px;
}
 
.reset-btn {
  width: 80px;
}
 
:deep(.el-form-item__label) {
  font-weight: 500;
}
 
:deep(.el-textarea__inner) {
  resize: vertical;
}
</style>