<template>
  <div class="Tables" :class="{'is-empty-table' : !tableDataTotal}">
    <div class="table-item">
      <el-table v-if="tableDataTotal>0" class="fixed-table" :class="headerFixed ? 'headerFixed':''" v-loading="tableLoading" :data="tableData"
        element-loading-text="Loading" ref="tableRef" v-horizontal-scroll="'hover'" border fit highlight-current-row :height="height"
        :maxHeight="comMaxHeight" :default-sort="defaultSort?defaultSort:{}" @sort-change="sortChange">
        <el-table-column v-if="isIndex" label="序号" :width="flexWidth(tableData)" align="left" :fixed="indexFixed" :resizable="false">
          <template slot-scope="scope">{{ queryParams.pageNum * queryParams.pageSize - queryParams.pageSize + scope.$index + 1 }}</template>
        </el-table-column>
        <template>
          <el-table-column v-for="(item,index) in forData" :key="index" :label="item.label" :prop="item.prop" :width="item.width"
            :min-width="item.minWidth" :align="item.align?item.align:'left'" :fixed="item.fixed"
            :sortable="item.sortable ?item.sortable=='custom'? 'custom':true : false" :show-overflow-tooltip="item.showOverflowTooltip"
            :resizable="false">
            <template v-if="item.children&&item.children.length">
              <el-table-column v-for="(cld, i) in item.children" :key="i" :prop="cld.prop" :label="cld.label" :width="cld.width" :resizable="false">
                <template slot-scope="cldscope">
                  <template v-if="cld.slot">
                    <slot :name="cld.prop" :row="cldscope.row" :data="cld"></slot>
                  </template>
                  <template v-else>
                    <span>{{cldscope.row[cld.prop] || '-'}}</span>
                  </template>
                </template>
              </el-table-column>
            </template>
            <template v-else-if="item.slotHeader" slot="header">
              <slot :name="item.slotName"></slot>
            </template>
            <template slot-scope="scope">
              <slot v-if="item.slot" :name="item.prop" :row="scope.row" :index="scope.$index" :data="item"></slot>
              <span v-else>
                {{ scope.row[item.prop] || '-' }}
              </span>
            </template>
          </el-table-column>

        </template>
        <template slot="empty">
        </template>
      </el-table>

      <div class="table-empty-container" v-else>
        <no-data />
      </div>
    </div>
    <div class="pagination-box" v-if="show_page && tableDataTotal>queryParams.pageSize">
      <el-pagination background :current-page="current_page" :page-size="queryParams.pageSize" :total="tableDataTotal"
        layout="prev, pager, next, jumper" @current-change="handleCurrentChange" @size-change="handleSizeChange" />
    </div>
  </div>
</template>

<script>
import NoData from '../component/noData';
export default {
  name: "Tables",
  props: {
    height: {
      type: [String, Number]
    },
    maxHeight: {
      type: Boolean
    },
    isIndex: {
      type: Boolean,
      default: true
    },
    headerFixed: {
      type: Boolean,
      default: false
    },
    indexFixed: {
      type: Boolean,
      default: false
    },
    tableLoading: {
      type: Boolean,
      default: false
    },
    defaultSort: {
      type: Object,
      default: null
    },
    tableData: {
      type: Array,
      default: []
    },
    forData: {
      type: Array,
      default: []
    },
    tableDataTotal: {
      type: Number,
      default: 0
    },
    queryParams: {
      type: Object,
      default: {}
    },
    paging: {
      type: Boolean,
      default: true
    },
    MaxPage: {  //最大页码
      type: Number,
      default: 1000000
    },
  },
  components: {
    NoData
  },
  data() {
    return {
      current_page: this.queryParams.pageNum,
      show_page: this.paging,
      comMaxHeight: null
    };
  },
  watch: {
    'queryParams.pageNum'(newVal, oldVal) {
      this.current_page = newVal;
    }
  },
  created() {
    this.maxHeight ? this.maxHeightInit() : null;
  },
  methods: {
    // 自适应当前容器
    async maxHeightInit() {
      try {
        await this.$nextTick();
        /**
         * @type {HTMLDivElement}
        */
        const container = this.$el.querySelector(".table-item");
        if (container) {
          this.comMaxHeight = `${container.offsetHeight}px`;
        }
      } catch (error) {

      }
    },
    handleCurrentChange(e) {
      if (this.MaxPage < e) {
        this.show_page = false;
        this.$nextTick(() => {
          this.current_page = this.queryParams.pageNum;
          this.$message.warning(`对不起，最多只能访问${this.MaxPage}页`);
          this.show_page = true;
        });
      } else {
        this.$emit('handle-current-change', e);
      }
    },
    handleSizeChange(e) {
      this.$emit('handle-current-change', e);
    },
    sortChange(e) {
      this.$emit('sort-change', e);
    },
    flexWidth(tableData) {
      let currentMax = this.queryParams.pageNum * this.queryParams.pageSize - this.queryParams.pageSize + tableData.length, wdth = 59;
      // return currentMax.toString().length*25 + 'px'
      if (currentMax.toString().length > 3) {
        wdth = wdth + (currentMax.toString().length - 3) * 10;
      }
      return wdth + 'px';
    }
  }
}
</script>

