import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import axios from 'axios'
import VueAMap from 'vue-amap'


import './theme/index.css'
import './animate.min.css'
import './swiper.css';
import './main.css'




// 初始化vue-amap
VueAMap.initAMapApiLoader({
    // 高德的key
    key: 'dd8f1d46b89cb06d842c321d0d021672',
    // 插件集合
    plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor'],
    // 高德 sdk 版本，默认为 1.4.4
    v: '1.4.7'
});



Vue.use(ElementUI);
Vue.use(VueAMap);


Vue.config.productionTip = false
const baseUrl = process.env.NODE_ENV === 'production' ? '../' : `http://${location.hostname}:8080`;
Vue.prototype.$baseUrl = baseUrl;
// const baseUrl = 'http://www.xiangjuwu.com'
axios.defaults.withCredentials = true;
axios.defaults.baseURL = baseUrl;


Vue.prototype.$http = {
    get(params) {
        return new Promise((resolve, reject) => {
            if (params instanceof String) {
                params = { url: params };
            } else if (!params instanceof Object) {
                reject('params error');
                return;
            }
            if (!params.url) {
                reject('url error');
                return;
            } else if (!params.url instanceof String) {
                reject('url error');
                return;
            } else if (params.url.length === 0) {
                reject('url error');
                return;
            }
            // if (!/^(http:\/\/)|(https:\/\/)/.test(params.url)) {
            //     params.url = baseUrl + params.url;
            // }
            store.commit('updateFetchingData', true);
            axios.get(params.url, {
                params: params.data
            }, { withCredentials: true }).then(res => {
                store.commit('updateFetchingData', false);
                if (res.status === 200) {
                    resolve(res.data);
                } else {
                    reject(res);
                }
                try {
                    if (res.data.code === 10001) {
                        axios({
                            method: 'post',
                            url: '/userInfo/logout'
                        });
                        store.commit('updateUserInfo', null);
                        router.replace('/login');
                    }
                } catch (e) {

                }
            }).catch(e => {
                store.commit('updateFetchingData', false);
                reject(e);
            });
        });
    },
    post(params) {
        if (params instanceof String) {
            params = { url: params };
        } else if (!params instanceof Object) {
            reject('params error');
            return;
        }
        if (!params.url) {
            reject('url error');
            return;
        } else if (!params.url instanceof String) {
            reject('url error');
            return;
        } else if (params.url.length === 0) {
            reject('url error');
            return;
        }
        // if (!/^(http:\/\/)|(https:\/\/)/.test(params.url)) {
        //     params.url = baseUrl + params.url;
        // }
        let data = new FormData();
        if (params.data) {
            for (let key in params.data) {
                if (params.data.hasOwnProperty(key)) {
                    if (params.data[key] instanceof Array) {
                        data.append(key, params.data[key].join());
                    } else if (!(params.data[key] instanceof Object)) {
                        data.append(key, params.data[key]);
                    }
                }
            }
        }
        return new Promise((resolve, reject) => {
            store.commit('updateFetchingData', true);
            axios.post(params.url, data, { withCredentials: true }).then(res => {
                store.commit('updateFetchingData', false);
                if (res.status === 200) {
                    resolve(res.data);
                } else {
                    reject(res);
                }
                try {
                    if (res.data.code === 10001) {
                        axios({
                            method: 'post',
                            url: '/userInfo/logout'
                        });
                        store.commit('updateUserInfo', null);
                        router.replace('/login');
                    }
                } catch (e) {

                }
            }).catch(e => {
                store.commit('updateFetchingData', false);
                reject(e);
            });
        });
    }
};


// const updateSize = () => {
//     var fontSize = 100
//     var html = document.getElementsByTagName("html")[0];
//     if (window.innerWidth > 1920) {

//     } else if (window.innerWidth > 1400) {
//         var scale = (window.innerWidth / 1920).toFixed(2)
//         var css = document.createElement('style')
//         fontSize = scale * 100

//     } else {
//         var scale = (1400 / 1920).toFixed(2)
//         var css = document.createElement('style')
//         fontSize = scale * 100

//     }
//     html.style.fontSize = `${fontSize}px}`;

// };

// window.onload = updateSize;
// window.onresize = updateSize;
// // setInterval(updateSize, 500);

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    components: { App },
    template: '<App/>'
})