<template>
    <view class="admin-bind">
        <image mode="aspectFit" src="/static/images/icon/success_tip.png"></image>
        <view class="ab-tip">扫码成功</view>
        <view class="ab-brand">品牌名称：{{brandInfo.name || ''}}</view>
        <view>请点击“确认绑定”按钮成为超级管理员；</view>
        <view>绑定后您将可以添加员工并查看品牌订单数据</view>
        <button 
            plain 
            hover-class="hover-active" 
            open-type="getUserInfo"
            lang="zh_CN"
            @getuserinfo="getuserinfo">确认绑定</button>
    </view>
</template>

<script>
import { API } from '../../js/api';
import { servers } from '../../js/server';
import util from '../../utils/util';
const uniLogin = util.promisify(uni.login);
export default {
    data(){
        return {
            brandInfo: {},
            sceneQuery: {
                b:'37',
                r:'1'
            },
        }
    },
    onLoad(options){
        // console.log(options)
        
        // 扫小程序码
        if (options.scene) {
            // "r=%s&b=%d"  ->  rid  brand_id -> 绑定接口需要传入
            let sceneQuery = util.formatScene(options.scene);
            this.sceneQuery = sceneQuery;
            this.getBrandInfo(sceneQuery.b);
        } else {
            console.log("no scene");
        }
        
    },
    methods: {
        async getuserinfo(userRes){
            if(!userRes.detail.userInfo){
                return util.showNone('获取用户信息失败！请稍后重试');
            }
            let loginRes = await uniLogin();

            if(!loginRes.code){
                return util.showNone('获取登陆凭证失败！稍后重试');
            }
            const APPID = uni.getAccountInfoSync().miniProgram.appId;

            let { sceneQuery } = this;
            if(!sceneQuery.r)return util.showNone('缺少rid！');
            util.showLoad();
            // 后端说要登陆才能拿到具体身份，不知后端怎么设计关系
            servers.post({
                url: API.assistantAuth,
                data: {
                    appid: APPID,
                    code: loginRes.code,
                    encryptedData: userRes.detail.encryptedData,
                    iv: userRes.detail.iv,
                    brand_id: sceneQuery.b,
                    rid: sceneQuery.r,
                    // brand_id: 37,
                    // rid: '@ed8dcd',
                    // 'avatar_url': userRes.detail.userInfo.avatarUrl,
                    // ...userRes.detail.userInfo,
                },
                isDefaultGet: false,
            })
            .then(res=>{
                util.hideLoad();
                if(res.data.code == 0){
                    let _data = res.data.data;
                    // if(_data.user.role === '')return util.routeTo(`/pages/merchant_login/merchant_login`,'rT');
                    util.showNone(res.data.message || '操作成功！');
                    
                    // uni.setStorageSync('token', _data.token);
                    setTimeout(_=>{
                        util.routeTo(`/pages/index/index`,'rL');
                    }, 1200);
                }else{
                    util.showNone(res.data.message || '操作失败！');
                }
                
            }).catch(util.hideLoad)
        },
        getBrandInfo(brand_id){
            util.showLoad();
            servers.get({
                url: API.brandInfo + '/' +brand_id,
                data: {},
                failMsg: '加载品牌信息失败！'
            })
            .then(res=>{
                util.hideLoad();
                this.brandInfo = res;
                console.log(res)
            })
            .then(util.hideLoad)
        }
    }
}
</script>

<style lang="scss">
    @import "../../style/public.scss";
    page{
        background-color: #fff;
    }
    .admin-bind{
        padding-top: 150upx;
        >image{
            display: block;
            margin: 0 auto;
            margin-bottom: 30upx;
            width: 150upx;
            height: 150upx;
        }
        >view{
            text-align: center;
            font-size: 28upx;
            color: #1a1a1a;
        }
        .ab-tip{
            margin-bottom: 50upx;
            font-size: 40upx;
            font-weight: 500;
        }
        .ab-brand{
            font-size: 32upx;
            margin-bottom: 50upx;
            @include textHide(1);
        }
        >button{
            margin-top: 70upx;
            width: 702upx;
            height: 88upx;
            line-height: 88upx;
            text-align: center;
            border: none;
            border-radius: 10upx;
            font-size: 32upx;
            color: #fff;
            background-color: $themeColor;

        }
    }
</style>