<template>
  <div class="LogAuthSearchBar">
    <div class="LogAuthSearchBar-SearchBlock">
      <q-input
        v-model="filter.search"
        dense
        outlined
        placeholder="Введите для поиска"
        @keydown.enter="executeSearch"
        @keydown.esc="filter.search = ''"
        class="LogAuthSearchBar-Search"
      >
        <template #prepend>
          <q-icon name="pgs-search" />
        </template>
      </q-input>
      <q-btn
        icon="pgs-filter"
        class="LogAuthSearchBar-FilterIcon"
        @click="toggleFilter"
      >
        <q-badge
          floating
          v-if="!!filterCount"
          class="LogAuthSearchBar-FilterCount"
        >
          {{ filterCount }}
        </q-badge>
      </q-btn>
    </div>
    <q-slide-transition>
      <q-card
        bordered
        flat
        v-show="filterVisible"
        class="LogAuthSearchBar-Card"
      >
        <q-card-section>
          <div class="LogAuthSearchBar-InputBlock">
            <r-input label="Фамилия" v-model="filter.lastName" />
            <r-input label="Имя" v-model="filter.firstName" />
            <r-input label="Отчество" v-model="filter.middleName" />
            <r-select
              v-model="filter.event"
              :options="events"
              label="Событие"
              map-options
              emit-value
            />
            <r-multi-select
              v-model="filter.loginType"
              :options="loginTypeOptions"
              label="Тип логина"
            />
            <r-date-input
              label="Дата события"
              :from.sync="filter.dateFrom"
              :to.sync="filter.dateTo"
            />
          </div>
          <div class="LogAuthSearchBar-AcceptBlock">
            <q-btn
              icon="pgs-search"
              class="LogAuthSearchBar-AcceptBtn"
              label="Применить"
              :loading="loading"
              @click="executeSearch"
            />
          </div>
        </q-card-section>
        <q-btn
          dense
          flat
          class="LogAuthSearchBar-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';

.LogAuthSearchBar {
  @include SearchBar;
}
</style>
<script lang="ts">
import {
  Component,
  Emit,
  Prop,
  ModelSync,
  Vue,
  Watch,
} from 'vue-property-decorator';

import MultiSelect from '@components/statements/components/multi-select.vue';
import DateApplicationInput from '@components/statements/components/date-application-input.vue';
import RadioSelect from '@/recruits/components/common/radio-select.vue';
import { LogAuthSearchFilter, LOGIN_TYPE_OPTIONS } from './search-filter';
import { QSelectOption } from '@models/models-ui';
import RInput from '@/recruits/components/common/r-input.vue';
import RDateInput from '@/recruits/components/common/r-date-input.vue';
import RSelect from '@/recruits/components/common/r-select.vue';
import RMultiSelect from '@/recruits/components/common/r-multi-select.vue';
import { Option } from '@/recruits/models';

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

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

  filterCount = 0;
  filterVisible = false;

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

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

  // FIXME реализовать с помощью словарей.
  get events(): QSelectOption[] {
    return [
      {
        label: 'Авторизация',
        value: 'USER_AUTHENTICATED',
      },
      {
        label: 'Ошибка авторизации',
        value: 'USER_AUTHENTICATION_ERROR',
      },
      {
        label: 'Выход из системы',
        value: 'LOGOUT_SUCCESSFUL',
      },
    ];
  }

  get loginTypeOptions(): Option[] {
    return LOGIN_TYPE_OPTIONS;
  }

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

  @Emit('search')
  executeSearch(): void {
    //
  }
}
</script>
