<template>
  <div>
    <label class="label-search" for="search-campaign">Digite o código ou nome da Campanha:</label>
    <b-form-input
      id="search-campaign"
      class="radius-theme"
      placeholder="Digite aqui sua pesquisa"
      autocomplete="off"
      :disabled="disabled"
      v-model="search"
      @keyup="fetchCampaigns"
      @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="campaign in campaigns"
            :key="campaign.id"
            @click="selected(campaign)"
          >
            <b-col md="8" class="bressay-bold-italic product-name">
              ({{campaign.code}})
              {{campaign.name}}
            </b-col>
           
          </b-row>
        </div>

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

        <p v-else-if="campaigns.length == 0">nenhuma campanha encontrada</p>
      </div>
    </transition>
  </div>
</template>

<script>
import CampaignService from "@/services/resources/CampaignService";

const service = CampaignService.build();
export default {
  name: 'search-campaigns',
  props: {
    value: {
      type: Object
    },
    disabled:{
      type: Boolean,
      default: () => {
        return false;
      }
    }
  },
  data: () => {
    return {
      campaigns: [],
      search: "",
      searching: false,
      campaignSelected: null,
      visibleResult: false
    };
  },
  watch:{
    value(newValue){
      if(newValue == null){
return;
      }
      this.selected(newValue);
    }
  },
  components: {
    campaignList() {
      return [];
    }
  },
  methods: {
    fetchCampaigns() {
      this.campaigns = [];
      if (this.search.length < 3 && !this.value) {
        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,
      };


      vm.searching = true;
      service
        .search(filters)
        .then(response => {
          vm.campaigns = response;
          vm.searching = false;
        })
        .catch(err => {
          console.log(err);
          vm.searching = false;
        });
    },
    selected(campaign) {
      this.search = campaign.name;
      this.$emit("input", campaign);
      this.$emit("change", campaign);
    },
    clear(){
      this.search = "";
      this.campaigns = [];
    }
  }
};
</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>