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

  /**
   * 页面的初始数据
   */
  data: {
    couponId : '',
    paramIsError : false,
    useStoreId : '',
    couponInfo : {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let param = options.id;
    if(param){
      this.getCurrentStoreId();
      this.data.couponId = param;
        //获取优惠券详情
      wx.showLoading({
        title: '加载中..',
      });
      this.getCouponDetail();
    }
    else{
      //参数格式异常 需要提示处理
      this.setData({
        paramIsError: true
      });
    }
  },
  //获取当前登录用户的storeId
  getCurrentStoreId(){
    //获取当前登录用户的storeid
    let member = wx.getStorageSync('_baseUserInfo') ? wx.getStorageSync('_baseUserInfo') : null;
    if (member) {
      this.data.useStoreId = member.member.storeId;
    } else {
      this.setData({
        paramIsError: true
      });
    }
  },
  //获取优惠券详情
  getCouponDetail(){
    wxService.post(`/coupon/coupon/get?cardNo=${this.data.couponId}`).then(res => {
      if(res){
        this.data.couponInfo = res.data.data ? res.data.data : {};
        this.data.couponInfo.takeCouponBgimg = this.data.couponInfo.couponDetail.style.takeCouponBgimg;
        this.data.couponInfo.title = this.data.couponInfo.couponSetting.title;
        this.data.couponInfo.faceAmountDesc = this.data.couponInfo.couponSetting.faceAmountDesc;
        let startTime = this.data.couponInfo.coupon.startTime ;
        let endTime = this.data.couponInfo.coupon.endTime;
        if (startTime){
          //固定日期
          this.data.couponInfo.startTime = startTime.substring(0, 10);
          this.data.couponInfo.endTime = endTime.substring(0, 10);
        }
      
        let notice = this.data.couponInfo.couponSetting.notice;
        this.data.couponInfo.notice = notice ? JSON.parse(notice) : [];
        if (this.data.couponInfo.notice){
          this.data.couponInfo.notice = this.data.couponInfo.notice.filter(item => item.type != 1);
        }

        this.setData({
          couponInfo: this.data.couponInfo
        });
      }
    }).finally(() => {
      wx.hideLoading();
    });
  },
  //确认核销
  onTapSureWhiteOffCoupon(){
    let self = this;
    wx.showModal({
      title: '核销确认',
      content: '您将核销该优惠券，是否继续？',
      confirmColor: '#ff3333',
      success(res) {
        if (res.confirm) {
          self.writeOffCoupon();
        } else if (res.cancel) {}
      }
    })
  },

  //核销优惠券
  writeOffCoupon(){
    wx.showLoading({
      title: '核销中..',
    });
    let param = {};
    param.cardNo = this.data.couponId;
    param.useStoreId = this.data.useStoreId;
    param.brandId = app.globalData.brandId;

    wxService.post(`/coupon/coupon/checkAndInvalid`, param).then(res => {
      wx.hideLoading();
      if(res){
        if (res.data.result == 0) {
          wx.showToast({
            title: '核销成功',
            icon : 'success',
            duration : 2000,
            mask : true,
            success : function(){
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1
                });
              },1500);
            }
          });
        }
      }      
    });
  }
});