<template>
  <view>
     <view v-for="item in mockData" @click="click(item)"
           class="item"
           :class="[item === active? 'u-border active' :'']">
       用户名:{{item.userName}}
     </view>
<!--     <button @click="getTenanList">getTenanList</button>-->
     <button @click="next" :disabled="active === -1">下一步</button>
     <button @click="login">下一步</button>
<!--     <view>encryptToken结果:{{token}}</view>-->
  </view>
</template>

<script>
export default {
  name: "index",
  data(){
    return {
      back: '',
      mockData: [
        // {
        //   "id": 0,
        //   "userNo": "string",
        //   "userName": "string",
        //   "phone": "string",
        //   "tenantId": "string",
        //   "tenantCode": "string",
        //   "tenantName": "string",
        //   "tenantPlat": "string",
        //   "globalUserId": "string"
        // }
      ],
      active: -1,
      token: "",
      userData: {}
    }
  },
  onLoad(){
    // #ifdef APP-PLUS
    uni.onNativeEventReceive((event,data)=>{
      if(event === 'getTenantList'){
        this.mockData = JSON.parse(data);
      }else if(event === 'encryptToken'){
        this.token = data;
        uni.setStorageSync('encryptToken', data);
        this.login()
      }
    })
    this.getTenanList();
    // #endif

  },
  methods:{
    click(item){
      this.active = item;
    },
    getTenanList(){
      // 向宿主App发送事件
      uni.sendNativeEvent('getTenantList', {

      }, ret => {
        // 返回的是 String 还是 JsonObject
        // this.back = ret;
        // this.mockData = JSON.parse(ret);
      })
    },
    encryptToken(){

        uni.sendNativeEvent('encryptToken',JSON.stringify(
            {
              globalId: this.active.globalUserId,
              tenantId: this.active.tenantId,
              subId: this.active.id,
            }
        ), ret => {
          // 返回的是 String 还是 JsonObject
          // this.token = ret;

        })

    },
    getToken(){
      uni.setStorageSync('encryptToken',"")
    },
    login(){
      uni.setStorageSync('encryptToken',"MBN9Om00MUE9CEcnizCOwinwRe5NJNdZb4JyDT3/ze+vyyv5QNXVoo0Sb65Gptgdy9BZZFX36tEge08iDEzLHIYsLS95bnrcwUOh1tp9pMiJ0XYNBFnFcHFXP9AgeAmYe9EXW+akUkx4Zr7PGiG7eqhuabLlPisHhoQOBfFFTwQ=")
      // 核对
      this.$http(
          "auth.checkSecretKey",
          uni.getStorageSync('encryptToken')
      ).then(res =>{
        let {
          code, data
        } = res;
        if(code === 200){
          let {
            userId,      // 用户id
            enterpriseId // 企业id
          } = data;
          this.userData = data;
          uni.setStorageSync('tenantId', data.tenantId);
          if(userId && enterpriseId){
            this.createToken(data);
          }else{
            this.toRegister();
          }
        }
      })
    },
    // 跳转到注册
    toRegister(){
      let {userId,enterpriseId,subId,tenantId} = this.userData;
      this.$Router.push({
        path: "/pages/user/register",
        query:{
          pcUserId: userId || "",
          pcBasicsId: enterpriseId || "",
          tenantUserId:subId,
          tenantId,
        }
      })
    },
    // 跳转到首页
    toIndexPage(){
      this.$Router.push({
        path: "/pages/index/index"
      })
    },
    checkStatus(data){
      this.$http(
          "user.checkMagStatus",{
            pcUserId: data.userId,
            pcBasicsId: data.enterpriseId,
      }).then(res =>{
        let { code ,data} = res;
        if(code === 200){
          // 1-面料供应商, 2-品牌商, 3-设计机构, 4-检测机构
          let { identityType} = data.userLastIdentity;
          if(identityType){
            this.assignType = identityType;
            this.filterStatus(data)
          }
        }
      })
    },
    filterStatus(data){
      // 0-未完善 1-已完善
      let{
        isBrandOwner,   // 我要选款
        isDesignAgency, // 完善设计机构信息
        isFabricSupplier, //完善面料供应商信息
        isTestingFacility, // 完善检测机构信息
      } = data;
      let { assignType} = this;
      switch (assignType){
        case "1":
          isFabricSupplier  ? this.toIndexPage() : this.toRegister();
          return ;
        case "2":
          isBrandOwner  ? this.toIndexPage() : this.toRegister();
          return ;
        case "3":
          isDesignAgency   ? this.toIndexPage() : this.toRegister();
          return ;
        case "4":
          isTestingFacility ? this.toIndexPage() : this.toRegister();
          return ;
        default:
          this.toRegister();
          return;
      }
    },
    createToken(data){

      this.$http("auth.createTokenByApp" ,{
        ...data,
        ...{
          identityType: uni.getStorageSync("identityType")
        }
      }).then(res =>{
        let {
          code, data
        } = res;
        if(code === 200){
          uni.setStorageSync('token', data.accessToken);
          uni.setStorageSync('pcUserId', data.userId);
          uni.setStorageSync('pcBasicsId', data.enterpriseId);
          // uni.setStorageSync('tenantId', this.userData.tenantId);

          this.checkStatus(data);
        }
      })
    },
    next(){
      this.encryptToken();
        // this.encryptToken();
       // uni.setStorageSync('encryptToken',"MBN9Om00MUE9CEcnizCOwinwRe5NJNdZb4JyDT3/ze+vyyv5QNXVoo0Sb65Gptgdy9BZZFX36tEge08iDEzLHIYsLS95bnrcwUOh1tp9pMiJ0XYNBFnFcHFXP9AgeAmYe9EXW+akUkx4Zr7PGiG7eqhuabLlPisHhoQOBfFFTwQ=")
       // this.$Router.push({
       //   path: "/pages/user/register"
       // })
    }
  }
}
</script>

<style scoped>
.item{
  padding: 30rpx;
}
.active{
  color: #1E90FF;
}
</style>