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 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++) {
			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;
	}	
	
	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 = '';
		let drugList = []
    for (let j = 0; j < treatment[i].medicitionsList.length; j++) {
      if(treatment[i].medicitionsList[j].selected) {
				treatmentStr = treatmentStr + treatment[i].medicitionsList[j].medicitionName + ', '
				drugList.push({questionId: treatment[i].medicitionsList[j].id, tagName: treatment[i].medicitionsList[j].medicitionName })
			}
	}
	treatment[i].treatmentStr = treatmentStr.substring(0,treatmentStr.length-2)
	treatment[i].drugList = drugList
  }
  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].bigdrugsName === treatment[j].bigdrugsName) {
				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
						}
						scheme[RepeatIndex].treatment[i].drugList.push({questionId: treatment[j].medicitionsList[z].id, tagName: 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)
	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 z = 0; z < scheme[i].treatment[j].drugList.length; z++) {
				drugList.push(scheme[i].treatment[j].drugList[z]);
			}
		}
	}
	return drugList
}

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

//设置提示信息详情页
export const setTipsDetails = (state, action) => {
  const res = Object.assign({}, state)
  res.tipsDetails = action.tipsDetails;
  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.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;
	return res;
}


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