import axiosApi from "@assets/js/axios-api";
import param from "@assets/js/param";

export default {
  name: 'listPagination',
  props: {
    pagination: '',
    currentPage: {
      type: Number,
      default: 1,
    },
    limit: {
      type: Number,
      default: 12,
    },
  },
  data() {
    return {
      nextPage : this.currentPage + 1,
      prevPage : this.currentPage - 1,
      lastPage : this.pagination[this.pagination.length - 1],
      renderPagination: this.pagination.filter(value => value <= 11),
      oldPagination: this.pagination.length,
      legendPagination:
        {
          labelLegend : "Exibindo de <b class='text-small'>"+((this.currentPage * this.limit) - (this.limit - 1))+" a "+ ( (this.pagination.length + 1) < this.limit ? (this.pagination.length + 1) : (this.currentPage * this.limit)) +" </b> do total de <b class='text-small'>"+((this.pagination.length +1 < this.limit) ? this.pagination.length +1  : (this.pagination.length * this.limit)) + " | "+(this.pagination.length)+"</b> Página(s)",
        }
    };
  },
  methods: {
    selected(SelectedPage) {
      this.renderPagination = SelectedPage >= this.lastPage - 5 ?  this.pagination.filter(value => value >= this.lastPage - 10) : (SelectedPage > 5 ? this.pagination.filter(value => value <= (SelectedPage + 5) && value >= (SelectedPage - 5)) : this.pagination.filter(value => value <= 11));
      this.legendPagination.labelLegend = "Exibindo de <b class='text-small'>"+((SelectedPage * this.limit) - (this.limit - 1))+" a "+ (SelectedPage * this.limit)+"</b> do total de <b class='text-small'>"+(this.pagination.length * this.limit )+" | "+(this.pagination.length)+"</b> Página(s)";
      this.nextPage = SelectedPage + 1;
      this.prevPage = SelectedPage - 1;
      this.$emit('callback', SelectedPage);
    },
  },
  watch: {
    pagination(){
      if(this.pagination.length != this.oldPagination){
        this.oldPagination = this.pagination.length;
        this.lastPage = this.pagination[this.pagination.length - 1],
        this.currentPage = 1;
        this.selected(this.currentPage);
      }
    }
  },
}