<template>
  <!-- 记录事项 -->
  <div class="detail-dialog">
    <!-- <el-dialog :title='`${major}${tabName}记录事项`' width='80%' :visible.sync='visible' style='height:900px;overflow-y:scroll;'> -->
    <div class="bottom-table" style="height: 100%;">
      <el-table
        :data="tableData"
        :height="tableHeight"
        :border="true"
        style="width: 100%; margin-bottom: 12px;"
        :span-method="objectSpanMethod"
        v-if="tableData.length > 0"
      >
        <el-table-column label="位置" prop width="230" show-overflow-tooltip resizable>
          <template slot-scope="{ row }">{{ row.smswz || '--' }}</template>
        </el-table-column>
        <el-table-column label="设备" width="280" resizable>
          <template slot-scope="{ row }">{{ row.smsasset || '--' }}</template>
        </el-table-column>
        <el-table-column label="记录事项" show-overflow-tooltip resizable>
          <template slot-scope="{ row , $index }">
            <p class="p-style" v-for="(item, index) in row.children" :key="item.sn">
              <span v-if="item.lable==1">{{ index + 1 }}) {{ item.matter }}</span>
            </p>
            <p>
              <span
                class="morestyle"
                @click="changeOpensta($index)"
                v-if="row.showNewlist && row.close"
              >
                <i>更多</i>
                <i v-if="row.close" class="el-icon-caret-bottom"></i>
              </span>
            </p>
            <section>
              <p class="p-style" v-for="(item, index) in row.children" :key="item.sn">
                <span v-if="item.lable==2 && !row.close">{{ index + 1 }}) {{ item.matter }}</span>
              </p>
            </section>
            <p>
              <span
                class="morestyle"
                @click="changeOpensta($index)"
                v-if="row.showNewlist && !row.close"
              >
                <i>收起</i>
                <i class="el-icon-caret-top"></i>
              </span>
            </p>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip resizable>
          <template slot="header" slot-scope="scope">
            <p>维护结果</p>
            <p>(本年度内此工作出现次数/此工作涉及到的设备数量)</p>
          </template>
          <template slot-scope="{ row }">
            <p
              class="p-style"
              v-for="(item, index) in row.children"
              @click="goTabs(row,item)"
              :key="index"
            >
              <span class="lastp">
                <span
                  v-if="item.lable==1"
                >{{ item.workNum?item.workNum:'-' }} / {{ item.equipmentNum?item.equipmentNum:'-'}}</span>
                <span
                  v-if="item.lable==2 && !row.close"
                >{{ item.workNum?item.workNum:'-' }} / {{ item.equipmentNum?item.equipmentNum:'-'}}</span>
              </span>
            </p>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { queryRecord } from "@/api/equipmentList.js";

export default {
  props: {
    paramsdata: {
      type: Array,
      default: () => {
        return [];
      }
    },
    smsxt: {
      type: [Number, String],
      default: "1002"
    }
  },
  data() {
    return {
      visible: false,
      major: "",
      tabName: "",
      tableData: []
    };
  },
  computed: {
    newparamsdata() {
      return this.paramsdata.slice(1, this.paramsdata.length - 1).map(item => {
        return {
          system_code: this.smsxt,
          apply: item.param.apply,
          plazaId: this.$store.state.plazaId,
          wznw: item.param.wznw,
          difference: item.param.difference,
          tab_code: item.param.tab_code
        };
      });
    },
    tableHeight() {
      if (window.screen.height <= 768) {
        return 560;
      } else {
        return 800;
      }
    },
    height() {
      if (window.screen.height <= 768) {
        return 500;
      } else {
        return 600;
      }
    }
  },
  methods: {
    changeOpensta(v) {
      this.tableData[v].close = !this.tableData[v].close;
    },
    goTabs(val, val2) {
      this.$emit("change-table", {
        tab_code: val.tab_code,
        from_mainpage: true,
        smsasset: val.smsasset,
        marks: val2.matter
      });
    },
    async addGetRecordList() {
      const result1 = await this.getRecordList(this.newparamsdata[0]);
      if (this.newparamsdata.length > 1) {
        const result2 = await this.getRecordList(this.newparamsdata[1]);
        this.tableData = [...result1, ...result2];
      } else {
        this.tableData = [...result1];
      }
      // debugger
      this.tableData.forEach(item => {
        if (item.children && item.children.length > 0) {
          //判断是否有新增得数据，lable为2 显示更多按钮，全是1不显示
          if (
            item.children.some(k => {
              if (k.lable == 2) {
                return true;
              }
            })
          ) {
            item.showNewlist = true;
            this.$set(item, "close", true);
          }
          item.children.sort((a, b) => {
            return a.lable - b.lable;
          });
        }
      });
    },
    open({ system_code, wznw, apply, difference }) {
      this.visible = true;
      this.getRecordList({ system_code, wznw, apply, difference });
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        if (row.col0Count > 0) {
          return {
            rowspan: row.col0Count,
            colspan: 1
          };
        }
        return {
          rowspan: 0,
          colspan: 0
        };
      }
    },
    /**
     * @param { string } system_code 八大系统code
     * @param { string } wznw   区分各系统内外(如: 慧云机房内,慧云机房外)
     * @param { string } apply  事项类型, 含: 维修维保,专维及其
     */
    getRecordList(params) {
      const getParams = { ...params };
      // 处理给排水
      if (getParams.difference) {
        delete getParams.wznw; //不上传内外
      }
      if (getParams.wznw == "0" || getParams.wznw == "-1") {
        delete getParams.wznw; //不上传内外
      }
      return new Promise((resolve, reject) => {
        queryRecord({ getParams })
          .then(res => {
            const tableData = [];
            res.data.forEach(i => {
              i.children.forEach((j, index) => {
                tableData.push({
                  tab_code: params.tab_code,
                  tableType: getParams.wznw,
                  smswz: i.smswz,
                  smsasset: j.smsasset,
                  col0Count: index === 0 ? i.children.length : 0,
                  children: j.children
                });
              });
            });
            resolve(tableData);
          })
          .catch(err => {
            console.log(err);
            reject(err);
          });
      });
    }
  },
  mounted() {
    this.addGetRecordList();
  }
};
</script>
<style lang="less" scoped>
.morestyle {
  color: #025baa;
  margin-left: 10px;
  cursor: pointer;
  i {
    color: #025baa;
  }
}
.lastp {
  cursor: pointer;
}
</style>
<style lang="less">
.detail-dialog {
  .el-dialog__wrapper {
    // overflow: hidden !important;
  }
  .el-dialog__header {
    // background: #fff !important;
    // color: #909399 !important;
  }
  .el-dialog__title {
    color: #fff !important;
  }
  .el-dialog__close {
    // color: #909399 !important;
  }
  /deep/.el-dialog {
    margin-top: 7vh !important;
    padding-bottom: 5px;
    // overflow-y: scroll !important;
  }
}
</style>
