<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"
        @row-selected="rowSelected"
        :resetSelect="resetSelect"
        :rows-per-page-options="[10, 25, 50, 100]"
      />
    </div>
  </div>
</template>

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

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

@Component({
  components: {
    DataTable,
    SearchBar,
    SortBar,
  },
})
export default class Recruits extends Mixins(UrlIntegratedSearch) {
  @Prop()
  resetSelect: boolean;

  @Prop()
  public listId: string;

  RecruitApi = new PersonRegistryApi();

  public sortButtons: Array<SortingOption<TargetsVKRecord>> = [
    { label: 'По ФИО', value: 'fullName' },
    { label: 'По дате рождения', value: 'birthDate' },
    { label: 'По ИД ЕРН', value: 'idern' },
    { label: 'По СНИЛС', value: 'snils' },
    { label: 'По статусу', value: 'status' },
  ];

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

  public async fetchData(
    query: UrlQueryParams
  ): Promise<{ totalRows: number | undefined }> {
    const params = prepareApiParams(query);
    const { data } = await this.RecruitApi.getNotCurrentVK(params);
    this.data = data.data || [];
    this.$emit('data-loading', { data: this.data, loading: false });
    return {
      totalRows: data.totalRows,
    };
  }

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