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

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

<script lang="ts">
import { Component, Mixins, Prop } from 'vue-property-decorator';
import {
  PersonRegistryApi,
  Notification,
} from '@/recruits/openapi/person-registry';
import SortBar from '@/recruits/components/common/sort-bar.vue';
import { SortingOption, UrlQueryParams } from '../../models';
import DataTable from './data-table.vue';
import SearchBar from './search-bar.vue';
import { SearchFilterUtils } from '@/recruits/utils/search-filter';
import UrlIntegratedSearch from '@/recruits/mixins/url-integrated-search';
import { prepareApiParams } from '@/recruits/utils/prepare-api-params';

@Component({
  components: {
    DataTable,
    SearchBar,
    SortBar,
  },
})
export default class RegistrationSigning extends Mixins(UrlIntegratedSearch) {
  RecruitApi = new PersonRegistryApi();

  @Prop()
  resetSelect: boolean;

  public sortButtons: Array<SortingOption<Notification>> = [
    { label: 'По наименованию ВК', value: 'nameMilitaryCommissariat' },
    { label: 'По ФИО', value: 'recruitFullName' },
    { label: 'По ИД ЕРН', value: 'idERN' },
    { label: 'По дате рождения', value: 'birthDate' },
    { label: 'По статусу', value: 'status' },
  ];

  public filter = new SearchFilterUtils();
  public data: Array<Notification> | undefined = [];

  public async fetchData(
    query: UrlQueryParams
  ): Promise<{ totalRows: number | undefined }> {
    const params = prepareApiParams(query);
    const { data } = await this.RecruitApi.getNotificationsV1(params);
    this.data = data.data || [];
    return {
      totalRows: data.totalRows,
    };
  }

  rowSelected(val: string[]): void {
    this.$emit('row-selected', val);
  }
}
</script>
