<template>
  <div class="row">
    <div class="col-12">
      <search-bar v-model="filter" @search="executeSearch" :loading="loading" />
    </div>
    <div class="col-12">
      <sort-bar
        v-model="sortButtons"
        :pagination="pagination"
        @click="handleSortClick"
        :loading="loading"
      />
    </div>
    <div class="col-12">
      <!-- Таблица с данными -->
      <data-table
        :loading="loading"
        :data="data"
        :pagination.sync="pagination"
        @request="onRequest"
        :rows-per-page-options="[10, 25, 50, 100]"
      />
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Mixins } from 'vue-property-decorator';
import SortBar from '@/recruits/components/common/sort-bar.vue';
import { Option, UrlQueryParams } from '@/recruits/models';
import DataTable from './data-table.vue';
import SearchBar from './search-bar.vue';
import {
  JournalServiceApi,
  JournalErrorRecord,
} from '@/recruits/openapi/journal-service';
import UrlIntegratedSearch from '@/recruits/mixins/url-integrated-search';
import RefreshKeyInterceptor from '@/recruits/mixins/refresh-key-interceptor';
import { QPagination } from '@models/models-ui';
import { prepareApiParams } from '@/recruits/components/errorsImport/prepare-api-params';
import { ErrorsImportSearchFilter } from '@/recruits/components/errorsImport/search-filter';

@Component({
  components: {
    DataTable,
    SearchBar,
    SortBar,
  },
})
export default class ErrorsImport extends Mixins(
  UrlIntegratedSearch,
  RefreshKeyInterceptor
) {
  JournalApi = new JournalServiceApi();

  public filter = new ErrorsImportSearchFilter();

  public sortButtons: Array<Option> = [
    { label: 'По дате создания', value: 'dateTime' },
    { label: 'По ФИО', value: 'memberName' },
  ];

  public data: JournalErrorRecord[] | undefined = [];

  pagination: QPagination = {
    sortBy: '',
    descending: false,
    page: 1,
    rowsPerPage: 50,
  };

  // @ts-ignore:next-line
  public async fetchData(
    query: UrlQueryParams
  ): Promise<{ totalRows: number | undefined } | undefined> {
    const params = prepareApiParams(query);
    try {
      // const id = String(this.$route.query?.importId) || undefined;
      const { data } = await this.JournalApi.getImportErrorsJournal(
        params,
        // @ts-ignore:next-line
        this.$route.query?.importId
      );
      this.data = data.data;
      return {
        totalRows: data.totalRows,
      };
    } catch (error) {
      console.log(error.message);
    }
  }
}
</script>
