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

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

    unreadMessageCount: 0, //未读消息数量
    showFamily: false,
    showSocial: false,
    showNews: true,

    volunteerEventList: [],
    familyEventList: [],
    mediaList: [],
    newsList: [],
    isMember: false,
    isvolunteerEventList: false,
    ismediaList: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  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 (!latitude || !longitude) {
      wx.getLocation({
        type: 'wgs84',
        success: function (res) {
          let location = {
            latitude: res.latitude,
            longitude: res.longitude
          };
          //本地保存地理位置数据
          wx.setStorage({
            key: 'location',
            data: location,
          })
          latitude = location.latitude
          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);
          }
        }
      })
    } else {
      //获取首页数据
      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) {
          var role = res.data.result.role;
          /**
          if ((role == 4 || role == 5 || role == 6) && res.data.result.childAge == 0) {
            wx.navigateTo({
              url: '/pages/ageChange/ageChange',
            })
            return;
          }
           */
          //保存用户当前的省市区县信息
          location.province = res.data.result.province
          location.city = res.data.result.city
          location.district = res.data.result.district
          wx.setStorageSync('isMember', res.data.result.isMember);
          that.setData({
            isMember: res.data.result.isMember
          })
          app.globalData.role = role;
          wx.setStorage({
            key: 'location',
            data: location,
          })
          //保存用户名称信息
          app.globalData.name = res.data.result.name
          config.name = res.data.result.name
          wx.setStorage({
            key: 'config',
            data: config,
          })

          //未注册用户的话。跳转到社会人士注册页面
          if (res.data.status == "REGIST") {
            //页面跳转
            wx.redirectTo({
              url: '../socialUserRegister/socialUserRegister',
            });
            return;
          }
          that.setData({
            roleTitle: res.data.result.name,
            role: res.data.result.role,
            childAge: res.data.result.childAge
          })
          if (res.data.result.headerImg) {
            that.setData({
              headerImg: res.data.result.headerImg,
            })
          }
          //菜单控制[亲子活动]
          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,
          newsList: res.data.result.newsList,
          isvolunteerEventList: res.data.result.volunteerEventList.length > 0 ? true : false,
          ismediaList: res.data.result.mediaList.length > 0 ? true : false,
          unreadMessageCount: res.data.result.unreadMessageCount
        })
        //是否显示志愿者活动
        if (res.data.result.volunteerEventList && res.data.result.volunteerEventList.length > 0) {
          that.setData({
            isvolunteerEventList: true,
          })
        }
      },
      fail: function (res) {},
      complete: function (res) {},
    })
  },

  //志愿要闻详情
  vNewsDetail(e) {
    let newsId = e.currentTarget.dataset['index']
    wx.navigateTo({
      url: '../vNewsDetail/vNewsDetail?newsId=' + newsId,
    });
  },

  //志愿者活动详情
  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',
    })
  },

  //在线答疑
  faqEvent() {
    wx.navigateTo({
      url: '../faq/faq',
    })
  },

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

  //跳转到志愿要闻
  volunteerNews() {
    wx.navigateTo({
      url: '../volunteerNews/volunteerNews',
    });
    wx.setNavigationBarTitle({
      title: '志愿要闻'
    })
  },

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