export const addDiagnostic = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    res.diagnosticList.push(action.item)
    return res;
}

export const delDiagnostic = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    res.diagnosticList = res.diagnosticList.filter(item => (item.id !== action.item.id && item.name !== action.item.name));
    return res;
}

export const upDiagnostic = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    const index = action.index;
    if ( index === 0) {
        return res
    }
    const temp = res.diagnosticList[index-1];
    res.diagnosticList[index-1] =res.diagnosticList[index];
    res.diagnosticList[index] = temp;
    return res;
}
export const downDiagnostic = (state, action) => {
    const res = JSON.parse(JSON.stringify(state));
    const index = action.index;
    if ( index === state.length-1) {
        return res
    }
    const temp = res.diagnosticList[index+1];
    res.diagnosticList[index+1] = res.diagnosticList[index];
    res.diagnosticList[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
    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
    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;
}