let util = require("../../../../utils/util")
let apiArr = require("../../../../api/partner")

// packages/partner/pages/login/login.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        account: "",
        password: "",
        checked:false

    },

   
    onChange(e){
        let that = this
        that.setData({
            checked:e.detail
        })
    },
    showTo(){
        wx.showModal({
          title: '提示',
          content: '请阅读并同意《用户服务协议和隐私政策》',
          complete: (res) => {
            if (res.cancel) {
              
            }
        
            if (res.confirm) {
              
            }
          }
        })
    },

    accountIpt(e) {
        let that = this
        that.setData({
            account: e.detail.value
        })
    },
    passwordIpt(e) {
        let that = this
        that.setData({
            password: e.detail.value
        })
    },
    accountLogin() {
        let that = this
        wx.showLoading({
            title: '登录中...',
            mask: true
        })

        wx.getSetting({
            withSubscriptions: true,
            success: (res) => {
                if (res.subscriptionsSetting && res.subscriptionsSetting.mainSwitch) {
                    const itemSettings = res.subscriptionsSetting.itemSettings || {};
                    const tmplMap = {
                        're34uubgvrwJLaiM3LhQEmvxrRxoNcJbo7b8gcbxx44': '新注册用户提醒',
                        '2BX7Zh5ccLzmHvvbCHuPWiaoZQyDCGfziCruxUct_EU': '订单续费通知',
                    };

                    // 收集未订阅的模板ID
                    const unsubscribedTmplIds = [];
                    Object.keys(tmplMap).forEach(tmplId => {
                        if (itemSettings[tmplId] !== 'accept') {
                            unsubscribedTmplIds.push(tmplId);
                        }
                    });

                    if (unsubscribedTmplIds.length > 0) {
                        // 逐个订阅未订阅的消息
                        wx.requestSubscribeMessage({
                            tmplIds: unsubscribedTmplIds,
                            success: (res) => {

                                util.postUrl(apiArr.login, {
                                    account: that.data.account,
                                    password: that.data.password
                                }, res => {
                                    wx.hideLoading()
                                    if (res.msg == '操作成功') {
                                        wx.setStorageSync('atoken', res.token)
                                        that.getInfo()
                                    }
                                })
                            },
                            fail: (err) => {
                                util.postUrl(apiArr.login, {
                                    account: that.data.account,
                                    password: that.data.password
                                }, res => {
                                    wx.hideLoading()
                                    if (res.msg == '操作成功') {
                                        wx.setStorageSync('atoken', res.token)
                                        that.getInfo()
                                    }
                                })
                            }
                        });
                    }else{
                        util.postUrl(apiArr.login, {
                            account: that.data.account,
                            password: that.data.password
                        }, res => {
                            wx.hideLoading()
                            if (res.msg == '操作成功') {
                                wx.setStorageSync('atoken', res.token)
                                that.getInfo()
                            }
                        })
                    }
                }
            },
        });


    },
    navigateToAgreement() {
        wx.navigateTo({
            url: '/pages/Agreement/Agreement',
        })
    },
    getInfo() {
        let that = this
        util.postUrl4(apiArr.loginInfo, {}, res => {
            console.log(res);
            wx.showToast({
                title: '登录成功！',
            })
            wx.setStorageSync('admin', res.data)
            setTimeout(() => {
                wx.navigateTo({
                    url: '/packages/partner/pages/partner',
                })
            }, 1500)

        })
    },

    tohome() {
        wx.redirectTo({
            url: '/pages/newLogin/newLogin',
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})