<template>
  <stms-scrollbar>
    <div ref="mesTable" class="mes__table">
      <span id="size" ref="size"></span>
      <el-table
        @expand-change="expand"
        @cell-mouse-leave="move"
        :data="tableData"
        style="width:100%"
        @selection-change="selectChange"
        :stripe="!sampleColor"
        :max-height="height"
        v-loading="tableLoading"
        element-loading-text="拼命加载中"
      >
        <el-table-column v-if="showCheckBox" type="selection" width="40"> </el-table-column>
        <el-table-column type="index" label="序号" width="70" fixed></el-table-column>
        <slot name="head"></slot>
        <el-table-column
          v-for="item in tableOption"
          :key="item.id"
          :prop="item.prop"
          :label="item.label"
          :min-width="item.width"
        >
          <!-- slot为true时进行自定义数据 -->
          <template slot-scope="scope">
            <slot v-if="item.slot" :name="item.prop" :row="scope.row"></slot>
            <span v-else>{{ scope.row[item.prop] || "--" }}</span>
          </template>
        </el-table-column>
        <slot name="prop"></slot>
        <slot name="operationTool"></slot>
      </el-table>
      <el-pagination
        background
        v-if="pageSize"
        @size-change="sizeChange"
        @current-change="pageChange"
        :current-page="pageNum"
        :page-size="pageSize"
        layout="sizes,total, prev, pager, next, jumper"
        :total="total"
        :pager-count="5"
      ></el-pagination>
    </div>
  </stms-scrollbar>
</template>

<script>
export default {
  props: {
    tableOption: {},
    tableData: {},
    tableLoading: {},
    total: {},
    pageNum: {},
    pageSize: {},
    pageChange: {},
    sizeChange: {
      // 分页条数自适应开启, 自动触发
      required: true
    },
    showCheckBox: {},
    maxHeight: {}
  },
  data() {
    return {
      height: this.maxHeight
    };
  },
  mounted() {
    for (const x of this.tableOption) {
      const arr = this.tableData.map(val => {
        const value = (val[x.prop] || "").toString();
        const len1 = value.split(";").length - 1;
        const len2 = value.split("&").length - 1;
        this.$refs.size.innerHTML = value.replace(/[&;]/g, "");
        return this.$refs.size.offsetWidth + 36 + len1 * 6 + len2 * 10;
      });
      this.$refs.size.innerHTML = x.label;
      arr.push(this.$refs.size.offsetWidth + 57);
      x.width = Math.max(...arr) + 20;
    }
  },
  watch: {
    tableData(curVal, oldVal) {
      for (const x of this.tableOption) {
        const arr = curVal.map(val => {
          const value = (val[x.prop] || "").toString();
          const len1 = value.split(";").length - 1;
          const len2 = value.split("&").length - 1;
          this.$refs.size.innerHTML = value.replace(/[&;]/g, "");
          return this.$refs.size.offsetWidth + 36 + len1 * 6 + len2 * 10;
        });
        this.$refs.size.innerHTML = x.label;
        arr.push(this.$refs.size.offsetWidth + 57);
        x.width = Math.max(...arr) + 20;
      }
      if (!curVal.length && this.pageNum > 1) {
        this.pageChange(this.pageNum - 1);
      }
    }
  },
  methods: {
    selectChange(arr) {
      const res = arr.map(val => val.id);
      this.$emit("selectChange", res);
    },
    expand(row, expanded) {
      this.$emit("expand", row, expanded);
    },
    // 鼠标悬浮出现名称
    move(row, column, cell, event) {}
  }
};
</script>
<style lang="less">
.mes__table {
  padding: 11px 20px;
}
.mes__table .el-pagination {
  margin-top: 10px;
  text-align: end;
  //float: right;
}

.mes__table #size {
  position: absolute;
  visibility: hidden;
  opacity: 0;
}

.el-table-column--selection .cell {
  padding-left: 16px;
  padding-right: 10px;
}

.mes__table .mCSB_container_wrapper {
  position: static;
}

.mes__table .el-table__fixed {
  bottom: 0 !important;
}

.mes__table .el-table__fixed-right {
  bottom: 0 !important;
  right: 0 !important;
}
</style>
