
const app = getApp()
import {phoneLogin} from "../../../api/api"
import {auth} from "../../../api/auth.js"
import {throttle} from "../../../utils/util"
let urlHost=app.hostConfig().agreehost;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    message:'',
    check: false,
    mobile:'',
    showSuccess:false,
    beforeClose(action){
    return new Promise((resolve) => {
      if (action === 'confirm') {
        resolve(true);
      } else {
        // 拦截取消操作
        resolve(false);
      }
  })
},
    showEntryTip:false,
    showEntryTip_second:false,
    redirecUrl:'',
    img_host:'https://oss.prod.applets.igandanyiyuan.com/applet/case/static'

  },
  onConfirmSuccess(){
      let {mobile}=this.data;
    this.setData({
        showSuccess:false
    })
    app.method.navigateTo({
        url:'/case/pages/improveInfo/improveInfo?mobile='+mobile
    })
  },
  onCancelSuccess(){
    this.setData({
        showSuccess:false
    })
  },
  handleAgree(){
    if(!this.data.check){
      wx.showToast({
        title: '请同意《肝胆相照用户服务协议》!',
        icon:'none'
      })
      return false
    };
  },

  onConfirmEntry(){
    this.setData({
      showEntryTip:false
    })
    //wx.setStorageSync('hasEntry', true);
  },
  onCloseEntry(){
    this.setData({
       showEntryTip:true,
      showEntryTip_second:true
    })
  },
  onConfirmEntry_second(){
    this.setData({
      showEntryTip_second:false,
    });
    //wx.setStorageSync('hasEntry', true);
   
  },
  onCloseEntry_second(){
     wx.exitMiniProgram({success: (res) => {}})
     this.setData({
      showEntryTip_second:false,
      showEntryTip:false,
    });
    
   },
   goRegister:throttle(function(){
       app.method.navigateTo({
           url:'/case/pages/register/register'
       })
   }),
  getPhoneNumber:throttle(function(e) {
      console.log(e.detail)
    if (e.detail.errMsg == 'getPhoneNumber:ok'){
         auth().then(res => {
        phoneLogin('wx415cbcf96f4a3b27',{
            code:e.detail.code
        }).then((data)=>{
            const { envVersion } = wx.getAccountInfoSync().miniProgram;
            let token=''
            if(envVersion=="develop" || envVersion=="trial"){
                token="DEV_CASE_TOKEN"
            }else{
                token="PROD_CASE_TOKEN"
            }
           wx.setStorageSync(token, data.token);
          let url=this.data.redirectUrl?this.data.redirectUrl:'/pages/index/index';
        
          if(url.indexOf('login')!=-1 || url.indexOf('mobileLogin')!=-1){
            wx.reLaunch({
                url:'/pages/index/index',
                fail(){
                    wx.switchTab({
                      url: '/pages/index/index',
                    })
                }
            })
          }else{
            wx.reLaunch({
              url:url,
              fail(){
                wx.reLaunch({
                    url:'/pages/index/index'
                })
              }
            })
          } 
        }).catch(error=>{
            if(error.code==10007){
               this.setData({
                 showSuccess:true,
                 message:error.msg,
                 mobile:error.data
               })
            }
        })
      });
    }else if(e.detail.errMsg == 'getPhoneNumber:fail user deny'){
      console.log('您拒绝手机号授权')
    }else{
      wx.showToast({
        title: '手机号授权失败',
        icon:'none'
      })
    }
  }),
  goMobile(){
    let url='/case/pages/mobileLogin/mobileLogin'; 
    let redirectUrl=this.data.redirectUrl?this.data.redirectUrl:'/pages/index/index';
    app.method.navigateTo({
      url: url+"?redirectUrl="+encodeURIComponent(redirectUrl)
    })
  },
  goAgreement:throttle(function(event){
    let id=event.currentTarget.dataset.id;
    app.method.navigateTo({
      url:"/case/pages/linkPage/linkPage?url="+encodeURIComponent(urlHost+'/basic/file/agreement.htm?id='+id)
    })
  }),
  checkboxChange(event) {
    this.setData({
      check:  event.detail
    })

  },
  getRedirect(urlParams){
  
    let url=decodeURIComponent(urlParams);
    let index=url.indexOf("redirectUrl=");
    let cur_url=''
    if(index!=-1){
      cur_url=url.substring(index+12,index.length);
     // console.log(cur_url)
      this.getRedirect(cur_url);
    }else{
      cur_url=url;
    }   
    return decodeURIComponent(cur_url); 
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      beforeClose: this.data.beforeClose.bind(this),
  })
    if(options.redirectUrl){ 
        //console.log(decodeURIComponent(options.redirectUrl))
      this.setData({
        redirectUrl:"/"+decodeURIComponent(options.redirectUrl)
      })
    }
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      img_host:app.hostConfig().imghost
    });
    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: () => {}
    })
  
  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  
})