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

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=>{})


    }
};
const listStores = ()=>{
    return (dispatch)=>{
        fetch(domain + 'machine_pad/list_store',{
            credentials : 'include',
            method : 'POST',
            mode : 'cors',
            headers: { 'Content-Type': 'application/json' },
            body:JSON.stringify({})
        }).then((response)=>response.json())
            .then(json=>{
                UTILPATH.myLogger.info("pad list stores ",json);
                if(json.meta.success){
                    let storeList = json.data;
                    dispatch(saveStoreList(storeList))
                    emit(CONFIG.customEvent.STORELISTREADY,{});
                }else{
                    //没有店铺信息
                }
            }).catch(e=>{
            UTILPATH.myLogger.error(`pad list stores  ${e}`);
            dispatch(listStores());
        });
    }
}
const getStoreList = ()=>{
     return (dispatch)=>{
         fetch(domain + 'machine_pad/power_cut_find_store',{
             credentials : 'include',
             method : 'POST',
             mode : 'cors',
             headers: { 'Content-Type': 'application/json' },
             body:JSON.stringify({})
         }).then((response)=>response.json())
             .then(json=>{
                 UTILPATH.myLogger.info("power_cut_find_store ",json);
                 if(json.meta.success){
                     let storeList = json.data;
                     dispatch(saveStoreList(storeList))
                 }else{
                     //没有店铺信息
                 }
             }).catch(e=>{
                UTILPATH.myLogger.error(`power_cut_find_store ${e}`);
                dispatch(getStoreList());
         });
     }
};

const saveStoreList = (data)=>{
    return{
        type:actionTypes.SAVESTORELIST,
        data
    }
};

export {getStoreInfo,getStoreList,listStores}

