// pages/article/article.js
var app = getApp();

Page({
  /**
	 * 页面的初始数据
	 */
  data: {
    kFootshow: true,
    active: "0",
    id: "",
    banner: "",
    title: "",
    datas: [],
    categories: [],
    loading: false,
    allLoaded: false,
    goload: true,
    isLoadMore: true,
    page: 1,
    total_page: 0,
    show1: false,
    noNotice: false,
    article_pay:'',

    searchText:''
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.id) {
      this.setData({
        id: options.id,
      });
    }
    
    this.initData();
    //获取数据
    // this.getNetData();
  },
  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {
    //获取数据
    this.getNetData();

  },

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

  },

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

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {
    this.initData();
    //获取数据
    this.getNetData();
  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    // 加载更多
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

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

  },
  onShareTimeline(){},
  tabrshowbtn(e) {
    let bol = e.detail;
    if (bol) {
      this.setData({
        kFootshow: false
      });
    }
  },
  initData() {
    this.setData({
      page: 1,
      total_page: 0,
      goload: true,
      loading: true,
      allLoaded: false,
      isLoadMore: true,
      datas: [],
    });
  },
  handSearch(){
    this.initData();
    //获取数据
    this.getNetData();
  },
  //获取数据
  getNetData() {
    let that = this;
    let json = {
      category_id: this.data.id,
      page: 1,
      keyword: this.data.searchText
    };
    let urlStr = app.getNetAddresss("plugin.article.api.article.get-articles");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            noNotice: false,
            total_page: res.data.articles.last_page,
            article_pay:res.data.article_pay
          });
          if (!that.data.total_page) {
            that.setData({
              total_page: 0
            });
          }
          if (res.data.categories.length <= 0) {
            that.setData({
              noNotice: true
            });
          }
          that.setData({
            title: res.data.title,
            datas: res.data.articles.data,
            categories: res.data.categories
          });
          console.log(that.data.datas);
          if(that.data.id){
            that.data.categories.forEach((item,index,key)=>{
              if(item.id==that.data.id){
                that.setData({
                  active:String(index+1)
                });
              }
            });
          }
          wx.setNavigationBarTitle({
            title: res.data.title
          });
        } else {
          that.setData({
            noNotice: true
          });
        }
        wx.stopPullDownRefresh();
      },
      fail: function(res) {
        console.log(res);
        wx.stopPullDownRefresh();
      }
    });

  },
  gofufei(){
    wx.navigateTo({
      url: '/packageD/article/PayarticleList/PayarticleList'
    });
  },
  toNoticeInfo(e) {
    let item = e.currentTarget.dataset.item;
    console.log(item);
    if (item.has_one_article_pay){
      if (item.has_one_record) {
        wx.navigateTo({
          url: '/packageA/member/article/articleContent/articleContent?article_id=' + item.id
        });
      }else{
        wx.navigateTo({
          url: '/packageD/article/articleList/articleList?article_id=' + item.id
        });
      }
    }else{
      wx.navigateTo({
        url: '/packageA/member/article/articleContent/articleContent?article_id=' + item.id
      });
    }
  },
  onClick(e) {
    let index = e.detail.index;
    this.initData();
    if (index === 0) {
      this.setData({
        id: ''
      });
    } else {
      this.setData({
        id: this.data.categories[index - 1].id
      });
    }
    this.getNetData();
  },
  getMoreData() {
    var that = this;
    if (this.data.page === this.data.total_page) {
      return;
    }
    if (this.data.page >= this.data.total_page) {
      that.setData({
        loading: true,
        allLoaded: true
      });
      return;
    } else {
      that.setData({
        page: this.data.page + 1
      });
      let json = {
        category_id: this.data.id,
        page: this.data.page,
        keyword: this.data.searchText
      };
      let urlStr = app.getNetAddresss("plugin.article.api.article.get-articles");
      app._getNetWork({
        url: urlStr,
        data: json,
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              total_page: res.data.articles.last_page,
              loading: false,
              allLoaded: false,
              datas: that.data.datas.concat(res.data.articles.data)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              loading: true,
              allLoaded: true,
              isLoadMore: false
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  }
});
