<template>
  <div class="full-width">
    <div class="full-width headers SigningTable-Row">
      <div class="header-checkbox">
        <input
          class="LabelSelect-Select"
          id="massSelect"
          type="checkbox"
          v-model="massSelected"
        />
        <label for="massSelect" class="LabelSelect" />
      </div>
      <div class="q-px-sm">ФИО / ИД ЕРН</div>
      <div class="q-px-sm">Адрес регистрации</div>
      <div class="q-px-sm">Процедура</div>
      <div class="text-center">Статус</div>
      <div class="text-center">Решение</div>
    </div>
    <q-table
      grid
      v-bind="{ ...$props, ...$attrs, data }"
      @request="requestHandler"
      @update:pagination="$emit('update:pagination', $event)"
      row-key="id"
      selection="multiple"
      :selected.sync="selected"
    >
      <template v-slot:item="props">
        <div
          class="full-width body SigningTable-Row"
          @dblclick="/*dblClickHandler(props.row.id)*/"
        >
          <div class="flex items-center checkbox-column">
            <input
              class="LabelSelect-Select"
              :id="props.row.id"
              type="checkbox"
              v-model="selected"
              :value="props.row.id"
            />
            <label :for="props.row.id" class="LabelSelect" />
          </div>
          <div>
            <div class="name">
              <router-link
                class="cursor-pointer"
                :to="{ name: 'recruit-personal', params: { id: props.row.id } }"
              >
                {{ props.row.fullName }}
              </router-link>
            </div>
            <div class="row gray-numbers q-pt-xs">
              <div class="flex col">
                <span class="text-no-wrap ellipsis">
                  {{ props.row.idern }}
                </span>
              </div>
            </div>
          </div>
          <div class="flex">
            <span class="multiline" :title="props.row.regAddress">{{
              props.row.regAddress
            }}</span>
          </div>
          <div class="flex">
            <span
              class="multiline"
              :title="
                props.row.procedure && props.row.procedure.type
                  ? props.row.procedure.type
                  : 'Постановка на ВУ'
              "
              >{{
                props.row.procedure && props.row.procedure.type
                  ? props.row.procedure.type
                  : 'Постановка на ВУ'
              }}
            </span>
          </div>
          <div class="status">
            <status-box :status="props.row.status" borderless />
          </div>
          <div class="full-width SigningTable-Col__Buttons">
            <base-preview
              class="preview-wrapper"
              v-if="get(props.row, 'signEntity.file.format')"
              :extension="get(props.row, 'signEntity.file.format')"
              :s3-url="get(props.row, 'signEntity.file.s3url')"
            >
              <q-icon
                class="SigningTable-Content__Icon"
                color="primary"
                name="pgs-view"
                size="26px"
              >
                <q-tooltip>Предпросмотр</q-tooltip>
              </q-icon>
            </base-preview>
            <q-icon
              v-else
              class="SigningTable-Content__Icon SigningTable-Content__Icon__Not"
              name="pgs-view"
            />
            <q-icon
              class="SigningTable-Content__Icon"
              name="pgs-import"
              @click="
                downloadFile(
                  props.row.signEntity.file.s3url,
                  props.row.signEntity.file.s3Name
                )
              "
            />
          </div>
        </div>
      </template>
    </q-table>
  </div>
</template>

