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

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

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

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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    //获取用户当前的经纬度信息并调用接口判断用户身份
    var config = wx.getStorageSync('config');
    var location = wx.getStorageSync('location');
    var latitude = location.latitude;
    var longitude = location.longitude;
    if(config.unionId!==undefined){
      that.getUserRole(config.unionId, latitude, longitude);
    }else{
      setTimeout(function(){
        config = wx.getStorageSync('config');
        that.getUserRole(config.unionId, latitude, longitude);
      },1000);
    }
    
  },
  getUserRole(unionId, latitude, longitude){
    var that = this;
    var location = wx.getStorageSync('location');
    var config = wx.getStorageSync('config');
    wx.request({
      url: app.url + 'ajax_get_user_role.php',
      data: {
        unionId: unionId,
        latitude: latitude,
        longitude: longitude
      },
      header: { 'content-type': 'application/json' },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        console.log(res)
        if (res.statusCode == 200) {
          //保存用户当前的省市区县信息
          location.province = res.data.result.province
          location.city = res.data.result.city
          location.district = res.data.result.district
          wx.setStorage({
            key: 'location',
            data: location,
          })
          //保存用户名称信息
          config.name = res.data.result.name
          wx.setStorage({
            key: 'config',
            data: config,
          })

          //未注册用户的话。跳转到社会人士注册页面
          if (res.data.status == "REGIST") {
            //页面跳转
            wx.redirectTo({
              url: '../socialUserRegister/socialUserRegister',
            });
          }
          that.setData({
            roleTitle: res.data.result.name,
            role: res.data.result.role,
            childAge: res.data.result.childAge
          })
          //菜单控制[亲子活动]
          if (res.data.result.role == 1 || res.data.result.childAge == 1) {
            that.setData({
              showFamily: true
            })
          }
          //菜单控制[社会实践]
          if (res.data.result.role == 2 || res.data.result.childAge == 2) {
            that.setData({
              showSocial: true
            })
          }
          //菜单控制[消息]
          if (res.data.result.role == 4) {
            that.setData({
              showNews: false
            })
          }
          that.getIndexData();
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '数据获取异常',
        })
      },
      complete: function (res) { },
    })
  },

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

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

  },

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

  },

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

  },

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

  },

  //获取首页数据
  getIndexData(e) {
    var that = this;
    var config = wx.getStorageSync('config');
    var location = wx.getStorageSync('location');
    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) { },
    })
  },

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

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

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

  //查看更多志愿者活动
  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: '公益课堂'
    })
  },
  // 跳到会员
  memberChange(){
    wx.navigateTo({
      url: '../memberChange/memberChange',
    })
  }
})