// 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,
    currentShareContent: null,
    pageNum: 1,
    pageSize: 10,
    extendProList:[],
    myExtendList:[]
  },

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

  },

  //滑动切换
  swiperTab: function (e) {
    const { current } = e.detail
    if (this.data.currentTab == current) {
      return false;
    } else {
      this.setData({
        currentTab: current
      })
    }
    // 可使用
    if (current == 0) {
      this.getExtendProList()
    } else if (current == 1) {
      // 我的推广
      wx.hideShareMenu();
      this.getMyExtendOrderList()
    }
  },
  // 点击切换
  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()
    } else if (current == 1) {
      // 我的推广
      wx.hideShareMenu();
      this.getMyExtendOrderList()
    }
  },

  // 选择商品
  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(){
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wxService.post(`/merchant/share/productBonus/buyer/findPage?pageNo=${this.data.pageNum}&pageSize=${this.data.pageSize}`,{}).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          this.setData({
            extendProList: this.data.pageNum == 1 ? [...data.content] : [...this.data.extendProList, ...data.content],
            totalPages: data.totalPages
          }, () => {
            this.setData({
              no_data: this.data.extendProList.length ? true : false
            })
          })
        }
      }
    })
  },

  // 获取我的推广订单列表
  getMyExtendOrderList(){
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wxService.post(`/marketing/tentacleContent/getTentacleContentDTO?pageNum=${this.data.pageNum}&pageSize=${this.data.pageSize}`).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          let extendData = []
          for (var i in data.content){
            if (data.content[i].content){
              var content = JSON.parse(data.content[i].content)
              extendData.push(content)
            }
            data.content[i].content = extendData
          }
          this.setData({
            myExtendList: this.data.pageNum == 1 ? [...data.content] : [...this.data.myExtendList, ...data.content],
            totalPages: res.totalPages
          }, () => {
            this.setData({
              no_data: this.data.orderList.length ? true : false
            })
          })
        }
      }
    })
  },

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

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

  },

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

  },

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

  },

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

  /**
   * 用户点击右上角分享
   */
  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
        }
      }
    });
   
  }
})