//index.js
//获取应用实例

const app = getApp()
Page({
  data: {
    status: '',
    buildid: '',
    childrenid: '',
    doctorid: '',
    hospid: '',
    activeid: '',
    goodtail_id: '',
    hospital_id:'',
    hospital_address_id:''
  },


  onLoad: function(options) {

    console.log('woshibuildid',options)
    
    this.setData({
      token: wx.getStorageSync('token'),
      status: options.status,
    })

    if (JSON.stringify(options) != '{}') {
      console.log('9999887766555454',options.buildid)
      if (options.buildid!=undefined){
        this.setData({
          buildid: options.buildid,
        })
      }
      if(options.hospital_id!=undefined){
        this.setData({
          hospital_id:options.hospital_id,
          hospital_address_id:options.hospital_address_id
        })
      }
      if (options.childrenid!=undefined){
        this.setData({
          childrenid: options.childrenid,
        })
      }
      if (options.doctorid != undefined) {
        this.setData({
          doctorid: options.doctorid,
        })
      }
      if (options.hospid != undefined) {
        this.setData({
          hospid: options.hospid,
        })
      }
      if (options.activeid != undefined) {
        this.setData({
          activeid: options.activeid,
        })
      }
      if (options.goodtail_id != undefined) {
        this.setData({
          goodtail_id: options.goodtail_id,
        })
      }


      this.setData({
        status: options.status,
      })
    }

    console.log(this.data.status)
  },


  // 登录
  start(e) {
  
    let that = this;

    console.log('666666',that.data.status)
    app.globalData.userInfo = e.detail.userInfo
    wx.login({
      success: (res) => {
        console.log(res)
        let url = '/portal/common/getToken';
        let params = {
          code: res.code,
          user_nickname: e.detail.userInfo.nickName,
          avatar: e.detail.userInfo.avatarUrl, //分享进入的需要此参数
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv

        }
        app.post(url, params).then((res) => {
          console.log('99999')
          wx.setStorageSync('token', res.token);
          if (that.data.status != undefined) {
            console.log('wohsiewuuiweurui',that.data.status)
            if (that.data.status == 1) {
              let buildid = that.data.buildid
              wx.navigateTo({
                url: "/pages/home/buildtextdetail/buildtextdetail?buildid=" + buildid,
              })
            } else if (that.data.status == 2) {
              let childrenid = that.data.childrenid
              wx.navigateTo({
                url: "/pages/home/childbirthnotedetail/childbirthnotedetail?childrenid=" + childrenid,
              })
            } else if (that.data.status == 3) {
              let doctorid = that.data.doctorid
              wx.navigateTo({
                url: "/pages/home/commentdoctordetail/commentdoctordetail?doctorid=" + doctorid,
              })
            } else if (that.data.status == 4) {
              let hospid = that.data.hospid
              wx.navigateTo({
                url: "/pages/home/commenthospitaldetail/commenthospitaldetail?hospid=" + hospid,
              })
            } else if (that.data.status == 5) {
              console.log('5', that.data.status)
              let activeid = that.data.activeid
              wx.navigateTo({
                url: "/pages/cityWide/monthDetail/monthDetail?activeid=" + activeid,
              })
            } else if (that.data.status == 6) {
              let goodtail_id = that.data.goodtail_id
              wx.navigateTo({
                url: "/pages/home/goodtail/goodtail_id?goodtail_id=" + goodtail_id,
              })
            }else if(that.data.status==7){
              wx.navigateTo({
                url: "/pages/home/hometail/hometail?hospital_id=" + that.data.hospital_id+'&hospital_address_id='+that.data.hospital_address_id,
              })
            } else{
              wx.switchTab({
                url: '/pages/homepage/homepage',
              })
            }
          } else {
            wx.switchTab({
              url: "/pages/homepage/homepage",
            })
          }

        }).catch((errMsg) => {
          console.log(errMsg)
        })

      }
    });
  },

  // enter(e){

  //   wx.getSetting({
  //     success(res) {
  //       console.log(res)
  //       console.log(res.authSetting["scope.userLocation"])


  //         console.log(8080)

  //         if (res.authSetting['scope.userLocation'] == undefined) {
  //           console.log(9090)
  //           wx.openSetting({
  //           })
  //         } else {
  //           console.log(666)
  //           //打开选择地址
  //           wx.getLocation({

  //             type: 'wgs84',
  //             success: function (res) {
  //               console.log(res)
  //               wx.switchTab({
  //                 url: "/pages/homepage/homepage",
  //               })
  //             }
  //           })
  //         }



  //     },
  //     fail(res) {
  //       console.log('调用失败')
  //     }
  //   })

  // },
  navigate() {
    let that = this;
    console.log('99999',that.data.status)

    if (that.data.status == 1) {
      let buildid = that.data.buildid
      wx.navigateTo({
        url: "/pages/home/buildtextdetail/buildtextdetail?buildid=" + buildid,
      })
    } else if (that.data.status == 2) {
      let childrenid = that.data.childrenid
      wx.navigateTo({
        url: "/pages/home/childbirthnotedetail/childbirthnotedetail?childrenid=" + childrenid,
      })
    } else if (that.data.status == 3) {
      let doctorid = that.data.doctorid
      wx.navigateTo({
        url: "/pages/home/commentdoctordetail/commentdoctordetail?doctorid=" + doctorid,
      })
    } else if (that.data.status == 4) {
      let hospid = that.data.hospid
      wx.navigateTo({
        url: "/pages/home/commenthospitaldetail/commenthospitaldetail?hospid=" + hospid,
      })
    } else if (that.data.status == 5) {
      console.log('5', that.data.status)
      let activeid = that.data.activeid
      wx.navigateTo({
        url: "/pages/cityWide/monthDetail/monthDetail?activeid=" + activeid,
      })
    } else if (that.data.status == 6) {
      console.log(2348982934889342)
      let goodtail_id = that.data.goodtail_id;
      console.log('23894823489',goodtail_id)
      wx.navigateTo({
        url: "/pages/home/goodtail/goodtail?goodtail_id="+ goodtail_id,
      })
    } else {
      wx.switchTab({
        url: "/pages/homepage/homepage",
      })
    }

  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  // 已登录直接跳
  redirect() {
    wx.navigateTo({
      url: "/pages/homepage/homepage",
    })

  },
  getUserInfo: function(e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  // enter:function(){
  //   console.log(11)
  //   wx.switchTab({
  //     url: '../homepage/homepage',
  //   })
  // }
})