// packageI/newMedia/newMediaGuide/newMediaGuide.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabsIndex:0,

    ordinaryIndex:0,
    ordinaryArray:[
      {name:'图文',value:1},
      {name:'视频',value:4},
      {name:'音频',value:3},
      {name:'文章',value:2},
      {name:'直播'},
    ],

    searchText:'',

    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数
    listData: [],
    networkLoading: false,

    is_release:false


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getListData();
  },

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

  },

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

  },

  setTabsIndex(evt){
    let index = evt.currentTarget.dataset.index;
    let name = evt.currentTarget.dataset.name;
    if(index == this.data[name]) return;
    this.setData({
      [name]:index
    });
    this.getListData();
  },

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

  },

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

  },

  searchConfirm(evt){

    let search = evt.detail;
    if(this.data.searchText == search) return;
    this.setData({searchText:search});
    let {tabsIndex,ordinaryIndex} = this.data;

    if(tabsIndex==0 && ordinaryIndex==4){
      this.selectComponent(".livestreaming").swichTabTItem();
      return ;
    }

    this.getListData();

    
  },

  
  initPage() {
    this.setData({
      listData: [],
      networkLoading: false
    });
    this.data.page = 1;
    this.data.total_page = 0;
    this.data.isLoadMore = true;
  },

  getListData(){
    let {tabsIndex,ordinaryIndex,ordinaryArray,searchText} = this.data;
    if(tabsIndex==0 && ordinaryIndex==4){
      return ;
    }
    this.initPage();
    let json={};
    if(tabsIndex==0){
      json.advertisingt_type=1;
      json.genre = ordinaryArray[ordinaryIndex].value;
    }else if(tabsIndex==1){
      json.advertisingt_type=2;
    }

    json.title = searchText;

    let urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.media-advertisingt.get-list");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.setData({
          isLoadMore: true,
          total_page: res.data.list.last_page,
          listData: res.data.list.data,
          is_release:res.data.is_release,
          networkLoading: true,
          
        });
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }

      }
    });


  },


  //获取更多数据
  getMoreData() {
    let urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.media-advertisingt.get-list");
    // 防止多次请求分页数据
    this.data.isLoadMore = false;
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page++;
      let {tabsIndex,ordinaryIndex,ordinaryArray,searchText} = this.data;
      let json = {
        page: this.data.page
      };
      if(tabsIndex==0){
        json.advertisingt_type=1;
        json.genre = ordinaryArray[ordinaryIndex].value;
      }else if(tabsIndex==1){
        json.advertisingt_type=2;
      }
  
      json.title = searchText;


      app._getNetWork({
        url: urlStr,
        data:json,
        success: (resdata) => {
          var res = resdata.data;

          if (res.result == 1) {
            this.data.isLoadMore = true;
            var myData = res.data.list.data;
            this.setData({
              listData: this.data.listData.concat(myData)
            });
          } else {
            this.data.isLoadMore = false;
            this.data.page--;
          }
        }
      });
    }
  },

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

  },

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

    if(this.data.tabsIndex==0 && this.data.ordinaryIndex==4){
      this.selectComponent(".livestreaming")._getMoreData();
      return;
    }

    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多数据');
    }

  },

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

  },




});




