import { postUrl, postUrl3 } from '../../utils/util';
import apiArr from '../../api/water_filter';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLogin: false,
        loginType: 'account', // 默认手机登录
        password: "",
        account: "",
        type: "",
        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) {
              
            }
          }
        })
    },

    navigateToAgreement() {
        wx.navigateTo({
            url: '../Agreement/Agreement',
        })
    },
    accountIpt(e) {
        let that = this
        that.setData({
            account: e.detail.value
        })
    },
    passwordIpt(e) {
        let that = this
        that.setData({
            password: e.detail.value
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this
        let loginType = ''
        if (wx.getStorageSync('LoginType')) {
            loginType = 'account'
        } else {
            loginType = 'phone'
        }
        that.setData({
            loginType,
            type: options.type
        })
    },
    accountLogin() {
        let that = this
        wx.showLoading({
            title: '登录中...',
            mask: true
        })
        let version  =  wx.getStorageSync('version')
        if(version){
            wx.login({
              success: (res) => {
                postUrl3(apiArr.accountLogin, {
                    phone: that.data.account,
                    code:res.code,
                    pass: that.data.password,
                    version
                }, version, res2 => {
                    wx.hideLoading()
                    wx.hideLoading()
                    if (res2.token) {
                        wx.setStorageSync('ctoken', res2.token);
                        wx.setStorageSync('loginMaster', 1);
                        wx.setStorageSync('is_deal', res2.is_deal);
                        wx.setStorageSync('is_dev', res2.is_dev);
                        wx.setStorageSync('is_shop', res2.is_shop);
                        postUrl(apiArr.loginInfo, {}, ress => {
                            if (ress.error) return;
                            // 保存userId 后续接口使用
                            wx.setStorageSync('userId', ress.user_id);
                            wx.setStorageSync('openId', ress.open_id);
                            wx.setStorageSync('shopId', ress.wshop_id);
                            wx.setStorageSync('mshopId', ress.mshop_id);
                            wx.setStorageSync('dealer_id', ress.dealer_id);
                            this.setData({
                                isLogin: true,
                            })
                        }, res.token)
        
                        wx.showToast({
                            title: '登录成功',
                            icon: 'success',
                            mask: true,
                            duration: 2000,
                            success: () => {
                                console.log(wx.getStorageSync('is_dev'),'isdev');
                                if(wx.getStorageSync('is_dev') == '0' || wx.getStorageSync('is_dev') == '1'){
                                    wx.navigateTo({
                                      url: '/pages/deposit/deposit',
                                    })
                                }else{
                                    setTimeout(() => {
                                        this.tohome();
                                    }, 2000)
                                }
                            }
        
                        });
        
                    }else{
        
                    }
                })
              },
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },
    headerLoginClick() {
        let that = this
        wx.showLoading({
            title: '加载中',
            mask: true
        });
        wx.login({
            success: (res) => {

                postUrl(apiArr.login, {
                    code: res.code
                }, res => {
                    console.log('eaasdaadd', res)
                    wx.setStorageSync('is_deal', res.is_deal);

                    if (res.error) return;
                    postUrl(apiArr.loginInfo, {}, ress => {
                        if (ress.error) return;
                        // 保存userId 后续接口使用
                        wx.setStorageSync('userId', ress.user_id);
                        wx.setStorageSync('openId', ress.open_id);
                        wx.setStorageSync('shopId', ress.wshop_id);
                        wx.setStorageSync('mshopId', ress.mshop_id);
                        wx.setStorageSync('dealer_id', ress.dealer_id);
                        wx.setStorageSync('is_dev', ress.is_dev);
                        wx.setStorageSync('userInfo', ress)
                        this.setData({
                            isLogin: true,
                        })
                    }, res.token)
                    wx.setStorageSync('ctoken', res.token);
                    wx.setStorageSync('loginMaster', 1);
                    wx.setStorageSync('is_deal', res.is_deal);
                    wx.setStorageSync('is_shop', res.is_shop);
                    wx.hideLoading();
                })
            },
        })
    },


    getPhoneNumber(event) {
        let that = this
        const { isLogin } = this.data;
        console.log('获取信息', event);
        if (event.detail.errMsg === "getPhoneNumber:ok") {
            wx.showLoading({
                title: '加载中',
                mask: true
            });
            
            postUrl(apiArr.loginGetPhone, {
                code: event.detail.code
            }, res => {
                if (isLogin) {
                    wx.setStorageSync('phone', res.phone);
                    wx.hideLoading();
                    wx.showToast({
                        title: '登录成功',
                        icon: 'success',
                        mask: true,
                        duration: 2000,
                        success: () => {
                            that.tohome();
                        }
                    });

                } else {
                    wx.hideLoading();
                    wx.showToast({
                        title: '登录失败',
                        icon: 'error',
                        mask: true,
                        duration: 2000,
                    })
                }
            })
        } else {
            wx.removeStorageSync('ctoken');
            wx.setStorageSync('loginMaster');
            wx.removeStorageSync('userId');
            wx.removeStorageSync('openId');
            wx.removeStorageSync('phone');
            wx.removeStorageSync('is_deal');
            wx.removeStorageSync('is_dev');
            wx.removeStorageSync('is_shop');
            wx.removeStorageSync('shopId');
            wx.removeStorageSync('mshopId');
            wx.removeStorageSync('dealer_id');
            console.error('用户拒绝授权:', event.detail.errMsg);
        }
    },



    // 返回主页
    tohome: function () {
        let that = this
        wx.redirectTo({
            url: '/pages/water_filter/water_filter'
        });
    },

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