import Vue from 'vue'
import XEUtils from 'xe-utils'
import { VXETable } from 'vxe-table'

import VxeTmplFilterCombination from './Combination.vue'
import FilterExcel from './FilterExcel'

Vue.component(VxeTmplFilterCombination.name, VxeTmplFilterCombination)
Vue.component(FilterExcel.name,FilterExcel)

VXETable.renderer.add('FilterAggregation', {
  showFilterFooter: false,
  renderFilter (h, renderOpts, params) {
    return [
      <vxe-tmpl-filter-combination render-params={ params } render-opts={ renderOpts }></vxe-tmpl-filter-combination>
    ]
  },
  filterResetMethod ({ options }) {
    options.forEach(option => {
      option.data = { checks: [], sVal: '' }
    })
  },
  filterMethod ({ option, row, column }) {
    /* eslint-disable eqeqeq */
    const { checks } = option.data
    const cellValue = XEUtils.get(row, column.property)
    return checks.includes(XEUtils.toString(cellValue))
  }
})

const reKeys = { '~*': '\\*', '~?': '\\?' }
const reLikes = { '*': '.*', '?': '.{1}', '~': '~' }

function getMatchRE (matchText, isbegin, isEndin) {
  return new RegExp((isbegin ? '^' : '') + '(' + matchText.replace(/(~\*)|(~\?)|(~+)|(\*+)|(\?)/g, match => reKeys[match] || reLikes[match.charAt(0)]) + ')' + (isEndin ? '$' : ''))
}

VXETable.renderer.add('FilterCombination', {
  showFilterFooter: false,
  renderFilter (h, renderOpts, params) {
    return [
      <vxe-tmpl-filter-combination menus={ ['sort', 'filter'] } show-menu render-params={ params } render-opts={ renderOpts }></vxe-tmpl-filter-combination>
    ]
  },
  filterResetMethod ({ options }) {
    options.forEach(option => {
      option.data = { checks: [], sVal: '', fMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' }
    })
  },
  filterMethod ({ option, row, column }) {
    /* eslint-disable eqeqeq */
    const { checks, fType1, fVal1, fMode, fType2, fVal2 } = option.data
    let cellValue = XEUtils.get(row, column.property)
    if (fType1 || fType2) {
      const calculate = (type, val) => {
        let matchText
        if (['gt', 'ge', 'lt', 'le'].includes(type)) {
          cellValue = XEUtils.toNumber(cellValue)
          matchText = XEUtils.toNumber(val)
        } else {
          cellValue = XEUtils.toString(cellValue).toLowerCase()
          matchText = XEUtils.toString(val).toLowerCase()
        }
        switch (type) {
          // 数值匹配
          case 'gt':
            return cellValue > matchText
          case 'ge':
            return cellValue > matchText || cellValue == matchText
          case 'lt':
            return cellValue < matchText
          case 'le':
            return cellValue < matchText || cellValue == matchText
          // 通用匹配
          case 'equal':
            return getMatchRE(matchText, true, true).test(cellValue)
          case 'unequal':
            return !getMatchRE(matchText, true, true).test(cellValue)
          case 'begin':
            return getMatchRE(matchText, true, false).test(cellValue)
          case 'notbegin':
            return !getMatchRE(matchText, true, false).test(cellValue)
          case 'endin':
            return getMatchRE(matchText, false, true).test(cellValue)
          case 'notendin':
            return !getMatchRE(matchText, false, true).test(cellValue)
          case 'include':
            return getMatchRE(matchText).test(cellValue)
          case 'exclude':
            return !getMatchRE(matchText).test(cellValue)
        }
        return true
      }
      const f1Rest = calculate(fType1, fVal1)
      if (fMode === 'and') {
        return f1Rest && calculate(fType2, fVal2)
      }
      return f1Rest || calculate(fType2, fVal2)
    } else if (checks.length) {
      return checks.includes(XEUtils.toString(cellValue))
    }
    return false
  }
})
VXETable.renderer.add('FilterExcell', {
  // 不显示底部按钮，使用自定义的按钮
  showFilterFooter: false,
  // 筛选模板
  renderFilter (h, renderOpts, params) {
    return [
      <filter-excel params={ params }></filter-excel>
    ]
  },
  // 重置数据方法
  filterResetMethod ({ options }) {
    options.forEach(option => {
      option.data = { vals: [], sVal: '', fMenu: '', f1Type: '', f1Val: '', fMode: 'and', f2Type: '', f2Val: '' }
    })
  },
  // 筛选数据方法
  filterMethod ({ option, row, column }) {
    const cellValue = row[column.property]
    const { vals, f1Type, f1Val } = option.data
    if (cellValue) {
      if (f1Type) {
        return cellValue.indexOf(f1Val) > -1
      } else if (vals.length) {
        // 通过指定值筛选
        return vals.includes(cellValue)
      }
    }
    return false
  }
})
