<style>
.eagle-table {
  width: 100%;
}
</style>

<template>
  <div>
    <pre>{{ tableList }}</pre>
    <el-table class="eagle-table" ref="table" :data="tableData" v-bind="{ size: 'small', ...tableProps }" v-on="tableEvents">
      <slot name="default"></slot>
      <template v-if="tableList && tableList.length > 0">
        <template v-for="(item, index) in tableList">
          <template v-if="bindItemVisible(item.visible)">
            <slot v-if="$scopedSlots[item.keyPath.join('-')] || $slots[item.keyPath.join('-')]" :name="item.keyPath.join('-')" v-bind="item"></slot>
            <el-table-column v-else-if="$scopedSlots[`value-${item.keyPath.join('-')}`] || $slots[`value-${item.keyPath.join('-')}`]"
              v-bind="item" :prop="item.agentKey || item.fullKey || item.key" :key="index"
              :min-width="item.minWidth || item['min-width']"
            >
              <slot slot-scope="{ row, column, $index }" :name="`value-${item.keyPath.join('-')}`" v-bind="item"
                :row="row" :value="$_get(row, item.fullKey)" :column="column" :index="$index"
              ></slot>
            </el-table-column>
            <el-table-column v-else v-bind="item" :prop="item.agentKey || item.fullKey || item.key"
              :key="index" :min-width="item.minWidth || item['min-width']"
            ></el-table-column>
          </template>
        </template>
      </template>
      <slot name="column-append"></slot>
      <slot name="column-end"></slot>
    </el-table>
  </div>
</template>

<script>
import { cloneDeep, get } from '../form-new/util';

export default {
  name: 'TableNew',
  props: {
    // 用于实例化本组件绑定v-model的值
    value: Array,
    // 配置列表
    list: {
      type: Array,
      required: true
    },
    // 表格参数
    tableProps: {
      type: Object,
      default() { return {} }
    },
    // 表格事件
    tableEvents: Object,
  },
  data() {
    return {
      tableList: [],
      tableData: [],
    };
  },
  computed: {
    // 表格实体
    instance: {
      get() {
        return this.$refs.table;
      }
    }
  },
  watch: {
    value: {
      handler(val = []) {
        this.tableData = val;
      },
      immediate: true
    },
    list: {
      handler(val) {
        // 深度克隆传入的列表，避免原始值被修改
        const newList = cloneDeep(this.list);
        // 生成列表值的全路径key，即列表项为对象时，对象内的key与上一级的key合并作为全路径key
        const generateFullKey = (list, parentKey) => {
          list.forEach(item => {
            if (item.group && item.list) {
              if (item.group.key) {
                item.fullKey = `${parentKey ? `${parentKey}.${item.group.key}` : item.group.key}`;
              } else {
                item.fullKey = parentKey || item.key;
              }
              generateFullKey(item.list, item.fullKey);
            } else {
              item.fullKey = `${parentKey ? `${parentKey}.${item.key}` : item.key}`;
            }
          });
        };
        // 生成fullKey
        generateFullKey(newList);
        // 创建输出列表
        const result = [];
        // 生成列表值的全路径key，即列表项为对象时，对象内的key与上一级的key合并作为全路径key
        const generateFlatList = (list) => {
          list.forEach(item => {
            if (item.group && item.list) {
              generateFlatList(item.list);
            } else if (!item.group && !item.list) {
              result.push({ ...item, keyPath: item.fullKey.split('.') });
            }
          });
        };
        generateFlatList(newList);
        this.tableList = result;
      },
      immediate: true
    }
  },
  methods: {
    $_get: get,
    // 绑定表格列显示隐藏状态
    bindItemVisible(visible = true) {
      let result = visible;
      if (typeof visible === 'function') {
        result = visible(this.tableData);
      }
      return result;
    },
  }
};
</script>