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

const discountBg = 'https://bigaka-xie.oss-cn-shanghai.aliyuncs.com/7_3_0/icon_dicount_coupon.png';
const dingeBg = 'https://bigaka-xie.oss-cn-shanghai.aliyuncs.com/7_3_0/icon_dinge_coupon.png';
const diyongBg = 'https://bigaka-xie.oss-cn-shanghai.aliyuncs.com/7_3_0/icon_diyong_coupon.png';
const defaultGoodsImg = 'https://bigaka-xie.oss-cn-shanghai.aliyuncs.com/product_defalut.jpg';

wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    coupon : {
      couponTypeArr: [
        { name: '打折券', type: 2, active: true },
        { name: '定额券', type: 3, active: false },
        { name: '抵用券', type: 1, active: false }
      ],
      currentCouponType: 2,
      couponList : []
    },
    goods : {
      goodsTypeArr: [
        { name: '最新', type: 2, active: true },
        { name: '最热🔥', type: 3, active: false },
        { name: '推荐👍', type: 1, active: false }
      ],
      currentGoodsType: 2,
      goodsList: []
    },
    
    showModal : false,
    shareModel : {
      title : '',
      path : '',
      imageUrl : ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCouponList();
    this.getGoodsList();
    wx.hideShareMenu();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      showModal : false,
    })
  },

  //跳转到更多商品列表
  onTapToMoreProducts() {
    wx.navigateTo({
      url: '/shoppingGuid/page/pages/moreProducts/moreProducts?type=' + this.data.goods.currentGoodsType,
    });
  },

  //跳转到优惠券列表
  onTapToCouponList() {
    wx.navigateTo({
      url: '/shoppingGuid/page/pages/moreCoupons/moreCoupons?type=' + this.data.coupon.currentCouponType,
    });
  },

  //点击事件
  //分类点击
  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.couponList = [];

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

  },

  //获取优惠券列表
  getCouponList() {
    wx.showLoading({
      title: '加载中..',
    });
    let type = this.data.coupon.currentCouponType;
    wxService.post(`/marketing/shoppingguide/coupon/page4c?pageSize=3&pageNo=1`, {
      couponName: '',
      type: type
    }).then(res => {
      if (res) {
        let list = res.data.data.content ? res.data.data.content : [];
        list.forEach(item => {
          item.couponType = item.couponSetting.type;
          item.couponBg = item.couponType == 1 ? diyongBg : (item.couponType == 2 ? discountBg : dingeBg);
        });

        this.data.coupon.couponList = list;
        this.setData({
          coupon: this.data.coupon,
        });
      }
    });
  },

  //展示优惠券二维码
  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.couponBg
    }
    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.shareModel.title = item.title;
        this.data.shareModel.imageUrl = item.couponBg;
        this.data.shareModel.path = item.path;
        this.setData({
          shareModel: this.data.shareModel,
          showModal: true
        });
      }
    });
  },
  //关闭模态框
  onTapCancelShare() {
    this.setData({
      showModal: 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) {
          }
        });
      }
    });
  },

  //商品筛选
  //筛选点击
  onTapChangeGoodsType(e) {
    let index = e.currentTarget.dataset.index;
    this.data.goods.goodsTypeArr.map(item => {
      item.active = false;
    });

    this.data.goods.goodsTypeArr[index].active = true;
    this.data.goods.currentGoodsType = this.data.goods.goodsTypeArr[index].type;
    this.data.goods.goodsList = [];
    this.getGoodsList();
    this.setData({
      goods: this.data.goods
    });
  },

  //获取商品列表
  getGoodsList() {
    wxService.post(`/merchant/share/productBonus/buyer/findPage?pageNo=1&pageSize=5`, {}).then(res => {
      if (res) {
        let list = res.data.data.content ? res.data.data.content : [];
        list.map(item => {
          item.productImgUrl = item.productImgUrl ? item.productImgUrl : defaultGoodsImg;
        });
        this.data.goods.goodsList = list;
        this.setData({
          goods: this.data.goods
        });
      }
    });
  },

  //定向发送
  //定向转发商品给用户 分享功能
  onTapToShare(e) {
    let item = e.currentTarget.dataset.item;
    // 生成触点对象
    let content = {
      title: item.productName,
      id: item.productId,
      url: item.productImgUrl
    }
    let tentacleInfo = {
      content: JSON.stringify(content),
      contentId: item.productId,
      contentType: app.globalData.contants.SHARE_TYPE.PRODUCT_DETAIL, //内容类型 
      title: item.productName, //标题
      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.productId, 10, 64);
        let inner_tentacleId = Integer.digit(tentacleId, 10, 64);
        var path = 'pages/productDetail/productDetail' + '?i=' + inner_id + '&t=' + inner_tentacleId;
        this.data.shareModel.title = item.productName;
        this.data.shareModel.imageUrl = item.productImgUrl;
        this.data.shareModel.path = path;

        this.setData({
          shareModel: this.data.shareModel,
          showModal: true,
        });
      }
    });
  },

  //立即群发商品给用户
  onTapToRecommandProduct(e) {
    let item = e.currentTarget.dataset.item;
    // 生成触点对象
    let content = {
      title: item.productName,
      id: item.productId,
      url: item.productImgUrl
    }
    let tentacleInfo = {
      content: JSON.stringify(content),
      contentId: item.productId,
      contentType: app.globalData.contants.SHARE_TYPE.PRODUCT_DETAIL, //内容类型 
      title: item.productName, //标题
      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.productId, 10, 64);
        let inner_tentacleId = Integer.digit(tentacleId, 10, 64);
        var path = 'pages/productDetail/productDetail' + '?i=' + inner_id + '&t=' + inner_tentacleId;
        //打开企业微信通讯录选择会员
        wx.qy.shareToExternalContact({
          appid: app.globalData.appId,//小程序的appid
          title: item.productName, //小程序消息的title
          imgUrl: item.productImgUrl,//小程序消息的封面图
          page: path, //小程序消息打开后的路径
          success: function (res) {
            wx.showToast({
              title: '推荐成功！'
            });
          },
          fail: function (err) {
            console.log('error---------------', err);
          }
        });
      }
    });

  },
  
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('pull done')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

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