<template>
  <table class="com-table">
    <thead>
      <tr>
        <th v-for="(col, index) of data.column" :key="index" :class="{ light: col.is_light }">{{ col.name }}</th>
      </tr>
    </thead>
    <tbody :class="{ scroll: canScroll }">
      <tr v-for="(row, index) of data.data" :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])"
          @mouseenter="hover(row, col)"
          @mouseleave="leave()"
        ></td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  // 名称
  name: "ComTable",
  // 使用组件
  components: {},
  // 传入参数
  props: {
    /**
             * {
                    column: [{
                        name: "风机名称",
                        field: "name",
                        is_num: false, // 是否为数字
                        is_light: false, // 是否高亮
                        template:function(){ }
                    },{
                        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,
    };
  },
  // 函数
  methods: {
    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;

        &.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>
