<template>
  <div class="RecruitDataTable">
    <div class="RecruitsToCallTable__Headers">
      <div
        class="RecruitsToCallTable__HeaderTitle RecruitsToCallTable__HeaderTitle_Center"
      >
        ФИО / ИД ЕРН
      </div>
      <div
        class="RecruitsToCallTable__HeaderTitle RecruitsToCallTable__HeaderTitle_Center"
      >
        Серия / Номер
      </div>
      <div
        class="RecruitsToCallTable__HeaderTitle RecruitsToCallTable__HeaderTitle_Center"
      >
        Причина повестки
      </div>
      <div
        class="RecruitsToCallTable__HeaderTitle RecruitsToCallTable__HeaderTitle_Center"
      >
        Сформирована
      </div>
      <div
        class="RecruitsToCallTable__HeaderTitle RecruitsToCallTable__HeaderTitle_Center"
      >
        Направлена
      </div>
      <div
        class="RecruitsToCallTable__HeaderTitle RecruitsToCallTable__HeaderTitle_Center"
      >
        Дата и время явки
      </div>
      <div
        class="RecruitsToCallTable__HeaderTitle RecruitsToCallTable__HeaderTitle_Center"
      >
        Статус
      </div>
    </div>
    <q-table
      grid
      v-bind="{ ...$props, ...$attrs }"
      @request="requestHandler"
      @update:pagination="paginationHandler"
      row-key="id"
      selection="multiple"
      :selected.sync="selected"
    >
      <template v-slot:item="props">
        <div class="RecruitsToCallTable__Row">
          <div class="RecruitsToCallTable__Cell RecruitsToCallTable__Cell_Name">
            <div class="RecruitsToCallTable__Name">
              <router-link
                class="RecruitsToCallTable__NameLink"
                :to="{
                  name: 'recruit-personal',
                  params: { id: props.row.recruitId },
                }"
              >
                {{ props.row.fullName }}
              </router-link>
              <div class="RecruitsToCallTable__Idern">
                <span>
                  {{ props.row.idERN }}
                </span>
              </div>
            </div>
          </div>
          <div
            class="RecruitsToCallTable__Cell RecruitsToCallTable__Cell_Center"
          >
            <router-link
              class="RecruitsToCallTable__NameLink"
              :to="{ name: 'subpoena-card', params: { id: props.row.id } }"
            >
              <span>{{ props.row.series }}</span> №
              {{ props.row.number }}
            </router-link>
          </div>
          <div class="RecruitsToCallTable__Cell RecruitsToCallTable__Cell_Row">
            <span>
              {{ getSummonObjective(props.row.summonObjective) }}
            </span>
          </div>
          <div
            class="RecruitsToCallTable__Cell RecruitsToCallTable__Cell_Center"
          >
            <date-time-cell
              class="black-date"
              icon="pgs-decision-date"
              :value="props.row.createDate"
            />
          </div>
          <div
            class="RecruitsToCallTable__Cell RecruitsToCallTable__Cell_Center"
          >
            <date-time-cell
              class="black-date"
              icon="pgs-request-date"
              :value="props.row.sendDate"
            />
          </div>
          <div
            class="RecruitsToCallTable__Cell RecruitsToCallTable__Cell_Center"
          >
            <date-time-cell
              class="black-date"
              icon="pgs-time"
              :value="props.row.turnoutDate"
              time
            />
          </div>
          <div>
            <status-box :status="props.row.status" type="subpoena" borderless />
          </div>
        </div>
      </template>
    </q-table>
  </div>
</template>

