import _default from './default';
import { Statistic } from 'ant-design-vue';

export function createFooting(solData) {
  let h = this.$createElement;
  const { footing } = this;
  let paramsPage = this.$parent.paramsPage;
  let has = Object.prototype.toString.call(footing);
  if (has === '[object Array]' && footing.length > 0) {
    let vNodeList = [];
    let { pageSize, pageNum } = paramsPage;
    const lents = pageSize * pageNum;

    //如果分页数与数据数量不相等,针对把所有数据一次性拉出的
    let data = [...solData];
    if (data && data.length > 0 && pageSize != data.length && pageSize < data.length) {
      let _index = pageSize * (pageNum - 1);
      let _howmany = pageSize;
      data = data.splice(_index, _howmany);
    }

    footing.forEach((field) => {
      let unit = field.unit || ''; //单位
      let title = this.columns.find((c) => field.key == (c.dataIndex || c.key));
      if (!title) return;
      title = title.title;
      let value = 0;
      //处理数据数量大与分页数的计算
      if (data && data.length > 0) {
        data.forEach((dataItem) => {
          value += Number(dataItem[field.key]);
        });
      }

      let item = h(Statistic, {
        staticClass: 't-table-footing-item baseline',
        props: {
          title: title + '：',
          value,
          suffix: unit,
          valueStyle: { color: '#1890ff' }
        }
      });
      vNodeList.push(item);
    }, this);
    data = null;

    let head = h('div', { class: 't-table-footing-head' }, '合计');
    return h('div', { class: 't-table-footing' }, [head, vNodeList]);
  } else {
    return undefined;
  }
}

export default createFooting;
