// Pages/login/index.js
import { API } from './../../utils/network/api'
const api = new API()
const app = getApp()
Page({
  data: {
    showEntryTip:false,
    showEntryTip_second:false,
    beforeClose(action){
      return new Promise((resolve) => {
        if (action === 'confirm') {
          resolve(true);
        } else {
          // 拦截取消操作
          resolve(false);
        }
    })
  },
    navbarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '登录', //导航栏 中间的标题
    },
    checked: false,
    static_host: api.getStaticHost()
  },
  onLoad(){
    let usertype =  wx.getStorageSync('usertype');
    this.setData({
      beforeClose: this.data.beforeClose.bind(this),
  })
    if(!usertype){
      wx.navigateTo({
        url: "/Pages/index/index"
      })
    };
    wx.getPrivacySetting({
      success: res => {
        console.log(res) // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
        if (res.needAuthorization) {
          // 需要弹出隐私协议
          this.setData({
            showEntryTip:true
          })
        } else {
          this.setData({
            showEntryTip:false
          })
          // 用户已经同意过隐私协议，所以不需要再弹出隐私协议，也能调用已声明过的隐私
        }
      },
      fail: () => {},
      complete: () => {}
    })
    // let isEntry=wx.getStorageSync('hasEntry');
    // if(!isEntry){
    //   this.setData({
    //     showEntryTip:true,
    //    });
    //  }else{
    //   this.setData({
    //     showEntryTip:false
    //    });
    //  }
  },
  onChange(event) {
    this.setData({
      checked: event.detail,
    });
  },
  changeCheck(){
    let checked = this.data.checked
    this.setData({
      checked: !checked
    })
  },
  _navback() {
    wx.navigateBack()
  },
  onConfirmEntry(){
   // wx.setStorageSync('hasEntry', true);
  },
  onCloseEntry(){
    this.setData({
      showEntryTip_second:true,
      showEntryTip:true
    }) 
  },
  onConfirmEntry_second(){
    this.setData({
      showEntryTip_second:false,
     // showEntryTip:false,
    });
    //wx.setStorageSync('hasEntry', true);
    
  },
  onCloseEntry_second(){
    
     this.setData({
      showEntryTip_second:false,
      showEntryTip:false,
    });
     wx.exitMiniProgram({success: (res) => {}})
   },
  go(e){
    let url = e.currentTarget.dataset.url;
    app.go(url);
  },
  getPhoneNumber (e) {
    console.log(e);
    console.log(e.detail.code);
    if(!this.data.checked){
      wx.showToast({
        title: '请同意协议',
        icon: "error"
      })
      return;
    }
    let THIS=this;
    let usertype =  wx.getStorageSync('usertype');
    let next_url =  wx.getStorageSync('next_url');
    wx.login({
      success (res) {
      if (res.code) {
        console.log('登录成功！' + res.errMsg)
        var params = {
          phone_code: e.detail.code,
          user_type: usertype,
          wx_code: res.code
        }
        api.wechatMobileLogin(params).then(response => {
          console.log(response);
          wx.setStorageSync('AUTH_TOKEN_'+usertype, response.data.token);
          wx.setStorageSync('user_id_'+usertype, response.data.user_id);
          wx.setStorageSync('client_user_id_'+usertype, response.data.client_user_id);
          app.globalData.config.userID = response.data.user_id;
          app.globalData.isLogin=true;
          app.imInit();
          //跳转页面
          wx.switchTab({
            url: next_url
          })
        }).catch(errors => {
          console.error(errors);
        })
      } else {
        console.log('登录失败！' + res.errMsg)
      }
      }
    })
  },
})