import { checkOut,calculateDiscount,ordersAndPrepay,currentUnlicensedPlate,unlicensedCarCheckIn,unlicensedCarCheckout } from '@/api/parking';
import { setSelected } from "@/store/order/utils";

export default {
  // 停车优惠(首停、会员减免、消费减免)
  getCheckedTotal( {commit,dispatch},orderDetail ) {
    let checkedTotal = 0;
    if(orderDetail?.parkingRule) {
      commit('setMemberLevelDiscount',orderDetail.discountInfo.memberLevelDiscount);
      commit('setEnableConsume',orderDetail.parkingRule.enableConsume);
    }
    
    if ( orderDetail?.discountInfo?.memberLevelDiscount && orderDetail?.discountInfo?.memberGrade.length ) {

      orderDetail.discountInfo.memberGrade.forEach(( elm ) => {
        if ( elm.hasOwnProperty('selected') ? elm.selected : elm.defaultSelected ) {
          checkedTotal++;
        }
      });
    }
    if ( orderDetail?.discountInfo?.consume?.length ) {
      orderDetail.discountInfo.consume.forEach(( elm ) => {
        if ( elm.hasOwnProperty('selected') ? elm.selected : elm.defaultSelected ) {
          checkedTotal++;
        }
      });
    }
    commit('setCheckedTotal',checkedTotal);
    dispatch('getDiscountDesc',{
      orderDetail,
      checkedTotal,
    });
  },
  getDiscountDesc( {commit,dispatch},{orderDetail,checkedTotal} ) {
    if ( (orderDetail.parkingRule.enableConsume && orderDetail?.discountInfo?.consume?.length) || orderDetail.discountInfo.memberLevelDiscount ) {
      if ( checkedTotal > 0 ) {
        return commit('setDiscountDesc',`已选择${ checkedTotal }项优惠`);
      }
      return commit('setDiscountDesc',`立即使用`);
    } else {
      return commit('setDiscountDesc',`暂无可用优惠`);
    }
  },
  async saveDiscounts( {commit,dispatch},{orderDetail,callback} ) {
    try {
      setSelected(['paperCoupons','coupons','points',],orderDetail)
      const res = await calculateDiscount({
        ...orderDetail,
      });
      console.log(48,res)
      dispatch('orderInitRule',res);
      callback && callback();
    } catch ( err ) {
      console.log('优惠修改失败！',err);
      callback && callback();
    }
  }
}