// pages/index/index.js
import Dialog from '../../dist/dialog/dialog';
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    isLogin:false,
    role:0,
    chileAge:0,
    roleTitle:'',

    showFamily: false,
    showSocial:false,
    showNews: true,

    volunteerEventList:[],
    familyEventList:[],
    mediaList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    if (this.data.role == 4){
      this.setData({
        roleTitle:'教育局领导'
      })
    } else {
      this.setData({
        roleTitle:'志愿者'
      })
    };
    var config = wx.getStorageSync('config');
    //获取用户当前的经纬度信息并调用接口判断用户身份
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        // console.log(res);
        var latitude = res.latitude;
        var longitude = res.longitude;
        wx: wx.request({
          url: app.url + 'ajax_get_user_role.php',
          data: {
            unionId: config.unionId,
            latitude: latitude,
            longitude: longitude
          },
          header: { 'content-type': 'application/json' },
          method: 'GET',
          dataType: 'json',
          success: function (res) {
            console.log(res)
            if(res.statusCode==200){
              //未注册用户的话。跳转到社会人士注册页面
              if (res.data.status =="REGIST") {
                //页面跳转
                wx.navigateTo({
                  url: '../socialUserRegister/socialUserRegister',
                });
              }
              that.setData({
                role:res.data.result.role,
                chileAge: res.data.result.chileAge
              })
              //菜单控制[亲子活动]
              if (res.data.result.role == 1 || res.data.result.chileAge==1) {
                that.setData({
                  showFamily: true
                })
              }
              //菜单控制[社会实践]
              if (res.data.result.role == 2 || res.data.result.chileAge == 2) {
                that.setData({
                  showSocial: true
                })
              }
              //菜单控制[消息]
              if (res.data.result.role == 4) {
                that.setData({
                  showNews: false
                })
              }
            }
          },
          fail: function (res) { },
          complete: function (res) { },
        })
      }
    })
    var location = wx.getStorageSync('location');
    wx: wx.request({
      url: app.url + 'ajax_get_top_dat.php',
      data: {
        unionId: config.unionId,
        province: location.province,
        city: location.city,
        district: location.district
      },
      header: { 'content-type': 'application/json' },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        //设定页面数据
        that.setData({
          volunteerEventList: res.data.result.volunteerEventList,
          familyEventList: res.data.result.familyEventList,
          socialEventList: res.data.result.socialEventList,
          mediaList: res.data.result.mediaList
        })
        console.log(res)
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

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

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  //志愿者活动详情
  volunteersEventDetail(e){
    let volunteersEventId = e.currentTarget.dataset['index']
    wx.navigateTo({
      url: '../volunteersEventDetail/volunteersEventDetail?volunteersEventId' + volunteersEventId,
    });
    wx.setNavigationBarTitle({
      title: '活动详情'
    })
  },

  //亲子活动详情
  familyEventDetail(e) {
    let eventId = e.currentTarget.dataset['index']
    wx.navigateTo({
      url: '../familyEventDetail/familyEventDetail?eventId' + eventId,
    });
    wx.setNavigationBarTitle({
      title: '活动详情'
    })
  },

  //社会实践详情
  socialEventDetail(e) {
    let eventId = e.currentTarget.dataset['index']
    wx.navigateTo({
      url: '../socialEventDetail/socialEventDetail?eventId' + eventId,
    });
    wx.setNavigationBarTitle({
      title: '活动详情'
    })
  },

  checksession(){
    if(!this.data.isLogin){
      Dialog.confirm({
        title: '标题',
        message: '请先登录！'
      }).then(() => {
        this.setData({
          isLogin: true
        })
        wx.redirectTo({
          url: '../registered/registered',
        })
      }).catch(() => {
        // on cancel
      });
      
    }
  },

  //查看更多志愿者活动
  moreVolunteers(){
    wx.navigateTo({
      url: '../volunteers/volunteers',
    });
    wx.setNavigationBarTitle({
      title: ''
    })
  },

  //获取用户授权
  authorize(){
    wx.getUserInfo({
      success: function (res) {
        var userInfo = res.userInfo
        console.log("获取登录用户的所有信息")
        console.log(res.userInfo)
      }
    })
  },

  //跳转区域管理模块首页
  area() {
    wx.navigateTo({
      url: '../area/area',
    });
    wx.setNavigationBarTitle({
      title: '区域管理'
    })
  },

  //跳转到志愿者活动模块
  volunteers() {
    wx.navigateTo({
      url: '../volunteers/volunteers',
    })
  },

  //消息通知模块首页
  news() {
    wx: wx.navigateTo({
      url: '../myNews/myNews',
    })
    wx.setNavigationBarTitle({
      title: '消息通知'
    })
  },

  //跳转到社会实践模块首页
  socialEvent(){
    wx.navigateTo({
      url: '../socialEvent/socialEvent',
    })
  },

  //跳转到亲子活动模块首页
  familyEvent(){
    wx.navigateTo({
      url: '../familyEvent/familyEvent',
    })
  },

  //跳转到公益课程
  mediaCourse() {
    wx.navigateTo({
      url: '../courseIndex/courseIndex',
    });
    wx.setNavigationBarTitle({
      title: '公益课堂'
    })
  },

  //跳转到公益课程详情
  classDetails(e) {
    let medidaId = e.currentTarget.dataset['index']
    wx.navigateTo({
      url: '../classDetails/classDetails?medidaId=' + medidaId,
    });
    wx.setNavigationBarTitle({
      title: '公益课堂'
    })
  },

})