<template>
  <div>
   <!--
     Espaço para adicionar modal, atualmente está com sweetalert
     -->
  </div>
</template>

<script>

export default {
  name: "alert-quantity-limit-per-order",
  computed: {
    products() {
      return this.$store.getters.getOrder();
    }
  },
  watch: {
    products: {
      handler: function(products) {
        for (let i = 0; i < products.length; i++) {
          if (products[i].product_per_campaign.quantity_limit_per_order < 0) {
            continue;
          }

          if (
            products[i].quantity >
            products[i].product_per_campaign.quantity_limit_per_order
          ) {
            this.$fire({
              title: "Limite excedido.",
              text:
                "Quantidade máxima do produto " +
                products[i].product_per_campaign.product.name +
                " no pedido é " +
                products[i].product_per_campaign.quantity_limit_per_order
            });

            if (
              products[i].product_per_campaign.quantity_limit_per_order === 0
            ) {
              this.$store.dispatch('removeProduct',i);
            } else {
              this.$store.dispatch("updateProductQuantity", {
                product_per_campaign: products[i].product_per_campaign,
                quantity:
                  products[i].product_per_campaign.quantity_limit_per_order
              });
            }

            break;
          }
        }
      },
      deep: true
    }
  },
};
</script>
