import checkOutQHResponse from "@/api/mockData/checkout.sy5.response.json";
import { checkOut,calculateDiscount,ordersAndPrepay,currentUnlicensedPlate,unlicensedCarCheckIn,unlicensedCarCheckout } from '@/api/parking';
import state from "@/store/order/state";
import mutations from "@/store/order/mutations";
import memberLevel from "@/store/order/memberLevel";
import points from "@/store/order/points";
import coupon from "@/store/order/coupon";
import consume from "@/store/order/consume";
import unlicensed from "@/store/order/unlicensed";
import paperCoupon from "@/store/order/paperCoupon";

export default {
  //开启命名模块
  namespaced: true,
  state: state,
  mutations,
  actions: {
    /**
     * 车辆订单的缴费信息初始化
     * @param commit
     * @param dispatch
     * @param state
     * @param gateId      车场闸机口ID
     * @param vehicleNo   车牌号（包含临牌）
     * @param callback    回调函数，用于接口报错时，展示对应的信息
     * @param endlessLoop 用于判断当前用户是否扫码进入
     */
    async orderInit( {commit,dispatch,state},{gateId = '124p3LK1',vehicleNo = '浙A616A1',callback,endlessLoop} ) {
      try {
        commit('clearOrderInfo');
        const unlicensed = vehicleNo.indexOf('临') > -1 || !vehicleNo; // true: 临时车牌；false：普通车牌
        // const res = await checkOut('浙A616A1');
        // const res = await checkOut('闽AAQ5519', unlicensed);
        // const res = await checkOut('粤A51113');
        // const res = await checkOut('沪DCJ986');
        // console.log('无牌车扫码出场', endlessLoop);
        // 场内缴费，调 check-out 接口，
        const method = unlicensed && endlessLoop || !vehicleNo ? unlicensedCarCheckout : checkOut
        const res = await method(vehicleNo, unlicensed, gateId);
        // const res = checkOutQHResponse
        if ( res?.parkingRecord?.totalFee <= 0 ) {
          callback && callback({
            unlicensed,
            endlessLoop,
            code: endlessLoop,
            orderDetail: res,
          }); // 统一处理错误原因
          return
        }
        // console.log(vehicleNo, res);
        // 所有的优惠时间长转为金额
        // console.log(112,'所有的优惠时间长转为金额');
        // dispatch('orderInitRule', checkOutQHResponse);
        dispatch('orderInitRule',res);
      } catch ( error ) {
        // console.log('151151151151151',error);
        /* if (/NOT_FOUND|PARKING_RECORD_NOT_FOUND/.test(error.code)) {
         callback && callback(error)
         } */
        callback && callback(error); // 统一处理错误原因
      }
    },
    orderInitRule( {commit,dispatch,state},checkOutResponse ) {
      // console.log(119,checkOutResponse);
      // 记录buildingId
      if ( checkOutResponse?.parkInfo?.buildingId ) {
        window.localStorage.setItem('buildingId',checkOutResponse.parkInfo.buildingId);
      }
      commit('setParkMallCode', checkOutResponse.parkInfo.parkMallCode)
      commit('setOrderDetail',checkOutResponse);
      const isShowDiscounts = checkOutResponse?.parkingRule?.enableCoupon || false;
      // 纸质优惠券
      dispatch('paperCouponRule')
      // 是否展示优惠
      commit('setIsShowDiscounts',isShowDiscounts);
      if ( checkOutResponse.parkingRule ) {
        // 会员等级减免
        // 停车优惠(首停、会员减免、消费减免)
        dispatch('getCheckedTotal',checkOutResponse);
        // 是否开启新会员积分(hold)
        commit('setEnableNewMemberPoints',checkOutResponse.parkingRule.enableNewMemberPoints);
        // 积分减免
        dispatch('pointsRule',checkOutResponse);
        // dispatch('getIntegralDesc', checkOutResponse.parkingRule.enablePoints);
        // 电子优惠券：是否启用
        // 处理电子优惠券相关逻辑
        dispatch('couponRule',checkOutResponse);
      }
      // 其他数据收集
      commit('setUsingTotalDiscount',checkOutResponse?.discountInfo?.usingTotalDiscount || 0);
      if ( checkOutResponse?.parkingRecord?.hasOwnProperty('actualPayFee') ) {
        commit('setActualPayFee',checkOutResponse.parkingRecord?.actualPayFee);
      } else {
        commit('setActualPayFee',checkOutResponse.parkingRecord?.totalFeeInYuan);
      }
    },
    // 会员等级
    ...memberLevel,
    // 消费减免
    ...consume,
    // 积分
    ...points,
    // 电子券
    ...coupon,
    // 无牌车
    ...unlicensed,
    // 纸质优惠券
    ...paperCoupon
  },
};