<template>
  <div class="redesigned Registered">
    <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>
      <re-table
        selection="multiple"
        :selected.sync="selected"
        :loading="loading"
        :data="data"
        :columns="columns"
        :pagination.sync="pagination"
        :check-selectable="checkStatuses"
        @request="onRequest"
        :rows-per-page-options="[10, 25, 50, 100]"
      >
      </re-table>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Mixins, Watch } from 'vue-property-decorator';
import { Option, UrlQueryParams } from '@/recruits/models';
import {
  PersonRegistryApi,
  SearchRecruitsResponseWithList,
} from '@/recruits/openapi/person-registry';
import { formatDate, prepareMilitaryRequestParams } from '@/recruits/utils';
import ReTable from '@/recruits/components/common/r-table/index.vue';
import SortBar from '@/recruits/components/common/sort-bar.vue';
import SearchBar from '@/recruits/components/for-cancel-registration/search-bar.vue';

import UrlIntegratedSearch from '@/recruits/mixins/url-integrated-search';
import SearchFilter from './search-filter';
import { RTableColumn } from '@/interfaces';

@Component({
  methods: {
    formatDate,
  },
  components: {
    SortBar,
    SearchBar,
    ReTable,
  },
})
export default class ConflictPerData extends Mixins(UrlIntegratedSearch) {
  RecruitApi = new PersonRegistryApi();

  public filter = new SearchFilter();

  public sortButtons: Array<Option> = [
    { label: 'По ФИО', value: 'fio' },
    { label: 'По дате рождения', value: 'birthDate' },
    { label: 'По ИД ЕРН', value: 'idern' },
    { label: 'По серии паспорта', value: 'document.series' },
    { label: 'По номеру паспорта', value: 'document.number' },
    { label: 'По статусу', value: 'status' },
  ];

  columns: RTableColumn<SearchRecruitsResponseWithList>[] = [
    {
      name: 'person',
      fullLabel: 'Данные гражданина',
      fields: [
        {
          label: 'ФИО',
          name: 'fullName',
          field: 'fullName',
          width: '100%',
          componentPath: 'link',
          componentOptions: {
            route: 'recruit-personal',
          },
        },
        {
          label: 'ИД ЕРН',
          name: 'idern',
          field: 'idern',
          width: 'auto',
          type: 'gray-numbrts',
        },
      ],
      width: '0.9fr',
      align: 'left',
    },
    {
      label: 'Адрес регистрации',
      name: 'regAddress',
      field: 'regAddress',
      width: '2fr',
      align: 'left',
      type: 'address',
    },
    {
      label: 'Статус',
      name: 'status',
      field: 'status',
      width: '0.7fr',
      align: 'center',
      borderless: true,
      collaps: true,
      componentPath: 'status-box',
      componentOptions: {
        type: 'recruit',
      },
      // class: 'text-grey',
    },
  ];

  selected: Array<SearchRecruitsResponseWithList> = [];

  public data: Array<SearchRecruitsResponseWithList> | undefined = [];

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

  @Watch('selected')
  rowSelected(val: unknown[]): void {
    this.$emit('row-selected', val);
  }

  public checkStatuses(row?: SearchRecruitsResponseWithList): boolean {
    if (!row?.status) return false;
    return ['14', '8.15'].includes(row?.status);
  }
}
</script>

<style lang="scss" scoped>
.flip-icon::v-deep {
  i {
    transform: scaleX(-1);
  }
}
</style>
