<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 { ecipCancelAuth } from 'ecip-web/api/user'

export default {
  name: 'EcipAuthLoginCallback',
  components: {},
  props: [],
  data() {
    return {
      time: '',
      title: title,
      errorMsg: '',
      dialogVisible: false
    }
  },
  computed: {
    defaultTheme() {
      return this.$store.state.settings.defaultTheme
    }
  },
  watch: {},
  created() {
    const token = this.getParamFromUrl('token')
    if (token) {
      setToken(token)
      this.$store.commit('user/SET_TOKEN', token)
      localStorage.setItem('isLoginFromZlt', 'true')
      router.push({ path: '/' })
    }
    // } else {
    //   debugger
    //   this.errorMsg = this.$route.query.errorMsg
    //   if (this.errorMsg) {
    //     this.dialogVisible = true
    //   } else {
    //     console.log(localStorage.getItem('isLoginFromZlt'))
    //     localStorage.removeItem('isLoginFromZlt')
    //     window.location.href = window._CONFIG['zltAuthPortalUrl']
    //     // this.toZltReLogin()
    //   }
    // }
  },
  mounted() {},
  methods: {
    getParamFromUrl(paraName) {
      const url = document.location.toString()
      const arrObj = url.split('?')
      if (arrObj.length > 1) {
        const arrPara = arrObj[arrObj.length - 1].split('&')
        let arr
        for (let i = 0; i < arrPara.length; i++) {
          arr = arrPara[i].split('=')
          if (arr != null && arr[0] === paraName) {
            return arr[1]
          }
        }
        return ''
      } else {
        return ''
      }
    },
    async toZltReLogin(paraName) {
      const { data } = await ecipCancelAuth()
      // debugger
      if (data.success) {
        this.$store.dispatch('user/resetToken').then(() => {
          window.location.href = window._CONFIG['zltAuthPortalUrl']
        })
      }
    },
    handleClose(done) {
      this.dialogVisible = false
      window.location.href = window._CONFIG['zltAuthPortalUrl']
    }
  }
}
</script>

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