<template>
  <div class="GepsNotificationsSearchBar">
    <div class="GepsNotificationsSearchBar-SearchBlock">
      <q-input
        v-model="filter.search"
        dense
        outlined
        placeholder="Введите для поиска"
        @keydown.enter="executeSearch"
        @keydown.esc="filter.search = ''"
        class="GepsNotificationsSearchBar-Search"
      >
        <template #prepend>
          <q-icon name="pgs-search" />
        </template>
      </q-input>
      <q-btn
        icon="pgs-filter"
        class="GepsNotificationsSearchBar-FilterIcon"
        @click="toggleFilter"
      >
        <q-badge
          v-if="!!filterCount"
          floating
          class="GepsNotificationsSearchBar-FilterCount"
        >
          {{ filterCount }}
        </q-badge>
      </q-btn>
    </div>
    <q-slide-transition>
      <q-card
        class="GepsNotificationsSearchBar-Card"
        bordered
        flat
        v-show="filterVisible"
      >
        <q-card-section>
          <div class="GepsNotificationsSearchBar-InputBlock">
            <r-input label="ИД ЕРН" v-model="filter.idERN" />
            <r-date-input
              label="Дата отправки"
              :from.sync="filter.sendDateFrom"
              :to.sync="filter.sendDateTo"
            />
            <r-date-input
              label="Дата получения ответа"
              :from.sync="filter.receiveDateFrom"
              :to.sync="filter.receiveDateTo"
            />
            <r-select
              map-options
              emit-value
              label="Тип уведомления"
              :options="typeOptions"
              v-model="filter.type"
            />
            <r-status-select
              label="Статус"
              :dictionary-path="statusDictionaryPath"
              :statuses="statuses"
              v-model="filter.statuses"
            />
            <r-input label="СНИЛС" v-model="filter.snils" />
          </div>

          <div class="GepsNotificationsSearchBar-AcceptBlock">
            <q-btn
              icon="pgs-search"
              class="GepsNotificationsSearchBar-AcceptBtn"
              label="Применить"
              :loading="loading"
              @click="executeSearch"
            />
          </div>
        </q-card-section>
        <q-btn
          dense
          flat
          class="GepsNotificationsSearchBar-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';

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

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

import RDateInput from '@/recruits/components/common/r-date-input.vue';
import RadioSelect from '@/recruits/components/common/radio-select.vue';
import RInput from '@/recruits/components/common/r-input.vue';
import RMultiSelect from '@/recruits/components/common/r-multi-select.vue';
import RStatusSelect from '@/recruits/components/common/r-status-select.vue';
import { SearchFilterUtils } from '@/recruits/utils/search-filter';
import { Option } from '@/recruits/models';
import RSelect from '@/recruits/components/common/r-select.vue';

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

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

  filterCount = 0;
  filterVisible = false;
  firstName = '';
  statusDictionaryPath = 'ervuDictionaries/statusGepsDictionary';

  get statuses(): Array<string> {
    const dictionary = this.$store.getters[this.statusDictionaryPath];
    return dictionary ? Object.keys(dictionary) : [];
  }

  get typeOptions(): Array<Option> {
    const dictionary =
      this.$store.getters['ervuDictionaries/typeGepsDictionary'] || [];
    return Object.keys(dictionary).map((key) => ({
      label: dictionary[key],
      value: key,
    }));
  }

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

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

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

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