import fetch from 'isomorphic-fetch';
import actionTypes from '../actiontype/user'
import {saveHeaderInfo} from './initHeader'
import {addErrorInfo} from './uploadError'
const domain = ENV.domain;

let json = {
    "meta": {
        "code": "200",
        "message": "成功",
        "success": true
    },
    "data": {
        "type":'ER',
        "employee":{
            "id": 5,
            "employeeName": "机器库管员",
            "headImageUrl": "http://www.mjitech.com/static/coupon/gounian/banner_hongbaotupian-3-14.jpg",
        },
    }
}

const saveUserinfo = (data)=>{
    return{
        type:actionTypes.SAVEUSERINFO,
        data
    }
};

var 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 getUserinfo = (info,headerInfo)=>{
    let employeeId = info.employeeId;
    let storeId = headerInfo.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=>{
                if(json.meta.success && json.data){
                    dispatch(saveUserinfo(json.data));
                    if(!json.data.type){
                        dispatch(addErrorInfo('没有任务需要处理'))
                    }
                }
            }).catch(e=>{console.error(e)})
    }
}

const logoutUser = ()=>{
    return {
        type : actionTypes.LOGOUTUSER,
    }
}

export {getUserinfo,logoutUser}

