<template>
  <div>
    <el-table :data="tableData" v-bind="$attrs" v-on="$listeners" border :row-style='toggleDisplayTr'
              header-cell-class-name="common-table-header" cellClassName="common-table-cell">
      <template v-for="(column) in columns">
        <el-table-column :align="column.align" header-align="center" :label="column.label" :prop="column.prop" :min-width="column.width" :fixed="column.fixed" :sortable="column.sortable" :show-overflow-tooltip="column.showOverflowTooltip" :key="column.prop">
          <template slot-scope="scope">
            <!-- 如果有格式化，处理格式化数据 -->
            <div style="display: flex;">
              <span v-if="!column.slot" style="flex: 1;">{{column.format?column.format(scope.row):scope.row[column.prop]}}</span>
              <i v-if="column.prop === firstProp" @click="toggleFoldingStatus(scope.row)" class="permission_toggleFold" :class="toggleFoldingClass(scope.row)"></i>
            </div>
          </template>
        </el-table-column>
      </template>
    </el-table>
  </div>
</template>

<script>
export default {
  props: {
    // 树结构数据
    treeData: {
      type: Array,
      default: function () {
        return []
      }
    },
    // 列数据
    columns: {
      type: Array,
      default: function () {
        return []
      }
    },
    // 第一列属性id
    firstProp: {
      type: String,
      default: function () {
        return 'productFileName'
      }
    }
  },
  data () {
    return {
      tableData: [],
      foldList: []
    }
  },
  computed: {
  },
  methods: {
    toggleFoldingStatus (params) {
      this.foldList.includes(params.__identity) ? this.foldList.splice(this.foldList.indexOf(params.__identity), 1) : this.foldList.push(params.__identity)
    },
    toggleDisplayTr ({row, index}) {
      for (let i = 0; i < this.foldList.length; i++) {
        let item = this.foldList[i]
        // 如果foldList中元素存在于 row.__family中，则该行隐藏。  如果该行的自身标识等于隐藏元素，则代表该元素就是折叠点
        if (row.__family.includes(item) && row.__identity !== item) return 'display:none;'
      }
      return ''
    },
    toggleFoldingClass (params) {
      return !params.children ? 'permission_placeholder' : (this.foldList.indexOf(params.__identity) === -1 ? 'iconfont iconjian tree-table-icon' : 'iconfont iconzengjia tree-table-icon')
    },
    loadTreeData () {
      this.tableData = this.formatConversion([], this.treeData)
      console.log(this.tableData)
    },
    formatConversion (parent, children) {
      let $this = this
      if (children.length > 0) {
        children.forEach((item, i) => {
          $this.$set(item, '__group', i)
          if (item.fileLogList.length > 0) {
            if (item.fileLogList.length === 1) {
              Object.assign(item, item.fileLogList[0])
              $this.$set(item, 'children', false)
              $this.$set(item, '__family', ['x_' + i])
              $this.$set(item, '__identity', 'x_' + i)
              parent.push(item)
            } else {
              item.fileLogList.forEach((item1, index1) => {
                if (index1 === 0) {
                  Object.assign(item, item1)
                  $this.$set(item, 'children', true)
                  $this.$set(item, '__family', ['x_' + i])
                  $this.$set(item, '__identity', 'x_' + i)
                  parent.push(item)
                } else {
                  $this.$set(item1, 'children', false)
                  $this.$set(item1, '__family', ['x_' + i, 'x_' + i + '_' + (index1 - 1)])
                  $this.$set(item1, '__identity', 'x_' + i + '_' + (index1 - 1))
                  parent.push(item1)
                }
              })
            }
          } else {
            $this.$set(item, 'children', false)
            $this.$set(item, '__family', ['x_' + i])
            $this.$set(item, '__identity', 'x_' + i)
            parent.push(item)
          }
        })
      }
      return parent
    }
  }
}
</script>

<style scoped lang="scss">
  .permission_placeholder {
    content: ' ';
    display: inline-block;
    width: 16px;
    font-size: 16px;
  }

  .permission_toggleFold {
    vertical-align: middle;
    padding-right: 5px;
    font-size: 16px;
    cursor: pointer
  }
</style>
