<template>
  <div>
    <label class="label-search" for="search-sales-consultants">Digite o código ou nome da consultora:</label>
    <b-form-input
      id="search-sales-consultants"
      class="radius-theme"
      placeholder="Digite aqui código ou nome"
      autocomplete="off"
      v-model="search"
      @keyup="fetchSalesConsultants"
      @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="salesConsultant in salesConsultants"
            :key="salesConsultant.id"
            @click="selectedSalesConsultant(salesConsultant)"
          >
            <b-col md="8" class="bressay-bold-italic sales-consultant-name">
              ({{salesConsultant.code}})  {{salesConsultant.name}}
            </b-col>
        
          </b-row>
        </div>

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

        <p class="text-center" v-else-if="salesConsultants.length == 0">Nenhuma consultora encontrada</p>
      </div>
    </transition>
  </div>
</template>

<script>
import SalesConsultantService from "@/services/resources/SalesConsultantsService";

const service = SalesConsultantService.build();
export default {
  props: {
    value: {
      type: Object
    }
  },
  data: () => {
    return {
      salesConsultants: [],
      search: "",
      searching: false,
      salesConsultantSelected: null,
      visibleResult: false
    };
  },
  watch:{
    value(newValue){
      if(newValue == null){
return;
      }
      this.selected(newValue);
    }
  },
  methods: {
    fetchSalesConsultants() {
      this.salesConsultants = [];
      if (this.search.length < 3) {
        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,
        name: this.search,
        campaign_id: 139
      };
      vm.searching = true;
      service
        .search(filters)
        .then(response => {
          vm.salesConsultants = response;
          vm.searching = false;
        })
        .catch(err => {
          console.log(err);
          vm.searching = false;
        });
    },
    selectedSalesConsultant(salesConsultant){
      this.$emit("input", salesConsultant);
      this.$emit("change", salesConsultant);
    
    },
    selected(salesConsultant) {
     
      this.search = "("+salesConsultant.code+ ") " +salesConsultant.name;
     // this.$emit("input", salesConsultant);
     // this.$emit("change", salesConsultant);
    },
    clear(){
      this.search = "";
      this.salesConsultants = [];
    }
  }
};
</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;
      }
      .sales-consultant-name {
        font-size: 0.8em;
      }
      .sales-consultant-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>