<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>
    <u-radio-group v-model="currentLib" @change="handleSwitchSystem">
      <u-radio v-for="(op,i) in libs" :key="i" :name="op.dir">{{ op.label }}</u-radio>
    </u-radio-group>
    <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, mapMutations, mapState} from 'vuex'
import AiResult from "../components/AiResult";
import AiSelect from "../components/AiSelect";

export default {
  name: 'loading',
  components: {AiSelect, 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: [],
      libs: [
        {label: "通用版", dir: "apps"},
        {label: "上架版", dir: "sass"},
      ],
      currentLib: "apps",
      search: ""
    }
  },
  methods: {
    ...mapMutations(['logout']),
    ...mapActions(['agentSign']),
    handleGotoApp(app) {
      uni.navigateTo({url: `/${this.currentLib}${app.path}`})
    },
    handleLogin() {
      uni.navigateTo({url: "./login"})
    },
    getApps() {
      this.apps = []
      let applications = this.currentLib == "apps" ? require.context('../apps', true, /\.(\/.+)\/App[^\/]+\.vue$/) :
          this.currentLib == "sass" ? require.context('../sass', true, /\.(\/.+)\/App[^\/]+\.vue$/) : null
      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')})
        }
      })
    },
    handleSwitchSystem() {
      this.$http.defaults.baseURL = this.currentLib == "apps" ? "/lan" :
          this.currentLib == "sass" ? "/saas" : "/"
      this.getApps()
      this.logout()
    }
  },
  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>
