<template>
  <div class="hy-table">
    <div class="header" v-if="contentTableHeader">
      <slot name="header">
        <div class="title">{{ title }}</div>
        <div class="handler">
          <slot name="headerHandler"></slot>
        </div>
      </slot>
    </div>
    <el-table
      ref="tableRef"
      v-loading="loading"
      :data="listData"
      border
      style="width: 100%;height: calc(100% - 50px);"
      @selection-change="handleSelectionChange"
	  @row-click="rowClick"
      v-bind="childrenProps"
      :default-expand-all="true"
      :row-key="rowKey"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column
        v-if="showSelectColumn"
        type="selection"
        align="center"
        width="60"
      ></el-table-column>
      <el-table-column
        v-if="showIndexColumn"
        type="index"
        label="序号"
        align="center"
        width="80"
      ></el-table-column>
      <template v-for="propItem in propList" :key="propItem.prop">
        <el-table-column 
          v-bind="propItem"
          :sortable="propItem.sortable"
          align="center"
          show-overflow-tooltip
        >
          <template #default="scope">
            <slot :name="propItem.slotName" :row="scope.row">
              {{ scope.row[propItem.prop] }}
            </slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="footer" v-if="showFooter" style="float: right" >
      <slot name="footer">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.currentPage"
          :page-size="page.pageSize"
          :page-sizes="[10, 20, 30]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listCount"
        >
        </el-pagination>
      </slot>
    </div>
  </div>
</template>

<script>
import { defineComponent , ref } from 'vue'

export default defineComponent({
  props: {
    title: {
      type: String,
      default: ''
    },
    rowKey: {
      type: String,
      default: ''
    },
    loading: {
      type: Boolean,
      default: false
    },
    listData: {
      type: Array,
      required: true
    },
    listCount: {
      type: Number,
      default: 0
    },
    contentTableHeader: {
      type: Boolean,
      default: false
    },
    propList: {
      type: Array,
      required: true
    },
    showIndexColumn: {
      type: Boolean,
      default: false
    },
    showSelectColumn: {
      type: Boolean,
      default: false
    },
    page: {
      type: Object,
      default: () => ({ currentPage: 0, pageSize: 10 })
    },
    childrenProps: {
      type: Object,
      default: () => ({})
    },
    showFooter: {
      type: Boolean,
      default: true
    }
  },
  emits: ['selectionChange', 'update:page'],
  setup(props, { emit }) {
    const handleSelectionChange = (value) => {
      emit('selectionChange', value)
    }

    const handleCurrentChange = (currentPage) => {
      emit('update:page', { ...props.page, currentPage })
    }

    const handleSizeChange = (pageSize) => {
      emit('update:page', { ...props.page, pageSize })
    }

	// 当前行数据
	const rowData = ref({});
	// 某行被点击时事件
	function rowClick (data) {
		rowData.value = data;
	}

  const tableRef = ref();

    return {
      tableRef,
      getSelectionRows: () => {
          return tableRef.value.getSelectionRows()
      },
      rowClick,
      rowData,
      handleSelectionChange,
      handleCurrentChange,
      handleSizeChange
    }
  }
})
</script>

<style scoped lang="scss">
.hy-table {
  display: flex;
  flex-direction: column;
  height: 100%;
}
.header {
  display: flex;
  height: 45px;
  padding: 0 5px;
  justify-content: space-between;
  align-items: center;

  .title {
    font-size: 20px;
    font-weight: 700;
  }

  .handler {
    align-items: center;
  }
}

.footer {
  flex-shrink: 0;
  margin-top: 15px;
  margin-bottom: 10px;
  display: flex;
  justify-content: flex-end;
  .el-pagination {
    text-align: right;
  }
}
</style>
