<template>
  <div class="ErrorsImportSearchBar">
    <div class="ErrorsImportSearchBar-SearchBlock">
      <q-input
        v-model="filter.search"
        dense
        outlined
        placeholder="Введите для поиска"
        @keydown.enter="executeSearch"
        @keydown.esc="filter.search = ''"
        class="ErrorsImportSearchBar-Search"
      >
        <template #prepend>
          <q-icon name="pgs-search" />
        </template>
      </q-input>
      <q-btn
        icon="pgs-filter"
        class="ErrorsImportSearchBar-FilterIcon"
        @click="toggleFilter"
      >
        <q-badge
          floating
          v-if="!!filterCount"
          class="ErrorsImportSearchBar-FilterCount"
        >
          {{ filterCount }}
        </q-badge>
      </q-btn>
    </div>
    <q-slide-transition>
      <q-card
        bordered
        flat
        v-show="filterVisible"
        class="ErrorsImportSearchBar-Card"
      >
        <q-card-section>
          <div class="ErrorsImportSearchBar-InputBlock">
            <r-input label="Источник импорта" v-model="filter.source" />
            <r-select
              v-model="filter.result"
              :options="statuses"
              emit-value
              label="Статус импорта"
            />
            <r-input label="Инициатор" v-model="filter.initiator" />
            <r-input label="Файл импорта" v-model="filter.fileName" />
            <r-input label="Организация" v-model="filter.nameVK" />
            <r-date-input
              label="Дата начала импорта"
              :from.sync="filter.dateFrom"
              :to.sync="filter.dateTo"
            />
          </div>
          <div class="ErrorsImportSearchBar-AcceptBlock">
            <q-btn
              icon="pgs-search"
              class="ErrorsImportSearchBar-AcceptBtn"
              label="Применить"
              :loading="loading"
              @click="executeSearch"
            />
          </div>
        </q-card-section>
        <q-btn
          dense
          flat
          class="ErrorsImportSearchBar-CloseButton"
          icon="close"
          size="10px"
          @click="resetFilter"
          label="сбросить все"
        />
      </q-card>
    </q-slide-transition>
  </div>
</template>

<style lang="scss" scoped>
@import '@/recruits/assets/styles/mixins/search-bar.mixin.scss';

.ErrorsImportSearchBar {
  @include SearchBar;
}
</style>

<script lang="ts">
import {
  Component,
  Emit,
  Prop,
  ModelSync,
  Vue,
  Watch,
} from 'vue-property-decorator';

import { SearchFilterUtils } from '@/recruits/utils/search-filter';
import RInput from '@/recruits/components/common/r-input.vue';
import RSelect from '@/recruits/components/common/r-select.vue';
import { ErrorsImportSearchFilter } from '@/recruits/components/errorsImport/search-filter';
import RDateInput from '@/recruits/components/common/r-date-input.vue';

@Component({
  components: {
    RDateInput,
    RSelect,
    RInput,
  },
})
export default class SearchBar extends Vue {
  @Prop({
    type: Boolean,
    default: false,
  })
  readonly loading: boolean;

  @ModelSync('data', 'change', { type: Object })
  filter: ErrorsImportSearchFilter;

  filterCount = 0;
  filterVisible = false;

  toggleFilter(): void {
    this.filterVisible = !this.filterVisible;
  }

  resetFilter(): void {
    this.filter = new ErrorsImportSearchFilter();
  }

  get statuses(): string[] {
    return [
      'Ошибка',
      'Ошибка при импорте',
      'Ошибка привязки военкомата',
      'Ошибка ФЛК',
      'Отсутствует',
    ];
  }

  @Watch('filter', { deep: true })
  private filterChange(filter: SearchFilterUtils) {
    this.filterCount = filter.countFilters();
  }

  @Emit('search')
  // eslint-disable-next-line @typescript-eslint/no-empty-function
  executeSearch(): void {}
}
</script>
