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

let json = {
    "meta": {
        "code": "200",
        "message": "成功",
        "success": true
    },
    "data": {
        "warehouse": {
            "id": 13,
            "name": "华贸商业街",
            "image": "http://static.mjitech.com/static/tmp/2018-05-25/1527243213701.jpg",
            "address": "华贸商业街（新光天地与利兹卡尔顿酒店之间广场）"
        }
    }
};

const saveStoreInfo = (data)=>{
    return{
        type:actionTypes.SAVESTORE,
        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 getStoreInfo = (data)=>{
    let storeId = data.storeId
    return(dispatch)=>{
        dispatch(saveHeaderInfo(data))
        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=>{
                if(json.meta.success && json.data.warehouse){
                    dispatch(saveStoreInfo(Object.assign({},json.data.warehouse,{id:storeId})))
                }else{
                    //没有店铺信息
                }
            }).catch(e=>{console.error(e)})


    }
}

export {getStoreInfo}

