import React from 'react';
import {connect} from 'react-redux';
import AssessResultHis from '@components/AssessResult/AssessHis';
import {ISREAD} from '@store/types/homePage.js';
import {billing} from '@store/async-actions/pushMessage';
import {getAssessHis,getIndexData} from '@store/async-actions/fetchModules';
import {SET_ASSESS_DATA,SET_INDEX_DATA} from '@types/assessResult';
import {SET_ASSESS_HISTORY_DATA,SET_ASSESS_HISTORY_LOADING} from '@types/assessResultHis';

/***
 * 管理评估逻辑：
 * 根据推送弹窗显示“智能推荐”，首次无选中项
 * 评估内容项接口返回，其中整体评估为右侧操作加入
 * 点确定等同于页面病例保存按钮（同时保存页面数据和评估结果）
 * 点确定后再次打开，保留上次操作结果（页面清空，引用除外），
 * 但推荐选项按最新推送显示
 *
 * ***/

function mapStateToProps(state) {
  const {assessResultHis,assessResult} = state;
  return {
    list:assessResultHis,    //已获取到的历史评估列表
    indexData:assessResult.indexData,
    loading:assessResultHis.loading,
    timeTypes:assessResult.indexTimeTypes,    //图表切换的维度
  }
}

function mapDispatchToProps(dispatch) {
  return {
    getAssessHis(id){
      dispatch({
        type:SET_ASSESS_HISTORY_LOADING,
        flag:true
      });
      dispatch(getAssessHis(id)).then((res)=>{
        if(res.data.code=='0'){
          const data = res.data.data[0]&&res.data.data[0].htmlContent;
          dispatch({
            type:SET_ASSESS_HISTORY_DATA,
            id:id,
            data:JSON.parse(data||null)
          });
        }
        dispatch({
          type:SET_ASSESS_HISTORY_LOADING,
          flag:false
        });
      }).catch(error=>{
        dispatch({
          type:SET_ASSESS_HISTORY_LOADING,
          flag:false
        });
        console.log(error);
      });
    },
    getIndexData(param){        //获取评估图表数据
      const {range,rangeType,index,pindex,getNew} = param;
      dispatch(getIndexData(range)).then((res)=>{
        if(res.data.code=='0'){
          dispatch({
            type:SET_INDEX_DATA,
            data:parseChartData(res.data.data),
            range,
            index,
            rangeType,
            pindex,
            getNew
          });
        }
      }).catch(error=>{
        console.log(error);
      });
    },
  }
}

function parseChartData(indexData){
  let arr=[],obj={},temp={};
  let nameArr=[],nameObj={};  //暂弃
  if(!indexData){
    return [];
  }
  indexData.itemIndexData.map((it)=>{
    temp=indexData.indexConfigData.find((i)=>{return it.itemName==i.indexUnique});
    arr=obj[temp.orderNo]||[];
    nameArr=nameObj[temp.orderNo]||[];
    obj[temp.orderNo]=[...arr,it];
    nameObj[temp.orderNo]=[...nameArr,it.itemName];
  });
  //nameObj,obj;
  return obj;
}
const AssessResultHisContainer = connect(mapStateToProps, mapDispatchToProps)(AssessResultHis);

export default AssessResultHisContainer;