import {SET_ASSESS_DATA,SET_INDEX_DATA,SET_SAVE_ASSESS_DATA,SET_INDEX_SUB_TYPE,ADD_ASSESS_ITEMS,
  REMOVE_ASSESS_ITEMS,CLEAR_INDEX_DATA,SET_CHRONIC_CALCU_RESULT} from '@types/assessResult';
import config from "@config";

const init = {
  data:[],        //评估模块数据
  indexData:{},     //评估图表数据
  indexTimeTypes:{},    //单个图表选中的维度
  wholeAssess:[],     //整体评估数据
  wholeIndexs:{},     //已加入的评估数据的index,加入按钮状态用
  wholeAssessText:'',   //整体评估补充说明
  chooseSelecteds:{},   //选项勾选状态
  wholeResults:{
    possible:{},
    radioVal:{}
  },    //可能结果
  calcuResult:{}
};
export default (state=init,action)=>{
  const res = Object.assign({},state);
  const {data,range,index,rangeType,pindex,getNew} = action;
  switch (action.type){
    case SET_ASSESS_DATA:
      res.data = action.data;
      return res;
    case SET_INDEX_DATA:
      res.indexData[range[0]+range[1]] = data;
      Object.keys(data).map((i)=>{
        if(getNew){
          res.indexTimeTypes[pindex]=Object.assign({},res.indexTimeTypes[pindex],{[i]:config.chartDismen});
        }else{
          if(i===index){
            res.indexTimeTypes[pindex][i]=rangeType;
          }
        }
      });
      res.update = Math.random();   //对象更新
      return res;
    case CLEAR_INDEX_DATA:      //删除图表数据，因为图表数据为记录，需实时更新
      res.indexData = {};
      res.indexTimeTypes = {};
      return res;
    case SET_INDEX_SUB_TYPE:
      if(!index){
        return res;
      }
      res.indexTimeTypes[index] = rangeType;
      res.update = Math.random();   //对象更新
      return res;
    case 'SAVE_POSSIBLE_RESULT':
      res.wholeResults = action.wholeResults||{};
      return res;
    case SET_SAVE_ASSESS_DATA:
      res.wholeIndexs = action.wholeAssessItems||{};
      res.wholeResults = action.wholeResults||{};
      //res.indexTimeTypes = action.chartTimeTypes;
      res.wholeAssessText = action.wholeAssessText||'';
      res.chooseSelecteds = action.chooseSelecteds||{};
      return res;
    //case CLEAR_ASSESS_DATA:
    case ADD_ASSESS_ITEMS:
      res.wholeAssess[action.index] = action.data;
      let arr = res.wholeIndexs[action.index];
      res.wholeIndexs[action.index] = arr?[...arr,action.subIndex]:[action.subIndex];
      res.update1 = Math.random();   //对象更新,与其他字段名不同因为绑定在pushItems,避免不必要的渲染
      return res;
    case REMOVE_ASSESS_ITEMS:
      const {index,pIndex} = action;
      const temp =[...res.wholeIndexs[pIndex]];
      res.wholeIndexs[pIndex].splice(temp.findIndex((i)=>i==index),1);
      //res.wholeAssess[pIndex].details[index].add=false;
      return res;
    case SET_CHRONIC_CALCU_RESULT:
      res.calcuResult[action.id] = action.data;
      return res;
    default:
      return res;
  }
}