import { calculateDiscount } from "@/api/parking";
import {paperCouponDescription} from '@/utils/api-crm-member'
import { setSelected } from "@/store/order/utils";
import uni from '@/utils/uniHooks'; 

export default {
  /*
   *   setPaperCoupons( state,payload ) {
   state.paperCoupons = payload;
   },
   setPaperCouponCount( state,payload ) {
   state.paperCouponCount = payload;
   },
   setPaperCouponDesc( state,payload ) {
   state.paperCouponDesc = payload;
   },
   * 
   * */
  async paperCouponRule( {commit,dispatch,state},checkOutResponse ) {
    const groupId = uni.getStorageSync('groupId')
    const mallId = uni.getStorageSync('mallId')
    if(!mallId ||!mallId) {
      commit('setEnablePaperCoupons', false);
      return
    }
    const res = await paperCouponDescription({
      groupId: groupId,
      mallId: mallId
    })
    console.log(30, res)
    if (res.msg === 'success' && res.data) {
      commit('setEnablePaperCoupons', true);
      commit('setPaperCouponsDesc', res.data)
    }
  },
  async savePaperCoupon( {commit,dispatch,state},{paperCoupons,callback} ) {
    try {
      const orderDetail = {...state.orderDetail};
      setSelected(['coupons','consume','points','memberGrade'], orderDetail)
      if (!orderDetail?.discountInfo?.paperCoupons) {
        orderDetail.discountInfo =  {
          paperCoupons: []
        }
      } 
      orderDetail.discountInfo.paperCoupons = paperCoupons.map(elm => {
        return {
          ...elm,
          selected: true,
        }
      })
      const res = await calculateDiscount({
        ...orderDetail,
      });
      dispatch('orderInitRule',res);
      callback && callback();
    } catch ( err ) {
      console.log('积分修改失败！',err);
      callback && callback();
    }
  }
}