import fetch from 'isomorphic-fetch';
import actionType from '../actiontype/common'
import {addErrorInfo} from './uploadError'
let domain1 = ENV.domain;
let domain = 'http://192.168.10.128:8080/web/'

let myHeaders = new Headers({
    // 'Access-Control-Allow-Origin': '*',
    // 'Content-Type': 'text/plain;charset=UTF-8',
    // 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Content-Type': 'application/json; charset=UTF-8',
});


//开始工作
// const getTaskInfo = (data)=>{
//     let {userInfo,storeInfo,taskInfo} = data;
//         return(dispatch)=>{
//             if(userInfo && userInfo.id && storeInfo && storeInfo.id) {
//                 let url = `${domain}flat/ready/${storeInfo.id}/${taskInfo.area}/${userInfo.id}`;
//                 fetch(url,{
//                     // credentials : 'include',
//                     method : 'GET',
//                     mode : 'cors',
//                 }).then((response)=>response.json())
//                     .then(json=>{
//                         let taskInfo=Object.assign({});
//                         if(json.meta.success && json.data){
//                             taskInfo = Object.assign({},json.data);
//                             dispatch(getTaskName(json.data.currentTaskNum))
//                         }
//                         dispatch(saveCommonInfo(Object.assign({},{taskInfo})))
//                     }).catch(e=>{console.error(e)})
//             }else {
//                 dispatch(addErrorInfo("用户或门店不对"))
//             }
//         }
// };
const getTaskInfo = (data)=>{
    let {userInfo,storeInfo,taskInfo} = data;
        return(dispatch)=>{
            if(userInfo && userInfo.id && storeInfo && storeInfo.id) {
                let url = `${domain}flat/ready/${storeInfo.id}/${taskInfo.area}/${userInfo.id}`;
                fetch(url,{
                    // credentials : 'include',
                    method : 'GET',
                    mode : 'cors',
                    headers: myHeaders,
                }).then((response)=>response.json())
                    .then(json=>{
                        let taskInfo=Object.assign({});
                        if(json.meta.success && json.data){
                            taskInfo = Object.assign({},json.data);
                            dispatch(getTaskName(json.data.currentTaskNum))
                        }
                        dispatch(saveCommonInfo(Object.assign({},{taskInfo})))
                    }).catch(e=>{console.error(e)})
            }else {
                dispatch(addErrorInfo("用户或门店不对"))
            }
        }
};

//获取店铺信息
const getStoreInfo = (storeId)=>{
    return(dispatch)=>{
        let bodyData = {storeId};
        fetch(domain1 + 'pad_warehouse/getMachineData.action',{
            // credentials : 'include',
            method : 'POST',
            mode : 'cors',
            headers: myHeaders,
            body: JSON.stringify(bodyData)
        }).then((response)=>response.json())
            .then(json=>{
                let storeInfo = Object.assign({},{id:storeId});
                if(json.meta.success && json.data.warehouse){
                    storeInfo = Object.assign({},storeInfo,json.data.warehouse)
                }
                dispatch(saveCommonInfo(Object.assign({},{storeInfo})))
            }).catch(e=>{console.error(e)})


    }
};


//获取用户信息
const getUserinfo = (employeeId,storeId)=>{
    let bodyData = Object.assign({},{employeeId,storeId})
    return(dispatch)=>{
        fetch(domain1 + 'machine_pad/get_alowed_task.action',{
            // credentials : 'include',
            method : 'POST',
            mode : 'cors',
            headers:myHeaders,
            body : JSON.stringify(bodyData)
        }).then((response)=>response.json())
            .then(json=>{
                let userInfo = Object.assign({},{id:employeeId})
                if(json.meta.success && json.data){
                    if(json.data.employee){
                        userInfo = Object.assign({},userInfo,json.data.employee)
                    }
                    if(json.data.type){
                        userInfo = Object.assign({},userInfo,{taskType:json.data.type});
                    }else{
                        dispatch(addErrorInfo('没有任务需要处理'))
                    }
                    dispatch(saveCommonInfo(Object.assign({},{userInfo})));
                }
            }).catch(e=>{console.error(e)})
    }
};

//test 没有散落货品
const hasNoSku = (commonInfo)=>{
    let {storeInfo={},userInfo={},taskInfo={}} = commonInfo;
    console.log("执行了没有散落商品的接口")
    return (dispatch)=>{
        let url = `${domain}flat/work/lr/status/${taskInfo.taskId}/${taskInfo.area}/${taskInfo.currentTaskType}`;
        fetch(url,{
            // credentials : 'include',
            method : 'PUT',
            mode : 'cors',
            headers: myHeaders
        }).then(response=>response.json())
            .then((json)=>{
                dispatch(getTaskList(commonInfo))
            })
    }
}


//拉取工单
const getTaskList = (data={})=>{
        let {storeInfo={},userInfo={},taskInfo={}} = data;
        console.log("getTaskList ",data)
        let taskId=taskInfo.taskId || 0;
        let url =`${domain}flat/work/list/${storeInfo.id}/${taskInfo.area}/${taskId}/${userInfo.id}`;
        return (dispatch)=>{
            fetch(url,{
                // credentials : 'include',
                method : 'GET',
                mode : 'cors',
                headers: myHeaders
            }).then(response=>response.json())
                .then((json)=>{
                    console.log(json,'这是吴加宝打印的工单')
                    let data =Object.assign({});
                    console.log("拉取工单 ",json)
                    if(json.meta.success && json.data){
                        data.taskInfo = Object.assign({},json.data.flatCommonVo)
                        data.taskList = Object.assign({},json.data.skuFlatVo);
                        let childNum = 0;
                        let currentTaskType = json.data.flatCommonVo.currentTaskType;
                        let currentTaskNum = json.data.flatCommonVo.currentTaskNum;
                        if(currentTaskType === 'RE'){
                            //回收
                            childNum = json.data.skuFlatVo.whetherType ? 1 : 2
                        }
                        dispatch(getTaskName(currentTaskType,childNum))
                    }
                    dispatch(saveCommonInfo(Object.assign({},data)))
                })
        }
}

const saveTaskListInfo = (data)=>{
    return{
        type : actionType.GETTASKLIST,
        data
    }
}

const setBarcodeInfo = (data)=>{
    return{
        type : actionType.SETBARCODEINFO,
        data
    }
};

const clearBarcodeInfo = (data)=>{
    return{
        type : actionType.EMPTYBARCODEINFO,
        data
    }
};

const saveCommonInfo = (data)=>{
    return {
        type : actionType.SAVECOMMONINFO,
        data
    }
};
const startalljob=(data)=>{
    return {
        type: actionType.STARTALLJOB,
        data
    }
}
const logout = ()=>{
    let data = Object.assign({},{userInfo:{},taskInfo:{}});
    return {
        type : actionType.EMPTYCOMMONINFO,
        data
    }
};

const getTaskName = (currentTaskType,childTaskNum=0)=>{
    return {
        type : actionType.GETTASKNAME,
        data : {currentTaskType,childTaskNum}
    }
}

export {getStoreInfo,saveCommonInfo,getUserinfo,logout,getTaskList,hasNoSku,getTaskName}
