<script setup name="tableList">
import Eexpand from './E-expand'
import { ref, reactive, onMounted } from 'vue'
const props = defineProps({
    column: {
      type: Object
    },
    data: {
      type: Array
    },
    size: String,
    emptyTemp: {
      type: Boolean,
      default: true
    },
    pagination: {
      type: Boolean,
      default: true
    },
    height: {
      type: Number,
      default: null
    },
    rowKey: {
      type: [String, Function]
    },
    selectable: {
      type: Function,
      default: () => true
    },
    // 合并单元格
    spanMethod: {
      type: Function,
      default: () => {
        return {
          rowspan: 1,
          colspan: 1
        }
      }
    },
    // 是否带斑马纹
    stripe: {
      type: Boolean,
      default: true
    },
    // 是否有边框
    border: {
      type: Boolean,
      default: false
    },
    // 分页
    pageSizes: {
      type: Array,
      default: () => [10, 20, 30, 40]
    },
    total: {
      type: Number,
      default: 0
    },
    small: {
      type: Boolean,
      default: false
    }
  }),
  emits = defineEmits(['selectionChange', 'getTableData', 'handleTable', 'radioChange', 'selectionRow', 'selectionAll']),
  tableRef = ref(null),
  // 多选
  selectionChange = selection => {
    emits('selectionChange', selection)
  },
  selectRow = (selection, row) => {
    emits('selectionRow', selection, row)
  },
  selectAll = (selection) => {
    emits('selectionAll', selection)
  },
  // 单选
  radio = ref(),
  radioChange = (row) => {
    emits('radioChange', row)
  },
  setRadio = (data) => {
    radio.value = data
  },
  // 分页
  pageParams = reactive({
    pageNumber: 1,
    pageSize: props.pageSizes[0]
  }),
  handleSizeChange = () => {
    emits('getTableData', pageParams)
  },
  handleCurrentChange = () => {
    emits('getTableData', pageParams)
  },
  // 多选表格-默认选中
  toggleRowSelect = ref((row, selected) => {
    tableRef.value.toggleRowSelection(row, selected)
  }),
  getSelectRows = () => {
    return tableRef.value.getSelectionRows()
  }
onMounted(() => {
  const tableDom = document.querySelector('#out-table')
  emits('handleTable', tableDom)
})
defineExpose({ tableRef, pageParams, setRadio, toggleRowSelect, getSelectRows })
</script>

<template>
  <div>
    <el-table
      id="out-table"
      ref="tableRef"
      :data="props.data"
      :stripe="props.stripe"
      :height="props.height"
      :row-key="props.rowKey"
      :size="size"
      :span-method="spanMethod"
      :border="props.border"
      @selection-change="selectionChange"
      @select="selectRow"
      @select-all="selectAll"
    >
      <template v-if="props.emptyTemp" #empty><el-empty /></template>
      <template v-for="item in props.column" :key="item.key">
        <template v-if="item.type">
          <el-table-column
            v-if="item.type === 'index'"
            type="index"
            :label="item.label"
            :width="item.width"
          />
          <el-table-column
            v-if="item.type === 'selection'"
            type="selection"
            :width="item.width"
            :reserve-selection="item.reserveSelection"
            :selectable="props.selectable"
          />
          <el-table-column v-if="item.type === 'radio'" type="radio" :width="item.width">
            <template #default="scope">
              <el-radio
                :key="scope.row[item.key]"
                v-model="radio"
                :label="scope.row[item.key]"
                :disabled="scope.row.isRadioDisabled"
                @change="radioChange(scope.row)"
              >&nbsp;</el-radio>
            </template>
          </el-table-column>
          <el-table-column v-if="item.type === 'expand'" type="expand">
            <template #default="scope">
              <slot name="expand" :scope="scope"></slot>
            </template>
          </el-table-column>
        </template>
        <template v-else-if="item.children">
          <el-table-column
            :label="item.label"
            :prop="item.prop"
            :width="item.width"
            :min-width="item.minWidth"
            :show-overflow-tooltip="item.showOverflowTooltip || false"
            :fixed="item.fixed || false"
            :column-key="item.columnKey"
          >
            <el-table-column
              v-for="(child,key) in item.children"
              :key="key"
              :label="child.label"
              :prop="child.prop"
              :width="child.width"
              :min-width="child.minWidth"
              :show-overflow-tooltip="child.showOverflowTooltip || false"
              :fixed="child.fixed || false"
              :column-key="child.columnKey"
            >
              <template #default="scope">
                <template v-if="child.slot">
                  <slot :name="child.slot" :scope="scope"></slot>
                </template>
                <template v-if="child.render">
                  <Eexpand
                    :column="child"
                    :row="scope.row"
                    :render="child.render"
                    :index="scope.$index"
                  />
                </template>
              </template></el-table-column>
          </el-table-column>
        </template>
        <template v-else>
          <el-table-column
            :label="item.label"
            :prop="item.prop"
            :width="item.width"
            :min-width="item.minWidth"
            :show-overflow-tooltip="item.showOverflowTooltip || false"
            :fixed="item.fixed || false"
            :column-key="item.columnKey"
          >
            <template #default="scope">
              <template v-if="item.slot">
                <slot :name="item.slot" :scope="scope"></slot>
              </template>
              <template v-if="item.render">
                <Eexpand
                  :column="item"
                  :row="scope.row"
                  :render="item.render"
                  :index="scope.$index"
                />
              </template>
            </template>
          </el-table-column>
        </template>
      </template>
    </el-table>
    <el-pagination
      v-if="pagination"
      v-model:currentPage="pageParams.pageNumber"
      v-model:page-size="pageParams.pageSize"
      :small="small"
      background
      :page-sizes="props.pageSizes"
      layout="total, sizes, prev, pager, next"
      :total="props.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<style scoped lang="less">
.el-pagination {
  padding: 16px 16px 0 16px;
  flex-wrap: wrap;
  justify-content: flex-end;
}
</style>
