export const changeAssay=(state,action)=>{
  const res=Object.assign({},state);
  res.assay = res.assay.map(item => {
    if(item.id === action.item.id && item.name === action.item.name){
      item.checked = !item.checked
    }
    return item
  })
  return res;
};

export const changeCheck=(state,action)=>{
  const res=Object.assign({},state);
  res.check = res.check.map(item => {
    if(item.id === action.item.id && item.name === action.item.name){
      item.checked = !item.checked
    }
    return item
  })
  return res;
};

//获取右侧推送信息
export const setAdvice=(state,action)=>{
	const res=Object.assign({},state);  
	// res.determine = action.determine
	//不展示确诊，将确诊跟疑似诊断放在一起展示
  res.doubt = action.determine.concat(action.doubt);
  res.possible = action.possible;
  res.vigilant = action.vigilant;
  res.assay = action.lab;
	res.check = action.pacs;
  return res;
};

//获取医嘱信息字符串
function getAdviceStr(advice) {
	const scheme = advice.scheme || '';
	const assay = advice.assay || '';
	const check = advice.check || '';
	let AdviceStr = advice.AdviceStr || '';
	for (let i = 0; i < scheme.length; i++) {
		for (let j = 0; j < scheme[i].treatment.length; j++) {
			if(scheme[i].treatment[j].treatmentStr === '') {
				AdviceStr = AdviceStr + scheme[i].treatment[j].treatmentStr
			} else {
				AdviceStr = AdviceStr + ', ' + scheme[i].treatment[j].treatmentStr
			}
			
		}
	}
	AdviceStr = AdviceStr.substring(1, AdviceStr.length)
	if(assay || check)  { 
		AdviceStr = assay + check +'; ' + AdviceStr;
	}	
	return AdviceStr;
}

//治疗方案添加到医嘱
export const addScheme = (state, action) => {
  const res = JSON.parse(JSON.stringify(state))
  const treatment = action.treatment;
  const treatItem = {};
  treatItem.name = action.title;
  const scheme = res.advice.scheme || [];
  let isRepeat = false; 
  let RepeatIndex;
  
  for (let i = 0; i < treatment.length; i++) {
    let treatmentStr = '';
    for (let j = 0; j < treatment[i].medicitionsList.length; j++) {
      if(treatment[i].medicitionsList[j].selected) {
				treatmentStr = treatmentStr + treatment[i].medicitionsList[j].medicitionName + ', '
			}
	}
	treatment[i].treatmentStr = treatmentStr.substring(0,treatmentStr.length-2)
  }
  treatItem.treatment = treatment;
  for (let i = 0; i < scheme.length; i++) {
    if (scheme[i].name === treatItem.name) {
      isRepeat = true
    }
	}
	
  //判断医嘱中是否包含该诊断
  scheme.map((item, index) => {
    if(item.name === treatItem.name) {
     isRepeat = true
     RepeatIndex = index;
    }
  })
  if (isRepeat) {
   //将同类药添加到一起
	for (let i = 0; i < scheme[RepeatIndex].treatment.length; i++) {
		for (let j = 0; j < treatment.length; j++) {
			if (scheme[RepeatIndex].treatment[i].id === treatment[j].id && scheme[RepeatIndex].treatment[i].drugsName === treatment[j].drugsName) {
				for(let z = 0; z < treatment[j].medicitionsList.length; z++) {
					if(treatment[j].medicitionsList[z].selected) {
						if(scheme[RepeatIndex].treatment[i].treatmentStr !== '') {
							scheme[RepeatIndex].treatment[i].treatmentStr = scheme[RepeatIndex].treatment[i].treatmentStr + ', ' + treatment[j].medicitionsList[z].medicitionName 
						} else {
							scheme[RepeatIndex].treatment[i].treatmentStr = scheme[RepeatIndex].treatment[i].treatmentStr + '' + treatment[j].medicitionsList[z].medicitionName
						}
					}
				}
			}
		}
	}

//    let repeatDiag = res.advice.scheme[RepeatIndex].treatment
//    for (let i = 0; i <  repeatDiag.length; i++) {
//      for (let j = 0; j < action.treatment.length; j++) {
//        if(repeatDiag[i].id === action.treatment[j].id) {
//          repeatDiag[i].medicitionsList = repeatDiag[i].medicitionsList.concat(action.treatment[j].medicitionsList)
//        }
//      }
//    }
//    repeatDiag = repeatDiag.concat(action.treatment)
  } else {
		for (let i = 0; i < treatItem.treatment.length; i++) {
			for (let j = 0; j < treatItem.treatment[i].medicitionsList.length; j++) {
				if(treatItem.treatment[i].medicitionsList[j].selected) {
					res.advice.scheme = scheme.concat(treatItem)
				}
			}
		}
     
  }
  res.AdviceStr = getAdviceStr(res.advice)
  return res;
}

export const setTips = (state, action) => {
  const res = Object.assign({}, state)
  res.tips = action.tips;
  return res;
}

export const setChangeAdviceTreatment = (state, action) => {
	const res = JSON.parse(JSON.stringify(state))
	const index = action.index; //诊断的下标
	const ii = action.ii //同类药下表
	const changeInput = action.changeInput;
	let scheme = res.advice.scheme;
	scheme[index].treatment[ii].treatmentStr = changeInput
	return res;
}

export const setChangeAdviceAssay = (state, action) => {
	const res = JSON.parse(JSON.stringify(state))
	res.advice.assay = action.changeInput;
	return res;
}

export const setChangeAdviceCheck = (state, action) => {
	const res = JSON.parse(JSON.stringify(state))
	res.advice.check = action.changeInput;
	return res;
}

export const addBilling = (state, action) => {
	const res = JSON.parse(JSON.stringify(state));
	const {assay,check} = action;
	res.assay = res.assay.map((item,index)=>{
		item.checked = false
		return item
	})
	res.check = res.check.map((item,index)=>{
		item.checked = false
		return item
	})
	res.advice.assay = res.advice.assay || '';
	res.advice.check = res.advice.check || '';
	for (let i = 0; i < assay.length; i++) {
		if ( res.advice.assay === '') { //如果最后一个，则不需要逗号
			res.advice.assay = res.advice.assay + assay[i].name
		} else {
			if( i === 0 && res.advice.check !== '') {
				res.advice.assay = res.advice.assay  +  assay[i].name
			} else {
				res.advice.assay = res.advice.assay + ', ' + assay[i].name
			}
		}
		
	}
	for (let i = 0; i < check.length; i++) {
		if ( res.advice.check === '') { //如果最后一个，则不需要逗号
			res.advice.check = res.advice.check + check[i].name
		} else {
			res.advice.check = res.advice.check + ', '+ check[i].name 
		}
	}
	if(res.advice.assay  && res.advice.check !== '') {
			if(res.advice.assay.substring(res.advice.assay.length-2,res.advice.assay.length-1) !== ', ') {
				res.advice.assay = res.advice.assay + ', '
			} 
	}
	res.AdviceStr = getAdviceStr(res.advice)
	return res;
}
export const clearAllPushMessage = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
	res.advice = action.data
	res.AdviceStr = action.saveText
	return res;
}