<!-- eslint-disable -->

<script>
import {mapGetters, mapMutations, mapActions, useStore} from "vuex"
import { validation } from './helper'

export default {
  name: 'Pagination',
  components: {
  },
  props: {
    params: {
      type: Object,
      default: () => ({})
    },

  },
  data() {
    return {
      inputValue: null,
      initLocalStorage: null,
    }
  },
  computed: {
    ...mapGetters('main', ['machinesData', 'historyData', 'historyMachines']),
  },
  watch: {
    params: {
      handler(value, oldValue) {
        if(value) {
          const loader = document.querySelector(`#table_loder_${this.params.id}`)
          loader.style.display = 'none'
        }
      },
    },
  },
  mounted () {
    
  },
  methods: {
    ...mapMutations('main', ['setInitHistory', 'setHistoryData', 'setHistoryMachines']),
    selectOptions: (selectOption, paginationOptions) => {
      const options = selectOption ? selectOption : optionsDefault
      const currentOptions = options.filter((el) => {
        if(el <= Math.ceil(paginationOptions.total / 10) * 10) {
        return el
        }
      })
      if(paginationOptions.show_all) {
        currentOptions.push('Все')
      }
      return currentOptions
    },
    currentEntries: (pagination) => {
      const endPageDataIdx = pagination.page_size * pagination.page_number
      const isEndPageDataIdxMore = endPageDataIdx > pagination.total
      if(isEndPageDataIdxMore || pagination.page_size > pagination.total) {
        return (`${pagination.page_size * (pagination.page_number - 1) + 1} ... ${pagination.total}`)
      } else {
        return (`${pagination.page_size * (pagination.page_number - 1) + 1} ... ${pagination.page_size * pagination.page_number}`)
      }
    },
    setPagination: function ({pageNumber, pageSize}) {
      const loader = document.querySelector(`#table_loder_${this.params.id}`)
      loader.style.display = 'block'
      this.params.updatePagination({pageNumber: pageNumber, pageSize: pageSize})
    },
    updateSelectedValue: function (e) {
      const loader = document.querySelector(`#table_loder_${this.params.id}`)
      loader.style.display = 'block'
      this.setPagination({
        pageNumber: 1, // this.params.paginationOptions.page_number,
        pageSize: e.target.value
      })
    },
    updateInputValue: function (e) {
      const pageNumber = e.target.value.trim()
      const buttonPageInput = document.querySelector(`.page-button-${this.params.id}`)
      if(pageNumber) {
        this.inputValue = pageNumber
        buttonPageInput.removeAttribute('disabled')
        buttonPageInput.classList.remove('opacity-50')
      } else {
        buttonPageInput.setAttribute('disabled', 'disabled')
        buttonPageInput.classList.add('opacity-50')
      }
    },
    setPaginationButton: function (e) {
      if(this.inputValue) {
        const error = validation(this.inputValue, this.params.paginationOptions)
        if(Number(this.inputValue) === this.params.paginationOptions.page_number) {
          return false
        }
        if(!error) {
            e.target.removeAttribute('disabled')
            e.target.classList.remove('opacity-50')
            const loader = document.querySelector(`#table_loder_${this.params.id}`)
            loader.style.display = 'block'
            e.target.focus()
            const params = {pageNumber: this.inputValue, pageSize: this.params.paginationOptions.page_size}
            return this.params.updatePagination(params)
        } else {
          e.target.setAttribute('disabled', 'disabled')
          e.target.classList.add('opacity-50')
          const errorObj = {
            id: this.params.id,
            color: "red",
            icon: "<i class='ri-alert-line'></i>",
            message: error,
            delay: 5000,
          }
          console.error(errorObj)
        }
      }
    }
  }
}

</script>

<!-- eslint-disable -->

