// pages/coupons.js
const app = getApp()
const wxService = require('../../utils/wxService')
const utils = require('../../utils/util')
const envInfo = require('../../config/index').envInfo

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    categoryList:[], //分类
    imageData: [
      {
        name: '大脸猫爱吃鱼大脸猫爱吃鱼大脸猫爱吃鱼大脸猫爱吃鱼大脸猫爱吃鱼',
        heart_num: '1',
        title: '你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识',
        url: 'http://f10.baidu.com/it/u=121654667,1482133440&fm=72',
        avatar: 'http://img4.imgtn.bdimg.com/it/u=349345436,3394162868&fm=26&gp=0.jpg'
      },
      {
        name: '大脸猫爱吃鱼',
        heart_num: '2',
        title: '你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识',
        url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg',
        avatar: 'http://img4.imgtn.bdimg.com/it/u=349345436,3394162868&fm=26&gp=0.jpg'
      },
      {
        name: '大脸猫爱吃鱼',
        heart_num: '3',
        title: '你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识',
        url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg',
        avatar: 'http://img4.imgtn.bdimg.com/it/u=349345436,3394162868&fm=26&gp=0.jpg'
      }, {
        name: '大脸猫爱吃鱼',
        heart_num: '4',
        title: '你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识',
        url: 'http://f10.baidu.com/it/u=121654667,1482133440&fm=72',
        avatar: 'http://img4.imgtn.bdimg.com/it/u=349345436,3394162868&fm=26&gp=0.jpg'
      },
      {
        name: '大脸猫爱吃鱼',
        heart_num: '5',
        title: '你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识',
        url: 'http://f10.baidu.com/it/u=121654667,1482133440&fm=72',
        avatar: 'http://img4.imgtn.bdimg.com/it/u=349345436,3394162868&fm=26&gp=0.jpg'
      },
      {
        name: '大脸猫爱吃鱼',
        heart_num: '6',
        title: '你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识',
        url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg',
        avatar: 'http://img4.imgtn.bdimg.com/it/u=349345436,3394162868&fm=26&gp=0.jpg'
      },
      {
        name: '大脸猫爱吃鱼',
        heart_num: '7',
        title: '你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识',
        url: 'http://img4.imgtn.bdimg.com/it/u=2748975304,2710656664&fm=26&gp=0.jpg',
        avatar: 'http://img4.imgtn.bdimg.com/it/u=349345436,3394162868&fm=26&gp=0.jpg'
      }, {
        name: '大脸猫爱吃鱼',
        heart_num: '8',
        title: '你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识',
        url: 'http://img2.imgtn.bdimg.com/it/u=1561660534,130168102&fm=26&gp=0.jpg',
        avatar: 'http://img4.imgtn.bdimg.com/it/u=349345436,3394162868&fm=26&gp=0.jpg'
      }
    ],
  
    baseImgUrl: app.globalData.imageUrl,
    listData: [{
      desc: "string",
      imageUrl: "/group3/M00/00/0B/Cv6CBF0RvcuAXoDUAA5M5vbNswU382.png",
      link: { linkId: "10", brandId: "2711", name: "会员服务", url: "5", type: 2, status: 0 },
      number: 1,
      redirectId: "10",
      type: 0
    }, {
      desc: "string",
        imageUrl: "/group3/M00/00/08/Cv6CBF0BqvyADM4iAABJQ-gxAy4311.png",
      link: { linkId: "10", brandId: "2711", name: "会员服务", url: "5", type: 2, status: 0 },
      number: 1,
      redirectId: "10",
      type: 0
    },
      {
        desc: "string",
        imageUrl: "/group3/M00/00/08/Cv6CBF0BqvyADM4iAABJQ-gxAy4311.png",
        link: { linkId: "10", brandId: "2711", name: "会员服务", url: "5", type: 2, status: 0 },
        number: 1,
        redirectId: "10",
        type: 0
      },
      {
        desc: "string",
        imageUrl: "/group3/M00/00/0B/Cv6CBF0RvcuAXoDUAA5M5vbNswU382.png",
        link: { linkId: "10", brandId: "2711", name: "会员服务", url: "5", type: 2, status: 0 },
        number: 1,
        redirectId: "10",
        type: 0
      }],
    pageNum: 1,
    pageSize: 10,
    invalidPageNo: 1,
    totalPages: 0,
    totalElements: 0,
    invalidTotalElements: 0,
    couponList: [],
    invalidCouponList: [],
    invalidTotalPages: 0,
    invalidTotalElements: 0,
    currentTab: 0,
    noMoreCoupon: false,
    noMoreCouponInvalid: false,
    contentList:[]
  },
 
  // 点击切换
  clickTab: function (e) {
    const { current } = e.target.dataset
    if (this.data.currentTab == current) {
      return false;
    } else {
      this.setData({
        currentTab: current,
      })
    }
    this.getContentList(current)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() { },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getCategoryList()
    
  },

// 获取分类
  getCategoryList(){
    wxService.post(`/merchant/contentMobileTerminal/gatClassify`).then(res => {
      if (!res) return false
      const { result, data } = res.data
      if (result == 0 ) {
        this.setData({
          categoryList:data,
          currentTab:data[0].id
        })

        this.getContentList(data[0].id)
      }

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

  // 获取发现内容
  getContentList(classifyId){
    const { memberId } = wx.getStorageSync('_baseUserInfo')
    wxService.post(`/merchant/contentMobileTerminal/getContentList?pageNum=${this.data.pageNum}&pageSize=${this.data.pageSize}`, {
      classifyId: classifyId,
      userId: memberId
    }).then(res => {
      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(() => {
    })
  },

  // 点赞
  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(this.data.currentTab)
          }

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

      }else{
        wxService.post(`/merchant/contentMobileTerminal/operationDelete`, {
          contentId: 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(this.data.currentTab)
          }

        }).finally(() => {
        })
      }
  },
  
  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  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)
    } 
  }
})