import React from 'react';
import {getDataFieldValue,getDataFieldValueByCol} from "../../utils/common";
// import { connect } from 'dva';

function TableFooter({ data, columns, selected, sumNum }) {
  // 查询字段对应内容
  const fieldQuery = function(field, obj) {
    const val= getDataFieldValueByCol(obj,field);
    if(!isNaN(val))
      return  parseFloat(val);
    return 'a';
  };

  let result = (item) => { // 统计合计
    let t = 0;
    const field=item.formatFuncName?((item.field?item.field:item.formatFuncName) +"_formatValue"): item.field;
    if (item.footer === 1) {
      data.forEach(i => {
        if (sumNum === 1) {
          if (!isNaN(fieldQuery(field, i))) {
            t = t + fieldQuery(field, i)
          }
        } else {
          selected.forEach(j => {
            if (i.ID === j) {
              if (!isNaN(fieldQuery(field, i))) {
                t = t + fieldQuery(field, i)
              }
            }
          })
        }
      })
    } else if (item.footer === 2) { // 统计平均值
      let x = 0;
      let s = 0;
      data.forEach(i => {
        if (sumNum === 1) {
          if (!isNaN(fieldQuery(field, i))) {
            s = s + fieldQuery(field, i);
            x++
          }
        } else {
          selected.forEach(j => {
            if (i.ID === j) {
              if (!isNaN(fieldQuery(field, i))) {
                s = s + fieldQuery(field, i);
                x++
              }
            }
          })
        }
      });
      if (x !== 0) {
        t = s/x
      } else {
        t = 0
      }
    } else if (item.footer === 3) { // 统计最大值
      let s = [];
      data.forEach(i => {
        if (sumNum === 1) {
          if (!isNaN(fieldQuery(field, i))) {
            s.push(fieldQuery(field, i))
          }
        } else {
          selected.forEach(j => {
            if (i.ID === j) {
              if (!isNaN(fieldQuery(field, i))) {
                s.push(fieldQuery(field, i))
              }
            }
          })
        }
      });
      t = Math.max.apply(Math, s)
    } else if (item.footer === 4) { // 统计最小值
      let s = [];
      data.forEach(i => {
        if (sumNum === 1) {
          if (!isNaN(fieldQuery(field, i))) {
            s.push(fieldQuery(field, i))
          }
        } else {
          selected.forEach(j => {
            if (i.ID === j) {
              if (!isNaN(fieldQuery(field, i))) {
                s.push(fieldQuery(field, i))
              }
            }
          })
        }
      });
      t = Math.min.apply(Math, s)
    } else if (item.footer === 5) { // 统计类别
      let s = [];
      data.forEach(i => {
        if (sumNum === 1) {
            const val= getDataFieldValue(i,field);
            s.push(val);
        } else {
          selected.forEach(j => {
            if (i.ID === j) {
              const val= getDataFieldValue(i,field);
              s.push(val);
            }
          })
        }
      });
      if(s&&s.length) {
        t = [...new Set(s)].length;
      }
    }
    // return t
    return parseFloat(t.toFixed(10))
  };

  return <div>
      当前选中共{sumNum === 1 ? data.length : selected.length}条记录，
      {
        columns.map(item => {
          switch (item.footer) {
            case 1:
              return <span><strong>累计{item.label}</strong><span>{result(item)}</span>,</span>;
            case 2:
              return <span><strong>{item.label}</strong>平均值<span>{result(item)}</span>,</span>;
            case 3:
              return <span><strong>{item.label}</strong>最大值<span>{result(item)}</span>,</span>;
            case 4:
              return <span><strong>{item.label}</strong>最小值<span>{result(item)}</span>,</span>;
            case 5:
              return <span><strong>{item.label}</strong><span>{result(item)}种</span>,</span>;
            default:
              return null
          }
        })
      }
  </div>
}

export default TableFooter;
