import fetch from 'isomorphic-fetch';
import actionType from '../actiontype/common'
import {addErrorInfo} from './uploadError'
const domain = ENV.domain;
//获取工单


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} = data;
        return(dispatch)=>{
            // if(userInfo && userInfo.id && storeInfo && storeInfo.id) {
                let url ='http://192.168.10.128:8080/web/flat/ready/13/A/28';
                // let url = `${ENV.domain}flat/ready/${storeInfo.id}/${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 getStoreInfo = (storeId)=>{
    return(dispatch)=>{
        let bodyData = {storeId};
        fetch(domain + '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 getInfoByBarcode = (data)=>{
    let {storeId,barcode} = data;
    let url = `flat/barcode/${storeId}/${barcode}`
    return (dispatch)=>{
        // dispatch(clearBarcodeInfo()); //清空barcode信息
        fetch(domain + url,{
            credentials : 'include',
            method : 'GET',
            mode : 'cors',
            headers: { 'Content-Type': 'application/json' }
        }).then(response=>response.json())
            .then(json=>{
                if(json.meta.success){
                    //扫码成功
                }else{
                    //扫码失败
                }
            })
    }
};

//获取用户信息
const getUserinfo = (employeeId,storeId)=>{
    let bodyData = Object.assign({},{employeeId,storeId})
    return(dispatch)=>{
        fetch(domain + '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)=>{
    // console.log(commonInfo,'没有散落物品')
    return (dispatch)=>{
        // let url=`${ENV.domain}/flat/work/status/1`;
        let url ='http://192.168.10.128:8080/web/flat/work/status/13/A/LR';
        fetch(url,{
            // credentials : 'include',
            method : 'PUT',
            mode : 'cors',
            headers: myHeaders
        }).then(response=>response.json())
            .then((json)=>{
                dispatch(getTaskList())
            })
    }
}


//拉取工单
const getTaskList = (data={})=>{
//    let url=`${ENV.domain}/flat/work/list/13`;
   let url ='http://192.168.10.128:8080/web/flat/work/list/13/A/336/28';
    return (dispatch)=>{
         fetch(url,{
            // credentials : 'include',
            method : 'GET',
            mode : 'cors',
            headers: myHeaders
         }).then(response=>response.json())
         .then((json)=>{
            let data =Object.assign({});
            if(json.meta.success && json.data){
                data.taskInfo = Object.assign({},json.data.flatCommonVo)
                data.taskList = Object.assign({},json.data.skuFlatVo);
                let childNum = 0;
                let currentTaskNum = json.data.flatCommonVo.currentTaskNum;
                if(currentTaskNum === 3){
                    //回收
                    childNum = json.data.skuFlatVo.whetherType ? 1 : 2
                }
                dispatch(getTaskName(currentTaskNum,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 = (currentTaskNum,childTaskNum=0)=>{
    return {
        type : actionType.GETTASKNAME,
        data : {currentTaskNum,childTaskNum}
    }
}

export {getStoreInfo,getTaskInfo,getInfoByBarcode,saveCommonInfo,getUserinfo,logout,startalljob,saveTaskListInfo,getTaskList,hasNoSku,getTaskName}
