<!--
 * @Date: 2020-12-14 09:07:03
 * @LastEditors: gtz
 * @LastEditTime: 2022-06-14 11:12:39
 * @FilePath: \mt-bus-fe\src\views\OperationalOverview\components\baseTable.vue
 * @Description:
-->
<template>
  <div class="visual-base-table-container">
    <el-table class="techy-el-table" v-loading="isLoading" :data="renderData" border>
      <el-table-column
        v-if="page && limit && showIndex"
        prop="_pageIndex"
        :label="'tableHeader.index' | i18nFilter"
        :width="70 * beilv"
        align="center"
      />
      <el-table-column
        v-for="(item, index) in renderTableHeadList"
        :key="item.prop"
        :show-overflow-tooltip="showOverflow"
        v-bind="item"
      >
        <template slot-scope="scope">
          <component
            :is="item.subcomponent"
            v-if="item.subcomponent"
            :key="index"
            :inject-data="{ ...scope.row, ...item }"
            @emitData="emitData"
          />
          <span v-else>{{ scope.row[item.prop] | commonFilter(item.filter) }}</span>
        </template>
      </el-table-column>
      <slot name="content" />
    </el-table>
  </div>
</template>
<script>
import { isObject, isString } from 'lodash'
export default {
  name: 'TechyTable',
  filters: {
    commonFilter: (source, filterType = a => a) => {
      return filterType(source)
    }
  },
  props: {
    tableData: {
      type: Array,
      required: true,
      validator: val => val.filter(item => !isObject(item)).length === 0
    },
    tableConfig: {
      type: Array,
      required: true,
      validator: val => val.filter(item => !isString(item.prop) || !isString(item.label)).length === 0
    },
    isLoading: {
      type: Boolean,
      required: false
    },
    page: {
      type: Number,
      required: false,
      default: 1
    },
    limit: {
      type: Number,
      required: false,
      default: 5
    },
    beilv: {
      type: Number,
      default: 1
    },
    showOverflow: {
      type: Boolean,
      default: true
    },
    showIndex: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      tableConfigBak: [],
      selectedBox: new Array(100).fill(true)
    }
  },
  computed: {
    renderData() {
      if (this.tableData.length && !this.tableData[0]._pageIndex) {
        this.tableData.forEach((item, index) => {
          item._pageIndex = (this.page - 1) * this.limit + index + 1
        })
      }
      return this.tableData.slice((this.page - 1) * this.limit, this.page * this.limit)
    },
    renderTableHeadList() {
      return this.tableConfig.filter((item, index) => {
        return this.selectedBox[index]
      })
    }
  },
  beforeMount() {
    this.selectedBox = new Array(100).fill(true)
  },
  methods: {
    emitData(val) {
      this.$emit('emitFun', val)
    }
  }
}
</script>

<style scoped>
.visual-base-table-container,
.visual-base-table-container >>> .el-table {
  min-width: 100%;
}
/* 清除默认样式 */
.visual-base-table-container >>> table {
  background: transparent;
}

.visual-base-table-container >>> .el-table,
.visual-base-table-container >>> th,
.visual-base-table-container >>> tr {
  background: unset;
}

/* 设置设计稿样式 */
.visual-base-table-container >>> .el-table::before,
.visual-base-table-container >>> .el-table--group::after,
.visual-base-table-container >>> .el-table--border::after,
.visual-base-table-container >>> .el-table--border::after {
  background: unset;
}

.visual-base-table-container >>> table * {
  border-color: #0d1728;
  border-width: calc(1px * var(--beilv));
}

.visual-base-table-container >>> td {
  padding: calc(5px * var(--beilv));
}

.visual-base-table-container >>> td span {
  color: #ffffff50;
  font-size: calc(12px * var(--beilv));
  line-height: calc(14px * var(--beilv));
}

.visual-base-table-container >>> thead th {
  padding: 0;
}

.visual-base-table-container >>> thead th .cell {
  color: #fff;
  font-size: calc(14px * var(--beilv));
  line-height: calc(14px * var(--beilv));
  padding: calc(6px * var(--beilv));
  white-space: nowrap;
}

.visual-base-table-container >>> .el-table__body tr:hover > td {
  background-color: #42537130;
}

.visual-base-table-container >>> .el-table tbody tr:nth-child(odd) {
  background-color: #0e203e90;
}

.visual-base-table-container >>> .el-table tbody tr:nth-child(even),
.visual-base-table-container >>> .el-table thead {
  background-color: #20376090;
}
</style>
