//var api = require("../../api.js"), app = getApp(), 
var is_loading_more = !1,
  is_no_more = !1;
var api = require('../../utils/api.js');

Page({
  data: {
    page: 1,
    video_list: [],
    article_list: [],
    url: "",
    hide: "hide",
    show: !1,
    animationData: {},
    show_video: null,
    tabList: [{
      tab: 'video',
      title: '视频'
    }, {
      tab: 'article',
      title: '文章'
    }],
    selectedTab: 'video',
    selectedVideoIndex: null,
    selectedVideoUrl: '',
    showVideo: false
  },
  onLoad: function(a) {
    // app.pageOnLoad(this);
    this.loadMoreGoodsList('both');
  },
  swiperTab: function(e) {
    var that = this;
    // console.log(e);
    if (e.detail.currentItemId != 'video') {
      this.setData({
        show_video: null,
        show: !0
      });
    }
    that.setData({
      selectedTab: e.detail.currentItemId
    });
  },
  clickTab: function(e) {
    var that = this;
    // console.log(e)
    if (e.target.dataset.current != 'video') {
      this.setData({
        show_video: null,
        show: !0
      });
    }
    
    if (this.data.selectedTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        selectedTab: e.target.dataset.current
      })
    }
  },
  onReady: function() {},
  onShow: function() {
    // app.pageOnShow(this);
  },
  onHide: function() {
    var context = wx.createVideoContext("video_" + this.data.show_video)
    context.stop()
    this.setData({
      show_video: null,
      show: !0
    });
  },
  onUnload: function() {},
  onPullDownRefresh: function() {},
  loadMoreGoodsList: function(tab) {
    var tab = tab || this.data.selectedTab;
    var o = this;
    // if (!is_loading_more) {
    //   o.setData({
    //     show_loading_bar: !0
    //   }), is_loading_more = !0;
    var i = o.data.page;
    // var v = [{
    //           "id":"234",
    //           "title":"小程序项目整体介绍",
    //           "url":"http://m1.beiyuesi.com/2.mp4",
    //           "sort":"9",
    //           "is_delete":"0",
    //           "addtime":"1515120216",
    //           "store_id":"20926",
    //       "pic_url":"https://u5.9026.com/addons/swdz_mall/core/web/uploads/image/f0/f0734ad93d46497483344846864596a4.jpg",
    //           "content":"想要自己开发APP，却备受组建开发团队的困扰更别说高昂的开发费用与漫长的开",
    //           "type":"0",
    //           "time":"01月05日",
    //           "show":-1
    //       },
    //   {
    //     "id": "235",
    //     "title": "小程序模版介绍",
    //     "url": "http://m1.beiyuesi.com/1.mp4",
    //     "sort": "99",
    //     "is_delete": "0",
    //     "addtime": "1515120395",
    //     "store_id": "20926",
    //     "pic_url": "https://u5.9026.com/addons/swdz_mall/core/web/uploads/image/0e/0e739254177172e4b815d75f88da77b4.png",
    //     "content": "小程序是腾讯旗下继朋友圈、公众号之后推出的又一款战略级的产品。是线下实体店实体零售转型升级、实现新突破的利器。小程序比公众号更容易传播和裂变，比传统电商更容易获客和成交，比朋友圈微商更专业和可信",
    //     "type": "0",
    //     "time": "01月05日",
    //     "show": -1
    //   },
    //       ];
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: api.getMoreVideosAndArticlesUrl,
      method: 'GET',
      data: {
        'video_offset': o.data.video_list.length,
        'article_offset': o.data.article_list.length,
        'type': tab
      },
      success: res => {
        if (res.data.status == 'success') {
          if (res.data.type != 'both' && res.data.list.length <= 0) {
            wx.showToast({
              title: '到底了',
              icon: 'none',
              duration: 800
            })
          } else {
            if (res.data.type == 'video') {
              var t = o.data.video_list.concat(res.data.list);
              o.setData({
                video_list: t,
                page: i + 1
              });
            } else if (res.data.type == 'article') {
              var t = o.data.article_list.concat(res.data.list);
              o.setData({
                article_list: t,
                page: i + 1
              });
            } else if (res.data.type == 'both') {
              var t = o.data.video_list.concat(res.data.video_list);
              o.setData({
                video_list: t,
                page: i + 1
              });
              var t = o.data.article_list.concat(res.data.article_list);
              o.setData({
                article_list: t,
                page: i + 1
              });
            }
          }
        }
      },
      complete: res => {
        wx.hideLoading()
      }
    })
    // app.request({
    //   url: api.default.video_list,
    //   data: {
    //     page: i
    //   },
    //   success: function (a) {
    //     0 == a.data.list.length && (is_no_more = !0);
    //     var t = o.data.video_list.concat(a.data.list);
    //     o.setData({
    //       video_list: t,
    //       page: i + 1
    //     });
    //   },
    //   complete: function () {
    //     is_loading_more = !1, o.setData({
    //       show_loading_bar: !1
    //     });
    //   }
    // });
    // }
  },
  play: function(a) {
    var url = a.currentTarget.dataset.url;
    var title = a.currentTarget.dataset.title;
    wx.navigateTo({
      url: '/pages/video/index?url=' + url + '&title=' + title,
    })
    // wx.createVideoContext("video_" + this.data.show_video).pause(), this.setData({
    //   show_video: t,
    //   show: !0
    // });
  },
  onReachBottom: function() {
    this.loadMoreGoodsList();
    // is_no_more || this.loadMoreGoodsList();
  },
  more: function(a) {
    var t = this,
      o = a.target.dataset.index,
      i = t.data.video_list,
      e = wx.createAnimation({
        duration: 1e3,
        timingFunction: "ease"
      });
    this.animation = e, -1 != i[o].show ? (e.rotate(0).step(), i[o].show = -1) : (e.rotate(0).step(),
      i[o].show = 0), t.setData({
      video_list: i,
      animationData: this.animation.export()
    });
  },
  redirectToArticle: function(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/article-detail/index?id=' + id
    })
  }
});