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

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

    //我看过的tab切换
    seeTabIndex:0,
    seeTabArray:[], //后台自定义

    //直播记录切换
    liveRecordIndex:0,
    liveRecordArray:[],  //后台自定义


    //我的发布下的tab切换
    releaseTabIndex:0,  
    releaseTabArray:[
      {name:"广告审核"},
      {name:"普通区"},
      {name:"广告专区"}
    ],
    
    //广告审核
    releaseExamineIndex:0,
    releaseExamineArray:[
      {name:'全部'},
      {name:'待审核'},
      {name:'审核通过'},
      {name:'审核驳回'},
    ],
   
    //普通区
    releaseOrdinaryIndex:0,
    releaseOrdinaryArray:[
      {name:'全部',value:0},
      {name:'图文',value:1},
      {name:'视频',value:4},
      {name:'音频',value:3},
      {name:'文章',value:2}],

    screenShow:false,


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

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // wx.setNavigationBarColor({
    //   frontColor: '#ffffff',
    //   backgroundColor:"#faa673"
    // })
    this.getBehavior();
   
  },

  gotoPages(evt){
    let options = evt.currentTarget.dataset;
    let page = options.page;
    wx.navigateTo({
      url: page,
    });
  },


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

  },

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

  },

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

  },

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

  },

  //取消关注
  cancelAttention(evt){
    let index = evt.detail;
    let listData = this.data.listData;
    listData.splice(index,1);
    this.setData({listData});
  },

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

  },

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: 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,
      networkLoading:false,
      screenShow:false
    });
    this.getListData();
  },
  setScreenShow() {
    this.setData({
      screenShow: !this.data.screenShow
    });
  },

  //flag==true 只更新流量值
  getBehavior(flag){
    let urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.member.my-behavior");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        if(flag==true){
          this.setData({
            'details.stream':res.data.stream
          });
          return;
        }
        let name_set = res.data.name_set;
        let record = "记录";
        let seeTabArray=[
          {name:name_set.read_name+record,value:3},
          {name:name_set.like_name+record,value:1},
          {name:name_set.favorites_name+record,value:5},
          {name:name_set.comment_name+record,value:6},
          {name:name_set.share_name+record,value:4},
          {name:name_set.attention_name+record,value:2},
          {name:"直播",value:"live"}];
        let liveRecordArray=[
          {name:name_set.read_name+record,value:3},
          {name:name_set.like_name+record,value:1},
          {name:name_set.comment_name+record,value:6},
          {name:name_set.share_name+record,value:4},
          {name:name_set.attention_name+record,value:2},
        ];
        this.setData({
          details:res.data,
          seeTabArray,liveRecordArray
        });
        this.getListData();
       
      }
    });
  },

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


  getListData(){
    let {tabsIndex,seeTabIndex,seeTabArray,liveRecordIndex,liveRecordArray,releaseTabIndex,releaseExamineIndex,releaseOrdinaryArray,releaseOrdinaryIndex} = this.data;
    this.initPage();
    let json={};
    let urlStr;

    if(tabsIndex == 0){
      if(seeTabArray[seeTabIndex].value == "live"){ //直播
        urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.member.get-my-behavior-room-list");
        json.behavior_type = liveRecordArray[liveRecordIndex].value;
      }else{
        urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.member.get-my-behavior-list");
        json.behavior_type = seeTabArray[seeTabIndex].value;
      }
    }else if(tabsIndex == 1){
      urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.media-advertisingt.get-advertisingt-list");
      json.advertisingt_type = releaseTabIndex;
      if(releaseTabIndex==0){
        if(releaseExamineIndex!=0){
          json.check_type = releaseExamineIndex-1;
        }
      }else if(releaseTabIndex==1){
        if(releaseOrdinaryIndex!=0){
          json.genre = releaseOrdinaryArray[releaseOrdinaryIndex].value;
        }
      }
    }
     
    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.last_page,
          listData: res.data.data,
          networkLoading: true,
          
        });
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }

      }
    });


  },


  //获取更多数据
  getMoreData() {
  
    // 防止多次请求分页数据
    this.data.isLoadMore = false;
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page++;
      let json = {
        page: this.data.page
      };

      let {tabsIndex,seeTabIndex,seeTabArray,liveRecordIndex,liveRecordArray,releaseTabIndex,releaseExamineIndex,releaseOrdinaryIndex} = this.data;
      let urlStr;
  
      if(tabsIndex == 0){
        if(seeTabArray[seeTabIndex].value == "live"){ //直播
          urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.member.get-my-behavior-room-list");
          json.behavior_type = liveRecordArray[liveRecordIndex].value;
        }else{
          urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.member.get-my-behavior-list");
          json.behavior_type = seeTabArray[seeTabIndex].value;
        }
      }else if(tabsIndex == 1){
        urlStr = app.getNetAddresss("plugin.new-media-advertising.frontend.media-advertisingt.get-advertisingt-list");
        json.advertisingt_type = releaseTabIndex;
        if(releaseTabIndex==0){
          if(releaseExamineIndex!=0){
            json.check_type = releaseExamineIndex-1;
          }
        }else if(releaseTabIndex==1){
          if(releaseOrdinaryIndex!=0){
            json.genre = releaseOrdinaryIndex;
          }
        }
      }

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

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



});