<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>

<style lang="scss" scoped></style>

<script lang="ts">
import { Component, Mixins } from 'vue-property-decorator';

import SortBar from '@/recruits/components/common/sort-bar.vue';
import { SortingOption, UrlQueryParams } from '@/recruits/models';
import DataTable from './data-table.vue';
import SearchBar from './search-bar.vue';
import {
  SubpoenaRegistryApi,
  SubpoenaListResponseV1,
} from '@/recruits/openapi/subpoena-service';
import UrlIntegratedSearch from '@/recruits/mixins/url-integrated-search';
import { SearchFilterUtils } from '@/recruits/utils/search-filter';
import { prepareApiParams } from '@/recruits/utils/prepare-api-params';

@Component({
  components: {
    DataTable,
    SearchBar,
    SortBar,
  },
})
export default class Subpoenas extends Mixins(UrlIntegratedSearch) {
  SubpoenaApi = new SubpoenaRegistryApi();

  public filter = new SearchFilterUtils();

  public sortButtons: Array<SortingOption<SubpoenaListResponseV1>> = [
    { label: 'По ФИО', value: 'fullName' },
    { label: 'По ИД ЕРН', value: 'idERN' },
    { label: 'По статусу', value: 'status' },
  ];

  public data: Array<SubpoenaListResponseV1> | undefined = [];
  // AccountChangesListSearchV1
  public async fetchData(
    query: UrlQueryParams
  ): Promise<{ totalRows: number | undefined }> {
    const params = prepareApiParams(query);
    const { data } = await this.SubpoenaApi.getSubpoenasList(params);
    this.data = data.data;
    return {
      totalRows: data.totalRows,
    };
  }
}
</script>
