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.likely = action.likely;
  res.assay = action.lab;
	res.check = action.pacs;
  res.setPushEmergency = action.setPushEmergency;
  res.setPushEmergencyIdx=action.setPushEmergencyIdx
  return res;
};

//获取医嘱信息字符串
function getAdviceStr(advice) {
	const commontreatment = advice.commontreatment
	const scheme = advice.scheme || '';
	const adviceInput = advice.adviceInput || '';
	const assay = advice.assay || '';
	const check = advice.check || '';
	const followUp = advice.followUp || '';
	let AdviceStr = advice.AdviceStr || '';
	if(assay || check) {
		AdviceStr = assay + check  +'; ' + AdviceStr;
	}
	if(commontreatment)  { 
		AdviceStr = AdviceStr + commontreatment + ';';
	}	
	if(followUp) {
		AdviceStr = AdviceStr + '回访时间：'+followUp + '后回访，不适随诊' + ';';
	}
	for (let i = 0; i < scheme.length; i++) {
		for(let j = 0; j < scheme[i].treatment.length; j++){
			for(let k = 0; k < scheme[i].treatment[j].meditionDetails.length; k++) {
				for(let l = 0; l < scheme[i].treatment[j].meditionDetails[k].treatment.length; l++) {
					const treatmentItem = scheme[i].treatment[j].meditionDetails[k].treatment[l]
					if(treatmentItem.treatmentStr === '') {
						AdviceStr += treatmentItem.treatmentStr
					}else {
						AdviceStr += treatmentItem.treatmentStr + ', '
					}
				}
			}

		}







		// 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 + ', '
		// 	}
		// }


	}
	if(AdviceStr.slice(AdviceStr.length-2) == ', ') {
		AdviceStr = AdviceStr.slice(0, AdviceStr.length-2)
	}
	// if(commontreatment)  { 
	// 	AdviceStr = commontreatment +'; ' + AdviceStr;
	// }	
	if(adviceInput)  { 
		AdviceStr = AdviceStr +'; ' + adviceInput;
	}	
	let reg=/<[^>]+>/gim;
	AdviceStr = AdviceStr.replace(reg,"");
	return AdviceStr;
}

