import actionTypes from '../actiontype/common';


const saveCommonInfo = (newState,data)=>{
    let keys = Object.keys(data);
    let state = JSON.parse(JSON.stringify(newState))
    keys.forEach(k=>
    {
        if(state[k]){
            if(typeof data[k] === 'string'){
                state[k] = data[k]
            }else{
                state[k] = Object.assign({},state[k],data[k]);
            }

        }else{
            state[k] = data[k]
        }
    });
    return Object.assign({},state)
};

const emptyCommInfo = (state,data)=>{
    return Object.assign({},state,data)
}

let testData={
    currentTaskNum:1,
    childTaskNum:1
}
// 传入一个对象   对象中包含currentTaskNum表示大任务
// childTaskNum 表示大任务下的小任务
const NumChangeWord = (currentTaskType,childTaskNum)=>{
    let arr=[
        {
            title:'散落商品检查',
            children:['']
        },
        {
            title:'核对数量',
            children:['']
        },
        {
            title:'回收区货品回收',
            children:['回收区货品回收','货道货品回收']
        },
        {
            title:'补货',
            children:['']
        }
    ]
    let type = currentTaskType;
    let taskName = '';
    let child = childTaskNum;
    switch(type) {
        case 'LR' :
            {
                let name=1;
                taskName = arr[name - 1].title;
                if (child) {
                    taskName = arr[name - 1].children[child - 1]
                }
            }
            break;
        case 'CK' :
           {
                let name=2;
                taskName = arr[name - 1].title;
                if (child) {
                    taskName = arr[name - 1].children[child - 1]
                }
           }
            break;
        case 'RE' :
            {
                let name=3;
                taskName = arr[name - 1].title;
                if (child) {
                    taskName = arr[name - 1].children[child - 1]
                }
            }
            break;
        case 'ADDR' :
            {
                let name=4
                taskName = arr[name - 1].title;
                if (child) {
                    taskName = arr[name - 1].children[child - 1]
                }
            }
            break;
    }

    return taskName;
      
};

const getTaskName = (content,data)=>{
    console.log(data,'这是打印改变任务名字的reducer')
    let {currentTaskType,childTaskNum} = data;
    let taskName = NumChangeWord(currentTaskType,childTaskNum);
    let taskInfo = Object.assign({},{taskName});
    return saveCommonInfo(content,Object.assign({},{taskInfo}));
}

const startalljob = (state,data)=>{
    let info =saveCommonInfo(state,data);
     return info;
}

const saveTaskList = (state,data)=>{
    return Object.assign({},state,data)
}
export default function(state={}, action) {
    switch (action.type) {
        case actionTypes.SAVECOMMONINFO:
            return saveCommonInfo(state,action.data);
        case actionTypes.EMPTYCOMMONINFO:
            return emptyCommInfo(state);
        case actionTypes.STARTALLJOB   :
            return startalljob(state,action.data);
        case actionTypes.GETTASKLIST   :
            return saveTaskList(state,action.data);
        case actionTypes.GETTASKNAME :
            return getTaskName(state,action.data);
        default:
            return state;
    }
}