<template>
  <el-table
    class="custom-table"
    stripe
    :data="data.data"
    :height="height"
    style="width: 100%"
    @cell-click="onClick"
    v-if="data && data.data"
    :header-cell-style="{
      background: 'rgb(30,30,30)',
      color: 'rgb(220,220,220)',
      padding: '4px',
      fontSize: '14px',
      'border-bottom': 'solid 1px rgba(77, 77, 77, 1)',
    }"
    :cell-style="{
      height: '40px',
      background: 'rgb(30,30,30)',
      color: 'rgb(220,220,220)',
      padding: '3px',
      fontSize: '12px',
      'border-bottom': '1px solid #000000',
    }"
  >
    <el-table-column
      v-for="col in data.column"
      :key="col"
      :prop="col.field"
      :label="col.name"
      :width="col.width"
      :min-width="col.minWidth"
      :sortable="col.sortable"
      :sort-orders="sortOrder"
      :sort-by="col.field + '.count'"
      :show-overflow-tooltip="!col.slot"
      :fixed="col.fixed"
      :align="'center'"
      :resizable="col.resizable"
      :header-align="'center'"
    >
      <template v-if="col.slot == true" #default="item">
        <slot
          :name="col.field"
          :column="col"
          :row="item.row"
          :all="item"
          :data="item.row[col.field]"
        ></slot>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    class="mg-t-8"
    v-if="pageable"
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    v-modal:page-size="selfPageSize"
    :total="data.total"
    v-bind="elPaggingProps"
  >
  </el-pagination>
</template>

<script>
export default {
  // 名称
  name: "ComTable",
  // 使用组件
  components: {},
  // 传入参数
  props: {
    /**
             * {
                    column: [{
                        name: "风机名称",
                        field: "name",
                        width:'', // 宽度
                        click:function(){} // 点击事件
                        sortable:fasle,
                        slot:false,
                        fixed:false,
                        align:'center',
                        resizable :false,
                    }],
                    total:200
                }
             */
    data: Object,
    height: {
      type: String,
      default: "",
    },
    pageSize: {
      type: Number,
      default: 0,
    },
    elPaggingProps: {
      type: Object,
      default: () => {
        return {
          layout: "total, sizes, prev, pager, next, jumper",
          // "page-sizes": [100, 200, 300, 400],
        };
      },
    },
    sortOrder: {
      type: Array,
      default: () => {
        return ["descending", "ascending", null];
      },
    },
  },
  // 自定义事件
  emits: {
    // 分页事件
    onPagging: null,
  },
  // 数据
  data() {
    return {
      currentPage: 1,
    };
  },
  computed: {
    tableData() {
      let that = this;
      if (this.sortCol == "") {
        return this.data.data;
      } else {
        let data = this.data.data;

        data.sort((a, b) => {
          let rev = 1;
          if (that.sortType == "ASC") rev = 1;
          else if (that.sortType == "DESC") rev = -1;

          if (a[that.sortCol] > b[that.sortCol]) return rev * 1;
          if (a[that.sortCol] < b[that.sortCol]) return rev * -1;
          return 0;
        });
        return data;
      }
    },
    pageable() {
      return this.pageSize != 0;
    },
    pages() {
      if (this.pageable) return parseInt(this.data.total / this.pageSize) + 1;
      else return 0;
    },
    startRow() {
      if (this.pageable) return (this.currentPage - 1) * this.pageSize;
      else return 0;
    },
    endRow() {
      if (this.pageable) return this.currentPage * this.pageSize;
      else return this.data.data.length;
    },
    // sortOrder: {
    //   type: Array,
    //   default: () => {
    //     return ["descending", "ascending", null];
    //   },
    // },
  },
  // 函数
  methods: {
    onClick(row, column, cell, event) {
      if (column.rawColumnKey.click) column.rawColumnKey.click(event, row);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.$emit("onPagging", {
        pageIndex: this.currentPage,
        pageSize: this.pageSize,
        start: this.startRow,
        end: this.endRow,
      });
    },
  },
  // 生命周期钩子
  beforeCreate() {
    // 创建前
  },
  created() {
    // 创建后
    this.selfPageSize = this.pageSize;
  },
  beforeMount() {
    // 渲染前
  },
  mounted() {
    // 渲染后
  },
  beforeUpdate() {},
  updated() {},
};
</script>

<style lang="less" scoped>
@titleGray: #9ca5a8;
@rowGray: #606769;
@darkBack: #536268;
.com-table {
  width: 100%;
  border-collapse: collapse;

  thead {
    tr {
      display: table;
      table-layout: fixed;
      width: 100%;

      th {
        background-color: fade(@darkBack, 20%);
        height: 30px;
        line-height: 30px;
        color: @titleGray;
        font-weight: 400;
        font-size: 12px;
        position: sticky;
        top: 0;
        cursor: pointer;

        &.light,
        &.always-light {
          color: #05bb4c;
        }
      }
    }
  }

  tbody {
    display: block;

    tr {
      display: table;
      table-layout: fixed;
      width: 100%;

      &:nth-child(2n) {
        background-color: fade(@rowGray, 20%);
      }

      td {
        padding: 0.556vh 0;
        color: @rowGray;
        text-align: center;
        font-size: 12px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;

        &.light,
        &.always-light {
          color: #05bb4c !important;
        }

        &.num {
          font-family: "Bicubik";
          font-weight: 400;
        }
      }
    }
  }

  .el-pagination {
    color: #606769;
    .el-pagination__total {
      color: #606769;
    }

    button {
      &.btn-next,
      &.btn-prev {
        background: center center no-repeat fade(#606769, 20);
        color: #b3bdc0;
      }
      &:disabled {
        color: #b3bdc0;
        background-color: fade(#606769, 20);
        cursor: not-allowed;
      }
    }

    .el-pager li {
      color: #b3bdc0;
      background: fade(#606769, 20);
      &.active {
        color: #05bb4c;
      }
    }

    .el-input__inner {
      color: #b3bdc0;
      background: fade(#606769, 20);
      border: 1px solid fade(#606769, 20);
    }
  }
}

</style>
