<template>
  <div v-loading="true"
       class="redirect-main"
       element-loading-background="#e1dfdd"
       element-loading-spinner="el-icon-loading"
       element-loading-text="快速登录中"
  >

  </div>
</template>

<script>
import Cookies from 'js-cookie'
import { fastLogin } from '@/api/open'
import StorageUtils from '@/utils/Storage'

export default {
  name: 'OpenRedirect',
  data() {
    return {}
  },
  created() {
    StorageUtils.setLocal('reservationLog', {
      url: window.location.href
    })
    const { query } = this.$route
    if (!query.username) {
      this.toError('未传递工号')
    } else {
      switch (query.type) {
        case 'HospitalizationSubscribe':
          this.toHospitalizationSubscribe(query)
          break
        default:
          this.toApply(query)
          break
      }
    }
  },
  methods: {
    toApply(query) {
      if (!query.applyNum) {
        this.toError('未传递工号')
      } else {
        this.fastLogin(query.username, () => {
          StorageUtils.setWisLocal('applyNum', {
            applyNum: query.applyNum,
            type: '1'
          })
          this.$router.push({ path: `/open/manualReservation` })
        })
      }
    },
    /**
     * http://localhost:8083/OpenRedirect?username=admin&type=HospitalizationSubscribe
     * http://172.16.9.101:8083/OpenRedirect?username=0389&type=HospitalizationSubscribe&wardCode=541
     * http://localhost:8083/OpenRedirect?username=0389&type=HospitalizationSubscribe&wardCode=541
     * @param query
     */
    toHospitalizationSubscribe(query) {
      this.fastLogin(query.username, () => {
        this.$router.push({ path: `/tranDetail/hospitalizationSubscribe`, query: { wardCode: query.wardCode } })
      })
    },
    toError(message) {
      this.$confirm(
        message,
        '异常提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true,
          showClose: false,
          closeOnClickModal: false,
          closeOnPressEscape: false,
          closeOnHashChange: false,
          roundButton: true,
          showCancelButton: false,
          showConfirmButton: false
        }
      )
    },
    /**
     * 快速登录
     * http://localhost:8083/OpenRedirect?username=admin&applyNum=1020220815092836990002346231
     * http://localhost:8083/OpenRedirect?username=admin&applyNum=1020220809090408326002308281
     * http://localhost:8083/OpenRedirect?username=admin&applyNum=1020220815092836990002346231,102022081509281360002312601
     * 1.判断当前有没有token,没有就走自动登录
     * 2.判断当前登录用户工号是否相同
     * 3.判断token有效性,已经过期就走自动登录
     * 4.token有效,就跳转手动预约界面
     * @param username
     * @param callback
     */
    fastLogin(username, callback) {
      let token = Cookies.get('Admin-Token')
      Cookies.remove('username')
      Cookies.remove('Admin-Token')
      fastLogin({ username, token })
        .then(res => {
          const { token } = res
          this.$store.dispatch('autoLogin', { token, username })
            .then(_ => {
              callback()
            })
        }).catch(err => {
        this.$confirm(
          `${err.message}`,
          '自动登录失败',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error',
            center: true,
            showClose: false,
            closeOnClickModal: false,
            closeOnPressEscape: false,
            closeOnHashChange: false,
            roundButton: true,
            showCancelButton: false,
            showConfirmButton: false
          }
        )
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.redirect-main {
  width: 100%;
  height: 100%;
}
</style>
