<template>
  <div class="comment-ability">
    <el-table ref="TableList" :data="dataList">
      <el-table-column
        prop="levelCode"
        label="等级"
        width="60"
      ></el-table-column>
      <el-table-column label="划分规划" width="310">
        <div
          slot-scope="scope"
          :class="[
            'rate-input',
            { 'rate-input-offset-left': scope.$index === dataList.length - 1 }
          ]"
        >
          <el-input-number
            v-if="scope.$index !== dataList.length - 1"
            v-model="scope.row.startRate"
            placeholder="请输入"
            :min="0"
            :max="100"
            :step="1"
            step-strictly
            size="small"
            :controls="false"
            @change="rateChange"
          ></el-input-number>
          <span v-if="scope.$index !== dataList.length - 1">≤</span>
          <span>百分位等级TOP</span>
          <span>＜</span>
          <el-input-number
            v-model="scope.row.endRate"
            placeholder="请输入"
            :min="0"
            :max="100"
            :step="1"
            step-strictly
            size="small"
            :controls="false"
            @change="rateChange"
          ></el-input-number>
        </div>
      </el-table-column>
      <el-table-column
        prop="levelName"
        label="水平层次"
        width="140"
      ></el-table-column>
      <el-table-column prop="result" label="诊断结果"></el-table-column>
      <el-table-column prop="advice" label="学习建议"></el-table-column>
      <el-table-column
        class-name="action-column"
        label="操作"
        width="80px"
        align="center"
      >
        <template slot-scope="scope">
          <el-button class="btn-primary" type="text" @click="toEdit(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- ModifyAbilityComment -->
    <modify-ability-comment
      ref="ModifyAbilityComment"
      :instance="curRow"
      @modified="modified"
    ></modify-ability-comment>
  </div>
</template>

<script>
import ModifyAbilityComment from "./ModifyAbilityComment.vue";

export default {
  name: "comment-ability",
  components: { ModifyAbilityComment },
  props: {
    rates: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  data() {
    return {
      dataList: [],
      curRow: {}
    };
  },
  mounted() {
    this.dataList = this.rates.map(item => {
      return { ...item };
    });
  },
  methods: {
    toEdit(row) {
      this.curRow = row;
      this.$refs.ModifyAbilityComment.open();
    },
    modified(data) {
      const ind = this.dataList.findIndex(
        item => item.levelCode === data.levelCode
      );
      const row = this.$objAssign(this.dataList[ind], data);
      this.dataList.splice(ind, 1, row);
      this.rateChange();
    },
    rateChange() {
      this.$emit("data-change", this.dataList);
    }
  }
};
</script>
