<!--
 * @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-wrapper">
    <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)
    },
    setRowStyle(v) {
      if (v.rowIndex % 2 === 0) {
        return {
          background: 'rgba(76,97,123,0.2)',
          color: 'rgba(255,255,255,0.5)',
          height: 'calc(16px * var(--beilv))',
          lineHeight: 'calc(16px * var(--beilv))',
          padding: 0,
          fontSize: 'calc(16px * var(--beilv))'
        }
      } else {
        return {
          background: 'rgba(79,114,136,0.29)',
          color: 'rgba(255,255,255,0.5)',
          height: 'calc(16px * var(--beilv))',
          lineHeight: 'calc(16px * var(--beilv))',
          padding: 0,
          fontSize: 'calc(14px * var(--beilv))'
        }
      }
    },
    setCellStyle(v) {
      return {
        lineHeight: 'calc(16px * var(--beilv))'
      }
    }
  }
}
</script>

<style>
.fix-table >>> .el-table td .cell {
  width: 60% !important;
  margin: auto;
  text-align: left;
}

.left-content-equipment-check {
  height: calc(100% - 32px);
  display: flex;
  gap: calc(100vmin / 1920 * 36);
  overflow: hidden;
}

.el-table-wrapper {
  flex: 1;
}

.left-content-equipment-check > div {
  overflow: hidden;
  /* overflow-x: scroll; */
}

.el-table-wrapper >>> * {
  background: transparent;
}
.el-table-wrapper >>> .el-table::before,
.el-table-wrapper >>> .el-table--group::after,
.el-table-wrapper >>> .el-table--border::after {
  background: transparent;
}

.el-table-wrapper >>> .el-table {
  font-size: 8px;
  background-color: transparent;
  color: #fff9;
  border: 0;
  height: 100%;
  overflow-y: auto;
}

.el-table-wrapper >>> .el-table th.is-leaf,
.el-table-wrapper >>> .el-table td {
  border-color: #0d1728;
  padding: 0;
}

.el-table__row--striped:hover > td,
.el-table-wrapper >>> .el-table__row:hover > td {
  background-color: rgba(79, 114, 136, 0.29) !important;
}

.el-table-wrapper >>> table {
  border: unset;
}

.el-table-wrapper >>> thead {
  color: #fff;
}
.el-table-wrapper >>> tr {
  background: unset;
}
.el-table-wrapper >>> tbody td > div,
.el-table-wrapper >>> thead th > div {
  white-space: nowrap;
}
.el-table-wrapper >>> th,
.el-table-wrapper >>> td {
  padding: 0;
}

.el-table-wrapper >>> tbody > tr:nth-child(even) {
  background: rgba(76, 97, 123, 0.2);
}
</style>