<script lang="ts">
import { Component, Emit, Prop, 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 { statusDictionary } from '@/recruits/models';
import S3UrlSignerFilesService from '@services/s3urlsigner/files';
import { saveBlob } from '@utils/files';
import get from 'lodash/get';
import { BatchSigning } from '@/recruits/openapi/person-registry';

@Component({
  components: { StatusBox },
  methods: { mask, formatDate, get },
})
export default class DataTable extends Vue {
  get: typeof get;

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

  @Prop()
  resetSelect: string;

  @Prop()
  data: BatchSigning[];

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

  @Watch('selected')
  onSelectedChange(evt: string[]): void {
    this.$emit('row-selected', evt);
  }

  @Watch('massSelected')
  massSelect(newData: boolean): void {
    if (newData) {
      this.selected = (this.data ?? []).map(({ id }: BatchSigning) => id);
    } else {
      this.selected = [];
    }
  }

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

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

  dblClickHandler(recruitId: string): void {
    this.$router.push({ name: 'recruit-personal', params: { id: recruitId } });
  }

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

  async downloadFile(s3url: string, fileName: string): Promise<void> {
    try {
      const { file: blob } = await S3UrlSignerFilesService.fetchFileFromS3ByUrl(
        {
          url: s3url,
        }
      );
      saveBlob({
        blob,
        fileName,
      });
    } catch (e) {
      console.log(e);
    }
  }
}
</script>

<style lang="scss" scoped>
div.header-checkbox {
  margin-top: -4px;
  margin-bottom: -4px;
  padding-left: 12px;
  padding-right: 12px;
}

.LabelSelect {
  display: block;
  width: 20px;
  height: 20px;
  background-color: rgba(15, 32, 160, 0.08);
  border: 1px solid rgba(1, 72, 116, 0.18);
  border-radius: 4px;
  position: relative;
  &-Select {
    display: none;
    &:checked + .LabelSelect {
      background-color: #1491cf;
      box-shadow: 0 0 3.5px rgba(1, 94, 152, 0.5);
      &:before,
      &:after {
        position: absolute;
        content: '';
        display: block;
        background-color: #fff;
        height: 3px;
        border-radius: 10px;
      }
      &:before {
        width: 10px;
        top: 10px;
        left: 1px;
        transform: rotate(60deg);
      }
      &:after {
        width: 14px;
        top: 8px;
        left: 5px;
        transform: rotate(-60deg);
      }
    }
  }
}

.headers {
  padding: 16px 0 6px 0;
  > div {
    color: rgba(2, 12, 18, 0.58);
    overflow: hidden;
    font-family: 'Golos Text';
    font-style: normal;
    font-weight: 600;
    font-size: 10px;
    line-height: 12px;

    letter-spacing: 0.05em;
    text-transform: uppercase;
  }
}

.body {
  max-height: 5em;
  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;

  ::v-deep i.pgs-calendar {
    top: -3px;
  }

  > div.checkbox-column {
    padding-left: 12px;
    padding-right: 12px;
  }
  > div {
    position: relative;
    padding: 0 8px;
    overflow: hidden;
    color: rgba(2, 12, 18, 0.9);
    font-family: 'Golos UI';
    font-style: normal;
    font-weight: 400;
    font-size: 14px;
    line-height: 16px;
    .name {
      font-weight: 500;
      font-size: 14px;
      line-height: 16px;
      // color: rgba(2, 12, 18, 0.9);
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      a {
        text-decoration: none;
        &:link {
          color: #015e98;
        }
        &:visited {
          color: rgba(2, 12, 18, 0.9);
        }
      }
    }
    .multiline {
      align-self: center;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 3;
      -webkit-box-orient: vertical;
    }
    .gray-numbers {
      font-weight: 400;
      font-size: 14px;
      line-height: 16px;
      // color: rgba(2, 12, 18, 0.58);
    }
    .snils {
      font-size: 14px;
      line-height: 16px;
      // color: rgba(2, 12, 18, 0.9);
      .copy-button {
        padding-left: 2px;
        color: #1491cf;
      }
    }
    &.status {
      margin-top: -4px;
      margin-bottom: -4px;
      padding: 0;
    }
    &.lists {
      font-weight: 400;
      font-size: 12px;
      line-height: 16px;
      // color: rgba(2, 12, 18, 0.58);

      a {
        text-decoration: none;
        &:link {
          color: #015e98;
        }
        &:visited {
          color: rgba(2, 12, 18, 0.9);
        }
      }
    }
  }
  &: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);
  }
  > div:not(:nth-last-of-type(-n + 3))::after {
    // Кроме последних двух
    position: absolute;
    content: '';
    width: 1px;
    height: 100%;
    top: 0;
    right: 0;
    background-color: rgba(1, 72, 116, 0.11);
  }
}

::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);
}

.SigningTable {
  &-Row {
    display: grid;
    grid-template-columns: 44px 300px 2.3fr 1.1fr 146px 146px;
  }
  &-Col {
    &__Proc {
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    &__Buttons {
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
  &-Content {
    &__Bold {
      font-family: 'Golos UI', sans-serif;
      font-style: normal;
      font-weight: 800;
      font-size: 12px;
      line-height: 14px;
      color: rgba(2, 12, 18, 0.9);
    }
    &__Grey {
      font-family: 'Golos UI', sans-serif;
      font-style: normal;
      font-weight: 400;
      font-size: 12px;
      line-height: 14px;
      color: rgba(2, 12, 18, 0.58);
    }
    &__BirthDate {
      margin-right: 22px;
    }
    &__Icon {
      font-size: 26px;
      cursor: pointer;
      color: #1491cf;
      &:not(:last-child) {
        margin-right: 10px;
      }
      &__Not {
        cursor: not-allowed;
      }
    }
  }
}
</style>