<style lang="scss" scoped>
.RecruitsToCallTable {
  width: 100%;
  margin-left: 0;
  margin-right: 0;
  &__Headers {
    padding: 16px 0 6px 0;
    color: rgba(2, 12, 18, 0.58);
    overflow: hidden;
    font-family: 'Golos Text', sans-serif;
    font-style: normal;
    font-weight: 600;
    font-size: 10px;
    line-height: 12px;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    display: grid;
    grid-template-columns: 1.1fr 0.7fr 1.7fr 0.7fr 0.7fr 0.7fr 0.7fr;
  }

  &__HeaderCheckBox {
    margin-top: -4px;
    margin-bottom: -4px;
    padding-left: 14px;
    padding-right: 12px;
    border-right: 1px solid rgba(1, 72, 116, 0.11);
  }

  &__HeaderLabelCheckBox {
    &_Disabled {
      opacity: 0.5;
    }
  }

  &__HeaderTitle {
    align-content: center;
    margin: -4px 0;
    width: 100%;
    &:not(:last-child) {
      border-right: 1px solid rgba(1, 72, 116, 0.11);
    }
    &_Center {
      text-align: center;
    }
    &_LeftPadding {
      padding-left: 4px;
    }
  }

  &__Row {
    display: grid;
    grid-template-columns: 1.1fr 0.7fr 1.7fr 0.7fr 0.7fr 0.7fr 0.7fr;
    width: 100%;
    padding: 4px 0 4px 0;
    margin: 2px 0 2px 0;
    background: #ffffff;
    border: 1px solid rgba(1, 72, 116, 0.11);
    border-radius: 8px;
    box-shadow: none;
    &:hover {
      border: 1px solid #1491cf;
      box-shadow: 0 0.6px 1.8px rgba(7, 46, 71, 0.12),
        0 3.2px 8px rgba(7, 46, 71, 0.15);
    }
  }

  &__Cell {
    display: flex;
    position: relative;
    padding: 0 0 0 4px;
    overflow: hidden;
    color: rgba(2, 12, 18, 0.9);
    font-family: 'Golos UI', sans-serif;
    font-style: normal;
    font-weight: 400;
    font-size: 14px;
    line-height: 14px;
    &:not(:last-child) {
      border-right: 1px solid rgba(1, 72, 116, 0.11);
    }
    &_LeftPadding {
      padding-left: 12px;
    }
    &_Column {
      flex-direction: column;
      flex-wrap: nowrap;
      justify-content: center;
    }
    &_Row {
      display: flex;
      flex-direction: row;
      align-items: flex-start;
      justify-content: flex-start;
    }
    &_Center {
      align-items: center;
      justify-content: center;
    }
    &_Name {
      flex-direction: row;
      align-items: flex-start;
      justify-content: flex-start;
      font-weight: 500;
      font-size: 14px;
      line-height: 16px;
      color: rgba(2, 12, 18, 0.9);
    }
    &_Status {
      margin-top: -4px;
      margin-bottom: -4px;
      padding: 0;
    }
  }
  &__Name {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    font-weight: 500;
    font-size: 14px;
    line-height: 16px;
    padding: 0;
    color: rgba(2, 12, 18, 0.9);
  }
  &__Idern {
    font-weight: 400;
    font-size: 14px;
    line-height: 16px;
    display: flex;
    align-items: center;
  }
  &__NameLink {
    text-decoration: none;
    &:link {
      color: #015e98;
    }
    &:visited {
      color: rgba(2, 12, 18, 0.9);
    }
  }
}
::v-deep .q-table__bottom {
  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);
}
</style>

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

import { formatDate, mask } from '@/recruits/utils';
import StatusBox from '@/recruits/components/common/status-box/index.vue';
import DateTimeCell from '@/recruits/components/common/cells/date-time.vue';
import { statusDictionary } from '@/recruits/models';

@Component({
  components: {
    StatusBox,
    DateTimeCell,
  },
  methods: { mask, formatDate },
})
export default class DataTable extends Vue {
  @Prop()
  resetSelect: boolean;

  @Watch('resetSelect')
  resetSelectedRows(val: boolean): void {
    if (val) {
      this.selected = [];
      this.massSelected = false;
    }
  }

  selected: string[] = [];
  massSelected = false;

  @Watch('massSelected')
  massSelect(newData: boolean) {
    this.selected = [];
    if (newData) {
      if (Array.isArray(this.$attrs.data)) {
        this.$attrs.data.forEach((item: Record<string, any>) => {
          this.selected.push(item.id);
        });
      }
    } else {
      this.selected = [];
    }
  }

  @Emit('row-selected')
  @Watch('selected')
  selectRow() {}

  public getStatusTitle(status: string): string {
    return statusDictionary[status] || status;
  }

  @Emit('request')
  requestHandler(value: any) {
    this.selected = [];
  }

  @Emit('update:pagination')
  paginationHandler() {}

  public sendToClipBoard(textValue: string) {
    copyToClipboard(textValue);
    this.$q.notify({
      message: `'${textValue}' скопирован`,
    });
  }

  getSummonObjective(summonObjective: string): string {
    return (
      this.$store.getters[
        'ervuDictionaries/militaryRegistrationUpdatingGoalsDictionary'
      ][summonObjective] || '-'
    );
  }
}
</script>
