// Pages/index/index.js
import { API } from './../../utils/network/api'
const api = new API()
const app = getApp()
Page({
  data: {
    loading: true,
    showEntryTip:false,
    showEntryTip_second:false,
    beforeClose(action){
      return new Promise((resolve) => {
        if (action === 'confirm') {
          resolve(true);
        } else {
          // 拦截取消操作
          resolve(false);
        }
    })
  },
    static_host: api.getStaticHost()
  },
  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);
  },
  onLoad(option){
    let from = option.from;
    let usertype = wx.getStorageSync('usertype');
    console.log("index onload: ", from, usertype)
    this.setData({
      beforeClose: this.data.beforeClose.bind(this),
  })
    if(from === "navbar"){
      //切换身份
    }else{
      if(usertype == 2 || usertype == 3){
        this.guideTo(usertype)
      }
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        tabBarShow:false,  //true/false
      })
    }
    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: () => {}
    })
    this.setData({ loading: false });
    // let isEntry=wx.getStorageSync('hasEntry');
    // console.log("onshow:"+isEntry)
    // if(!isEntry){
    //   this.setData({
    //     showEntryTip:true
    //    });
     
    //  }else{
    //   this.setData({
    //     showEntryTip:false
    //    });
    //  }
  },
  
  goCheck(e){
    console.log(e);
    let usertype = e.currentTarget.dataset.usertype;
    let url = e.currentTarget.dataset.url;
    wx.setStorageSync('usertype', usertype);
    wx.setStorageSync('next_url', url);
    this.guideTo(usertype);
  },
  guideTo(usertype){
    let url;
    if(usertype == 2){
      if(!wx.$TUIKit){
        app.imInit();
      }
      url = "/Pages/yishi/index/index";
    }else{
      app.imLogout();
      url = "/Pages/yaoshi/home/home";
    }
    let token = wx.getStorageSync('AUTH_TOKEN_'+usertype);
    console.log(token);
    console.log(url);
    if(token){
      wx.switchTab({
        url: url
      })
    }else{
      app.go(app.globalData.login_url);
    }
    //test git
  }
})
