import fetch from 'isomorphic-fetch';
import actionType from '../actiontype/common'
import {addErrorInfo} from './uploadError'
let 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 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 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)})
    }
};



//拉取工单
const getTaskList = (data={})=>{
        let {storeInfo={},userInfo={},taskInfo={}} = data;
        let taskId=taskInfo.taskId || 0;
        let url =`${domain}machine_pad/work/list/${storeInfo.id}/${taskInfo.area}/${taskId}/${userInfo.id}`;
        return (dispatch)=>{
            if(!userInfo.id){
                dispatch(addErrorInfo('没有获取用户信息'))
            }else if(!taskInfo.area){
                dispatch(addErrorInfo('没有获取区域信息'))
            }else {
                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);
                            // if(json.data.flatCommonVo.currentTaskType !== 'ADDR'){
                            //     data.taskList = Object.assign({},json.data.skuFlatVo);
                            // }
                            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'){
                                //回收
                                if(taskInfo.currentTaskType !== "RE"){
                                    childNum = json.data.skuFlatVo.whetherType ? 1 : 2
                                }else{
                                    childNum = 2
                                }
                            }
                            dispatch(getTaskName(currentTaskType,childNum))
                        }
                        dispatch(saveCommonInfo(Object.assign({},data)))
                    })
            }
        }
}

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

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

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