<template>
  <table class="com-table">
    <thead>
      <tr>
        <th v-for="(col, index) of data.column" :key="index" :class="{ light: col.is_light }" @click="onSort(col)">{{ col.name }}</th>
      </tr>
    </thead>
    <el-scrollbar>
      <tbody>
        <tr v-for="(row, index) of tableData" :key="index">
          <td
            v-for="(col, i) of data.column"
            :key="i"
            :class="{
              light: hoverRow == row || hoverCol == col,
              num: col.is_num,
              'always-light': col.is_light || row.is_light,
            }"
            v-html="template(col, row[col.field])"
            @click="onClick(col, row)"
            @mouseenter="hover(row, col)"
            @mouseleave="leave()"
          ></td>
        </tr>
      </tbody>
    </el-scrollbar>
  </table>
</template>

<script>
export default {
  // 名称
  name: "ComTable",
  // 使用组件
  components: {},
  // 传入参数
  props: {
    /**
             * {
                    column: [{
                        name: "风机名称",
                        field: "name",
                        is_num: false, // 是否为数字
                        is_light: false, // 是否高亮
                        template:function(){ }
                        click:function(){} //点击事件
                        sortable:fasle // 排序
                    },{
                        name: "冷却风温度",
                        field: "lqf",
                        is_num: true,
                        is_light: false
                    }],
                    data: [{
                        name: "1E01",
                        lqf: 15.78,
                        is_light: false
                    }]
                }
             */
    data: Object,
    // hover 样式
    showHover: {
      type: Boolean,
      default: true,
    },
    canScroll: {
      type: Boolean,
      default: true,
    },
  },
  // 自定义事件
  emits: {},
  // 数据
  data() {
    return {
      hoverRow: -1,
      hoverCol: -1,
      sortCol: "",
      sortType: "",
    };
  },
  computed: {
    tableData() {
      let that = this;
      if (this.sortCol == "") {
        return this.data.data;
      } else {
        let data = JSON.parse(JSON.stringify(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;
      }
    },
  },
  // 函数
  methods: {
    onClick(col, data) {
      if (col.click) col.click(event, data);
    },
    onSort(col) {
      if (col.sortable == true) {
        this.sortCol = col.field;
        switch (this.sortType) {
          case "":
            this.sortType = "DESC";
            break;
          case "DESC":
            this.sortType = "ASC";
            break;
          case "ASC":
            this.sortType = "";
            break;
        }
      }
    },
    template(col, data) {
      if (!col.template) return data;
      else return col.template(data);
    },
    hover(row, col) {
      if (this.showHover) {
        this.hoverRow = row;
        this.hoverCol = col;
      }
    },
    leave() {
      this.hoverRow = -1;
      this.hoverCol = -1;
    },
  },
  // 生命周期钩子
  beforeCreate() {
    // 创建前
  },
  created() {
    // 创建后
  },
  beforeMount() {
    // 渲染前
  },
  mounted() {
    // 渲染后
  },
  beforeUpdate() {
    // 数据更新前
  },
  updated() {
    // 数据更新后
  },
};
</script>

<style lang="less">
@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%);
        padding: 1.481vh 0;
        color: @titleGray;
        font-weight: 400;
        font-size: @fontsize-s;
        position: sticky;
        top: 0;
        cursor: pointer;

        &.light,
        &.always-light {
          color: @green;
        }
      }
    }
  }

  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: @fontsize-s;

        &.light,
        &.always-light {
          color: @green !important;
        }

        &.num {
          font-family: "Bicubik";
          font-weight: 400;
        }
      }
    }
  }
}
</style>
