<template>
  <div class="ButtonsBlock">
    <r-selection-filter
      v-if="$AC.haveOneOfScope([$AC.scopes.MILITARY_RECRUIT_SPECIALIST])"
      :selected="selected"
      :filter="checkStatuses(['1', '2'])"
    >
      <template #default="props">
        <multi-add-to-list
          :model="props.filtred"
          :sign="false"
          :button-code="buttonCode"
          class="DopButton-Button"
          process-key="milBaseMassSignProcess"
          action-type="addToSummonList"
          label="Добавить в список на вызов"
          icon="pgs-add-form"
          default-time="09:00"
          :personRegistryListFilter="personRegistryListFilter"
          :disable="selected.length > 50"
          @done="onReload"
          :activity-recorder-options="activityOptions"
        />
      </template>
    </r-selection-filter>
    <r-selection-filter
      v-if="
        $AC.haveOneOfScope([
          $AC.scopes.MILITARY_RECRUIT_SPECIALIST,
          $AC.scopes.SYSTEM_ADMINISTRATOR,
        ])
      "
      :selected="selected"
      :filter="checkStatuses(['1', '4', '4.1', '4.2', '10.2', '10.3'])"
    >
      <template #default="props">
        <multi-init
          :model="props.filtred"
          label="Поставить на учёт"
          title="Решение о первоначальной постановке на воинский учёт:"
          @done="onReload"
          @onBeforeAction="activityRecorder"
        />
      </template>
    </r-selection-filter>
  </div>
</template>

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import MultiInit from '@/recruits/components/dialogs/MultiInit.vue';
import { formatDate } from '@/recruits/utils';
import MultiAddToList, {
  ButtonCode,
} from '@/recruits/components/dialogs/MultiAddToList.vue';
import { SearchRecruitResponse } from '@/recruits/openapi/person-registry';
import RSelectionFilter from '@/recruits/components/common/r-selection-filter.vue';

@Component({
  components: {
    MultiInit,
    MultiAddToList,
    RSelectionFilter,
  },
})
export default class ListButtons extends Vue {
  @Prop({ default: () => [] }) selected: SearchRecruitResponse[];
  @Prop() data: any;

  personRegistryListFilter = {
    statuses: ['1'],
    summonObjectiveAny: ['101'],
  };

  get selectedIds(): string[] {
    return this.selected.map((item) => item.id);
  }

  get activityOptions(): any[] {
    return this.selected.map((item: any) => ({
      id: item.id,
      fio: item.fullName,
      idERN: item.idern,
      birthDate: formatDate(item.birthDate),
    }));
  }

  get buttonCode(): ButtonCode {
    return {
      openDialog: 'primary_list_add',
      closeDialog: 'primary_list_cancel',
      createList: 'primary_list_new',
      showList: 'primary_list_show',
      addToList: 'primary_list_exist_add',
      addToNewList: 'primary_list_new_add',
    };
  }

  public onReload(): void {
    this.$emit('onReload');
  }

  public showAddListDialog = false;
  public addList(): void {
    this.showAddListDialog = true;
  }

  public checkStatuses(
    currentStatuses: string[]
  ): { (selected: SearchRecruitResponse): boolean } {
    return (selected) => currentStatuses.includes(selected.status);
  }

  activityRecorder(buttonCode: string, data: any) {
    this.$recordActivity({
      buttonCode: buttonCode || 'register_military',
      extraInfo: { list: this.activityOptions, type: 'recruit', ...data },
    });
  }
}
</script>

<style lang="scss">
.ButtonsBlock {
  display: flex;
  flex-flow: row;
  align-items: flex-start;
  gap: 16px;
}
.DopButton {
  box-sizing: border-box;
  background-color: rgba(15, 32, 160, 0.08);
  border-radius: 20px;
  padding: 4px;
  margin-top: -5px;
  margin-bottom: -5px;
  &-TextFiled {
    margin-left: 8px;
    margin-right: 8px;
    font-family: 'Golos UI', sans-serif;
    font-style: normal;
    font-weight: 400;
    font-size: 14px;
    line-height: 16px;
    color: rgba(2, 12, 18, 0.58);
    &__Bold {
      font-weight: 500;
      color: #015e98;
    }
  }
  &-Button {
    &__Color {
      &__DarkBlue {
        background: linear-gradient(92.91deg, #1491cf 0%, #0f68a0 100%);
        box-shadow: 0 0.6px 1.8px rgba(7, 46, 71, 0.12),
          0 3.2px 8px rgba(7, 46, 71, 0.15);
        color: rgba(255, 255, 255, 0.92);
      }
    }
  }
}
</style>
