// 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;
    console.log(param)
    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;
        this.data.couponInfo.startTime = this.data.couponInfo.couponSetting.startTime.substring(0,10);
        this.data.couponInfo.endTime = this.data.couponInfo.couponSetting.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: '核销中..',
    });
    wxService.post(`/coupon/coupon/checkAndInvalid`,{
      cardNo : this.data.couponId,
      useStoreId: this.data.useStoreId,
      brandId: app.globalData.brandId
    }).then(res => {
      if(res){
        if (res.data.result == 0) {
          wx.showToast({
            title: '核销成功',
          });

          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            });
          },500);
        }
      }      
    }).finally(() => {
      wx.hideLoading();
    });
  }
});