<template>
  <!-- 状态栏 -->
  <view class="status_bar"></view>
  <!-- 导航栏 style="padding:20rpx 0" -->
  <uni-nav-bar left-icon="left" :dark="!appTheme" rightWidth="200rpx"  @clickLeft="clickLeft"  >
    <view style="flex: 1;flex-direction: row;display:flex;align-items: center;justify-content: center;overflow: hidden;">
      <text style="font-weight: bold;font-size: 36rpx;">{{ navTitle }}</text>
    </view>
    <template #right>
      <view @tap="switchTerrace" class="switchTerrace">
        <jx-icon icon="qiehuan" color="#2dd091" />
        <text style="color: #2dd091;font-size:4vw"> 切换平台</text>
      </view>
    </template>
  </uni-nav-bar>

  <view class="logo" >
    <image @tap="switchTerrace" :src="log" mode="scaleToFill" />
  </view>

  <view class="login">
    <!-- 果园商家版-未安装微信 不展示 -->
    <template v-if="isWx && getStorage('terrace') == 'jxcs'">
      <view class="btn" @tap="wxPopup.open()">
        <jx-icon icon="weixin" color="#fff" :size="50"></jx-icon>
        <text>微信登录</text>
        <image
          src="https://image.jxc4.com/image/64ff764213150d4c8929c4d554fbaccf.png"
          mode="widthFix"
        />
      </view>
    </template>
    <view class="btn phone" @tap="phoneLogin">
      <jx-icon icon="shoujihao" color="#fff" :size="50"></jx-icon>
      <text>账号登录</text>
      <image
        v-if="getStorage('terrace') == 'jxgy'"
        src="https://image.jxc4.com/image/64ff764213150d4c8929c4d554fbaccf.png"
        mode="widthFix"
      />
    </view>
  </view>

  <view class="info">欢迎进入京西到家商家版</view>
  

  <uni-popup ref="wxPopup" type="bottom">
    <view class="wxPopup-root">
      <view class="text">
        <view class="agreement-title">用户协议及隐私协议</view>
        <view class="content">
          本协议系您与成都若溪科技有限公司就京西到家帐号的注册和使用而订立的。成都若溪科技有限公司（如下简称“京西到家”）在此特别提醒您认真阅读、充分理解本协议各条款，特别是涉及服务费用条款，免除或者限制京西到家责任的条款，对您的权利限制条款，争议解决和法律适用条款等，此类条款将以加粗的形式提示您注意。请您审慎阅读并选择接受或不接受本协议（限制民事行为能力人应在监护人或法定代理人陪同下阅读）。除非您接受本协议所有条款，否则您无权注册、使用京西到家帐号。您的注册和使用京西到家帐号行为将视为您已充分理解本协议（即本协议及后续更新版本或开通或使用某项服务的单项协议），并同意接受本协议各项条款的约束。
        </view>
        <view class="agreement">
          点击同意即代表您已阅读并同意
          <text class="text" @tap="user">《用户协议》</text>
          <text class="text" @tap="privacy">《隐私协议》</text>
        </view>
      </view>
      <view class="btn">
        <view class="reject" @tap="wxPopup.close()">拒绝</view>
        <button class="resolve" @tap="appWxLoginFn">同意</button>
      </view>
    </view>
  </uni-popup>
</template>

<script lang="ts" setup>
  import toast from "@/utils/toast";
