import {
  tools
} from 'hq-tools'
export default (datas, columns, asyncDicts,subTotal, vue) => {
  subTotal.row = {};
  subTotal.total = {};
  if (!datas) {
    return datas;
  }
  for (let column of columns) {
    //异步翻译
    if (column.asyncTranslate) {
      let left = new Set();
      for (let data of datas) {
        if (!column.codeTable[data[column.key]]) {
          left.add(data[column.key]);
        }
      }
      let leftList = Array.from(left).filter((item) => {
        if (item && tools.trim(item) !== '') {
          return true;
        }
      })
      if (leftList.length > 0) {
        if ( typeof column.asyncTranslate === 'string'){
          vue.$http.post(column.asyncTranslate,
            leftList
          )
          .then(dict => {
            for (let key of Object.keys(dict)) {
              vue.$set(asyncDicts[column.key], key, dict[key])
            }
          })
        }else if ( column.asyncTranslate.length > 0 ) {
          Promise.all(column.asyncTranslate.map(item=>{
            return vue.$http.post(item,leftList)
          }))
          .then(dictList => {
            for ( let dict of dictList){
              for (let key of Object.keys(dict)) {
                vue.$set(asyncDicts[column.key], key, dict[key])
              }
            }
          })
        }
      }
    }
    //页面小计
    if (column.type == 'number') {
		if (column.subTotal) {
			subTotal.total[column.title] = 0;
			subTotal.row[column.key] = 0;
		}
		for (let data of datas) {
			if (data[column.key]) {
			if (column.subTotal && data[column.key]) {
				subTotal.total[column.title] += data[column.key];
				subTotal.row[column.key] += data[column.key];
			}
			}
		}
		if (column.subTotal) {
			subTotal.total[column.title] = vue.$tools.partition(subTotal.total[column.title], column.format, true) +
			((typeof column.subTotal == 'string') ? column.subTotal : '');
		}
    }else {
		if ( column.subTotal ){
			subTotal.row[column.key] = column.subTotal;
		}
    }
    if (column.subTotalName) {
      subTotal.name = column.subTotalName;
    }

  }
  if (vue.subtotal && vue.subtotal === 'row' && datas) {
    datas.push(subTotal.row);
  }
  return datas;
}
