import {connect} from 'react-redux';
import ChronicInfo from '../components/ChronicInfo';
import {SHOW_TABLE_LIST,HIDE_TABLE_LIST, SET_CHRONIC_PUSHS,TOGGLE_CHRONIC_INFO} from '@store/types/pushMessage';
import {getTableList,getScaleInfo,getConceptDetail} from '../store/async-actions/pushMessage';
import {ADD_ASSESS_ITEMS,SET_SAVE_ASSESS_DATA,SET_CHRONIC_CALCU_RESULT,ADD_WHOLE_SCALE_ITEMS,SET_CALCU_VALUES} from "@types/assessResult";
import {saveMessage} from "../store/async-actions/print";
import {saveClickNum} from '@store/async-actions/homePage';
import { getFormulaResult } from '@store/async-actions/fetchModules'
import {deepClone} from '@utils/tools';
import Notify from '@commonComp/Notify';
import {dragBox} from '@utils/drag';

function mapStateToProps(state){
  const {pushMessage,assessResult} = state;
  return{
    chronicMagItem: state.diagnosticList.chronicMagItem,//慢病疾病
    //data:pushMessage.chronicPushItems,     //慢病右侧推送
    tableList: pushMessage.tableList,
    showList: pushMessage.showList,
    showHide: pushMessage.showHide,
    scaleInfo: pushMessage.scaleInfo,//量表内容
    chronicDesease:state.mainSuit.chronicDesease,//主诉存的慢病
    update:assessResult.update1,
    formulaResult:pushMessage.formulaResult, //量表信息
    indexs:assessResult.wholeIndexs,
    wholeResults:assessResult.wholeResults,     //整体评估可能结果
    calcuResult:assessResult.calcuResult,      //计算公式结果
    calcuValues:assessResult.calcuValues,      //计算公式填的值
    wholeScaleItems:assessResult.wholeScaleItems,     //全部量表中已加入病例的量表
    addedScaleIds:assessResult.addedScaleIds,      //已加入的量表id
    slideUp:pushMessage.slideUp,    //是否收起指标推送模块
    typeConfig: state.typeConfig
  }
}

function mapDispatchToProps(dispatch){
  return {
    toggleSlide(flag){
      dispatch({
        type:TOGGLE_CHRONIC_INFO,
        flag
      });
    },
    getTableList(name){
      dispatch(getTableList(name))
      // dispatch({type:SHOW_TABLE_LIST})
    },
    hideList(param,item){
      dispatch({
        type:SHOW_TABLE_LIST,
        name:param.name,
        value:param.value,
        item:item?Object.assign({},item):undefined
      })
      if(!param.value){
        dragBox('dragModalWrap','dragModalTitle','del')
      }
    },
    getScaleInfo(item){
      dispatch(getScaleInfo(item))
    },
    addAssessItem(row,pIndex,i){         //加入评估记录
      let obj = Object.assign({},row);
      const it = obj.details[i];
      dispatch({
        type:ADD_ASSESS_ITEMS,
        data:obj,
        index:pIndex,
        subIndex:i,
        id:it.type==1?it.content.conceptId:undefined,      //只有量表需要记录id,去重用
      })
    },
    addScaleItems(obj,id){
      dispatch({
        type:ADD_WHOLE_SCALE_ITEMS,
        data:obj,
        id
      })
    },
    //保存管理评估
    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())
        }
      }
    },
    savePossibleResult(data){
      dispatch({
        type: 'SAVE_POSSIBLE_RESULT',
        wholeResults: data
      })
    },
    //设置量表推送内容
    setChronicPush(data,id,calcuItem) {
      dispatch({
        type: SET_CHRONIC_PUSHS,
        data: data
      });
     
    },
    setCalcuInfo(id, calcuVal, calcuResult) {
      dispatch({
        type: SET_CHRONIC_CALCU_RESULT,
        result:calcuResult,
        id:id
      });
      dispatch({
        type:SET_CALCU_VALUES,
        data:deepClone(calcuVal),
        id:id
      })
    },
    // 计算公式计算
    calcuFormula(param,data,i, j) {
      getFormulaResult(param).then((res)=>{
        if(+res.data.code==0){
          const result = res.data.data.result;
          const dataCopy = deepClone(data)
          dataCopy[i].details[j].content.result = result
          dispatch({
            type: SET_CHRONIC_PUSHS,
            data: dataCopy
          });
          // dispatch({
          //   type: SET_CHRONIC_CALCU_RESULT,
          //   result:deepClone(result),
          //   id:param.disId
          // });
          // dispatch({
          //   type:SET_CALCU_VALUES,
          //   data:param.data.content.details,
          //   id:param.disId
          // })
        }else{
          Notify.error(res.data.msg||'计算没有结果返回');
        }
      })

    },
    // 获取量表静态知识
    getInfomation(item){
      dispatch(getConceptDetail(item));
    }
  }
}

const ChronicInfoContainer = connect(mapStateToProps,mapDispatchToProps)(ChronicInfo);
export default ChronicInfoContainer;


