<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, Prop, Mixins } from 'vue-property-decorator';

import SortBar from '@/recruits/components/common/sort-bar.vue';
import { SortingOption, UrlQueryParams } from '@/recruits/models';
import {
  PersonRegistryApi,
  SearchRecruitResponse,
} from '@/recruits/openapi/person-registry';
import DataTable from './data-table.vue';
import SearchBar from './search-bar.vue';
/** Подключаем глобальную функцию подготовки данных для отправки данных на бэк **/
import { prepareApiParams } from '@/recruits/utils/prepare-api-params';
/** Подключаем глобальный класс для фильтров **/
import { SearchFilterUtils } from '@/recruits/utils/search-filter';
import UrlIntegratedSearch from '@/recruits/mixins/url-integrated-search';

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

  @Prop()
  public listId: string;

  RecruitApi = new PersonRegistryApi();

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

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

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

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