import Vue from 'vue'
import Main from './pages/Main'
import router from './router'
import store from './vuex'
import ElementUI from 'element-ui'
import axios from 'axios'
import MultiUpload from './components/MultiUpload'
import SingleUpload from './components/SingleUpload'
import VueI18n from 'vue-i18n'
import VueAMap from 'vue-amap'

import 'normalize.css/normalize.css'
import 'element-ui/lib/theme-chalk/index.css'
import './main.less'
import { format } from 'date-fns'
import zh from 'date-fns/locale/zh_cn'

VueAMap.initAMapApiLoader({
    key: 'bf91055058a47a7dc387e40ab6256a5f',
    plugin: ['Autocomplete', 'PlaceSearch', 'Scale', 'OverView', 'ToolBar', 'MapType', 'PolyEditor', 'AMap.CircleEditor', 'AMap.Geolocation'],
    v: '1.4.7'
});

Vue.config.productionTip = false;
Vue.use(ElementUI);
Vue.use(VueI18n);
Vue.component('multi-upload', MultiUpload);
Vue.component('single-upload', SingleUpload);
const baseUrl = process.env.NODE_ENV === 'production' ? '../' : 'http://localhost: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;
            // }
            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: '/auth/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) {
                        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) => {
            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: '/auth/logout'
                        });
                        store.commit('updateUserInfo', null);
                        router.replace('/login');
                    }
                } catch (e) {

                }
            }).catch(e => {
                store.commit('updateFetchingData', false);
                reject(e);
            });
        });
    }
};
Vue.mixin({
    methods: {
        timeFormatter(row, column, cellValue, index) {
            return format(new Date(cellValue), 'HH:mm', { locale: zh })
        },
        datetimeFormatter(row, column, cellValue, index) {
            if (!cellValue) return '';
            return format(new Date(cellValue), 'YYYY/MM/DD HH:mm', { locale: zh })
        }
    }
});

const updateTableHeight = () => {
    try {
        const total = document.getElementsByTagName('main')[0].clientHeight;
        const filter = document.getElementsByClassName('filters-container')[0].clientHeight;
        const page = document.getElementsByClassName('pagination-wrapper')[0].clientHeight;
        store.commit('updateTableHeight', total - filter - page - 44);
    } catch (e) {
        // console.error(e)
    }
};

window.onload = updateTableHeight;
window.onresize = updateTableHeight;
setInterval(updateTableHeight, 500);

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