// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";
//import WechatAuth from '@/utils/auth';
//import './pollyjs'

import FastClick from "fastclick";
import VueLazyload from "vue-lazyload";

import Vant from "vant";
import "@/assets/css/vant_index.css";

import "@/assets/font/font_sail_client";

import getQueryString from "@/utils/getQueryString";
// Import Helpers for filters
import { currency, onlyCurrency, convertCurrency, normalDate, chinaDate, accurateTime } from "@/filter";

// Import Install and register helper items
Vue.filter("currency", currency);
Vue.filter("onlyCurrency", onlyCurrency);
Vue.filter("convertCurrency", convertCurrency);
Vue.filter("normalDate", normalDate);
Vue.filter("accurateTime", accurateTime);
Vue.filter("chinaDate", chinaDate);

import { Toast } from "vant";

Vue.use(Vant);
// By default we import all the components.
// Only reserve the components on demand and remove the rest.
// Style is always required.
import {
  Style,
  Button,
  //Toolbar,
  //CheckboxGroup,
  Radio,
  //Input,
  //Textarea,
  //Select,
  //Switch,
  //Rate,
  //Validator,
  //Form,
  Loading,
  Tip,
  //Popup,
  //Toast,
  //Picker,
  CascadePicker,
  DatePicker,
  SegmentPicker,
  TimePicker,
  //Dialog,
  //ActionSheet,
  //Drawer,
  //Scroll,
  //Slide,
  //IndexList,
  //Swipe,
  Upload
} from "cube-ui";

import {sync} from "vuex-router-sync";
//import {INIT_ADD_HOT} from './store/mutation_types.js'
import locStore from "@/utils/store";

import Api from "@/api/index.js";
import store from "./store";

import App from "./App";
import router from "./router";
import { List } from "vant";
Vue.use(List);
import { Cell, CellGroup } from "vant";
Vue.use(Cell).use(CellGroup);
//import { Popup } from 'vant'
import { Search } from "vant";

Vue.use(Search);
//Vue.use(Toolbar)
//Vue.use(CheckboxGroup)
Vue.use(Radio);
//Vue.use(Input)
//Vue.use(Textarea)
//Vue.use(Select)
//Vue.use(Switch)
//Vue.use(Rate)
//Vue.use(Validator)
//Vue.use(Form)
Vue.use(Loading);
Vue.use(Tip);
//Vue.use(Toast)
//Vue.use(Picker)
Vue.use(CascadePicker);
Vue.use(DatePicker);
Vue.use(SegmentPicker);
Vue.use(TimePicker);
//Vue.use(Dialog)
//Vue.use(ActionSheet)
//Vue.use(Drawer)
//Vue.use(Scroll)
//Vue.use(Slide)
//Vue.use(IndexList)
//Vue.use(Swipe)
Vue.use(Upload);

Vue.config.productionTip = false;


//匹配要渲染的视图后，再获取数据：
Vue.mixin({
  beforeMount () {
    const { asyncData, initData } = this.$options;
    setTimeout(()=> {
      window.scrollTo(0, 0);
    }, 10);
    if (asyncData) {
      // 将获取数据操作分配给 promise
      // 以便在组件中，我们可以在数据准备就绪后
      // 通过运行 `this.dataPromise.then(...)` 来执行其他任务
      //store.commit('UPDATE_LOADING_STATUS', true)
      const toast = Toast.loading({
        duration: 0,       // continuous display toast
        forbidClick: true, // forbid click background
        loadingType: "spinner",
        message: "正在加载..."
      });
      this.dataPromise = asyncData({
        store: this.$store,
        route: this.$route
      }).then(() => {
        Toast.clear();
        //store.commit('UPDATE_LOADING_STATUS', false)
      });
    }
    if(initData) {
      initData({
        store: this.$store,
        route: this.$route
      });
    }
  }
});

// Some middleware to help us ensure the user is authenticated.


router.beforeEach((to, from, next) => {
  let cUrl = location.href;
  if(cUrl.indexOf("login") === -1 && cUrl.indexOf("code") === -1 && cUrl.indexOf("state") === -1) {
    locStore("lastPage", location.href);
  }
  //
  let code = getQueryString("code");
  //
  let state = getQueryString("state");
  if(code && !state) {
    //
    store.dispatch("loginModule/wxLoginInfo", code).then(res => {
      //
      //
      if(res.data.code !== 10000) {
        // next({
        //   path: '/login'
        // })
        location.href = "/wx/index.html#/login";
        return;
      }
      if(to.matched.some(record => record.meta.requiresLogin) && 
        (!router.app.$store.state.loginModule.loginInfo.phone
        || router.app.$store.state.loginModule.loginInfo.phone === "")){
        next({
          path: "/login"
        });
      }
    });
  }
  if(code && state) {
    store.dispatch("loginModule/wxPoneLoginCallBack", {
      code,
      state
    }).then(res => {
      //
      if(to.matched.some(record => record.meta.requiresLogin) && 
         (!router.app.$store.state.loginModule.loginInfo.phone
         || router.app.$store.state.loginModule.loginInfo.phone === "")){
        // let hasLogout = locStore('hasLogout')
        // if(hasLogout) {
        // }
        next({
          path: "/login"
        });
        return;
        //
        // store.dispatch('loginModule/wxLogin').then(res => {
        //   if(res) {
        //      location.href = res.data.data
        //   }else {
        //      next({
        //        path: '/login'
        //      })
        //   }
        // })
      }
      //
    });
  }
  //
  if(!code && !state && to.matched.some(record => record.meta.requiresLogin) && 
    (!router.app.$store.state.loginModule.loginInfo.phone
    || router.app.$store.state.loginModule.loginInfo.phone === "")){
    //
    // let hasLogout = locStore('hasLogout')
    // if(hasLogout === true) {
    //    next({
    //      path: '/login'
    //    })
    //    return;
    // }
    // next({
    //   path: '/login'
    // })
    store.dispatch("loginModule/wxLogin").then(res => {
      if(res) {
        location.href = res.data.data;
      }else {
      }
    });
  }

  if (to.matched.some(record => record.meta.requiresAuth) && 
    (!router.app.$store.state.loginModule.loginInfo.cardCode
    || router.app.$store.state.loginModule.loginInfo.cardCode === "")) {
    // this route requires auth, check if logged in
    // if not, redirect to login page.
    next({
      path: "/RealNameCheck/auth"
    });
  } else {
    next();
  }
});

sync(store, router);

/* eslint-disable no-new */
let vm = new Vue({
  el: "#app",
  store,
  router,
  template: "<App/>",
  components: { App }
});

Api.interceptorsMethod(store, vm);
