import fetch from 'isomorphic-fetch';
import actionType from '../actiontype/common'
import {saveHeaderInfo} from "./initHeader";
import actionTypes from "../actiontype/user";
const domain = ENV.domain;
//获取工单
const getTaskInfo = (data)=>{
    // let {employeeId,storeId} = data;
    console.log(data)
    console.log('获取工单信息')
    let test='http://192.168.10.128:8080/'
    // // let url = `${test}web/flat/ready${storeId}/${employeeId}`
    let url=`${test}web/flat/ready/1/2`;
    return(dispatch)=>{
        fetch(url,{
            credentials : 'include',
            method : 'GET',
            mode : 'cors',
            headers: { 'Content-Type': 'application/json' },
        }).then((response)=>response.json())
            .then(json=>{
                let taskInfo=Object.assign({})
                if(json.meta.success && json.data){
                    taskInfo = Object.assign({},json.data)
                }
                console.log(taskInfo)
                dispatch(startalljob(Object.assign({},taskInfo)))
            }).catch(e=>{console.error(e)})

    }
};

//获取店铺信息
const getStoreInfo = (storeId)=>{
    return(dispatch)=>{
        let bodyData = {storeId};
        fetch(domain + 'pad_warehouse/getMachineData.action',{
            credentials : 'include',
            method : 'POST',
            mode : 'cors',
            headers: { 'Content-Type': 'application/json' },
            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=>{
                console.log(json);
                if(json.meta.success){
                    //扫码成功
                }else{
                    //扫码失败
                }
            })
    }
};

//获取用户信息
const getUserinfo = (employeeId,storeId)=>{
    let bodyData = Object.assign({},{employeeId,storeId})
    return(dispatch)=>{
        dispatch(saveHeaderInfo(info));
        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 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 : actionTypes.EMPTYCOMMONINFO,
        data
    }
}

export {getStoreInfo,getTaskInfo,getInfoByBarcode,saveCommonInfo,getUserinfo,logout,startalljob}
