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

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

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

import { LogImportSearchFilter } from '@/recruits/components/log/log-import/search-filter';
import { QSelectOption } from '@models/models-ui';
import RSelect from '@/recruits/components/common/r-select.vue';
import RInput from '@/recruits/components/common/r-input.vue';
import RDateInput from '@/recruits/components/common/r-date-input.vue';

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

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

  filterCount = 0;
  filterVisible = false;

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

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

  // FIXME реализовать с помощью словарей.
  get statuses(): QSelectOption[] {
    return [
      {
        label: 'Завершена',
        value: 'SUCCESS',
      },
      {
        label: 'В процессе обработки',
        value: 'IN_PROGRESS',
      },
      {
        label: 'Не успешно',
        value: 'FAILED',
      },
    ];
  }

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

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