// stat_total	计算列中所有值的和
// stat_multi_total	计算2列合计的乘积
// stat_max	显示列中的最大值
// stat_min 显示列中的最小值
// stat_average	计算列中所有值的平均值
// stat_count	计算列中的行数
              //  Math.round((~~count + ~~obj[key] + Number.EPSILON) * 1000) / 1000
import HeadTable from 'ant-design-vue/es/vc-table/src/HeadTable';
const total = (key, currentPageData) => {
  return currentPageData.reduce((pre, cur) => isFinite(+cur[key]) ? +cur[key] + pre : pre, 0)
}
const average = (key, currentPageData) => {
  const len = currentPageData.length
  return len === 0 ? 0 : total(key, currentPageData) / len
}
const statFns = {
  total,
  average
}
export function createFooting(currentPageData) {

  let h = this._c;

  let { attachFooter,vsColumn,prefixCls,saveRef, scroll={} } = this;

  let sColumns = vsColumn;


  if(!sColumns) return null
  // delete rowSelection.onSelect;
  // delete rowSelection.onChange;
  // delete rowSelection.onSelectAll;
  // delete rowSelection.selectedRowKeys;

//  let sColumns = columns.map(item => {
//   let el = { ...item }
//   delete el.scopedSlots
//   delete el.customRender
//   return el
// });

  /**@ stat
   * stat.total 计算总和
   * stat.average 平均值
   */

  if (attachFooter) {

    if (Array.isArray(attachFooter)) {
      attachFooter = attachFooter.reduce((obj, { key, ...attrs }) => {
        return key ? { ...obj, [key]: attrs } : obj
      }, {})
    }



    sColumns = sColumns.map((item, i) => {
      let key = item.dataIndex || item.key
      let { width, align, fixed } = item
      let col = attachFooter[key]
      let className =''
      if (fixed === 'left' || fixed === true) {
        className= 'ant-table-column-fixed-sticky-left'
      } else if (fixed == 'right') {
        className = 'ant-table-column-fixed-sticky-right';
      }


      let title = ''
      if (key == 'selection-column') {
        title = '';
      }

      if (key == this.defaultCountKey) {
        title = '合计';
      }

      if (col) {
        const statFn = statFns[col.stat] || total
        let statResult = statFn(key, currentPageData)
        statResult=Math.round((statResult + Number.EPSILON) * 10000) / 10000
        title=(col.prefixUnit || '') + statResult + (col.unit || '')
      }
      return {
        title,
        key,
        width,
        align,
        className
      }
    })

  }

  const tableClassName = scroll.x || false ? `${prefixCls}-fixed-footer` : '';
  const looh=() => {

  }
  let config = {
    key:"attachFooter",
    fixed:'attachFooter',
    columns: sColumns,
    tableClassName,
    handleBodyScrollLeft:looh,
    expander: {
      handleExpandChange: looh,
      renderExpandIndentCell: looh,
      renderRows: looh,
      props:{},

    }
  };


 return <div class={`t-attach-footer-table-scroll`}>
   <HeadTable
          {...{
            directives: [
              {
                name: 'ant-ref',
                value: saveRef(config.key)
              },
            ],
          }}
     props={config} style={{ marginTop: '-2px',  marginBottom:0,paddingBottom:0,overflow:'hidden'}} />

</div>

}

export default createFooting;
