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

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

<script lang="ts">
import {
  Component,
  Emit,
  ModelSync,
  Prop,
  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 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';
import RMultiSelect from '@/recruits/components/common/r-multi-select.vue';
import { GlobalSearchFilter } from '@/recruits/utils/global-search-filter';
import { UserActionsSearchFilter } from '@/recruits/components/log/user-actions/search-filter';

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

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

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

  @Emit('resetFilter')
  resetFilter(): void {
    this.filter = new UserActionsSearchFilter();
  }

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

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