// 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 : {},
    subStoreData : []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  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('guidBaseInfo') ? wx.getStorageSync('guidBaseInfo') : null;
    if (member) {
      this.data.useStoreId = member.shoppingGuideStoreId;
    } 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);
        }

        let subStoreData = [];
        if (this.data.couponInfo.couponDetail){
          subStoreData = this.data.couponInfo.couponDetail.useCondition ? this.data.couponInfo.couponDetail.useCondition.subStoreData : [];
        }

        this.data.subStoreData = subStoreData;

        this.setData({
          couponInfo: this.data.couponInfo,
          subStoreData: this.data.subStoreData
        });
      }
    }).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(){
    if (!this.data.useStoreId){
      wx.showToast({
        title: '未获取到导购门店信息',
        icon : 'none'
      });
      return ;
    }

    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);
            }
          });
        }
      }      
    });
  }
});