// packageG/independent_store/store_album/store_album.js
const app = getApp();
Page({

  data: {
    store_id: '',
    label_id: '',
    imgList: [],
    navList: [],
    // showVideo: false,
    playVideoSrc: '',

    page: 1,
    finished: false,
    
  },


  onLoad: function (options) {
    if (options.store_id) {
      this.setData({
        store_id: options.store_id
      });
    }
    this.getData();
  },

  onShow: function () {
  },

  changeTab (e) {
    this.setData({
      page: 1,
      finished: false,
      imgList: [],
      label_id: e.detail.name,
    });
    this.getData();
  },

  getData () {
    if (this.data.finished) {
      return;
    }
    let strUrl = app.getNetAddresss("plugin.store-alone-temp.frontend.home.get-photo");
    wx.showLoading({title: "loading"});
    app._postNetWork({
      url: strUrl,
      data: {
        store_id: this.data.store_id,
        label_id: this.data.label_id,
        page: this.data.page
      },
      success: (res) => {
        wx.hideLoading();
        if (res.data.result !== 1) {
          wx.showToast({ title: res.data.msg, icon: "none"});
          return;
        }
        let resData = res.data.data.data;
        let finished;
        let imgList = [];
        if (resData.current_page >= resData.last_page || resData.data.length < resData.per_page) {
          finished = true;
        }
        imgList = this.data.page > 1 ? this.data.imgList.concat(resData.data) : resData.data;
        this.setImgList(imgList);
        this.setData({
          imgList,
          finished,
          page: (this.data.page + 1),
          navList: res.data.data.nav
        });
      },
      fail () {
        wx.hideLoading();
      }
    });
  },
  setImgList (list) {
    list.forEach((item, index) => {
      if (item.data_type == 0) {
        item.image_src = `${item.image_src}?id=${index}`;
      }
    });
  },
  // 视频播放
  palyVideo (e) {
    let videoSrc = e.currentTarget.dataset.videosrc;
    this.setData({
      playVideoSrc: videoSrc
    });
    let videoContext = wx.createVideoContext('polyvLiveVideo');
    videoContext.requestFullScreen({direction: 90});
    videoContext.seek(0);
    videoContext.play();
  },
  // 预览图片
  previewImage (e) {
    let imgsrc = e.currentTarget.dataset.imagesrc;
    let preViewList = [];
    this.data.imgList.forEach((item) => {
      if (item.data_type == 0) {
        preViewList.push(item.image_src);
      }
    });
    wx.previewImage({
      current: imgsrc,
      urls: preViewList,
      showmenu: true,
      fail () {
        wx.showToast({
          title: '图片预览失败',
          icon: 'none'
        });
      }
    });
  },

  onReachBottom () {
    this.getData();
  }
});