<template>
  <div class="pagination w-full flex flex-row items-center justify-between gap-3 mt-2 mb-8 xl:mb-0 pb-8 xl:p-2.5 bg-slate-100 border-2 border-solid border-slate-300 rounded-lg flex-col 2xl:flex-row">
      <div class="pagination-left flex flex-row gap-1">
        <div class="current-entries flex text-sm font-bold">Показаны {{currentEntries(params.paginationOptions)}}</div>
        <div class="count-entries flex text-sm font-bold">(Всего записей - {{params.paginationOptions.total}})</div>
      </div>
      <div class="pagination-right flex flex-col xl:flex-row items-center gap-3">
        <div :id="`table_loder_${params.id}`" class="hidden w-full h-auto bg-light flex items-center justify-center px-2"><div type-node="loader" class="flex my_margin-3 items-center justify-center">
          <div role="status">
            <svg aria-hidden="true" class="mr-2 w-6 h-6 text-gray-400 animate-spin dark:text-gray-400 fill-white" viewBox="0 0 100 101" fill="none" >
              <path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/>
              <path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/>
            </svg>
          </div>
        </div></div>
        <div class="buttons flex h-fit gap-1 sm:gap-3 text-sm">
          <div class="flex gap-1 sm:gap-3" v-if="params.paginationOptions.page_number > 1">
            <div v-if="params.paginationOptions.page_number - 1 !== 1">
              <button :table_btn="`${params.id}`" class="previous flex items-center text-white font-bold bg-slate-600 hover:bg-slate-700 py-1 px-3 gap-2 rounded" @click="setPagination({pageNumber: 1, pageSize: params.paginationOptions.page_size})">
                <span>1</span>
              </button>
            </div>
            <button :table_btn="`${params.id}`" class="previous flex items-center text-white font-bold bg-slate-600 hover:bg-slate-700 py-1 px-3 gap-2 rounded" @click="setPagination({pageNumber: params.paginationOptions.page_number - 1, pageSize: params.paginationOptions.page_size})">
              <span class="hidden sm:flex">Назад</span><i class="ri-arrow-left-line sm:hidden"></i>
            </button>
            <button :table_btn="`${params.id}`" class="previous flex items-center text-white font-bold bg-slate-600 hover:bg-slate-700 py-1 px-3 gap-2 rounded" @click="setPagination({pageNumber: params.paginationOptions.page_number - 1, pageSize: params.paginationOptions.page_size})">
              <span>{{params.paginationOptions.page_number - 1}}</span>
            </button>
          </div>
          <button :table_btn="`${params.id}`" class="active flex items-center text-blue-700 font-bold bg-white py-1 px-3 gap-2 rounded cursor-default">
            <span>{{params.paginationOptions.page_number}}</span>
          </button>
          <div class="flex gap-1 sm:gap-3" v-if="params.paginationOptions.page_number < params.paginationOptions.total_pages">
            <button :table_btn="`${params.id}`" class="next flex items-center text-white font-bold bg-slate-600 hover:bg-slate-700 py-1 px-3 gap-2 rounded" @click="setPagination({pageNumber: params.paginationOptions.page_number + 1, pageSize: params.paginationOptions.page_size})">
              <span>{{params.paginationOptions.page_number + 1}}</span>
            </button>
            <button :table_btn="`${params.id}`" class="next flex items-center text-white font-bold bg-slate-600 hover:bg-slate-700 py-1 px-3 gap-2 rounded" @click="setPagination({pageNumber: params.paginationOptions.page_number + 1, pageSize: params.paginationOptions.page_size})">
              <span class="hidden sm:flex">Вперёд</span><i class="ri-arrow-right-line sm:hidden"></i>
            </button>
            <div v-if="params.paginationOptions.page_number + 1 !== params.paginationOptions.total_pages">
              <button :table_btn="`${params.id}`" class="next flex items-center text-white font-bold bg-slate-600 hover:bg-slate-700 py-1 px-3 gap-2 rounded" @click="setPagination({pageNumber: params.paginationOptions.total_pages, pageSize: params.paginationOptions.page_size})">
                <span>{{params.paginationOptions.total_pages}}</span>
              </button>
            </div>
          </div>
        </div>
        <div class="flex flex-row items-center justify-center gap-2"><input v-on:input="updateInputValue" :class="`page-input-${params.id}`" class="flex max-w-[80px] items-center text-slate-700 text-sm font-bold focus-visible:outline-blue-300 bg-white py-1 px-2 gap-2 rounded" value="" placeholder="Страница"/><button @click="setPaginationButton" disabled :class="`page-button-${params.id}`" class="opacity-50 flex items-center text-white font-bold bg-slate-600 hover:bg-slate-700 py-1 px-1 rounded">Перейти</button></div>
        <div id="select-on-page" class="select-on-page flex items-center justify-center gap-3 p-1 text-sm text-slate-700 font-bold">
          Показать
            <select name="on_page" v-bind:value="params.paginationOptions.page_size" v-on:change="updateSelectedValue" :table_option="`${params.id}`" class="flex text-gray-700 pl-3 pr-9 py-0.5 rounded cursor-pointer border-none" style="--tw-ring-color:focus: none" >
              <option :key={idx} v-for="(el, idx) in selectOptions(params.selectOption, params.paginationOptions)" :value="`${el === 'Все' ? params.paginationOptions.total : el}`">
                {{ el }}
              </option>`
            </select>
        </div>
      </div>
    </div>
</template>

<style scoped>
</style>