const util = require('../../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: 0,
    navheight: 0,
    pagetop: 0,
    scrollTop: 0,
    numop: 0,
    key: 1,
    paramsDetail: {
      pageSize: 10
    },
    pageIndex: 1,
    topImgText: [], //顶部数组
    allActivePhoto: [], //活动相册所有数据
    activePhoto: [], //活动相册9条数据
    activityRecord: [],
    url: '',
    historyTitle: '',
    historyBgImage: '',
    topHeight: 47,
    aid: '',
    isshare: 0, //不是分享页面进入
  },
  // 点击左上返回
  back() {
    wx.navigateBack({
      delta: 1,
    })
  },
  backJT() {
    wx.navigateBack({
      delta: 1,
    })
  },
  // 相册记录切换
  changeType(e) {
    let key = e.currentTarget.dataset.key
    let that = this
    if (key == 1) {
      this.getActivityPhoto()
    }
    if (key == 2) {
      that.data.paramsDetail.articleType = 1
      let data = that.data.paramsDetail
      util.ajax({
        func: 'v2/article/product_system/history/list',
        data,
        method: 'POST'
      }, ({
        data,
        code
      }) => {
        if (code == 0) {
          that.setData({
            activityRecord: data
          })
        }
      })
    }
    this.setData({
      key
    })
  },
  backHome: function () {
    wx.reLaunch({
      url: '/pages/home/index'
    })
  },
  onLoad(options) {
    console.log(options);
    if (options.isshare == 1) {
      console.log('是分享进入');
      this.setData({
        'isshare': options.isshare
      })
    }
    wx.showShareMenu({
      withShareTicket: true,
      //设置下方的Menus菜单，才能够让发送给朋友与分享到朋友圈两个按钮可以点击
      menus: ["shareAppMessage", "shareTimeline"]
    })
    this.data.paramsDetail.psId = options.psId
    this.data.paramsDetail.articleType = this.data.key
    this.setData({
      aid: options.aid,
      //  状态栏的高度
      statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'] + 'px',
      //  胶囊的高度 也就是自定导航栏的高度
      navheight: wx.getMenuButtonBoundingClientRect()['height'] + 'px',
      //  状态栏加导航栏的高度 加上下的padding的高度 12
      pagetop: wx.getMenuButtonBoundingClientRect()['height'] + wx.getSystemInfoSync()['statusBarHeight'] + 12 + 'px',
    })
    console.log(this.data.statusBarHeight);
    this.getTopImgText()
    this.getActivityPhoto()
    // console.log(this.data.statusBarHeight)
    // console.log(this.data.navheight)
    // console.log(this.data.pagetop)
  },
  onPageScroll(e) { //页面滚动就会触发
    let height = this.data.statusBarHeight
    this.setData({
      scrollTop: e.scrollTop,
      topHeight: parseInt(height)
    })
    // console.log(this.data.topHeight);
  },
  // 获取顶部图文信息
  getTopImgText() {
    console.log(this.data.paramsDetail);
    let that = this
    util.ajax({
      func: 'v2/article/product_system/history/info',
      data: this.data.paramsDetail,
      method: 'POST'
    }, ({
      data,
      code
    }) => {
      if (code == 0) {
        // console.log(data);
        that.setData({
          topImgText: data,
          historyTitle: data.historyTitle,
          historyBgImage: data.historyBgImage
        })
      }
    })
  },
  // 获取活动相册信息
  getActivityPhoto(p) {
    let that = this
    that.data.paramsDetail.articleType = 0
    // that.data.paramsDetail.pageSize = 10
    // that.data.paramsDetail.pageIndex = 0
    console.log(that.data.paramsDetail);
    util.ajax({
      func: 'v2/article/product_system/history/list',
      data: that.data.paramsDetail,
      method: 'POST'
    }, ({
      data,
      code
    }) => {
      if (code == 0) {
        // console.log(data);
        that.data.allActivePhoto = data
        data.forEach((item, index) => {
          if (index < 9) {
            that.data.activePhoto.push(item)
          }
        });
        that.setData({
          activePhoto: data
        })
        // console.log(that.data.activePhoto);
      }
    })
  },
  //下拉刷新
  onReachBottom: function (e) { //触底开始下一页
    // var that = this;
    // that.data.pageIndex = that.data.pageIndex + 1; //获取当前页数并+1
    // that.setData({
    //   pageIndex: that.data.pageIndex
    // })
    // console.log(that.data.pageIndex);
    // that.getActivityPhoto(that.data.pageIndex); //重新调用请求获取下一页数据
    // console.log(this.data.allActivePhoto);
    const {
      allActivePhoto,
      activePhoto
    } = this.data;
    const start = activePhoto.length;
    const end = Math.min(start + 6, allActivePhoto.length - 1);
    if (activePhoto.length == allActivePhoto.length) {
      return;
    }
    for (let i = start; i <= end; i++) {
      activePhoto.push(allActivePhoto[i])
    }
    this.setData({
      activePhoto
    })
  },
  // 分享给朋友
  onShareAppMessage(options) {
    console.log(options);
    return {
      title: this.data.historyTitle,
      path: '/pages/product/Previous/index?psId=' + this.data.paramsDetail.psId + '&isshare=1',
      imageUrl: this.data.historyBgImage
    }
  },
  //分享到朋友圈
  onShareTimeline: function () {
    return {
      title: this.data.historyTitle,
      imageUrl: this.data.historyBgImage
    }
  },
  //图片预览
  previewImage(e) {
    let urls = []
    let imgUrl = e.currentTarget.dataset.imgurl
    let act = this.data.allActivePhoto
    act.forEach((e) => {
      if (e.type == 4) {
        urls.push(e.logo)
      }
    })
    // urls.push(imgUrl)
    wx.previewImage({
      current: e.currentTarget.dataset.imgurl, // 当前显示图片的http链接
      urls,
    })
    // console.log(urls);
  },
  //视频播放
  previewVideo(e) {
    console.log(e);
    wx.previewMedia({
      sources: [{
        url: e.currentTarget.dataset.vdurl, //视频播放路径
        type: 'video' //video视频 image图片
      }]
    })
  },
  channelVideo(e) {
    console.log(e);
    let feedId = e.currentTarget.dataset.vdnum
    let finderUserName = e.currentTarget.dataset.uname
    console.log(feedId);
    console.log(finderUserName);
    wx.openChannelsActivity({
      finderUserName,
      feedId,
      success: function (res) {
        console.log(res)
      },
      complete: function () {
        console.log('调用');
      }
    })
  },
  //推文跳转
  tweet(e) {
    console.log(e);
    let url = e.currentTarget.dataset.content
    let title = e.currentTarget.dataset.title
    wx.navigateTo({
      url: '/pages/public/swiper/web?url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title),
    })
  },
})