<template>
  <div class="row Registered-Content">
    <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>
    <data-table
      :loading="loading"
      :data="data"
      :resetSelect="resetSelect"
      :pagination.sync="pagination"
      @request="onRequest"
      :rows-per-page-options="[10, 25, 50, 100]"
      @row-selected="rowSelected"
    />
  </div>
</template>

<script lang="ts">
import {Component, Mixins, Prop} from 'vue-property-decorator';
import DataTable from '@/recruits/components/recruits-to-call/data-table.vue';
import { SortingOption, UrlQueryParams } from '@/recruits/models';
import {
  PersonRegistryApi,
  RecruitsForListsRecord,
} from '@/recruits/openapi/person-registry';
import SortBar from '@/recruits/components/common/sort-bar.vue';
import SearchBar from '@/recruits/components/recruits-to-call/search-bar.vue';

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,
    SortBar,
    SearchBar,
  },
})
export default class RecruitsToCall extends Mixins(UrlIntegratedSearch) {
  @Prop()
  resetSelect: boolean;

  RecruitApi = new PersonRegistryApi();

  public filter = new SearchFilterUtils();

  public sortButtons: SortingOption<RecruitsForListsRecord>[] = [
    { label: 'По ФИО', value: 'fullName' },
    { label: 'По статусу', value: 'status' },
    { label: 'По полу', value: 'gender' },
  ];

  public data: RecruitsForListsRecord[] = [];

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

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

<style lang="scss" scoped>
.Registered {
  &-Header {
    font-family: 'Golos Text', sans-serif;
    font-style: normal;
    font-weight: 500;
    font-size: 28px;
    line-height: 32px;
    letter-spacing: -0.02em;
    color: rgba(2, 12, 18, 0.9);
  }
}
.flip-icon::v-deep {
  i {
    transform: scaleX(-1);
  }
}
</style>
