<template>
  <div
    v-loading="loading"
    class="security-check-table-wrapper"
    :style="{ height: tableWrapperHeight }"
  >
    <a-table
      ref="table"
      :data="data"
      stripe
      fit
      border
      height="100%"
      :show-summary="showSummary"
    >
      <template v-if="withSelection">
        <a-table-column type="selection" width="35" />
      </template>
      <template v-for="col in cols">
        <NestTableColumn :key="col.prop" :col="col" />
      </template>
      <template v-if="withOperateColumn">
        <a-table-column label="操作" class-name="column-operate" :width="130">
          <template slot-scope="scope">
            <span class="cell-operate-edit" @click="editRow(scope.row)"
              >修改</span
            >
            <span class="cell-operate-delete" @click="deleteRow(scope.row)"
              >删除</span
            >
          </template>
        </a-table-column>
      </template>
    </a-table>
  </div>
</template>

<script>
import NestTableColumn from "./nestTableColumn.vue";

export default {
  name: "SecurityCheckTable",
  components: { NestTableColumn },
  props: {
    height: {
      type: [Number, String],
      default: "50vh",
    },
    tableCols: {
      type: Array,
      default: () => [],
    },
    tableData: {
      type: Array,
      default: () => [],
    },
    withSelection: {
      type: Boolean,
      default: false,
    },
    showSummary: {
      type: Boolean,
      default: false,
    },
    withOperateColumn: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      cols: [],
      data: [],
    };
  },
  computed: {
    tableWrapperHeight() {
      return typeof this.height === "number" ? this.height + "px" : this.height;
    },
  },
  watch: {
    tableCols: {
      handler(val) {
        this.cols = val;
      },
      deep: true,
      immediate: true,
    },
    tableData: {
      handler(val) {
        this.data = val;
      },
      deep: true,
      immediate: true,
    },
  },
  updated() {
    this.$refs["table"].doLayout();
  },
  methods: {
    editRow(row) {
      console.log("edit");
    },
    deleteRow(row) {
      console.log("delete");
    },
  },
};
</script>

<style lang="scss" scoped>
.security-check-table-wrapper {
  width: 100%;
  ::v-deep .ant-table {
    width: 100%;
    .ant-table__cell {
      .cell {
        // padding: 0 19px;
        font-size: 14px;
        font-family: Helvetica, "Microsoft YaHei";
        color: #101116;
      }
      &.ant-table-column--selection .cell {
        padding: 0;
        text-align: center;
      }
    }
    .ant-table__header {
      .ant-table__cell {
        > .cell {
          font-weight: bold;
        }
        &:not(.is-leaf) > .cell {
          text-align: center;
        }
      }
    }
    .ant-table__body {
      .cell .cell-click {
        color: #2d67e3;
        cursor: pointer;
      }
      .column-operate .cell {
        span {
          cursor: pointer;
          &:not(:last-child) {
            margin-right: 25px;
          }
          &.cell-operate-edit {
            color: #2d67e3;
          }
          &.cell-operate-delete {
            color: #ea4545;
          }
        }
      }
    }
  }
}
</style>