<style lang="scss" scoped>
.Tables {
  ::v-deep .el-table__body tr.current-row > td.el-table__cell {
    background-color: #ffffff;
  }

  ::v-deep .el-table__row {
    &:nth-child(even) {
      background-color: #f9fcff;
      .more {
        background: #f8fbff;
        span {
          color: #0081ff;
        }
      }
    }
    &:nth-child(odd) {
      .more {
        span {
          color: #0081ff;
        }
      }
    }
  }

  &.is-empty-table {
    .table-item {
      max-height: unset;
      height: 100%;
    }
  }

  ::v-deep .table-item {
    position: relative;
    max-height: calc(100% - 56px);
    .table-empty-container {
      min-height: 360px;
      display: flex;
      height: 100%;
      width: 100%;
      align-items: center;
      justify-content: center;
      box-sizing: border-box;

      .no-data {
        min-height: unset;
      }
    }
    .el-table td.el-table__cell {
      border-bottom: 0;
    }
    .el-table {
      .cell {
        font-size: 14px;
      }
      .el-table__header-wrapper {
        min-height: 40px;
      }
    }
    .el-table--border th.gutter:last-of-type {
      display: block !important;
      padding-right: 16px;
    }
  }
  ::v-deep .el-table th.el-table__cell.is-leaf,
  ::v-deep .el-table td.el-table__cell {
    border-bottom: 1px solid #e6eaf1;
  }
  ::v-deep .el-table--border .el-table__cell {
    border-right: 1px solid #e6eaf1;
  }
  ::v-deep .el-table__body tr.hover-row.current-row > td,
  ::v-deep .el-table__body tr.hover-row.el-table__row--striped.current-row > td,
  ::v-deep .el-table__body tr.hover-row.el-table__row--striped > td,
  ::v-deep .el-table__body tr.hover-row > td {
    background-color: #dcebff !important;
    .more {
      background: #dcebff;
    }
  }

  ::v-deep .el-table--enable-row-hover .el-table__body tr:hover > td {
    background-color: #dcebff;
  }
  ::v-deep .fixed-table {
    overflow: visible;
  }
  ::v-deep .el-table__header-wrapper {
    position: sticky;
    top: 0;
    z-index: 9;
  }
  ::v-deep .el-table__fixed-header-wrapper {
    position: sticky;
    z-index: 9;
    top: 0;
  }
  .headerFixed {
    ::v-deep .el-table__header-wrapper {
      position: sticky;
      top: 80px;
      z-index: 9;
    }
    ::v-deep .el-table__fixed-header-wrapper {
      position: sticky;
      z-index: 9;
      top: 80px;
    }
  }
  ::v-deep .el-table__fixed {
    overflow-x: clip;
    overflow-y: clip;
  }
}
</style>
