<template>
  <div>
    <div v-for="(productOrKitting, index) in getCartInstItems" :key="index">
      <product-inst-address-selection
        :identifier="`productSelection${index}`"
        :e-voucher-enabled="eVoucherEnabled"
        :product-id="productOrKitting.product_id"
        :kitting-id="productOrKitting.kitting_id"
        :img="productOrKitting.image"
        :title="productOrKitting.name"
        :quantity="productOrKitting.quantity"
        :cv="productOrKitting.total_cv.basic"
        :amp="typeof productOrKitting.total_cv.amp !== 'undefined'"
        :amp-cv="productOrKitting.total_cv.amp"
        :fpp="typeof productOrKitting.total_cv.fpp !== 'undefined'"
        :installation-date="productOrKitting.installation_date"
        :installation-time="productOrKitting.installation_time"
        :fpp-cv="productOrKitting.total_cv.fpp"
        :is-dummy-code="productOrKitting.is_dummy_code"
        :is-free="productOrKitting.free"
        :code="productOrKitting.sku"
        :price="productOrKitting.price"
        @change="saveChanges()"
        ref="productInstAddressSelection"
      />
    </div>
  </div>
</template>
<script>
import moment from 'moment'
import { mapGetters, mapActions } from 'vuex'
import ProductInstAddressSelection from '@/app/_global/components/blocks/ProductInstAddressSelection'

export default {
  name: 'SelectSizes',
  inject: ['$validator'],
  components: { ProductInstAddressSelection },
  props: {
    eVoucherEnabled: {
      type: Boolean,
      default: true,
    },
  },
  async created() {
    await this.loadCartSizesDetails()
    this.removeError('cartError')
  },
  methods: {
    ...mapActions('eshop', ['loadCartSizesDetails', 'selectCartProductsSizes', 'selectCartProductsSizes']),
    ...mapActions(['logError', 'removeError']),
    async validate() {
      this.removeError('cartError')
      let result = false
      if (this.getCartInstDetails.inst_details.length > 0) {
        let instDate = this.getCartInstDetails.inst_details[0].installation_date
        let instSession = this.getCartInstDetails.inst_details[0].installation_time
        result = moment(instDate).isAfter(moment()) && !!instSession
      }

      if (!result) {
        this.logError({ cartError: this.$t('Eshop.InstSelection.Invalid') })
        await this.$swal({
          title: 'Attention !!',
          text: this.$t('Invalid Installation Date'),
          type: 'error',
          timer: '5000',
          confirmButtonClass: 'outline-primary',
        })
      }
      return result
    },
    selectedEvoucherQuantity(productId, kittingId) {
      let quantity = 0
      if (this.$helpers.isSet(this.getCartIsAllDummiesSelected, 'evoucher')) {
        let selectedDumy = this.getCartIsAllDummiesSelected['evoucher'].find(evoucher => {
          return evoucher.product_id === productId && evoucher.kitting_id === kittingId
        })
        quantity = selectedDumy ? selectedDumy.quantity : 0
      }
      return quantity
    },
    selectedSizes(productId, kittingId) {
      let selectedSizes = []
      if (this.$helpers.isSet(this.getCartIsAllDummiesSelected, 'sizes')) {
        selectedSizes =
          this.getCartIsAllDummiesSelected['sizes'].filter(size => {
            return (
              (size.kitting_id !== null && size.kitting_id !== null && size.kitting_id === kittingId) ||
              (size.product_id !== null && size.product_id !== null && size.product_id === productId)
            )
          }) || []
      }
      return selectedSizes
    },
    saveChanges() {
      let selectedInstDetails = {
        inst_details: [],
      }
      this.selectCartProductsInstDetails(selectedInstDetails)
    },
    getProductInstDate(product) {
      let inst_date = ''
      let inst_details = this.getCartProductInstDetails(product)
      if (inst_details) {
        inst_date = inst_details['installation_date']
      }
      return inst_date
    },
    getProductInstTime(product) {
      let inst_time = ''
      let inst_date = ''
      let inst_details = this.getCartProductInstDetails(product)
      if (inst_details) {
        inst_date = inst_details['installation_time']
      }
      return inst_time
    },
  },
  computed: {
    ...mapGetters('eshop', [
      'getCartProductQuantity',
      'getProductOrKittingDummies',
      'getCartIsAllDummiesSelected',
      'getCartInstItems',
      'getCartSelectedInstDetails',
      'getCartProductInstDetails',
      'getCartInstDetails',
    ]),
  },
}
</script>
