<template>
  <div>
    <label class="label-search" for="search-product">Promotora indicante:</label>
    <b-form-input
      id="search-product"
      class="radius-theme"
      placeholder="Digite aqui sua pesquisa"
      v-model="search"
      autocomplete="off"
      @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="item in products" :key="item.id" @click="selected(item)">
            <b-col md="2" class="bressay-bold-italic">{{item.code}}</b-col>
            <b-col md="10" class="bressay-bold-italic">{{item.name}}</b-col>
          </b-row>
        </div>

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

        <p v-else-if="products.length == 0">Nenhuma promotora encontrada.</p>
      </div>
    </transition>
  </div>
</template>

<script>
import SalesConsultantsWebService from "@/services/resources/SalesConsultantsWebService";
const service = SalesConsultantsWebService.build();

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

      var vm = this;

      let filters = {
        limit: 5,
        promoter: true,
        query: this.search
      };

      vm.searching = true;

      service
        .search(filters)
        .then(response => {
          vm.products = response;
          vm.searching = false;
        })
        .catch(err => {
          vm.searching = false;
        });
    },
    selected(item) {
      this.search = item.code + " - " + item.name;
      this.$emit("input", item.id);
      this.$emit("change", item.id);
    }
  },
  mounted() {
    var vm = this;
    vm.searching = true;
    console.log("##");

    console.log(vm.value);
    console.log("##");
    if (vm.value == 0 || typeof vm.value == "undefined") {
      this.$emit("input", 0);
      this.$emit("change", 0);
      return;
    }
    service
      .read({ id: vm.value })
      .then(response => {
        vm.selected(response);
        vm.searching = false;
      })
      .catch(err => {
        vm.searching = false;
      });
  }
};
</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;
    }
    .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>