var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    randomName: true,
    showPhone: true,
    gotCode: false,
    restTime: 120,
    timer: '',
    indicatorDots: true,
    autoplay: true,
    token: '',
    news: {
      newsUrl: app.newsUrl,
      bannerList: [],
      newsList: []
    },
    venue: {
      venueUrl: app.venueUrl,
      venueList: []
    },
    activity: {
      activityUrl: app.activityUrl,
      activityList: []
    },
    live: {
      liveUrl: app.liveUrl,
      liveList: []
    },
    usercenterUrl: app.usercenterUrl,
    isConfirm: false
  },
  doLogin: function () {
    var self = this;
    wx.login({
      success(res) {
        if (res.code) {
          app.restAjax.post(app.restAjax.path('{url}/wxminiapp/auth/login', [app.loginUrl]), {
            jsCode: res.code
          }, null, function (code, data) {
            var tokenArray = data.data.split('_');
            var token = tokenArray[0];
            var isRandomUsername = tokenArray[1];
            self.setData({
              token: token
            })
            wx.setStorageSync('token', token);
            wx.setStorageSync('isRandomUsername', isRandomUsername)
          }, function (code, data) {
            app.dialog.msg(data.msg);
          });
        } else {
          app.dialog.msg('登录失败!'+ res.errMsg);
        }
      }
    })
  },
  doGetBanner: function() {
    var self = this;
    app.restAjax.get(app.restAjax.path('{newsUrl}/app/newscontent/listpagenewscontentrelease', [self.data.news.newsUrl]), {
      newsDirectoriesId: 'eab0ca3c-d35d-4c02-9397-ace2bdd10fc1',
      page: 1,
      rows: 5
    }, null, function(code, data) {
      self.setData({
        'news.bannerList': data.rows
      })
    }, function(code, data) {
      app.dialog.msg(data.msg);
    });
  },
  doGetVenueList: function() {
    var self = this;
    app.restAjax.get(app.restAjax.path('{venueUrl}/app/venuesinfo/listpagevenuesinforelease', [self.data.venue.venueUrl]), {
      page: 1,
      rows: 3
    }, null, function(code, data) {
      for(var i = 0, item; item = data.rows[i++];) {
        item.venuePanoramaArray = item.venuePanorama.split(',');
      }
      self.setData({
        'venue.venueList': data.rows
      });
    }, function(code, data) {
      app.dialog.msg(data.msg);
    });
  },
  doGetActivityList: function() {
    var self = this;
    app.restAjax.get(app.restAjax.path('{activityUrl}/app/activitylibrary/listpageactivitylibraryrelease', [self.data.activity.activityUrl]), {
      page: 1,
      rows: 3
    }, null, function(code, data) {
      for(var i = 0, item; item = data.rows[i++];) {
        item.activityImageArray = item.activityImage.split(',');
      }
      self.setData({
        'activity.activityList': data.rows
      });
    }, function(code, data) {
      app.dialog.msg(data.msg);
    });
  },
  doGetNewsList: function() {
    var self = this;
    app.restAjax.get(app.restAjax.path('{newsUrl}/app/newscontent/listpagenewscontentrelease', [self.data.news.newsUrl]), {
      newsDirectoriesParentId: 'f497904b-7727-4832-891c-604c36ae4167',
      page: 1,
      rows: 5
    }, null, function(code, data) {
      self.setData({
        'news.newsList': data.rows
      })
    }, function(code, data) {
      app.dialog.msg(data.msg);
    });
  },
  doGetLiveList: function() {
    var self = this;
    app.restAjax.get(app.restAjax.path('{liveUrl}/app/liveplan/listpageliveplanrelease', [self.data.live.liveUrl]), {
      page: 1,
      rows: 4
    }, null, function(code, data) {
      self.setData({
        'live.liveList': data.rows
      })
    }, function(code, data) {
      app.dialog.msg(data.msg);
    });
  },
  goNewsDetail: function (event) {
    wx.navigateTo({
      url: '../newsDetail/newsDetail?templateRecordUrl='+ event.currentTarget.dataset.templateRecordUrl,
    })
  },
  goNewsList: function(event) {
    wx.navigateTo({
      url: '../newsList/newsList?type='+ event.currentTarget.dataset.type,
    })
  },
  goVolunteer: function () {
    wx.navigateTo({
      url: '../volunteer/volunteer',
    })
  },
  goBroadcast: function () {
    wx.navigateTo({
      url: '../broadcast/broadcast',
    })
  },
  goVenue: function () {
    wx.switchTab({
      url: '../venue/venue',
    })
  },
  goVenueDetail: function(event) {
    wx.navigateTo({
      url: '../venueDetail/venueDetail?venuesInfoId=' + event.currentTarget.dataset.venuesInfoId,
    })
  },
  goBroadcastDetail: function (event) {
    wx.navigateTo({
      url: '../broadcastDetail/broadcastDetail?livePlanId='+ event.currentTarget.dataset.livePlanId,
    })
  },
  goActivity: function () {
    wx.switchTab({
      url: '../activity/activity',
    })
  },
  goActivityDetail: function(event) {
    wx.navigateTo({
      url: '../activityDetail/activityDetail?id=' + event.currentTarget.dataset.activityLibraryId
    })
  },
  goHeritage: function () {
    wx.switchTab({
      url: '../heritage/heritage',
    })
  },
  goCultureDetail: function(event) {
    wx.navigateTo({
      url: '../cultureDetail/cultureDetail?libraryId='+ event.currentTarget.dataset.libraryId,
    })
  },
  // 获取用户信息
  getUserInfo: function () {
    var self = this
    app.restAjax.get(app.restAjax.path('{usercenterUrl}/app/user/getappuser', [app.usercenterUrl]), {}, {
      headers: {
        token: self.data.token
      }
    }, function (code, data) {
      self.setData({
        userInfo: data
      })
    }, function (code, data) {
      app.dialog.msg(data.msg);
    })
  },
  goTrain: function () {
    wx.navigateTo({
      url: '../train/train',
    })
  },
  goTravel: function () {
    wx.navigateTo({
      url: '../travel/travel',
    })
  },
  goResource: function () {
    wx.navigateTo({
      url: '../resource/resource',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    self.doLogin();
    self.doGetBanner();
    self.doGetNewsList();
    self.doGetLiveList();
    self.doGetVenueList();
    self.doGetActivityList();
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})