/*
 * @Author: Badguy
 * @Date: 2022-02-11 09:20:58
 * @LastEditTime: 2022-05-25 17:55:05
 * @LastEditors: your name
 * @Description: 表格用
 * have a nice day!
 */

import _ from 'lodash'

/**
 * @description: 表格行合并
 * @param {Object} config
 * @return {Array}
 */
export function mergeTableRow(config) {
  let data = config.data
  const { mergeColNames, firstMergeColNames, firstMerge } = config
  if (!mergeColNames || mergeColNames.length === 0) {
    return data
  }
  mergeColNames.forEach(m => {
    const mList = {}
    data = data.map((v, index) => {
      const rowVal = v[m]
      if (mList[rowVal] && mList[rowVal].newIndex === index) {
        const flag =
          firstMergeColNames.filter(f => {
            return f === m
          }).length !== 0
        const mcFlag =
          mergeColNames.filter(mc => {
            return mc === firstMerge
          }).length === 0
        if (
          (mcFlag && flag) ||
          (flag && data[index][firstMerge + '-span'] && data[index][firstMerge + '-span'].rowspan === 1)
        ) {
          v[m + '-span'] = {
            rowspan: 1,
            colspan: 1
          }
        } else {
          data[mList[rowVal]['index']][m + '-span'].rowspan++
          v[m + '-span'] = {
            rowspan: 0,
            colspan: 0
          }
          mList[rowVal]['num']++
          mList[rowVal]['newIndex']++
        }
      } else {
        mList[rowVal] = { num: 1, index: index, newIndex: index + 1 }
        v[m + '-span'] = {
          rowspan: 1,
          colspan: 1
        }
      }
      return v
    })
  })
  return data
}

// 表格单元格class设置
export function commonTableCellClass({ row, column, rowIndex, columnIndex }) {
  const classes = []
  if (['ActualDepartureTime', 'ActualLandingTime'].includes(column.property)) {
    classes.push('pre-line')
  }
  return classes
}

// 获取对应时区的时间
export function timeInZone(date, timeZone = 0, local = 8) {
  if (!(date instanceof Date)) {
    if (typeof date === 'string' && date.length) {
      date = new Date(date)
    } else {
      return ''
    }
  }

  function formatDate(num) {
    return num < 10 ? '0' + num : num
  }
  const time = date.getTime() + (timeZone - local) * 60 * 60 * 1000
  date.setTime(time)
  const year = date.getFullYear()
  const month = formatDate(date.getMonth() + 1)
  const day = formatDate(date.getDate())
  const hour = formatDate(date.getHours())
  const minute = formatDate(date.getMinutes())
  const second = formatDate(date.getSeconds())
  return `${year}-${month}-${day} ${hour}:${minute}:${second}`
}

// // 表格添加过滤条件
// export function setTableFilters(tableData, filterKeys) {
//   const tempSets = {}
//   const result = {}
//   filterKeys.forEach(key => {
//     tempSets[key] = new Set()
//   })
//   tableData.forEach(item => {
//     Object.keys(tempSets).forEach(key => {
//       (item[key] ?? '') !== '' && tempSets[key].add(item[key])
//     })
//   })
//   Object.keys(tempSets).forEach(key => {
//     result[key] = _.sortBy(
//       [...tempSets[key]].map(value => ({
//         text: value,
//         value
//       })),
//       ['value']
//     )
//   })
//   return result
// }

// 表格添加过滤条件
export function setTableFilters(tableData, filters) {
  const tempSets = {}
  Object.keys(filters).forEach(key => {
    tempSets[key] = new Set()
  })
  tableData.forEach(item => {
    Object.keys(tempSets).forEach(key => {
      (item[key] ?? '') !== '' && tempSets[key].add(item[key])
    })
  })
  Object.keys(tempSets).forEach(key => {
    filters[key] = _.orderBy(
      [...tempSets[key]].map(value => ({
        text: value,
        value
      })),
      o => o.value
    )
  })
}
