<template>
  <mx-table :prop-defines="scoreWeightDefines" :rows="settingModel.importWeight">
    <template #weight="{row}">
      <el-input-number :min="0" :max="1" :step="0.1" controls v-model="row.weight"
                       style="width: 120px"></el-input-number>
    </template>
    <template #delete="{row}">
      <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row)">删除
      </el-button>
    </template>
  </mx-table>
</template>

<script>
export default {
  name: 'round-setting-weight',
  props: {
    settingModel: {
      type: Object,
      default: _ => ({})
    }
  },
  data() {
    return {
      scoreWeightDefines: {
        importId: {
          label: '编号'
        },
        scoreName: {
          label: '名称'
        },
        type: {
          label: '类型'
        },
        weight: {
          label: '权重',
          slot: 'weight'
        },
        action: {
          label: '操作',
          slot: 'delete'
        }
      }
    }
  },
  methods: {
    shouldGoNext() {
      const sumWeight = this.settingModel.importWeight.sum(s => s.weight)
      if (sumWeight != 1) {
        const error = '全部成绩的权重和必须为1'
        this.$alert(error, '错误提示', { type: 'error' })
        return Promise.reject(error)
      }
      return Promise.resolve(this.settingModel)
    },
    handleDelete(row) {
      this.$confirm(`确定删除[${row.scoreName}]`).then(_ => {
        this.settingModel.importWeight.remove(row)
        if (this.settingModel.importWeight.length == 0) {
          // 没有成绩之后步骤自动后退
          this.$emit('resetStep')
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
