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 {getIndexData} from '@store/async-actions/fetchModules';
import {SET_INDEX_DATA,SET_INDEX_SUB_TYPE,REMOVE_ASSESS_ITEMS,CLEAR_INDEX_DATA,SET_CHRONIC_CALCU_RESULT,SET_CALCU_VALUES} from '@types/assessResult';
import {parseChartData,deepClone} from '@utils/tools';
import {SHOW_TABLE_LIST,HIDE_TABLE_LIST} from '@store/types/pushMessage';
import {saveMessage} from "../store/async-actions/print";
import {SET_SAVE_ASSESS_DATA} from "@types/assessResult";
import {saveClickNum} from '@store/async-actions/homePage';

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

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

function mapDispatchToProps(dispatch) {
  return {
    getIndexData(param){        //获取评估图表数据
      const {range,rangeType,index,pindex,getNew,disName} = param;
      dispatch(getIndexData(range,disName)).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
      })
    },
    //保存管理评估
    saveAssessInfos(obj,flg,noSave){     //noSave未true则不调页面保存接口
      dispatch(Object.assign({},obj,{type:SET_SAVE_ASSESS_DATA}));
      if(!noSave){
        dispatch(saveClickNum);
        if(flg != 1 && flg != 2){
          dispatch(() => saveMessage())
        }
      }
    },
  }
}


const AssessResultContainer = connect(mapStateToProps, mapDispatchToProps)(AssessResult);

export default AssessResultContainer;