<template>
  <b-col md="12" class="text-left">
      <label for="" class="text-left">
          Selecione uma consultora e inclua os itens do pedido:
      </label>
    <b-row v-if="isPromoterInOtherOrder || !isPromoter" class="default-card row">
      <b-col md="2">
        <b-form-group class="col-md-12 col-sm-12">
          <text-input title="Campanha" place-holder="Campanha" :disabled='true' v-model="campaign" />
        </b-form-group>
      </b-col>

      <b-col md="5" >
        <sales-consultant-search @change="setSector" v-model="salesConsultant" />
      </b-col>

      <b-col md="2">
        <b-form-group class="col-md-12 col-sm-12">
          <text-input title="Setor" place-holder="Setor" :disabled='true' v-model="sector" />
        </b-form-group>
      </b-col>

      <b-col md="2">
        <b-form-group class>
          <select-input
            title="Atendimento:"
            v-model="service"
            :options="[
             {
              value: null,
              text: '--'  
                },
            {
              value: 'ACTIVE',
              text: 'Ativo'  
                },
                {
              value: 'RECEPTIVE',
              text: 'Receptivo'  
                }
                
                
                ]"
          />
        </b-form-group>
      </b-col>

      <b-col md="1"></b-col>
      <b-col v-if="isPromoter" md='5' class="text-center" offset-md="2" >
<p>
<button  @click="createMyOrder" class="btn btn-primary btn-default text-light">Digitar meu pedido</button>

</p>
      </b-col>
    </b-row>

    <b-row v-else class="default-card row">
      <b-col v-if="isPromoter" md='5'  offset-md="2" >
        <p>
          &nbsp;
        </p>
        <p class="text-left">
          Você está digitando um pedido para você.
        </p>
<p>

<button @click="clearOrder" class="btn btn-primary btn-default text-light">Digitar para outra consultora</button>

</p>
      </b-col>
    </b-row>
  </b-col>
</template>

<script>
import TextInput from "@/components/shared-components/inputs/TextInput";
import SelectInput from "@/components/shared-components/inputs/SelectInput";
import SalesConsultantSearch from "@/components/shared-components/searchs/SalesConsultant";

export default {
  name: "getSalesConsultant",
  components: {
    SelectInput,
    TextInput,
    SalesConsultantSearch
  },
  data: () => {
    return {
      sector: "",
      salesConsultant: null
    };
  },
  computed:{
    campaign:{

      get(){
 let calendar = this.$store.state.orderCreate.calendar;
 console.log(this.$store.state);
      if(calendar){
        return ""+calendar.campaign.name
      }

      return "";
      }
     
    },
    isPromoterInOtherOrder(){
      return this.$store.getters.isPromotherInOtherOrder;
    },
    isPromoter(){
      return this.$store.getters.isPromoter;
    },
    service:{
      get(){
        return this.$store.state.orderCreate.service;
      },
      set(value){
        this.$store.commit('setServiceOrder',value);
      }
    }
  },
  methods:{
    clearOrder(){
     this.$store.dispatch('resetOrderCreate');
    },
      setSector(salesConsultant){
          var vm = this;
          this.$store.dispatch('resetOrderCreate');
          if(salesConsultant === null){
              this.sector = "";
              return;
          }

        this.initOrder(salesConsultant);

              this.sector = salesConsultant.sector.name;

      },
      createMyOrder(){
         this.initOrder(this.$store.state.user.user.sales_consultant);
      },
      initOrder(salesConsultant){
      
        var vm = this;
         this.$store.dispatch('resetOrderCreate');
          this.$store.dispatch('setSalesConsultantIdOrderCreate',salesConsultant);
            this.$store.dispatch('initOrderCreate')
            .catch( err => {
              let resp = JSON.parse(err.response.data);
          vm.$fire({
            title: "Falha ao consultar dados",
            text: resp.msg,
            type: "error"
          });
            });

      }
  },
  mounted(){
   this.salesConsultant = this.$store.state.orderCreate.salesConsultant;
  }
};
</script>

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

label{
    color: #3E3E3E;
    font-family: 'Akkurat Regular';
    font-size: 14px;
}
</style>