import { mapActions, mapGetters } from 'vuex'
import { CurrencyMixin } from '@/app/_global/mixins'
import ITokenCommonMixins from '@/app/i-token/mixins/iTokenCommonMixins'

export default {
  mixins: [ITokenCommonMixins, CurrencyMixin],
  data() {
    return {
      redemptionCartDetails: {},
    }
  },
  computed: {
    ...mapGetters(['getMemberDetails']),
    summaryCount() {
      let totalItems = this.getRedeemedProductList.reduce((accumulate, current) => accumulate + current.quantity, 0)
      return [{ label: this.$t('Eshop.Order.Summary.Items'), value: totalItems }]
    },
    summaryRedeem() {
      return [{ label: this.$t('Itoken.Redemption.TotalPointsRedeemed'), value: this.getTotalPointsRedeemed }]
    },
    summaryAmount() {
      return [
        {
          label: this.$t('Common.Sales.TotalNetExcludesGST'),
          value: this.getPreRedemptionCharges.totalNmpFormatted || 0,
        },
        { label: this.$t('Common.Sales.AdminFee'), value: this.getPreRedemptionCharges.totalAdminFeeFormatted || 0 },
        {
          label: this.$t('Common.Sales.DeliveryFee'),
          value: this.getPreRedemptionCharges.totalDeliveryFeeFormatted || 0,
        },
        { label: this.$t('Common.Sales.TaxAmount'), value: this.getPreRedemptionCharges.totalTaxFormatted || 0 },
        {
          label: this.$t('Common.Sales.RoundingAdjustmentAmount'),
          value: this.getPreRedemptionCharges.totalPriceRoundingAdjustmentFormatted || 0,
        },
      ]
    },
    getGrandTotal() {
      return parseFloat(this.getPreRedemptionCharges.grandTotal || 0)
    },
    orderSummaryProps() {
      return {
        currencySymbol: this.currency,
        continueDisabled: this.isBusy,
        grandTotal: this.getGrandTotal,
        summaryCount: this.summaryCount,
        summaryRedeem: this.summaryRedeem,
        summaryAmount: this.summaryAmount,
        continueText: this.currentRoute.next,
        showPayCycle: this.$appSettings.evo.enable,
        showContinueButton: Boolean(this.currentRoute.showContinueButton),
        payCycle: this.$helpers.getValue(this.redemptionCartDetails, 'cw.cw_name', '-'),
      }
    },
    getPreRedemptionCharges() {
      return {
        totalNmpFormatted: this.redemptionCartDetails.total_nmp_formatted || 0,
        totalAdminFeeFormatted: this.redemptionCartDetails.total_admin_fee_formatted || 0,
        totalDeliveryFeeFormatted: this.redemptionCartDetails.total_delivery_fee_formatted || 0,
        totalTaxFormatted: this.redemptionCartDetails.total_tax_formatted || 0,
        totalPriceRoundingAdjustmentFormatted:
          this.redemptionCartDetails.total_price_rounding_adjustment_formatted || 0,
        grandTotal: this.redemptionCartDetails.total_price_formatted || 0,
      }
    },
  },
  methods: {
    ...mapActions('itoken', ['loadITokenRedemptionChargesStatement']),
    async getRedemptionDeliveryFees(shippingInfo = false) {
      let payload = {
        voucherId: this.voucherDetails.id,
        selectedProducts: this.getRedeemedProductList,
        shippingInfo: shippingInfo || this.getITokenRedemptionFormDetails.shippingInfo,
      }

      await this.loadITokenRedemptionChargesStatement(payload).then(result => {
        this.redemptionCartDetails = { ...(result || {}) }
      })
    },
  },
}
