// pages/login/login.js
const app = getApp()
const $request = require('../../utils/request.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {

        userInfo: null,
        // 是否获取到了用户信息
        hasUserInfo: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const userInfo = wx.getStorageSync('userInfo') || null;
        if (app.globalData.userInfo || userInfo) {
            this.setData({
                userInfo
            })
        }
    },
    onShow(){
        this.setData({
            currentIndex: 2
        })
    },
    // 没有授权用户信息点授权手机提示
    showTotal(){
        wx.showToast({
            title: '请先允许访问头像、昵称及地区信息',
            icon: "none",
            duration: 2000 
        })
    },
    // 获取手机号授权
    getPhoneNumber(e) {
        console.log(e);
        if (!!e.detail.iv && !!e.detail.encryptedData) {
            const iv = e.detail.iv
            const encryptedData = e.detail.encryptedData
            this.appletLogin(iv, encryptedData)
        } else {
            wx.showToast({
                title: '请允许授权',
                icon: "none",
                duration: 3000
            })
        }
    },
    // 保存用户信息
    appletLogin(iv, encryptedData) {
        wx.showLoading({
            title: '登录中...',
            mask: true
        })
        let { nickName, gender, country, city, province, avatarUrl } = this.data.userInfo;
        $request.get('/com/appletLogin.action', {
            // openid: app.globalData.openid,
            iv: iv,
            encryptedData: encryptedData,
            sessionKey: app.globalData.sessionKey,
            loginState: "需求对对碰",
            avatarUrl,
            gender,
            nickName,
            country,
            province,
            city
        }).then(res => {
            console.log(res, '保存用户信息返回的Res')
            this.getUserSession()
        }).catch(error => {
            console.log(error, 'error appletLogin')
        })
    },
    // 获取用户信息弹框
    onCheckboxChange() {
        if (!this.data.hasUserInfo) {
            if (!this.data.userInfo) {
                this.getUserProfile()
            } else {
                this.setData({
                    hasUserInfo: true
                })
            }
        } else {
            this.setData({
                hasUserInfo: false
            })
        }
    },
    // 获取用户信息
    getUserProfile(e) {
        // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
        // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            lang:"zh_CN",
            success: (res) => {
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                })
                wx.setStorageSync('userInfo', res.userInfo)
            },
            fail: () => {
                wx.showToast({
                    title: '请允许授权',
                    icon: "none",
                    duration: 3000 
                })
            }
        })
    },
    // 保存个人信息后拿到新数据
    getUserSession() {
        let that = this
        $request.post('/script/getSession.action', {}).then(res => {
            wx.hideLoading()
            console.log(res, '返回的res')
            
            let businessCommunicationCustomer = res.data.businessCommunicationCustomer;
            app.globalData.customerId = businessCommunicationCustomer.customerId;
            app.globalData.customer = res.data.customer;
            app.globalData.businessCommunicationCustomer = businessCommunicationCustomer;
            wx.setStorageSync('businessCommunicationCustomer', businessCommunicationCustomer)
            let pages = getCurrentPages();
            let Page = pages[pages.length - 1];//当前页
            let prevPage = pages[pages.length - 2];  //上一个页面
            let info = prevPage.data //取上页data里的数据也可以修改
            // 如果是上一页是我的页面
            if (prevPage.route == "pages/myModule/index") {
                if (res.data.customer.paidByMonth) {
                    businessCommunicationCustomer.nickName = res.data.customer.nickName;
                    businessCommunicationCustomer.isPartner = true;
                } else {
                    businessCommunicationCustomer.isPartner = false;
                }
                prevPage.setData({
                    user: businessCommunicationCustomer,
                    isLogin:true
                })
                wx.navigateBack()
            } else {
                wx.navigateBack()
            }
            // let tempListData = info.listData;
            // tempListData.forEach(el => {
            //     if (el.businessCommunicationDemandId == businessCommunicationDemandId) {
            //         el.info = Page.data.detailValue
            //     }
            // })
            // prevPage.setData({ listData: tempListData })//设置数据
            // wx.navigateBack()
        }).catch(error => {
            console.log(error, "获取error")
        })
    },
})