// pages/Information/Information.js
const app = getApp(),
  { hostname, business_id } = app.globalData


Page({

  /**
   * 页面的初始数据
   */
  data: {
    InformationUrls:'',
    page: 1,
    InformationList:[],
    InformationRE:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that =this;
    this.getInformationUrls(that);
    this.getInformationList(that);
    this.getInformationRE(that);
  },

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

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

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

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

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

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

  /**
    * 用户点击右上角分享
    */
  onShareAppMessage: function (res) {
    var userid = wx.getStorageSync('openId')
    // var userid = 1

    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: app.globalData.share_title,
      path: '/pages/index/index?u_openid=' + userid,
      success: function (res) {
        var pages = getCurrentPages()    //获取加载的页面
        var currentPage = pages[pages.length - 1]    //获取当前页面的对象
        //点击分享传进userid 截取userid 从onload拿到
        var url = currentPage.route + '?u_openid=' + userid;  //当前页面url
        console.log(url, '/pages/index/index?u_openid=' + userid);
      },
      fail: function (res) {
        // 转发失败
      },
    }
  },
  //获得资讯首页图片
  getInformationUrls: function (that) {
    wx.request({
      url: app.globalData.hostname + 'Zixun/index',
      data: { business_id},
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'POST',
      dataType: '',
      success: function (res) {
        // console.log(res, "资讯图");
        if (res.statusCode == 200) {
          var InformationUrls = '';
          var InformationUrlsData = res.data.data;
          if (InformationUrlsData) {
            that.setData({
              InformationUrls: InformationUrlsData
            })
          }
          // console.log(imgUrls,11);
        } else {
          that.setData({
            InformationUrls: []
          })
        }
      },
      fail: function (res) { },
      complete: function (res) {},
    })
  },
  // 获取资讯列表
  getInfomationList: function(){
    var page = this.data.page;
    wx.request({
      url: app.globaData.hostname+'Zixun/get_list',
      data:{business_id,page},
      header:{'content-type':'application/x-www-form-urlencoded'},
      method:'POST',
      dataType:'',
      success:function(){
        if(res.statusCode == 200){
          if(res.statusCode == 200){
            var InformationList = [];
            var InformationListData = res.data.data;
            if(InformationListData.length != undefined){
              for(var i=0;i<InformationListData.length;i++){
                informationList.push(informationListData[i]);
              }
            }
            that.setData({
              informationList:informationList
            })  
          }
        }
      }
    })
  },
  //获取资讯列表
  getInformationList: function (that) {
    // console.log(this.data.page)
    var page = this.data.page;
   
    wx.request({
      url: app.globalData.hostname + 'Zixun/get_list',
      data: { business_id,page},
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'POST',
      dataType: '',
      success: function (res) {
        // console.log(res, "资讯列表");
        if (res.statusCode == 200) {
          var InformationList = [];
          var InformationListData = res.data.data;
          // console.log(InformationListData);
          if (InformationListData.length != undefined) {
            for (var i = 0; i < InformationListData.length; i++) {
              InformationList.push(InformationListData[i]);
            }
          }
          // console.log(InformationList,11);
          that.setData({
            InformationList: InformationList
          })
        } else {
          that.setData({
            InformationList: []
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  //获取资讯推荐列表
  getInformationRE: function (that) {
    wx.request({
      url: app.globalData.hostname + 'Zixun/hot_index',
      data: { business_id},
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'POST',
      dataType: '',
      success: function (res) {
        // console.log(res, "资讯推荐列表");
        if (res.statusCode == 200) {
          var InformationRE = [];
          var InformationREData = res.data.data;
          // console.log(InformationREData);
          if (InformationREData.length != undefined) {
            for (var i = 0; i < InformationREData.length; i++) {
              InformationRE.push(InformationREData[i]);
            }
          }
          // console.log(InformationRE,11);
          that.setData({
            InformationRE: InformationRE
          })
        } else {
          that.setData({
            InformationRE: []
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  goInformationListDetails(e){
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: './information_article/information_article?id='+e.currentTarget.dataset.id,
    })
  },
  seeMore(e) {
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: './information_seemore/information_seemore?id=' + e.currentTarget.dataset.id,
    })
  }
})