<template>
  <section class="loading">
    <!--    <div class="iconfont iconfont-iconWeChat"/>-->
    <!--    <div class="iconfont iconfont-iconjuminxinxi"/>-->
    <!--    <div class="iconfont iconfont-iconLogo"/>-->
    <ai-result v-if="result.tips" v-bind="result"/>
    <template v-if="isDev">
      <input v-if="!!$route.query.code" class="codeText" :value="$route.query.code"/>
      <div class="codeBtn" @click="devGetCode">获取code</div>
      <div flex class="appsPane wrap">
        <b v-for="app in apps" :key="app.key" @tap="gotoApp(app.key)">{{ app.name }}</b>
      </div>
    </template>

  </section>
</template>

<script>
import {mapActions, mapState} from 'vuex'
import AiResult from "../components/AiResult";
import UTag from "../uview/components/u-tag/u-tag";

export default {
  name: 'loading',
  components: {UTag, AiResult},
  inject: ['root'],
  computed: {
    ...mapState(['token', 'apps', 'openUser', 'user']),
    currentApp() {
      return this.apps.find(e => e.key == this.$route.query.app) || {}
    },
    isDev() {
      return this.$route.hash == "#dev"
    }
  },
  data() {
    return {
      result: {}
    }
  },
  methods: {
    ...mapActions(['getToken', 'getAccount', 'agentSign', 'getUserInfo', 'getCode', 'closeAgent']),
    initAccess() {
      if (this.$route.hash == "#error" || this.isDev) {
        return Promise.resolve()
      } else if (this.$route.hash == "#form") {
        if (this.openUser?.openId || !!this.$route.query.preview) {
          this.openForm()
        } else if (this.$route.query?.code) {
          this.getToken(this.$route.query?.code)
          .then(() => this.getUserInfo())
          .then(() => this.openForm())
        } else this.getCode(location.href)
      } else if (this.token) {//获取账号信息
        return this.getAccount()
      } else if (this.$route.query?.code) {//获取token
        return this.getToken(this.$route.query?.code)
      } else {//获取应用配置
        this.getCode(location.href)
      }
    },
    openForm() {
      this.redirectTo("/askForm/askForm")
    },
    redirectTo(path) {
      let {query, hash} = this.$route
      delete query.app
      uni.redirectTo({
        url: `/pages${path}`, success: () => {
          this.$router.push({query, hash})
        }
      })
    },
    gotoApp(app) {
      uni.reLaunch({url: '/pages/loading?app=' + app})
    },
    devGetCode() {
      this.getCode(location.origin + '/pages/loading#dev')
    }
  },
  created() {
    uni.showLoading({
      title: "加载中"
    })
    this.initAccess()?.then(() => {
      uni.hideLoading()
      if (this.token) {
        if (this.currentApp.name) {
          this.redirectTo(this.currentApp.path)
        } else if (this.$route.query.url) {
          this.redirectTo(this.$route.query.url)
        } else {
          this.result = {
            status: "error",
            tips: "应用加载失败",
            btn: "重新加载",
            btnTap() {
              location.href = location.href?.replace("#error", '')
            }
          }
        }
      } else if (this.isDev) {
        this.result = {
          tips: "欢迎进入开发应用",
        }
      } else {
        this.result = {
          status: "error",
          tips: "应用加载失败",
          btn: "重新加载",
          btnTap() {
            location.href = location.href?.replace("#error", '')
          }
        }
      }
    })?.catch(() => {
      uni.navigateTo({url: './login'})
    })
  }
}
</script>

<style lang="scss" scoped>
.loading {
  height: 100%;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;

  & > span {
    font-size: 36px;
  }

  .codeText {
    font-size: 24px;
    margin-top: 16px;
    padding: 4px 0;
    width: 100%;
    text-align: center;
    background: #ccc;
  }

  .codeBtn {
    width: 180px;
    text-align: center;
    cursor: pointer;
    font-size: 24px;
    background: $uni-color-warning;
    color: #fff;
    padding: 8px;
    margin-top: 16px;
    border-radius: 8px;
    font-weight: normal;
  }

  .appsPane {
    justify-content: center;
    margin-top: 16px;
    padding: 0 16px;

    b {
      cursor: pointer;
      font-size: 24px;
      background: $uni-color-primary;
      color: #fff;
      padding: 8px;
      margin: 4px;
      border-radius: 8px;
      font-weight: normal;
    }
  }
}
</style>
