<template>
  <div id="login" class="container">
    <theme-picker v-show="false" />
    <div class="login-weaper animate__animated animate__bounceInDown">
      <div class="login-left" :style="`background:${defaultTheme}`">
        <div class="login-time">
          {{ time }}
        </div>
        <svg-icon class="img" icon-class="logo" />
        <p class="title">{{ title }}</p>
      </div>
      <div class="login-border">
        <div class="login-main">
          <h4 class="login-title">
            {{ $t('ec.login.logIn') }}授权中...
          </h4>
        </div>
      </div>
    </div>
    <el-dialog
      title="提示"
      :append-to-body="true"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <span>{{ errorMsg }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import { title } from '@/settings'
import { setToken } from '@/utils/auth'
import router from '@/router'
import { getToken } from '@/utils/auth'
import { getParamFromUrl, urlFormat } from 'ecip-web/utils/center-auth-utils'

export default {
  name: 'EcipAuthLoginCallback1',
  components: {},
  props: [],
  data() {
    return {
      time: '',
      title: title,
      errorMsg: '',
      dialogVisible: false
    }
  },
  computed: {
    defaultTheme() {
      return this.$store.state.settings.defaultTheme
    }
  },
  watch: {},
  created() {
    const token = getToken() ? getToken() : getParamFromUrl('token', document.location.toString())
    const split = decodeURIComponent(location.hash).split('redirect=')
    let redirectHash = ''
    if (split.length > 1) {
      redirectHash = split[split.length - 1]
    }
    urlFormat()
    if (token) {
      setToken(token)
      this.$store.commit('user/SET_TOKEN', token)
      if (redirectHash) {
        // 如何redirectHash包含&，则替换第一个&为？
        if (redirectHash.indexOf('&') > -1) {
          redirectHash = redirectHash.replace('&', '?')
        }
        router.push({ path: redirectHash })
      } else {
        router.push({ path: '/' })
      }
    } else {
      console.info('center-auth-vue', 'redirect to toCenterAuthLogin')
      this.$store.dispatch('user/toCenterAuthLogin')
    }
  },
  mounted() {
  },
  methods: {
    handleClose(done) {
      this.dialogVisible = false
      // window.location.href = window._CONFIG['centerAuthUrl']
      this.$store.dispatch('user/toCenterAuthLogin')
    }
  }
}
</script>

<style lang="scss">
@import 'ecip-web/styles/login.scss';
</style>