import { computed, ref } from "vue";
import useGlobalFunc from "@/composables/useGlobalFunc";
import { getStorage, setStorage } from "@/utils/storage";
import { store } from "@/store";
import { onReady, onShow } from "@dcloudio/uni-app";
import login from "@/api/https/login"
const { setUserInfo, appWxLogin } = useGlobalFunc()
// export default {
//     setup() {
        const appTheme = computed(() => { return store.getters['serveInfo/appTheme'] === 'light'})
        const isLogin = computed(() => { return getStorage('token') ? true : false })

        // 跳转手机号登录
        function phoneLogin() {
            // console.log('当前是属于哪个平台',getStorage('terrace'))
            uni.navigateTo({
                url: '/subPages/login/index',
            })
        }

        // const log = ref<string>('https://image.jxc4.com/image/9b9436561e7ff7d8d764787b1aa5182e.jpg')
        const log = ref<string>('https://image.jxc4.com/image/987f5c2f23452cdcbddde16f055437f5.png')
        const navTitle = ref<string>('京西到家商家')
        onReady(() => {
            uni.setNavigationBarColor({
              frontColor: appTheme.value ? '#000000': '#ffffff', // 前景文字颜色，必选
              backgroundColor: appTheme.value ? '#ffffff' : '#000000', // 背景颜色，必选
          });
        })
        onShow(() => {
            isDownWx()
            if (getStorage('terrace') == 'jxcs') {
                // log.value = 'https://image.jxc4.com/image/9b9436561e7ff7d8d764787b1aa5182e.jpg'
                log.value = 'https://image.jxc4.com/image/987f5c2f23452cdcbddde16f055437f5.png'
                navTitle.value = '京西到家商家'
            } else if(getStorage('terrace') == 'jxgy'){
                // log.value = 'https://image.jxc4.com/image/9e26e6d4e8646d340c21dfe595ac4d08.jpg'
                log.value = 'https://image.jxc4.com/image/5b24d0fae35b45d99a911bb09ecfa927.png'
                navTitle.value = '京西果园商家'
            }else{
                // log.value = 'https://image.jxc4.com/image/5cd356df441a32295798f78a39491464.png'
                log.value = 'https://image.jxc4.com/image/a2164dbb2289734a7d96e778040c5776.png'
                navTitle.value = '京西超市商家'
            }
            // 获取cid
            plus.push.getClientInfoAsync((info) => {
                let cid = info["clientid"];
                setStorage('cid', cid)
            }, () => { });
        })

        // ******************** 是否下载微信 *****************************
        const isWx = ref<boolean>(false)
        function isDownWx() {
            // if (plus.runtime.isApplicationExist({ pname: 'com.tencent.mm', action: 'weixin://' })) {
            //     isWx.value = true
            // } else {
            //     isWx.value = false
            // }

            isWx.value = false
            // // return
            // uni.getProvider({
            //     service: 'share',
            //     success: function (res) {
            //       // res.provider[0] === 'weixin' ? 微信已经安装 : 微信尚未安装
            //       isWx.value = res.provider[0] == 'weixin' ? true : false
            // }})
        }

        /**
         * 用户协议
         */
        function user() {
            let url = 'https://www.jxc4.com/managerApp/jxdjService.html'
            uni.navigateTo({ url: `/subPages/agreement/jxcs?url=${url}` })
        }


        /**
         * 隐私协议
         */
        function privacy() {
            let url = 'https://www.jxc4.com/managerApp/jxdjPrivacy.html'
            uni.navigateTo({ url: `/subPages/agreement/jxcs?url=${url}` })
        }

        // 微信登录弹窗实例
        const wxPopup = ref<any>(null)

        // ******************** wx登录类 *****************************
        //#region 
        // 微信登录
        function appWxLoginFn() {
            appWxLogin({
                bangding: (authToken: string) => {
                    // 显示自定义加载图(vuex)
                    store.commit('storeInfo/jxLoadingFn', true)
                    uni.navigateTo({ url: `/subPages/login/addAuth/addAuth?authToken=${authToken}` })
                },
                success: (data: AnyObject) => {
                    setUserInfo(data)
                    saveUserType()
                }
            })
            wxPopup.value.close()
        }

        /*************************************************
         * 存储用户信息
        */
        async function saveUserType() {
            let res = await login.get_self_info()
            setStorage('userType', res.code == 0 ? res.data.type : 0)
        }
        //#endregion 


        // ******************** 切换平台 *****************************
        //#region 
        let timer: any = null
        function switchTerrace() {
            uni.showActionSheet({
                title: '请选择使用平台',
                itemColor: '#5ab83e',
                itemList: ['京西到家商家', '京西果园商家','京西超市商家'],
                popover: {
                    width: 5000,
                },
                success: function (res) {
                    uni.showLoading({
                        title: '正在切换系统',
                    })
                    timer = setTimeout(() => {
                        // 京西菜市商家
                        if (res.tapIndex == 0) {
                            setStorage('terrace', 'jxcs')
                            log.value = 'https://image.jxc4.com/image/987f5c2f23452cdcbddde16f055437f5.png'
                            navTitle.value = '京西到家商家'
                        }
                        // 京西果园商家
                        if (res.tapIndex == 1) {
                            setStorage('terrace', 'jxgy')
                            log.value = 'https://image.jxc4.com/image/5b24d0fae35b45d99a911bb09ecfa927.png'
                            navTitle.value = '京西果园商家'
                        }
                        // 京西到家商家
                        if (res.tapIndex == 2) {
                            setStorage('terrace', 'gblm')
                            log.value = 'https://image.jxc4.com/image/a2164dbb2289734a7d96e778040c5776.png'
                            navTitle.value = '京西超市商家'
                        }

                        uni.hideLoading()
                        toast('切换成功', 1)
                        clearTimeout(timer)
                    }, 500)
                },
                fail: () => {
                    console.log('ZSW-取消')
                },
            })
        }
        //#endregion 

        /**
         * 返回
         */
        const clickLeft = (() => {
            console.log('点击返回，当前的登录状态,11111111',isLogin.value)
            if(isLogin.value){

                uni.navigateBack({
                    delta: 1
                });
            }else{
                uni.switchTab({ url: '/pages/merchant/index' })
            }
        // uni.navigateBack({
            // uni.navigateBack({
            //     delta: 1
            // });
        // })
        })
    
//         return {
//             appTheme,
//             phoneLogin,
//             user,
//             privacy,
//             wxPopup,
//             appWxLoginFn,
//             switchTerrace,
//             log,
//             navTitle,
//             isWx,
//             getStorage,
//             clickLeft,
//             isLogin
//         }
//     }
// }
</script>

<style lang="scss" scoped>
@import './wxLogin.scss';
</style>
<style>
@media (prefers-color-scheme: light) {
  page {
    background-color: #fff;
  }
}
</style>