import { mapGetters, mapActions } from 'vuex'
import { CurrencyMixin, mapMasterDataId } from '@/app/_global/mixins/index'
import _ from 'lodash'

export default {
  mixins: [CurrencyMixin],
  data() {
    return {
      shippingModel: {},
      currentDeliveryMethod: null,
      orderSummaryDetails: {},
      totalNmpFormatted: null,
      externalDataSet: {},
    }
  },
  props: {
    next: { type: Function },
    isEkSmart: { type: Boolean, default: false },
    showRedeemSummary: { type: Boolean, default: false },
  },
  methods: {
    ...mapActions('eshop', ['saveOrderShipping', 'loadCart']),
    orderProps: function() {
      return {
        summaryRedeem: this.summaryRedeem,
        orderItem: this.mappedCartItems,
        currencySymbol: this.currency,
        summaryCount: this.summaryCount,
        summaryAmount: this.summaryAmount,
        grandTotal: this.getCartTotalPriceFormatted,
        continueText: this.$t('Esac.EsacVoucherRedemption.Button.Continue'),
        showNoteForFocPwpItems: this.getCartPromotions.length > 0,
        showItemImage: true,
        showItemCv: true,
        showEsacVoucher: false,
        esacVoucherItem: [],
        showEwallet: true,
        ewalletAmount: 0,
        showPromoCode: true,
        promoCode: '',
        applyPromoCode: null,
        continueDisabled: false,
        payCycle: this.getCartPayCycle,
      }
    },
    getOnHoldPaymentKey() {
      return [this.getCartHasRental ? 'ek_smart_on_hold_payment_options' : 'general_on_hold_payment_options']
    },
  },
  computed: {
    ...mapMasterDataId({
      SALE_DELIVERY_METHOD: {
        deliveryMethodId: 'DELIVERY',
      },
      PROMOTION_FREE_ITEM_PROMO_TYPES: {
        focId: 'PROMO_TYPE_FOC',
        pwpfId: 'PROMO_TYPE_PWPF',
        pwpnId: 'PROMO_TYPE_PWPN',
      },
    }),
    ...mapGetters('eshop', [
      'getCartPayCycle',
      'getCartItems',
      'getCartTotal',
      'getCartCv',
      'getCartTotalPriceRoundingAdjustmentFormatted',
      'getCartAllTotalPrices',
      'getCartPrice',
      'getCartTotalNpmFormatted',
      'getCartTotalAdminFeeFormatted',
      'getCartTotalDeliveryFeeFormatted',
      'getCartTotalTaxFormatted',
      'getCartTotalPriceFormatted',
      'getCartPromotions',
      'getProductOrKittingDetails',
      'getCartHasRental',
      'getCartCampaignDetails',
      'getITokenCartItems',
    ]),
    ...mapGetters('eshop', {
      hasSubscription: 'getCartHasSubscription',
    }),
    mappedCartItems() {
      let cartItems = _.map(this.getCartItems, function(item) {
        return {
          item_name: item.name,
          product_id: item.product_id,
          kitting_id: item.kitting_id,
          item_image: item.image,
          item_code: item.sku,
          quantity: item.quantity,
          amount: item.total_price,
          cv: item.total_cv.eligible,
          cbp: item.total_cv.cbp_point,
          sv: item.total_cv.sv,
          subItems: item.size ? item.subItems : undefined,
          size: item.size || false,
          free: item.free || false,
          removable: item.is_enrolment_item || item.is_subscription_item || false,
        }
      })
      let self = this
      let promoItems = _.map(this.getCartPromotions, function(item) {
        return {
          item_name: item.name,
          item_code:
            item.promo_type_id === self.focId ? self.$t('Eshop.Order.FocPwp.FOC') : self.$t('Eshop.Order.FocPwp.PWP'),
          subItems: item.products,
          quantity: item.quantity,
          amount: item.total_price,
          cv: 0,
          cbp: 0,
          sv: 0,
          size: false,
          free: item.promo_type_id === self.focId,
          removable: true,
        }
      })
      return _.union(cartItems, promoItems)
    },
    getTotalItems() {
      let totalQty = this.getCartTotal
      totalQty += this.getCartPromotions.reduce((carry, item) => {
        return carry + item.quantity
      }, 0)
      return totalQty
    },
    summaryCount() {
      return [
        {
          label: this.$t('Eshop.Order.Summary.Items'),
          value: this.externalDataSet.totalItems || this.getTotalItems,
        },
        {
          label: this.$t('Common.Terminology.TotalBV'),
          value: this.externalDataSet.totalBv || this.getCartCv.eligible || 0,
        },
        {
          label: this.$t('Common.Terminology.TotalSv'),
          value: this.externalDataSet.totalSv || this.getCartCv.sv || 0,
          show: this.$appSettings.evo.enable,
        },
        {
          label: this.$t('Common.Terminology.TotalCbp'),
          value: this.externalDataSet.totalCbp || this.getCartCv.cbp_point || '0.0000',
          show: this.$appSettings.evo.enable,
        },
      ].filter(data => data.show !== false)
    },
    summaryAmount() {
      return [
        {
          label: this.$t('Common.Sales.TotalNetExcludesGST'),
          value: this.externalDataSet.totalNmpFormatted || this.getCartTotalNpmFormatted,
        },
        {
          label: this.$t('Common.Sales.AdminFee'),
          value: this.externalDataSet.totalAdminFeeFormatted || this.getCartTotalAdminFeeFormatted,
        },
        {
          label: this.$t('Common.Sales.DeliveryFee'),
          value: this.externalDataSet.totalDeliveryFeeFormatted || this.getCartTotalDeliveryFeeFormatted,
        },
        {
          label: this.$t('Common.Sales.TaxAmount'),
          value: this.externalDataSet.totalTaxFormatted || this.getCartTotalTaxFormatted,
        },
        {
          label: this.$t('Common.Sales.RoundingAdjustmentAmount'),
          value:
            this.externalDataSet.totalPriceRoundingAdjustmentFormatted ||
            this.getCartTotalPriceRoundingAdjustmentFormatted,
        },
      ]
    },
    summaryRedeem() {
      if (this.getCartCampaignDetails) {
        return [
          {
            label: this.$t('Itoken.Redemption.TotalPointsEarned'),
            value: this.$helpers.getValue(this.getCartCampaignDetails, 'calculation.total_point', 0),
          },
          {
            label: this.$t('Itoken.Redemption.TotalPointsRedeemed'),
            value: this.$helpers.getValue(this.getCartCampaignDetails, 'calculation.selected_point', 0),
          },
          {
            label: this.$t('Itoken.Redemption.TotalPointsAccumulated'),
            value: this.$helpers.getValue(this.getCartCampaignDetails, 'calculation.accumulated_point', 0),
          },
        ]
      }
    },
  },
  watch: {
    shippingModel() {
      let shippingModel = _.cloneDeep(this.shippingModel)
      shippingModel.validate = true
      this.saveOrderShipping(shippingModel)
      if (
        shippingModel.sale_delivery_method === this.deliveryMethodId ||
        this.currentDeliveryMethod !== shippingModel.sale_delivery_method
      ) {
        this.loadCart({ ekSmart: this.isEkSmart })
      }
      this.currentDeliveryMethod = shippingModel.sale_delivery_method
    },
    orderSummaryDetails: {
      handler(val) {
        this.externalDataSet = val
      },
      deep: true,
    },
  },
}
