export default {
  data () {
    return {
      tableMaxHeight: 0
    }
  },
  inject: ['app'],
  methods: {
    setTableHeight () {
      let wrap = document.querySelector('.app-main').getBoundingClientRect()
      // let table = this.$refs['table-wrap'].getBoundingClientRect()
      this.tableMaxHeight = wrap.height - 50
    },
    totalRowClassName ({ row, rowIndex }) {
      if (row.ticket_spot_name === '总计') {
        return 'table-total-row'
      }

      if (row.manager_name === '小计' && row.payChannel === '小计' && row.ticketTypeName === '小计') {
        return 'table-total-row table-total-row1'
      }

      if (row.payChannel === '小计' && row.ticketTypeName === '小计') {
        return 'table-total-row table-total-row2'
      }

      if (row.ticketTypeName === '小计' || row.price === '小计') {
        return 'table-total-row table-total-row3'
      }
    },
    isSumSpan (row, arr) {
      let flag = true
      for (let i = 0; i < arr.length; i++) {
        if (row[arr[i]] !== '小计') {
          flag = false
          break
        }
      }
      return flag
    },
    arraySpanMethod ({ row, column, rowIndex, columnIndex }) {
      // const isMember = this.app.tableData[rowIndex].hasOwnProperty('memberTypeName')

      if (!this.colunmSpan0) {
        this.colunmSpan0 = this.getSpanArrayByRepeat(this.app.tableData, 'ticket_spot_name')
      }

      // let lastSpan = this.colunmSpan0[this.colunmSpan0.length - 1]

      if (columnIndex === 0) {
        if (this.isSumSpan(row, ['ticket_spot_name']) || this.isSumSpan(row, ['manager_name', 'payChannel'])) {
          return [1, 1]
        }
        return this.getRowSpan(this.app.tableData, { 'manager_name': '小计' }, rowIndex, false)
      }

      if (columnIndex === 1) {
        if (this.isSumSpan(row, ['payChannel', 'ticketTypeName'])) {
          return [1, 1]
        }
        if (this.isSumSpan(row, ['payChannel', 'manager_name'])) {
          return [1, 1]
        }
        return this.getRowSpan(this.app.tableData, { 'payChannel': '小计' }, rowIndex, false)
      }

      if (columnIndex === 2) {
        if (this.isSumSpan(row, ['ticketTypeName', 'payChannel'])) {
          return [1, 1]
        }
        return this.getRowSpan(this.app.tableData, { 'ticketTypeName': '小计', price: '小计' }, rowIndex, false)
      }

      if (columnIndex === 3) {
        if (this.isSumSpan(row, ['ticketTypeName', 'price'])) {
          return [1, 1]
        }
        return this.getRowSpan(this.app.tableData, { 'price': '小计' }, rowIndex, false)
      }

      return [1, 1]
    },
    getRowSpanByRepeat (spanArray, rowIndex) {
      let span = spanArray.find(i => {
        return rowIndex >= i[0] && rowIndex <= i[1]
      })
      if (!span || (span[1] === span[0])) {
        return [1, 1]
      }

      if (rowIndex === span[0]) {
        return [span[1] - span[0] + 1, 1]
      } else {
        return [0, 0]
      }
    },
    handleCellStyle ({ row, column, rowIndex, columnIndex }) {
      if (columnIndex > 4 && columnIndex <= 7) {
        if (columnIndex === 7) {
          return 'greenBg boldBorder'
        }
        return 'greenBg'
      }

      if (columnIndex > 7 && columnIndex <= 10) {
        if (columnIndex === 10) {
          return 'redBg boldBorder'
        }
        return 'redBg'
      }

      if (columnIndex > 10) {
        if (columnIndex === 10) {
          return 'blueBg boldBorder'
        }
        return 'blueBg'
      }

      if (columnIndex === 4) {
        return 'boldBorder'
      }
    },
    getSpanArrayByRepeat (data, tag) {
      let indexArray = []
      data.forEach((item, index) => {
        if (index > 0) {
          if (item[tag] !== data[index - 1][tag]) {
            indexArray.push(index)
          }
        }
      })
      if (!indexArray.length) {
        indexArray = [data.length]
      }

      let rowSpanArray = indexArray.map((i, index) => {
        if (index === 0) {
          return [0, i - 1]
        } else {
          return [indexArray[index - 1], i - 1]
        }
      })
      if (indexArray.length > 0) {
        rowSpanArray.push([indexArray[indexArray.length - 1], data.length - 1])
      }
      return rowSpanArray
    },
    /**
     * @tag 合并单元格标识数组，如[‘manager_name’,'售票点小计']
     * @rowIndex 行序号
     * @flag 是否合并边界
     */
    getRowSpan (data, tag, rowIndex, flag) {
      let totalIndex = data.map((item, index) => {
        let flag = true

        for (let key in tag) {
          if (tag[key] !== item[key]) {
            flag = false
            break
          }
        }

        if (flag) {
          return index
        } else {
          return null
        }
      }).filter(Boolean)

      let rowSpanArray = totalIndex.map((i, index) => {
        if (index === 0) {
          return [0, i]
        } else {
          return [totalIndex[index - 1] + 1, i]
        }
      })

      let span = rowSpanArray.find(i => {
        return rowIndex >= i[0] && rowIndex <= i[1]
      })

      if (!span) {
        return [1, 1]
      }

      if (rowIndex === span[0]) {
        return [span[1] - span[0] + (flag ? 1 : 0), 1]
      } else if (rowIndex === span[1]) {
        return flag ? [0, 0] : [1, 1]
      } else {
        return [0, 0]
      }
    },
    cashSpanMethod ({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === this.app.cashData.length - 1) return [1, 1]
      if (columnIndex === 0) {
        return this.getRowSpan(this.app.cashData, ['manager_name', '售票点小计'], rowIndex, true)
      }
    }
  },
  mounted () {
    this.setTableHeight()
    window.addEventListener('resize', this.setTableHeight)
  },
  beforeDestroy () {
    window.removeEventListener('resize', this.setTableHeight)
  }
}
