<template>
  <div class="qtReportTable">
    <el-table :header-cell-style="headerRowClass" :cell-style="tableRowClassName" style="width: 100%" :data="tData"
      :stripe="true" size="medium" height="400">
      <el-table-column label="待检验地物类型" property="待检验地物类型" align="center" width="160">
        <el-table-column label="样本真值" property="样本真值" align="center" width="160">
        </el-table-column>
      </el-table-column>
      <el-table-column v-for="(item, index) in tHeader" :key="index" :label="item" :property="item" align="center">
        <template slot-scope="scope">{{ scope.row[item] }}</template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  props: {
    errorMatrix: {
      require: false,
    },
    preResults: {
      require: false,
    },
    activeTabs: {
      require: true,
    },
  },
  data() {
    return {
      realCheckRes: [],
      tableKey: [],
      tData: [],
      tHeader: [],
    };
  },
  created() {
    this.creatTableData();
  },
  methods: {
    headerRowClass() {
      return "background: #E4E9F1;text-align:center";
    },
    tableRowClassName({ rowIndex }) {
      if ((rowIndex + 1) % 2 !== 0) {
        return "background:#F5F7FA;text-align:center";
      } else {
        return "background:#FFFFFF;text-align:center";
      }
    },
    creatTableData() {
      if (this.errorMatrix[0]) {
        const em = this.errorMatrix;
        const at = this.activeTabs;
        const pr = this.preResults;
        let tes = null;
        this.tHeader.push("样本真值");
        let rowHeaderRaw = em[em[0].length];
        for (let i = 0; i < rowHeaderRaw.length; i++) {
          this.tHeader.push(rowHeaderRaw[i]);
        }
        switch (at) {
          //像元数count
          case "pixel":
            tes = "count";
            this.tHeader.push("合计");
            break;
          case "userPre":
            tes = "count";
            this.tHeader.push("用户精度");
            break;
          // producerAccuracy(生产者精度)
          case "producerPre":
            tes = "count";
            break;
          // commissionErrors(错分误差)
          case "errorPre":
            tes = "count";
            this.tHeader.push("错分精度");
            break;
          // omissionErrors(漏分误差)
          case "LeakagePre":
            tes = "count";
            this.tHeader.push("漏分精度");
            break;
        }

        let rows = [];
        let colHeaderRaw = em[em[0].length + 1];
        let rcr = [];
        let all = 0;
        for (let i = 0; i < em[0].length; i++) {
          const ei = em[i];
          let objR = new Object();
          let tv = 0;
          let rowJson = {};
          rowJson[this.tHeader[0]] = colHeaderRaw[i];
          for (let j = 0; j < em[i].length; j++) {
            rowJson[this.tHeader[j + 1]] = parseFloat(ei[j][tes].toFixed(4));
            objR[[j]] = ei[j][tes];
            //计算合计值
            tv += ei[j][tes];
          }
          all += tv;
          if (at === "userPre" || at === "errorPre" || at === "LeakagePre")
            rowJson[this.tHeader[em[i].length + 1]] = parseFloat(
              pr[rowJson[this.tHeader[0]]].toFixed(3)
            );
          else rowJson[this.tHeader[em[i].length + 1]] = tv.toFixed(0);
          rows.push(rowJson);
          rcr.push(objR);
        }
        const newRcr = this.verticalTotal(rcr);
        newRcr.push(all);
        let lastRow = {};
        if (at === "pixel") {
          lastRow[this.tHeader[0]] = "合计";
          let m = 1;
          for (let i in newRcr) {
            lastRow[this.tHeader[m++]] = newRcr[i].toFixed(0);
          }
          rows.push(lastRow);
        } else if (at === "producerPre") {
          lastRow[this.tHeader[0]] = "生产者精度";
          for (var i in pr) {
            lastRow[i] = parseFloat(pr[i].toFixed(3));
          }
          rows.push(lastRow);
        }
        this.tHeader = this.tHeader.slice(1);
        this.tData = rows;
      }
    },
    // 数组根据key值进行合并
    newMap(a1, a2, key) {
      let ttt = [];
      for (let i in a1) {
        const rv = a1[i][key];
        let newObj = {};
        for (let j in a2) {
          const rv2 = a2[j][key];
          if (rv2 === rv) {
            ttt.push(Object.assign(newObj, a1[i], a2[j]));
          }
        }
      }
      return ttt;
    },
    // 数组对象根据key值去重
    deweight(arr, key) {
      let ret = [];
      arr.forEach((item) => {
        let compare = [];
        ret.forEach((retitem) => {
          compare.push(retitem[key]);
        });
        if (compare.indexOf(item[key]) === -1) {
          ret.push(item);
        }
      });
      return ret;
    },
    verticalTotal(matrix) {
      let result = [];
      let len = matrix.length;
      for (let i = 0; i < len; i++) {
        let sum = 0;
        for (let j = 0; j < len; j++) {
          sum += matrix[j][i];
        }
        result.push(sum);
      }
      return result;
    },
    arrSum(a) {
      let res = a.reduce((result, next) => {
        if (!result) result = {};
        Object.keys(next).forEach((key) => {
          //数值类型
          if (typeof next[key] == "number") {
            result[key] = (result[key] ? result[key] : 0) + next[key];
          }
          //数组类型
          if (next[key] instanceof Array) {
            result[key] = (result[key] ? result[key] : []).concat(next[key]);
          }
        });
        return result;
      });
      return res;
    },
    changeRowCodeToName(rowCode, rowCodeMap) {
      if (rowCode == null) return null;
      else return rowCodeMap[rowCode];
    },
  },
};
</script>
<style lang="less">
.qtReportTable {
  .el-table thead.is-group th {
    background: none;
    padding: 0px;
  }

  .el-table thead.is-group tr:first-of-type th:first-of-type {
    border-bottom: none;
  }

  .el-table thead.is-group tr:first-of-type th:first-of-type div.cell {
    text-align: right;
  }

  .el-table thead.is-group tr:last-of-type th:first-of-type div.cell {
    text-align: left;
  }

  .el-table thead.is-group tr:first-of-type th:first-of-type:before {
    content: "";
    position: absolute;
    width: 2px;
    height: 200px;
    top: 0;
    left: 0;
    background-color: #000;
    opacity: 0.3;
    display: block;
    transform: rotate(-73.5deg);
    -webkit-transform-origin: top;
    transform-origin: top;
  }

  .el-table thead.is-group tr:last-of-type th:first-of-type:before {
    content: "";
    position: absolute;
    width: 2px;
    height: 200px;
    bottom: 0;
    right: 0;
    background-color: #000;
    opacity: 0.3;
    display: block;
    transform: rotate(-74deg);
    -webkit-transform-origin: bottom;
    transform-origin: bottom;
  }
}
</style>