<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"/>
    <input v-if="!!$route.query.code" class="codeText" :value="$route.query.code"/>
    <div class="codeBtn" @click="handleLogin">去登录</div>
    <input class="codeText" v-model="search" placeholder="搜索要查找的应用"/>
    <div flex class="appsPane wrap">
      <b v-for="app in appsList" :key="app.key" @tap="handleGotoApp(app)">{{ app.name }}</b>
    </div>
  </section>
</template>

<script>
import {mapActions, mapState} from 'vuex'
import AiResult from "../components/AiResult";

export default {
  name: 'loading',
  components: {AiResult},
  inject: ['root'],
  computed: {
    ...mapState(['token', 'openUser', 'user']),
    appsList() {
      let {search} = this
      return this.apps?.filter(e => !!search ? e.name.indexOf(search) > -1 : true) || []
    },
    currentApp() {
      return this.apps.find(e => e.key == this.$route.query.app) || {}
    }
  },
  data() {
    return {
      result: {},
      apps: [],
      search: ""
    }
  },
  methods: {
    ...mapActions(['agentSign']),
    redirectTo(path) {
      let {query, hash} = this.$route
      delete query.app
      uni.navigateTo({
        url: `/apps${path}`, success: () => {
          this.$router.push({query, hash})
        },
        fail: err => {
          console.error(err)
        }
      })
    },
    handleGotoApp(app) {
      uni.navigateTo({url: `/apps${app.path}`})
    },
    handleLogin() {
      uni.navigateTo({url: "./login"})
    },
    getApps() {
      this.apps = []
      let applications = require.context('../apps', true, /\.(\/.+)\/App[^\/]+\.vue$/)
      console.log(applications.keys())
      applications.keys().map(path => {
        if (applications(path).default) {
          let {name: key, appName: name} = applications(path).default
          this.apps.push({key, name, path: path.replace(/^\.(.+).vue$/g, '$1')})
        }
      })
    }
  },
  onLoad() {
    this.agentSign(this.$route.query)
  },
  onShow() {
    this.getApps()
    this.result = {
      tips: "欢迎进入开发应用",
    }
  }
}
</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>
