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

wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    isAllSelect: false,
    showShareModal:false,
    showCardModal: false,
    totalPages:0,
    myTotalPages:0,
    currentShareContent: null,
    pageNo:1,
    pageNum: 1,
    pageSize: 10,
    extendProList:[],
    myExtendList:[],
    myShareCount:'',
    extendProBanner: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

 
  // 点击切换
  clickTab: function (e) {
    const { current } = e.target.dataset
    if (this.data.currentTab == current) {
      return false;
    } else {
      this.setData({
        currentTab: current,
        myExtendList: [],  // 清空数据
        // invalidCouponList: [],
      })
    }
    // 可使用
    if (current == 0) {
      this.getExtendProList(1,10)
    } else if (current == 1) {
      // 我的推广
      wx.hideShareMenu();
      this.getMyShareCount()
      this.getMyExtendOrderList(1,10)
    }
  },

  // 选择商品
  selectPro(e){
    let proData = []
    let extendProList = this.data.extendProList
    var index = e.currentTarget.dataset.index
    if (extendProList[index].isSelect) {
      extendProList[index].isSelect = false
    } else {
      extendProList[index].isSelect = true
    }
    this.setData({
      extendProList: extendProList
    })
    for (var i in extendProList) {
      if (extendProList[i].isSelect) {
        proData.push(extendProList[i])
      }
    }
    console.log(proData)
    this.setData({
      proData: proData
    })
  },

  //全选
  allSelect() {
    this.setData({
      isAllSelect: !this.data.isAllSelect
    })
    if (this.data.isAllSelect) {
      this.setData({
        extendProList: this.data.extendProList.map((item) => {
          item.isSelect = true
          return item
        }),
        proData: this.data.extendProList
      })
    } else {
      this.setData({
        extendProList: this.data.extendProList.map((item) => {
          item.isSelect = false
          return item
        }),
        proData: []
      })
    }
  },

  //快速分享
  onTapShare(){
    console.log('111')
    this.setData({
      showShareModal: true
    })
  },

  //生成卡片
  onTapPoster() {
    console.log('----------',this.data.proData)
    if (!this.data.proData){
      wx.showToast({
        title: '请先选择商品',
        icon: 'none'
      })
      return false;
    }
    let tentacleInfo = {
      content: JSON.stringify(this.data.proData),
      contentType: 2, //内容类型 1.文章 2.商品 3.页面
      title: this.data.proData[0].productName,
      type: 3// 1:门店,2:员工(暂时不做),3:会员,4:第三方外部渠道,5:智能营销
    }

    this.data.currentShareContent = this.data.proData[0];

    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 = 'pages/extendProduct/extendProduct?i=' + id + '&t=' + tentacleId;
        this.setData({
          showCardModal: true,
          cardTitle: this.data.proData[0].productName,
          cardUrl: this.data.proData[0].url,
          pagePath: path
        });
      }
    })
  },

  // 获取推广商品列表
  getExtendProList(pageNo, pageSize){
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wxService.post(`/merchant/share/productBonus/buyer/findPage?pageNo=${pageNo}&pageSize=${pageSize}`,{}).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          this.setData({
            extendProList: this.data.pageNo == 1 ? [...data.content] : [...this.data.extendProList, ...data.content],
            totalPages: data.totalPages
          }, () => {
            // this.setData({
            //   no_data: this.data.extendProList.length ? true : false
            // })
          })
        }
      }
    })
  },

  // 获取我的推广订单列表
  getMyExtendOrderList(pageNum, pageSize){
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wxService.post(`/marketing/tentacleContent/getTentacleContentDTO?pageNum=${pageNum}&pageSize=${pageSize}`).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          let extendData = []
          // for (var i in data.content){
          //   //内容类型 1.文章 2.商品 3.页面
          //   if (data.content[i].contentType != 3){
          //     console.log('=====', data.content[i].content)
          //     console.log('--------', JSON.parse(data.content[i].content))
          //     var content = JSON.parse(data.content[i].content)
          //     data.content[i].content = content
          //   }
            
          // }
          this.setData({
            myExtendList: this.data.pageNum == 1 ? [...data.content] : [...this.data.myExtendList, ...data.content],
            myTotalPages: data.totalPages
          }, () => {
            // this.setData({
            //   no_data: this.data.orderList.length ? true : false
            // })
          })
          console.log('00000', this.data.myExtendList)
        }
      }
    })
  },

  // 分享促成的订单数
  getMyShareCount(){
    wxService.get(`/sale/trade/buyer/countByMyShare`).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          this.setData({
            myShareCount: data
          }, () => {
            
          })
        }
      }
    })
  },

  // 推广商品banner
  getExtendProBanner() {
    wxService.get(`/merchant/share/productSectionBonus/buyer/query`).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          this.setData({
            extendProBanner: data.backgroundImgUrl
          }, () => {

          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getExtendProBanner()
    this.getExtendProList(1,10)
    this.data.currentShareContent = null;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('onReachBottom')
    console.log(this.data.currentTab)
    if (this.data.currentTab == 0){
      console.log('11111')
      if (this.data.pageNo < this.data.totalPages){
        console.log('22222')
        this.setData({
          pageNo: this.data.pageNo + 1,
        }, () => {
          this.getExtendProList(this.data.pageNo,10)
        })
      }
    }else{
      if (this.data.pageNum < this.data.myTotalPages) {
        console.log('333')
        this.setData({
          pageNum: this.data.pageNum + 1,
        }, () => {
          this.getMyExtendOrderList(this.data.pageNum,10)
        })
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  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 = 'pages/extendProduct/extendProduct?i=' + id + '&t=' + tentacleId;
      console.log(path)
      return {
        path: path
      }
    }

    let tentacleInfo = {
      content: this.route,
      title: this.route,
      contentType: 3,
      type: 3// 1:门店,2:员工(暂时不做),3:会员,4:第三方外部渠道,5:智能营销
    };
    wxService.getTentacleContent(tentacleInfo).then(res => {
      if (res && res.tentacleId) {
        this.data.currentShareContent.tentacleId = res.tentacleId;
        let tentacleId = Integer.digit(res.tentacleId, 10, 64);
        var path = `pages/extendProduct/extendProduct?t=${tentacleId}`;
        return {
          path: path
        }
      }
    });
   
  }
})