export const editDiagMsg = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    let tmpLis = res.diagnosticList
    for(let i = 0;i < tmpLis.length;i++){
        if(action.idx == i){
            tmpLis[i].name = action.name
            if(!!tmpLis[i].hasTreat){
                let obj = {
                    "dateValue": '',
                    "name": action.name,
                    "uniqueName": tmpLis[i].uniqueName
                }
                res.diseaseName = obj
            }
        }
        res.diagnosticStr = tmpLis[i].name + ','
    }
    res.diagnosticList = [...tmpLis]
    return res;
}

export const addDiagMsg = (state, action) => {  //选中诊断依据
    const res = JSON.parse(JSON.stringify(state));
    const {isTcm} = action;
    console.log('addDiagMsg',action)
    let tmpLis = isTcm?res.tcmList:res.diagnosticList;
    for(let i = 0;i < tmpLis.length;i++){
        if(action.idx == i){
            tmpLis[i].hasTreat = 1
        }else{
            tmpLis[i].hasTreat = 0
        }
    }
    tmpLis = [...tmpLis]
    if(isTcm){
        res.otherIndex=tmpLis[action.idx];
    }else{
        res.diseaseName = tmpLis[action.idx]
    }
    return res;
}

export const addTcmDiagnostic = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    //console.log('addTcmDiagnostic',action)
    const { isSym } = action;
    let obj=null;
    //如果前面有一个诊断和症候不全，则不添加新行（不一定是上一个，但肯定只有一个不全）
    const len = res.tcmList.length;
    if(len===0){    //len==0,只可能是添加中医诊断
        obj = {
            "tcmDiag":action.item.name, //中医诊断
            "tcmSyndrome":"",      //中医症候
            "hasTreat":1,       //默认第一个为治疗方案，作为推送入参
        };
        res.tcmList.push(obj);
    }else{
        if(isSym){      //上一个症候没填，不用新增
            const noAllInx = res.tcmList.findIndex((it)=>!it.tcmSyndrome);
            res.tcmList[noAllInx].tcmSyndrome=action.item.name;
            if(len===1){
                res.otherIndex = {
                    "tcmDiag":res.tcmList[0].tcmDiag, //中医诊断
                    "tcmSyndrome":action.item.name,      //中医症候
                }
            }
        }else{
            obj = {
                "tcmDiag":action.item.name, //中医诊断
                "tcmSyndrome":"",      //中医症候
                "hasTreat":0,
            };
            res.tcmList.push(obj);
        }
    }
    return res;
}

export const addDiagnostic = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    let list = res.diagnosticList;
    list.push(action.item)
    let tmpLis = list,num=0
    for(let i = 0;i < tmpLis.length;i++){
        tmpLis[i].uniqueName = tmpLis[i].name
        if(!tmpLis[i].hasTreat){
            ++num
        }
    }
    if(num == tmpLis.length){
        tmpLis[0]['hasTreat'] = 1
        let obj  = {
                "dateValue": '',
                "name": tmpLis[0].name,
                "uniqueName": tmpLis[0].uniqueName
            }
        res.diseaseName = obj
    }
    list = [...tmpLis];
    return res;
}

export const delDiagnostic = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    const {isTcm} = action;
    let k = isTcm?'tcmList':'diagnosticList';
    let list = res[k].filter((item,idx) =>{
        if(idx != action.idx){
            return item
        }
    });
    let tmpLis = list,num=0
    for(let i = 0;i < tmpLis.length;i++){
        if(!tmpLis[i].hasTreat){
            ++num
        }
    }
    if(tmpLis.length>0&&num == tmpLis.length){
        tmpLis[0]['hasTreat'] = 1
        let obj = {
            "dateValue": '',
            "name": tmpLis[0].name,
            "uniqueName": tmpLis[0].uniqueName
        }
        if(isTcm){
            res.otherIndex=tmpLis[0];
        }else{
            res.diseaseName = obj
        }
    }else{
        if(isTcm){
            res.otherIndex=tmpLis[0];
        }else{
            res.diseaseName = {}
        }
    }
    res[k] = [...tmpLis]
    return res;
}

export const upDiagnostic = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    const {index,isTcm} = action;
    const list = isTcm?res.tcmList:res.diagnosticList;
    if ( index === 0) {
        return res
    }
    const temp = list[index-1];
    list[index-1] =list[index];
    list[index] = temp;
    return res;
}
export const downDiagnostic = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    const {index,isTcm} = action;
    const list = isTcm?res.tcmList:res.diagnosticList;
    if ( index === state.length-1) {
        return res
    }
    const temp = list[index+1];
    list[index+1] = list[index];
    list[index] = temp;
    return res;
}

export const setTreat = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    const resLength = res.diagnosticList.length;
    res.diagnosticList[resLength-1].treat = action.treat;
    return res;
}

function getDiagType(type) {
    if (type === 1) {
        return '初诊'
    } else {
        return '复诊'
    }
}

export const getDiagnosticStr = (state, action) => {
    const res = Object.assign({}, state)
    const diagnosticList = res.diagnosticList
    let diagnosticStr= '';
    let diagnosticStrNoType = ''
    for (let i = 0; i < diagnosticList.length; i++) {
        diagnosticStr = diagnosticStr + diagnosticList[i].name + '(' + getDiagType(diagnosticList[i].type) + ')；'
        diagnosticStrNoType = diagnosticStrNoType + diagnosticList[i].name + ','
    }
    res.diagnosticStrNoType = diagnosticStrNoType
    // res.diagnosticStr = diagnosticStr
    // 2020/09/07 lcq 本次版本没有初诊复诊字样
    res.diagnosticStr = diagnosticStrNoType
    return res;
}

export const setDiagToMainSuit = (state, action) => {
    const res = Object.assign({}, state);
    res.mainSuitStr = action.data;
    return res;
}

export const clearAllDiag = (state, action) => {
    const res = Object.assign({}, state);
    res.diagnosticList = action.data
    res.diagnosticStr = action.saveText
    res.mainSuitStr = action.mainSuitStr
    res.diagnosticStrNoType = action.saveText
    res.chronicMagItem = action.chronicMagItem
    res.diseaseName = {}
    return res;
}

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

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

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

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

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

export const addTreat = (state, action) => {
    const res = JSON.parse(JSON.stringify(state))
    const diagnosticList = res.diagnosticList
    for(let i = 0; i< diagnosticList.length; i++) {
        if(diagnosticList[i].conceptId == action.item.conceptId && diagnosticList[i].name == action.item.name) {
            diagnosticList[i] = action.item
        }
    }
    return res;
}

// 引用回显数据 
export function reDiagData(state, action) {
    let res = Object.assign({}, state);
    res.diagnosticStr = action.params[10].content;
    res.diagnosticList = action.params[10].contentValue;
    res.diagnosticStrNoType = action.params[10].contentJson;
    res.diseaseName = action.params[10].diseaseName;
    //中医数据
    if(action.params[12]){
        res.tcmList =action.params[12].contentValue;
        res.otherIndex = action.params[12].diseaseName;
    }else{
        res.tcmList =[];
        res.otherIndex ={};
    }
    return res;
}