<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 { Option, UrlQueryParams } from '@/recruits/models';

import {
  PersonRegistryApi,
  SearchRecruitsResponseWithList,
} from '@/recruits/openapi/person-registry';

import UrlIntegratedSearch from '@/recruits/mixins/url-integrated-search';

import DataTable from './data-table.vue';
import SearchBar from './search-bar.vue';
import SortBar from '@/recruits/components/common/sort-bar.vue';
import { SearchFilterUtils } from '@/recruits/utils/search-filter';
import { prepareApiParams } from '@/recruits/utils/prepare-api-params';

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

  private personRegistryApi = new PersonRegistryApi();
  public filter = new SearchFilterUtils();
  public data: Array<SearchRecruitsResponseWithList> | undefined = [];

  public sortButtons: Array<Option> = [
    { label: 'По ФИО', value: 'fullName' },
    { label: 'По ИД ЕРН', value: 'idERN' },
  ];

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

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