<template>
  <view class="pageBox _index_page">
    <view class="container ">
      <image src="@/common/assets/images/sign/logo_w.png" mode="widthFix" class="logo" />
      <image src="@/common/assets/images/sign/bg.jpg" class="bg" />
    </view>


    <view class="identity" v-if="showIdentity">
      <view class="mask">
        <text class="title">选择要登陆的身份：</text>
        <view class="picker">
          <picker @change="bindPickerChange" :value="identityIndex" :range="identityArray" range-key="identity_role_name">
            <view class="uni-input">
              <text>{{identityLabel}}</text>
              <iconfont icon="right" />
            </view>
          </picker>
        </view>
        <button class="btn" size="mini" hover-class="hover" @click="confirm()">确定</button>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, reactive, computed, onBeforeMount, onMounted, onUnmounted, watch, nextTick } from 'vue'
import Storage from '@/common/utils/storage/index'
import { login_wechat_applets, bind_wechat_applets } from '@/common/apis/base'
import { indexCreate } from '@/common/utils/create'
import { set_db , saveMenus} from '@/common/utils/create/index'
import EXEC from "@/common/utils/exec"

const wxLogin = () => {
  /*#ifdef MP-WEIXIN*/
  uni.login({
    provider: 'weixin', //使用微信登录
    success: function (res:any) {
      if (res.code) {
          Storage.set('wxCode', res.code);
          let params = {
						'applets_code':'V3_Applets',
						'code':res.code,
            'exec_comp_id': 'IA',
					}
          login_wechat_applets(params).then((data:any) => {
            let res_user:any = data['result']
            EXEC.comp_id = data['result'].comp_id
            EXEC.comp_name = data['result'].comp_name
            Storage.set('comp_id', EXEC.comp_id)
            Storage.set('comp_name', EXEC.comp_name)
            Storage.set('userToken',res_user.userToken)
          }).then(async () => {
            indexCreate()
            if(Storage.get('identity_role')){
              let obj = Storage.get('identity_role')
              EXEC.identity_role = obj.identity_role
              EXEC.identity_role_name = obj.identity_role_name
            }
            let timer = setTimeout(()=>{
              if(Storage.get('identity_role')){
                uni.reLaunch({
                  url: '/common/views/pages/home/index',
                })
              }else{
                identityArray.value = EXEC.identity_roles_arr
                if(identityArray.value.length>1){
                  identityIndex.value = 0
                  identityLabel.value= identityArray.value[identityIndex.value].identity_role_name
                  showIdentity.value = true
                } else if(identityArray.value.length==1){
                  EXEC.identity_role = identityArray.value[0].identity_role
                  EXEC.identity_role_name = identityArray.value[0].identity_role_name
                  Storage.set('identity_role',{
                    identity_role: identityArray.value[0].identity_role,
                    identity_role_name: identityArray.value[0].identity_role_name
                  })
                  uni.reLaunch({
                    url: '/common/views/pages/home/index',
                  })
                } else {
                  EXEC.identity_role = ''
                  EXEC.identity_role_name = ''
                  Storage.remove('identity_role')
                  uni.reLaunch({
                    url: '/common/views/pages/home/index',
                  })
                }
              }
              clearTimeout(timer)
            },1500)
          }).catch((data)=>{
              let res_user:any = data['result']
              Storage.set('userToken',res_user.userToken)
              uni.showToast({
                title: '授权失败',
                duration: 2000,
                icon: 'error',
              });
              let timer = setTimeout(()=>{
                goSign()
                clearTimeout(timer)
              },1000)
          })
				}
      }
    });
    /*#endif*/
    
    /*#ifdef APP-PLUS*/
    goSign()
    /*#endif*/

    function goSign () {
      uni.showToast({
        title: '即将跳转到登录',
        duration: 2000,
        icon:'none',
        complete: (res) => { 
          setTimeout(()=>{
            uni.reLaunch({
              url: '/common/views/pages/sign/index',
            })
          },2000)
        }
      });
    }
}


const identityArray = <any>ref([])
const identityIndex = ref(0)
const identityLabel = ref('暂无身份')
const showIdentity = ref(false)
const bindPickerChange = (e:any) => {
  identityIndex.value = e.detail.value
  identityLabel.value= identityArray.value[e.detail.value].identity_role_name
}
const confirm = () => {
  EXEC.identity_role = identityArray.value[identityIndex.value].identity_role
  EXEC.identity_role_name = identityArray.value[identityIndex.value].identity_role_name
  Storage.set('identity_role',{
    identity_role: identityArray.value[identityIndex.value].identity_role,
    identity_role_name: identityArray.value[identityIndex.value].identity_role_name
  })
  saveMenus()
  uni.reLaunch({
    url: '/common/views/pages/home/index',
  })
}

onBeforeMount(async() => {
})
onMounted( async() => {
  wxLogin()
})
onUnmounted(() => {
})
</script>

<style lang="scss">
._index_page {
  background: #3f94d7;
  .bg{
    position: absolute;
    bottom: 0;
    left: 0;  
    right: 0;
    height: 40vh;
    width: 100vw;
    z-index: 1;
  }
  .logo{
    width: 300rpx;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translateX(-50%) translateY(-50%);
    z-index: 2;
  }
  .identity{
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,.5);
    z-index: 3;
    .mask{
      position: absolute;
      top: 50%;
      right: 50%;
      transform: translateX(50%) translateY(-50%);
      width: 80vw;
      padding: 30rpx;
      background: #fff;
      border-radius: 30rpx;
      >.title{
        color: #999;
      }
      >.picker{
        width: 100%;
        height: 80rpx;
        box-sizing: border-box;
        line-height: 80rpx;
        margin-top: 20rpx;
        margin-bottom: 20rpx;
        border: 2rpx solid #ddd;
        padding: 0 20rpx;
        border-radius: 20rpx;
        .uni-input{
          display: flex;
          justify-content: space-between;
        }
      }
      >.btn{
        background: $primary-color;
        color: #fff;
        width: 100%;
        line-height: 80rpx;
        &.hover{
          opacity: .8;
        }
      }
    }
  }
}

</style>
