import Vue from 'vue'
import Login from '../pages/Login'
import ElementUI from 'element-ui'
import axios from 'axios'
import VueI18n from 'vue-i18n'

import 'normalize.css/normalize.css'
import 'element-ui/lib/theme-chalk/index.css'
import '../main.less'

Vue.config.productionTip = false;
Vue.use(ElementUI);
Vue.use(VueI18n);
const baseUrl = process.env.NODE_ENV === 'production' ? '../' : `http://${location.hostname}:8080`;
Vue.prototype.$baseUrl = baseUrl;
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;
            // }
            axios.get(params.url, {
                params: params.data
            }, { withCredentials: true }).then(res => {
                if (res.status === 200) {
                    resolve(res.data);
                } else {
                    reject(res);
                }
            }).catch(e => {
                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) {
                        let hasObject = false;
                        params.data[key].forEach(i => {
                            if (i instanceof Object) {
                                hasObject = true;
                            }
                        });
                        if (!hasObject) {
                            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) => {
            axios.post(params.url, data, { withCredentials: true }).then(res => {
                if (res.status === 200) {
                    resolve(res.data);
                } else {
                    reject(res);
                }
            }).catch(e => {
                reject(e);
            });
        });
    }
};


new Vue({
    el: '#app',
    components: { Login },
    template: '<Login/>'
});