<template>
  <div>
      <consults-header :isDisable="!isAnalyst" />
    <b-row>
      <b-col cols="12" v-if="salesConsultantsList.length > 0">
        <div v-for="item in salesConsultantsList" :key="item.id">
          <consults-info-card
            :sales_consultant="item"
            :isDisable="isAnalyst"
            @openModal="openModal"
          />
        </div>
      </b-col>
      <b-col cols="12">
          <b-alert :show="salesConsultantsList.length == 0" variant="primary">Sem consultoras em análise no momento.</b-alert>
      </b-col>
    </b-row>

    <consultas-spc-modal v-if="showViewModal" :spc="sales_consultant.spc[0].dados" :status="sales_consultant.status" :sales_consultant_id="sales_consultant.id" @close="closeModal" />
  </div>
</template>

<script>
import SalesConsultantService from "@/services/resources/SalesConsultantsWebService";
import ConsultsInfoCard from "@/components/consults/consults-list/analyzing/ConsultsInfoCard.vue";
import ConsultsHeader from "@/components/consults/consults-list/analyzing/ConsultsHeader.vue";
import ConsultasSpcModal from "@/components/consults/consults-list/modals/ViewModal.vue";

const service = SalesConsultantService.build();

export default {
  name: "product-list",
  props:{
    active:{
      type: Boolean,
      default: false
    }
  },
  components: {
    ConsultsInfoCard,
    ConsultsHeader,
    ConsultasSpcModal
  },
  data() {
    return {
      sales_consultants: [],
      sales_consultant: {
        id: -1,
        spc: [],
        status: []
      },
      pagination:{
        totalPages: 1,
        currentPage: 1,
        perPage: 10
      },
      showViewModal: false

    };
  },
  watch:{
    active(newValue){
      if(newValue)
        this.fetchSalesConsultants();
    },
    showViewModal: function (newValue) {
      if(newValue){
        this.$nextTick(function() {
          this.$bvModal.show('consultas-spc-modal');
        });
      } 
    }
  },
  computed: {
    salesConsultantsList() {
      return this.sales_consultants;
    },
    isAnalyst(){
      var roles = this.$store.state.user.user.roles;
      for(var i = 0; i < roles.length; i++) {
        if(roles[i].name == 'Analyst'){
          return true;
        }
      }

      return false;
    }
  },
  methods: {
    closeModal(reload){
      this.showViewModal = false;
      if(reload)
        this.fetchSalesConsultants();
    },
    openModal(sales_consultant){
      this.showViewModal = true;
      this.sales_consultant = sales_consultant;
    },
    toPage(page){
      this.pagination.currentPage = page;
      this.fetchSalesConsultants();
    },
    perPage(qtd){
      this.pagination.currentPage = 1;
      this.pagination.perPage = qtd;

      this.fetchSalesConsultants();
    },
    fetchSalesConsultants() {
      var vm = this;

      let filters = {
        status: 'SPC_REANALYZE',
        paginate: true,
        current_page: this.pagination.currentPage,
        per_page: this.pagination.perPage
      };
      this.pagination.totalPages = 1;

      service
        .search(filters)
        .then(response => {
          vm.sales_consultants = response.data;
          vm.pagination.totalPages = response.last_page;
        })
        .catch(err => {
          console.log(err);
        });
    }
  },
  mounted() {
    this.fetchSalesConsultants();
  }
};
</script>

<style scoped>
</style>
