import React from 'react';
import {connect} from 'react-redux';
import AssessResult from '@components/AssessResult';
import {ISREAD} from '@store/types/homePage.js';
import {billing} from '@store/async-actions/pushMessage';
import {getAssessData,getIndexData} from '@store/async-actions/fetchModules';
import {SET_ASSESS_DATA,SET_INDEX_DATA,SET_INDEX_SUB_TYPE,REMOVE_ASSESS_ITEMS,CLEAR_INDEX_DATA} from '@types/assessResult';
import store from '@store';
import {SET_SCALE_INFO} from "../store/types/pushMessage";

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

function mapStateToProps(state) {
  const {assessResult,pushMessage} = state;
  return {
    data:assessResult.data,
    indexData:assessResult.indexData,
    update:assessResult.update,     //评估数据更新
    update1:assessResult.update1,       //加入记录更新
    timeTypes:assessResult.indexTimeTypes,    //图表切换的维度
    //wholeAssessData:assessResult.wholeAssess,   //评估内容
    wholeIndexs:assessResult.wholeIndexs||{},      //已加入评估记录的推送项
    wholeResults:assessResult.wholeResults,     //整体评估可能结果
    chooseSelecteds:assessResult.chooseSelecteds,    //已选中的选项
    wholeAssessText:assessResult.wholeAssessText,     //整体评估补充说明
    scaleInfo: pushMessage.scaleInfo,//量表内容
    scaleUpdate:pushMessage.scaleUpdate,
    chronicPushItems: pushMessage.chronicPushItems, //慢病右侧推送
    formulaResult:pushMessage.formulaResult,       //量表计算结果
    calcuResult:pushMessage.calcuResult,      //评估弹窗可能结果计算结果
  }
}

function mapDispatchToProps(dispatch) {
  return {
    getAssess(isRecommend){        //获取评估内容数据
      if(isRecommend){
        const data = {
          regionName: "整体评估",
          regionType: "0",
          isRecommend:true
        };
        dispatch({
          type:SET_ASSESS_DATA,
          data:[data]
        });
        return;
      }
      dispatch(getAssessData()).then((res)=>{
        if(res.data.code=='0'){
          dispatch({
            type:SET_ASSESS_DATA,
            data:res.data.data
          })
        }
      }).catch(error=>{
        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);
      });
    },
    clearChartData(){   //清除评估图表数据
      dispatch({
        type:CLEAR_INDEX_DATA
      })
    },
  }
}

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.indexDesc]||[];
    nameArr=nameObj[temp.indexDesc]||[];
    obj[temp.indexDesc]=[...arr,it];
    nameObj[temp.indexDesc]=[...nameArr,it.itemName];
  });
  //nameObj,obj;
  return obj;
}
const AssessResultContainer = connect(mapStateToProps, mapDispatchToProps)(AssessResult);

export default AssessResultContainer;