// pages/school/school.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search: '',
    isWechat:0,
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData(options)
    console.log(options)
    const {status}=options;
    const parentVipList = app.globalData.parentVipList || wx.getStorageSync('parentVipList');
    const parentVipLiIdx = app.globalData.parentVipLiIdx || wx.getStorageSync('parentVipLiIdx');
    this.setData({parentVipList, parentVipLiIdx});
    this.getLocation()
    setTimeout(() => {
      wx.getSetting({
        success: res => {
          if (!res.authSetting['scope.userLocation']) {
            this.openLocationConfirm()
          }
        }
      })
    }, 1000)
  },

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

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  /**
   * 路由跳转
   */
  redirectCtl: app.redirectCtl,
  navigateCtl: app.navigateCtl,

  /**
   * 输入学校
   */
  inputCtl: function (e) {
    const { value } = e.detail
    this.setData({
      search: value
    })
    this.getSchoolList()
  },

  /**
   * 获取定位经纬度
   */
  getLocation: function () {
    wx.showLoading({
      title: '定位中'
    })
    wx.getLocation({
      type: 'wgs84',
      success: res => {
        console.log(res)
        this.setData({
          location: {
            longitude: res.longitude,  // '113.36403' ||
            latitude: res.latitude // '23.14203' ||
          }
        })
        this.getSchoolList({
          longitude: res.longitude,
          latitude: res.latitude
        })
      },
      fail: res => {
        wx.showToast({
          title: '定位失败',
          icon: 'none'
        })
      },
      complete: res => {
        wx.hideLoading()
      }
    })
  },

  /**
   * 再次弹起弹出框要求重新获取地位 
   */
  openLocationConfirm: function () {
    wx.showModal({
      content: '检测到您没打开定位权限,是否去设置打开?',
      confirmText: "确认",
      cancelText: "取消",
      success: res => {
        if (res.confirm) {
          wx.openSetting({
            success: res => {
              if (res.authSetting['scope.userLocation']) {
                this.getLocation()
              } else {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        } else {
          wx.navigateBack({
            delta: 1
          })
        }
      }
    })
  },

  /**
   * 获取学校列表 
   */
  getSchoolList: function (e) {
    const postData = { fn: this.getSchoolList, param: { ...arguments } }
    const { location, search, libId, schoolState, schoolList, isAddChild} = this.data
    const { longitude, latitude } = location || e
    if (isAddChild==1){
      var data = { longitude, latitude, search, libId: !schoolList ? '' : libId }
    }else{
      var data = { longitude, latitude, search, libId: schoolState == 1 ? '' : libId }
    }
    wx.showLoading({
      title: '加载中...',
    })
    app.post('/api/parents/parents/V2/getSchoolList', data, 0, postData).then(res => {
      wx.hideLoading()
      const {list}=res.data;
      // if(list.length==0){
      //   this.saveSchool();
      // }
      this.setData({ list})
    })
  },
  /**
   * 保存学校接口
   */
  saveSchool:function(){
    const postData = { fn: this.saveSchool, param: { ...arguments } }
    const {search } = this.data;
    app.post('/api/parents/parents/V2/saveSchoolCopy', { name:search }, 0, postData).then(res => {

    })
  }
})