<template>
  <div class="table-container">
    <!-- 琛ㄦ牸 https://blog.csdn.net/h_jQuery/article/details/128128309-->
    <el-table
      ref="table"
      :data="tableData"
      style="width: 100%"
      :header-cell-class-name="headerBg ? 'headerCell' : ''"
      :row-key="rowKey"
      @selection-change="handleSelectionChange"
      @current-change="handleCurrentRowChange"
      :highlight-current-row="highlightCurrentRow"
      :stripe="stripe"
      :max-height="maxHeight"
      :show-summary="summary"
      :border="border"
      :size="size"
      :summary-method="summaryMethod"
      :span-method="spanMethod"
      :row-class-name="tableRowClassName"
    >
      <!-- 灞曞紑琛� -->
      <el-table-column v-if="expand" type="expand" width="70" align="center">
        <template slot-scope="scope">
          <!-- 浣滅敤鍩熸彃妲斤紝 scope锛氭嬁鍒拌繖涓€琛岀殑鏁版嵁-->
          <slot name="expand" :row="scope.row" :index="scope.$index" :column="scope.column"> </slot>
        </template>
      </el-table-column>
      <!-- 澶氶€� -->
      <el-table-column v-if="selection" type="selection" width="45" align="center" :selectable="selectBox" />
      <!-- 搴忓垪鍙� -->
      <el-table-column v-if="index" type="index" width="45" :label="indexLabel" align="center" />
      <!-- 鏁版嵁 -->
      <el-table-column
        v-for="column in columns"
        :prop="column.prop"
        :label="column.label"
        :width="column.width || 'auto'"
        :key="column.prop"
        :show-overflow-tooltip="column.tooltip ? true : column.tooltip"
        :type="column.type || ''"
        :sortable="column.sortable || false"
        :fixed="column.prop == 'operation' ? 'right' : column.fixed || false"
        :align="column.prop == 'operation' ? 'left' : column.align || 'center'"
      >
        <!-- table鑷甫鐨勪綔鐢ㄥ煙鎻掓Ы -->
        <template slot-scope="scope">
          <!-- 浣滅敤鍩熸彃妲斤紝 scope锛氭嬁鍒拌繖涓€琛岀殑鏁版嵁-->
          <slot :name="column.prop" :row="scope.row" :index="scope.$index" :column="scope.column">
            <!-- 榛樿鏄剧ず -->
            <span>{{ scope.row[column.prop] }}</span>
          </slot>
        </template>
      </el-table-column>
    </el-table>
    <!-- 鍒嗛〉 -->
    <template v-if="hasPage">
      <el-pagination
        align="left"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagination.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
      >
      </el-pagination>
    </template>
  </div>
</template>
<script>
export default {
  props: {
    columns: {
      //琛ㄦ牸column閰嶇疆椤�
      type: Array,
      default: () => [],
    },
    dataList: {
      //琛ㄦ牸灞曠ず鏁版嵁
      type: Array,
      default: () => [],
    },
    pagina: {
      //鍒嗛〉鏁版嵁
      type: Object,
      default: () => {
        return {
          pageNo: 1,
          pageSize: 1,
          total: 0,
        };
      },
    },
    maxHeight: {
      //鏈€澶ч珮搴�
      type: String,
      default: "550",
    },
    stripe: {
      //鏄惁鏄剧ず鏂戦┈绾�
      type: Boolean,
      default: false,
    },
    border: {
      //鏄惁鏄剧ず绾靛悜鐨勮竟妗�
      type: Boolean,
      default: true,
    },
    selection: {
      //鏄惁鍙閫�
      type: Boolean,
      default: false,
    },
    index: {
      //鏄惁鏄剧ず搴忓垪鍙�
      type: Boolean,
      default: true,
    },
    expand: {
      //鏄惁灞曞紑琛�
      type: Boolean,
      default: false,
    },
    hasPage: {
      // 鏄惁鏄剧ず鍒嗛〉缁勪欢
      type: Boolean,
      default: true,
    },
    rowKey: {
      //琛屾暟鎹殑key
      type: String,
      default: "",
    },
    indexLabel: {
      type: String,
      default: "搴忓彿",
    },
    headerBg: {
      //琛ㄥご琛屾槸鍚︽湁鑳屾櫙鑹�
      type: Boolean,
      default: true,
    },
    size: {
      type: String,
      default: "mini",
    },
    summary: {
      // 鏄惁鏄剧ず鍚堣椤�
      type: Boolean,
      default: false,
    },
    selectBox: {
      //澶嶉€夋鍙敤/绂佺敤鐘舵€佸鐞嗗嚱鏁�
      type: Function,
      default: null,
    },
    summaryMethod: {
      //鍚堣琛屽鐞嗗嚱鏁�
      type: Function,
      default: null,
    },
    spanMethod: {
      //鍚堣琛屽鐞嗗嚱鏁�
      type: Function,
      default: null,
    },
    currentIndex: {
      type: Number,
    },
    highlightCurrentRow: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      tableData: null,
      pagination: {
        pageNo: 1,
        pageSize: 1,
        total: 0,
      },
    };
  },
  computed: {
    table() {
      return this.$refs.table;
    },
  },
  methods: {
    tableRowClassName({ rowIndex }) {
      if (this.currentIndex === rowIndex) {
        return "current-row";
      }
      return "";
    },
    // 澶氶€�
    handleSelectionChange(selection) {
      this.$emit("selectionChange", selection);
    },
    handleCurrentRowChange(val) {
      this.$emit("currentChange", val);
    },
    handleSizeChange(pageSize) {
      this.pagination.pageSize = pageSize;
      this.$emit("pageChange", this.pagination);
    },
    handleCurrentChange(pageNo) {
      this.pagination.pageNo = pageNo;
      this.$emit("pageChange", this.pagination);
    },
    setCurrentRow(index) {
      const row = this.tableData[index];
      console.log(row);
      this.$refs.table.setCurrentRow(row);
    },
  },
  watch: {
    dataList: {
      handler(newVal) {
        this.tableData = JSON.parse(JSON.stringify(newVal));
      },
      immediate: true,
      deep: true,
    },
    pagina: {
      handler(newVal) {
        this.pagination = JSON.parse(JSON.stringify(newVal));
      },
      immediate: true,
      deep: true,
    },
  },
};
</script>

<style lang="scss" scoped>
.table-container {
  ::v-deep .el-table {
    margin-bottom: 20px;
    .headerCell {
      background: #e8e8e8 !important;
      color: #333;
    }
    .current-row {
      background: #999999;
    }
  }

  .el-button {
    margin-bottom: 10px !important;
  }
}
</style>