//治疗方案添加到医嘱
export const addScheme = (state, action) => {
  const res = JSON.parse(JSON.stringify(state))
  const treatment = action.treatment;
  const treatItem = {};
  treatItem.name = action.title;
  let scheme = res.advice.scheme || [];
  let isRepeat = false; 
  let RepeatIndex;
  let SelectedDrugNum = 0 	//选中的药品数量
  for (let i = 0; i < treatment.length; i++) {
		for(let j = 0; j < treatment[i].meditionDetails.length; j++) {
			for(let k = 0; k < treatment[i].meditionDetails[j].treatment.length; k++) {
				let treatmentStr = '';
				let drugList = []
				for(let l = 0; l < treatment[i].meditionDetails[j].treatment[k].medicitionsList.length; l++) {
					const drugItem = treatment[i].meditionDetails[j].treatment[k].medicitionsList[l]
					if(drugItem.selected) {
						treatmentStr = treatmentStr + drugItem.medicitionName + ', '
						drugList.push({conceptId: drugItem.conceptId, conceptName: drugItem.medicitionName })
						SelectedDrugNum++
					}
					
				}
				treatment[i].meditionDetails[j].treatment[k].treatmentStr = treatmentStr.substring(0,treatmentStr.length-2)
				treatment[i].meditionDetails[j].treatment[k].drugList = drugList
			}
		}
	
  }
  if(SelectedDrugNum > 0) { //如果有选中的药品，滚动到医嘱框
	document.getElementById("adviceBox").scrollIntoView(true)
  }
  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) {
	 //将同类药添加到一起
	const schemeTreatmentCopy = scheme[RepeatIndex].treatment.slice()
	 for (let i = 0; i < scheme[RepeatIndex].treatment.length; i++) {
		for(let x = 0; x < treatment.length; x++) {
			const hasNoCont = scheme[RepeatIndex].treatment.findIndex(item => item.title === treatment[x].title) 
			if(hasNoCont === -1) {
				if(treatment[x].title != scheme[RepeatIndex].treatment[i].title) {
					const treatmentCopy = JSON.parse(JSON.stringify(treatment[x]))
					for(let o = 0; o < treatmentCopy.meditionDetails.length; o++) {
						for(let p = 0; p < treatmentCopy.meditionDetails[o].treatment.length; p++) {
							treatmentCopy.meditionDetails[o].treatment[p].treatmentStr = ""
						}

					}
					schemeTreatmentCopy.splice(x, 0, treatmentCopy)
				}
			}
		}
			
	 }
	 scheme[RepeatIndex].treatment = schemeTreatmentCopy
	 for (let i = 0; i < scheme[RepeatIndex].treatment.length; i++) {
		const schemeTreatmentCopy2 = scheme[RepeatIndex].treatment[i].meditionDetails.slice()

			for(let x = 0; x < treatment.length; x++) {
				if(treatment[x].title === scheme[RepeatIndex].treatment[i].title) {
				for(let j = 0; j < scheme[RepeatIndex].treatment[i].meditionDetails.length; j++) {
					for(let y = 0; y < treatment[x].meditionDetails.length;y++) {
							const hasNoCont =  scheme[RepeatIndex].treatment[i].meditionDetails.findIndex(item => item.description === treatment[x].meditionDetails[y].description)  
							if(hasNoCont === -1) {
								schemeTreatmentCopy2.splice(y, 0, treatment[x].meditionDetails[y])
							}
						}
				}

			}
		}
		scheme[RepeatIndex].treatment[i].meditionDetails = schemeTreatmentCopy2

	 }
	for (let i = 0; i < scheme[RepeatIndex].treatment.length; i++) {
		for(let j = 0; j < scheme[RepeatIndex].treatment[i].meditionDetails.length; j++) {
			for(let k = 0; k < scheme[RepeatIndex].treatment[i].meditionDetails[j].treatment.length; k++) {
				const schemeItem = scheme[RepeatIndex].treatment[i].meditionDetails[j].treatment[k]

				for(let x = 0; x < treatment.length; x++) {
					for(let y = 0; y < treatment[x].meditionDetails.length;y++) {
						for(let z = 0; z < treatment[x].meditionDetails[y].treatment.length; z++) {
							let treatItem = treatment[x].meditionDetails[y].treatment[z]
							if(schemeItem.id === treatItem.id && schemeItem.bigdrugsName === treatItem.bigdrugsName && schemeItem.subdrugsName === treatItem.subdrugsName) {
								for(let w = 0; w < treatItem.medicitionsList.length; w++) {
									const medicitionsItem = treatItem.medicitionsList[w]
									if(medicitionsItem.selected) {
										if(schemeItem.treatmentStr !== '') {
											schemeItem.treatmentStr += ', ' +medicitionsItem.medicitionName
										} else {
											schemeItem.treatmentStr += '' +medicitionsItem.medicitionName
										}
										schemeItem.drugList.push({conceptId: medicitionsItem.conceptId, conceptName: medicitionsItem.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 {
	  	loop: 
		for (let i = 0; i < treatItem.treatment.length; i++) {
			for(let j = 0; j < treatItem.treatment[i].meditionDetails.length; j++) {
				for(let k = 0; k < treatItem.treatment[i].meditionDetails[j].treatment.length; k++) {
					for(let l = 0; l < treatItem.treatment[i].meditionDetails[j].treatment[k].medicitionsList.length; l++) {
						if(treatItem.treatment[i].meditionDetails[j].treatment[k].medicitionsList[l].selected) {
							res.advice.scheme = scheme.concat(treatItem)
							break loop;
						}
					}
				}
			}
		}
     
  }
	res.AdviceStr = getAdviceStr(res.advice)
	if (res.advice.scheme) {
		res.advice.drugList = getDrugList(res.advice.scheme)
	}
	
  return res;
}


//获取开单到医嘱的药品列表
function getDrugList(scheme) {
	const drugList = [];
	for(let i = 0 ;  i < scheme.length; i++) {
		for(let j = 0; j <scheme[i].treatment.length; j++ ) {
			for(let k = 0; k < scheme[i].treatment[j].meditionDetails.length; k++) {
				for(let l = 0; l < scheme[i].treatment[j].meditionDetails[k].treatment.length; l++) {
					for (let m = 0; m < scheme[i].treatment[j].meditionDetails[k].treatment[l].drugList.length; m++) {
						drugList.push(scheme[i].treatment[j].meditionDetails[k].treatment[l].drugList[m]);
					}
				}
			}
		}
	}
	return drugList
}

//设置提示信息
export const setTips = (state, action) => {
  const res = Object.assign({}, state)
  res.tips = action.tips;
  res.tmpFlg = action.tmpFlg
  res.showPartName = action.showPartName
  return res;
}

//设置提示信息详情页
export const setTipsDetails = (state, action) => {
  const res = Object.assign({}, state)
  res.tipsDetails = action.tipsDetails;
  res.showAllName = action.showAllName
  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;
	res.tips = {};
	res.vigilant = [];
	res.doubt = [];
	res.possible = [];
	res.determine = [];
	res.assay = [];
	res.check = [];
	res.chronicPushItems = [];//量表
	res.formulaResult = {};   //量表计算结果
	res.scaleInfo = {};			//量表选中项
	res.drugList = []; //开单药品列表
	return res;
}

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

export const hideTipsDetails = (state, action) => {
	const res = Object.assign({}, state);
	res.showTipsDetails = false;
	return res;
}

//一般治疗添加到医嘱
export const setCommontreatment = (state, action) => {
	const res = JSON.parse(JSON.stringify(state));
	res.advice.commontreatment = action.commontreatment;
	res.AdviceStr = getAdviceStr(res.advice)
	return res;
}

export const setAdviceInput = (state, action) => {
	const res = JSON.parse(JSON.stringify(state));
	res.advice.adviceInput = action.adviceInput;
	res.AdviceStr = getAdviceStr(res.advice)
	return res;
}

//保存回访时间
export const saveFollowUp = (state, action) => {
	const res = JSON.parse(JSON.stringify(state));
	res.advice.followUp = action.followUp;
	if(action.hasFollowUp) {
		res.advice.hasFollowUp = action.hasFollowUp 
	}
	return res;
}


//删除回访时间
export const delFollowUp = (state, action) => {
	const res = JSON.parse(JSON.stringify(state));
	res.advice.follow = '';
	return res;
}

