// shoppingGuid/page/pages/moreCoupons/moreCoupons.js
const wxService = require('../../../../utils/wxService')
import { Integer } from '../../../../utils/integerDigitalConvertion'
const app = getApp();
wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    coupon: {
      couponTypeArr: [
        { name: '打折券', type: 2, active: true },
        { name: '定额券', type: 3, active: false },
        { name: '抵用券', type: 1, active: false }
      ],
      currentCouponType: 2,
      couponList: [],
      pageNum : 1,
      pageSize : 10,
      totalPage : 0,
      noData : false
    },
    shareCouponObj : {
      title : '',
      imageUrl : '',
      path : ''
    },
    showShareModel : false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let type = options.type ;
    if(type){
      this.data.coupon.currentCouponType = type;
      this.data.coupon.couponTypeArr.map(item => {
        item.active = false;
        if(item.type == type){
          item.active = true;
        }
      });

      this.setData({
        coupon: this.data.coupon
      });
    }

    //获取优惠券列表
    this.getCouponList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  //展示优惠券二维码
  onTapShowQrcode(e) {
    let item = e.currentTarget.dataset.item;
    wx.setStorageSync('_guidCouponInfo', item);
    wx.navigateTo({
      url: '/shoppingGuid/page/pages/couponQrcode/couponQrcode',
    });
  },
  //定向发送优惠券
  onTapToSendConpon(e) {
    let item = e.currentTarget.dataset.item;
    let id = item.id;
    let content = {
      title: item.title,
      id: id,
      url: item.imageBg
    }
    let tentacleInfo = {
      content: JSON.stringify(content),
      contentId: item.id,
      contentType: app.globalData.contants.SHARE_TYPE.PRODUCT_DETAIL, //内容类型 STAFF_RECOMMAND
      title: item.title, //标题
      type: 3// 1:门店,2:员工(暂时不做),3:会员,4:第三方外部渠道,5:智能营销
    }

    wxService.getTentacleContent(tentacleInfo).then(res => {
      if (res && res.id) {
        let tentacleId = res.id;
        let inner_id = Integer.digit(item.id, 10, 64);
        let inner_tentacleId = Integer.digit(tentacleId, 10, 64);
        var path = 'subPackage/page/pages/scanCoupon/scanCoupon' + '?i=' + inner_id + '&t=' + inner_tentacleId;
        this.data.shareCouponObj.title = item.title;
        this.data.shareCouponObj.imageUrl = item.imageBg;
        this.data.shareCouponObj.path = item.path;
        this.setData({
          shareCouponObj: this.data.shareCouponObj,
          showShareModel : true
        });
      }
    });
  },
  //关闭模态框
  onTapCancelShare(){
    this.setData({
      showShareModel: false
    });
  },
  //群发
  onTapToSendByGroup(e){
    let item = e.currentTarget.dataset.item;
    let id = item.id;
    let content = {
      title: item.title,
      id: item.id,
      url: item.imageBg
    }

    let tentacleInfo = {
      content: JSON.stringify(content),
      contentId: item.id,
      contentType: app.globalData.contants.SHARE_TYPE.PRODUCT_DETAIL, //内容类型 STAFF_RECOMMAND
      title: item.title, //标题
      type: 3// 1:门店,2:员工(暂时不做),3:会员,4:第三方外部渠道,5:智能营销
    }

    wxService.getTentacleContent(tentacleInfo).then(res => {
      if (res && res.id) {
        let tentacleId = res.id;
        let inner_id = Integer.digit(item.couponId, 10, 64);
        let inner_tentacleId = Integer.digit(tentacleId, 10, 64);
        var path = 'subPackage/page/pages/scanCoupon/scanCoupon' + '?i=' + inner_id + '&t=' + inner_tentacleId;
        //打开企业微信通讯录选择会员
        wx.qy.shareToExternalContact({
          appid: app.globalData.appId,//小程序的appid
          title: item.couponName, //小程序消息的title
          imgUrl: item.imageBg,//小程序消息的封面图
          page: path, //小程序消息打开后的路径
          success: function (r) {
            wx.showToast({
              title: '发送成功!'
            });
          },
          fail: function (err) {
          }
        });
      }
    });
  },

  //优惠券tab点击
  onTapChangeCouponType(e) {
    let index = e.currentTarget.dataset.index;
    this.data.coupon.couponTypeArr.map(item => {
      item.active = false;
    });

    this.data.coupon.couponTypeArr[index].active = true;
    this.data.coupon.currentCouponType = this.data.coupon.couponTypeArr[index].type;
    this.data.coupon.pageNum = 1;
    this.data.coupon.totalPage = 0;
    this.data.coupon.couponList = [];

    this.getCouponList();

    this.setData({
      coupon: this.data.coupon
    });

  },
  //获取优惠券列表
  getCouponList(refresh) {
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    let pageNum = this.data.coupon.pageNum,
      pageSize = this.data.coupon.pageSize,
      type = this.data.coupon.currentCouponType,
      param = { couponName: '', type: type};
    wxService.post(`/marketing/shoppingguide/coupon/page4c?pageSize=${pageSize}&pageNo=${pageNum}`,
    param).then(res => {
      if (res) {
        const { result, data } = res.data
        wx.hideLoading();
        if (result == 0) {
          let list = data.content ? data.content : [];
          list.forEach(item => {
            item.canUse = (item.couponSetting.status == 0 || item.couponSetting.status == 2) ? false : true;
            item.imageBg = 'https://bigaka-xie.oss-cn-shanghai.aliyuncs.com/coupon_bg.png';
            item.typeText = item.couponSetting.typeDesc;
            item.price = parseFloat(item.couponSetting.faceAmount / 100).toFixed(2);
            item.couponType = item.couponSetting.type;
          });

          this.data.coupon.totalPage = Math.ceil(data.totalElements / this.data.coupon.pageSize);
          this.data.coupon.couponList = this.data.coupon.couponList.concat(list);
          if (this.data.coupon.couponList.length == 0) {
            this.data.coupon.noData = true;
          }

          this.setData({
            coupon: this.data.coupon
          });
        }

        if (refresh){
          wx.stopPullDownRefresh();
          wx.showToast({
            title: '刷新成功',
          });
        }
      }
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.coupon.couponList = [];
    this.data.coupon.pageNum = 1;
    this.data.coupon.totalPage = 0;
    this.getCouponList(true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.coupon.pageNum < this.data.coupon.totalPage){
      this.data.coupon.pageNum ++ ;
      this.getCouponList();
    }
    else{
      if (this.data.coupon.pageNum > 1){
        wx.showToast({
          title: '没有更多数据了~',
          icon : 'none'
        });
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from == 'button') {
      return {
        title: this.data.shareCouponObj.title,
        path: this.data.shareCouponObj.path,
        imageUrl: this.data.shareCouponObj.imageUrl
      }
    }
  }

})