//显示治疗方案
export const showTreat = (state, action) => {
    const res = Object.assign({}, state);
    res.show = true;
    return res
}

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

export const clearTreat = (state, action) => {
    const res = Object.assign({}, state);
    res.generalTreat = '';
    res.surgeryTreat = '';
    res.drugHistory = {};
    res.treatment = [];
    res.adversReactionList=[];
    res.hasFollowUp = false;
    res.followUp = '';
    return res
}

export const setTreatment = (state, action) => {
    const res = Object.assign({}, state);
    res.treatment = action.treatment;
    res.generalTreat = action.generalTreat;
    res.surgeryTreat = action.surgeryTreat;
    res.drugHistory= action.drugHistory
    return res
}

//选择取消药品
export const selectDrug = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    const drugTreat = res.treatment.slice();
    const index = action.index  //药品类的索引
    const ii = action.ii   //药品索引
    const selectItem = drugTreat[index].medicitionsList[ii]
    const selected =  selectItem.selected//所选择药品的选择状态
    selectItem.selected = !selected;
    return res
}

//其他药品推荐
export const setOtherDrug = (state, action) => {
    // const res = Object.assign({}, state)
    const res = JSON.parse(JSON.stringify(state));
    res.treatDesc = {};
    res.treatDesc.title = '药品推荐';
    res.treatDesc.otherDrugIndex = action.index;
    return res
}

//药品说明
export const setDrugInfo = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    const instroduce = action.instroduce;
    res.drugInfo = {};
    res.drugInfo.title = action.name;
    res.drugInfo.drugDesc = instroduce;
    return res
}

export const clearDrugInfo = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    // res.drugInfo.drugDesc = [];
    if (res.drugInfo && res.drugInfo.drugDesc) { res.drugInfo.drugDesc = [];}
    return res
}

export const setRecommendBasic = (state, action) => {
    // const res = Object.assign({}, state)
    const res = JSON.parse(JSON.stringify(state));
    const treatDesc = res.treatDesc;
    treatDesc.basic = action.recommendBasic
    return res
}

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

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

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

//设置多个药品说明书（添加数据时查看数据用）
export const setDrugInfoList = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    const drugInfoList = action.drugInfoList;
    res.drugInfoList = drugInfoList
    return res
}

//是否第一次主诊断
export const isFirstMainDiag = (state, action) => {
    const res = Object.assign({}, state);
    res.isFirstMainDiag = false;
    return res;
}
//回读清除是否第一次主诊断
export const clearFirstMainDiag = (state, action) => {
    const res = Object.assign({}, state);
    res.isFirstMainDiag = action.flag;
    return res;
}

//设置不良反应
export const setAdverseReactions = (state, action) => {
    const res = Object.assign({}, state);
    res.adversReactionList = action.adversReactionList;
    return res;
}
//保存所有不良反应（第二次打开质量方案不良反应有所变化时进行比较）
export const setALLAdverseReactions =(state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    let allAdversReactionList = res.allAdversReactionList
    let hasAdversReactionList = false
    for( let i = 0; i < allAdversReactionList.length; i++) {
        if (action.adversReactionList.id == allAdversReactionList[i].id) {
            allAdversReactionList[i] = action.adversReactionList
            hasAdversReactionList = true
        } 
    }
    if(!hasAdversReactionList) {
        allAdversReactionList.push(action.adversReactionList)
    }
    res.allAdversReactionList = allAdversReactionList;
    return res;
}

//改变不良反应选中状态
export const changeReact=(state,action)=>{
    const res = JSON.parse(JSON.stringify(state));
    const index = action.index
    res.adversReactionList[index].details = res.adversReactionList[index].details.map(item => {
      if(item.name == action.it.name){
        item.value = item.value == 0 ? 1 : 0
      }
      return item
    })
    return res;
  };

// 删除该疾病不良反应
export const delReact = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    let allAdversReactionList = res.allAdversReactionList;
    for( let i = 0; i < allAdversReactionList.length; i++) {
        if (action.item.id == allAdversReactionList[i].id) {
            allAdversReactionList.splice(i, 1)
        } 
    }
    res.allAdversReactionList = allAdversReactionList;
    return res;
}

// 设置回访时间
export const setFollwUp = (state, action) => {
    const res = Object.assign({}, state);
    res.followUp = action.followUp;
    if(action.hasFollowUp && (action.hasFollowUp.toString() == 'true' || action.hasFollowUp.toString() == 'false')  ) {
        res.hasFollowUp = action.hasFollowUp;
    }
    return res;
}

export const setAllFollowUp = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    let followUpList = res.followUpList
    let hasfollowUp = false
    for( let i = 0; i < followUpList.length; i++) {
        if (action.followUp.id == followUpList[i].id) {
            followUpList[i] = action.followUp
            hasfollowUp = true
        } 
    }
    if(!hasfollowUp) {
        followUpList.push(action.followUp)
    }
    res.followUpList = followUpList;
    return res;
}

export const delFollowUp = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    let followUpList = res.followUpList;
    for( let i = 0; i < followUpList.length; i++) {
        if (action.delItem.id == followUpList[i].id) {
            followUpList.splice(i, 1)
        } 
    }
    res.followUpList = followUpList;
    return res;
}
