// pages/coupons.js
const app = getApp()
const wxService = require('../../../../utils/wxService')
const utils = require('../../../../utils/util')
const envInfo = require('../../../../config/index').envInfo
const { memberId } = wx.getStorageSync('_baseUserInfo')
import { Integer } from '../../../../utils/integerDigitalConvertion'

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    categoryList: [], //分类
    baseImgUrl: app.globalData.imageUrl,
    pageNum: 1,
    pageSize: 10,
    totalPages: 0,
    totalElements: 0,
    currentTab: 0,
    showCardModal: false,
    cardTitle: '',
    cardUrl: '',
    pagePath:'',
    contentList: [],
    contentPermission: false,
    currentShareContent: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getBannerInfo()
    this.getContentList()
    this.getContentPermission()
    this.data.currentShareContent = null;
  },

  // 发布内容权限
  getContentPermission(){
    wxService.post(`/merchant/contentMobileTerminal/getReleasePermissions`, {
      userId: memberId
    }).then(res => {
      if (!res) return false
      const { result, data } = res.data
      if (result == 0) {
        this.setData({
          contentPermission: data
        })
      }
    }).finally(() => {
    })
  },

  // banner
  getBannerInfo(){
    wxService.post(`/merchant/contentMobileTerminal/getBannerAndInviteSum`, {
      userId: memberId
    }).then(res => {
      if (!res) return false
      const { result, data } = res.data
      if (result == 0) {
        this.setData({
          bannerUrl: data.bannerUrl,
          inviteSum: data.inviteSum,
          yesterdayInviteSum: data.yesterdayInviteSum
        })
      }
    }).finally(() => {
    })
  },



  // 获取发现内容
  getContentList() {
    wx.showLoading({
      title: '加载中'
    })
    const { memberId } = wx.getStorageSync('_baseUserInfo')
    wxService.post(`/merchant/contentMobileTerminal/getContentList?pageNum=${this.data.pageNum}&pageSize=${this.data.pageSize}`, {
      userId: memberId
    }).then(res => {
      wx.hideLoading()
      if (!res) return false
      const { result, data } = res.data
      if (result == 0) {
        if (data.content) {
          this.setData({
            contentList: this.data.pageNum == 1 ? [...data.content] : [...this.data.contentList, ...data.content],
            totalPages: data.totalPages,
            totalElements: data.totalElements
          })
        } else {
          this.setData({
            contentList: []
          })
        }
      }
    }).finally(() => {
      wx.hideLoading()
    })
  },

  // 点赞
  gotoLike(e) {
    console.log(e.detail)
    let params = e.detail
    if (!e.detail.type) {
      wxService.post(`/merchant/contentMobileTerminal/operationSave`, params).then(res => {
        if (!res) return false
        const { result, data } = res.data
        if (result == 0) {
          wx.showToast({
            title: '点赞成功',
            icon: 'none'
          })
          this.getContentList()
        }

      }).finally(() => {
      })

    } else {
      wxService.post(`/merchant/contentMobileTerminal/operationDelete`, {
        id: e.detail.contentId,
        userId: e.detail.userId
      }).then(res => {
        if (!res) return false
        const { result, data } = res.data
        if (result == 0) {
          wx.showToast({
            title: '取消点赞成功',
            icon: 'none'
          })
          this.getContentList()
        }

      }).finally(() => {
      })
    }
  },

  // 分享
  _onTapShare(e) {
    console.log(e)
    let tentacleInfo = {
      content: JSON.stringify(e.detail),
      contentId: e.detail.id,
      contentType: app.globalData.contants.SHARE_TYPE.CONETNT, //内容类型 1.文章 2.商品 3.页面
      title: e.detail.title, //标题
      type: 3// 1:门店,2:员工(暂时不做),3:会员,4:第三方外部渠道,5:智能营销
    }

    this.data.currentShareContent = e.detail;

    wxService.getTentacleContent(tentacleInfo).then(res => {
      if (res && res.tentacleId) {
        this.data.currentShareContent.tentacleId = res.tentacleId;
        let id = Integer.digit(this.data.currentShareContent.id, 10, 64);
        let tentacleId = Integer.digit(this.data.currentShareContent.tentacleId, 10, 64);
        var path = 'subPackage/page/pages/myReleaseDetail/myReleaseDetail?i=' + id + '&t=' + tentacleId;
        this.setData({
          showCardModal: true,
          cardTitle: e.detail.title,
          cardUrl: e.detail.url,
          pagePath: path
        });
      }
    })
  },

  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh: function () {
    this.setData({
      pageNum: 1,
    })
    this.getContentList(this.data.currentTab)
    wx.stopPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const { pageNum, pageSize, totalPages } = this.data
    if (pageNum < totalPages) {
      this.setData({
        pageNum: pageNum + 1,
      })
      this.getContentList(this.data.currentTab)
    }
  },
  /**
 * 用户点击右上角分享
 */
  onShareAppMessage: function (res) {
    if (res.from != 'menu') {
      let id = Integer.digit(this.data.currentShareContent.id, 10, 64);
      let tentacleId = Integer.digit(this.data.currentShareContent.tentacleId, 10, 64);
      var path = 'subPackage/page/pages/myReleaseDetail/myReleaseDetail?i=' + id + '&t=' + tentacleId;
      console.log(path)
      return {
        path: path
      }
    }

    let tentacleInfo = {
      content: '我的发布',
      title: '我的发布',
      contentType: app.globalData.contants.SHARE_TYPE.PAGE,
      type: 3// 1:门店,2:员工(暂时不做),3:会员,4:第三方外部渠道,5:智能营销
    };
    wxService.getTentacleContent(tentacleInfo).then(res => {
      if (res && res.tentacleId) {
        let tentacleId = Integer.digit(res.tentacleId, 10, 64);
        var path = `subPackage/page/pages/myRelease/myRelease?t=${tentacleId}`;
        return {
          path: path
        }
      }
    });
  }
})