<template>
  <div>
    <label class="label-search" for="search-product">Digite o código ou nome do produto:</label>
    <b-form-input
      id="search-product"
      class="radius-theme"
      placeholder="Digite aqui sua pesquisa"
      autocomplete="off"
      :disabled="disabled"
      v-model="search"
      @keyup="fetchProducts"
      @blur="visibleResult = false"
      @focus="visibleResult = true"
    ></b-form-input>
    <transition name="fade">
      <div class="search-result default-card" v-show="visibleResult">
        <div class="result-list">
          <b-row
            v-for="productPerCampaign in products"
            :key="productPerCampaign.id"
            @click="selected(productPerCampaign)"
          >
            <b-col md="8" class="bressay-bold-italic product-name">
              ({{productPerCampaign.product.code}})
              {{productPerCampaign.product.name}}
            </b-col>
            <b-col md="4" class="akkurat-bold-italic product-price">R$ {{ parseFloat(productPerCampaign.catalog_value) | formatCoin }}</b-col>
          </b-row>
        </div>

        <b-spinner medium type="grow" v-if="searching"></b-spinner>

        <p v-else-if="products.length == 0">nenhum produto encontrado</p>
      </div>
    </transition>
  </div>
</template>

<script>
import ProductService from "@/services/resources/ProductPerCampaignService";

const service = ProductService.build();
export default {
  props: {
    value: {
      type: Object
    },
    campaignId: {
      type: Number,
      default: () => {
        return 0;
      }
    },
    disabled:{
      type: Boolean,
      default: () => {
        return false;
      }
    }
  },
  data: () => {
    return {
      products: [],
      search: "",
      searching: false,
      productSelected: null,
      visibleResult: false
    };
  },
  components: {
    productList() {
      return [];
    }
  },
  methods: {
    fetchProducts() {
      this.products = [];
      if (this.search.length < 3) {
        return;
      } else if (this.value) {
        if (this.value.product.name == this.search) {
          return;
        }
        this.$emit("input", null);
        this.$emit("change", null);
      }

      var vm = this;

      let filters = {
        limit: 5,
        name: this.search,
        digitable: true,
        active: true
      };

if(this.campaignId > 0){
 filters.campaign_id = this.campaignId;
}

      vm.searching = true;
      service
        .search(filters)
        .then(response => {
          vm.products = response;
          vm.searching = false;
        })
        .catch(err => {
          console.log(err);
          vm.searching = false;
        });
    },
    selected(productPerCampaign) {
      this.search =  "("+productPerCampaign.product.code+") " + productPerCampaign.product.name;
      this.$emit("input", productPerCampaign);
      this.$emit("change", productPerCampaign);
    },
    clear(){
      this.search = "";
      this.products = [];
    }
  }
};
</script>

<style lang="scss" scoped>
@import "@/assets/css/main.scss";
@import "@/assets/css/cards.scss";

.radius-theme {
  border-radius: $main-border-radius;
}

.label-search {
  text-align: left !important;
  display: block;
}

.search-result {
  background: white;
  margin-top: 3px;
  z-index: 9;
  position: absolute;
  width: 94%;
  .result-list {
    padding: 10px 0px;

    .row {
      margin: 5px 0px;
      cursor: pointer;
      &:hover,
      &:active {
        box-shadow: $box-shadow;
      }
      .product-name {
        font-size: 0.8em;
      }
      .product-price {
        color: #FF9B64;
        letter-spacing: 0.45px;
        font-size: 0.8em;
      }
    }
    .row:not(:last-child) {
      border-bottom: 1px solid #979797;
    }
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: all 0.3s ease;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
  transform: translateY(-10px);
}
</style>