<template>
  <div class="ProfileAccounts">
    <div class="ProfileAccounts__BlockTitle">
      <block-header class="ProfileAccounts__Title" :withBullet="false">
        Учётные записи
      </block-header>
    </div>
    <div class="ProfileAccounts__BlockData">
      <div class="BlockData__Header">
        <div class="BlockData__HeaderElement">Организации (подразделения)</div>
        <div class="BlockData__HeaderElement">Роли</div>
      </div>
      <div
        class="BlockData__Content"
        v-for="account of accounts"
        :key="account.id"
      >
        <div class="BlockData__ContentElement">
          <span v-if="account.organization">
            {{ account.organization.shortname }}
          </span>
          <span v-if="account.department">
            {{ account.department.shortname }}
          </span>
          <template v-if="!account.organization && !account.department">
            -
          </template>
        </div>
        <div class="BlockData__ContentElement BlockData__ContentElement_Roles">
          <div
            class="ContentElement__Role"
            v-for="role of getDisplayableRoles(account)"
            :key="role"
          >
            {{ role }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, InjectReactive, Mixins } from 'vue-property-decorator';
import { accountsInjectionKey } from '../utils/keys';
import BlockHeader from '@/recruits/components/common/block-header.vue';
import { ComputedAccount } from '../utils/types';
import { oidc } from '../utils/mixins';

@Component({
  components: {
    BlockHeader,
  },
})
export default class ProfileAccounts extends Mixins(oidc) {
  @InjectReactive(accountsInjectionKey) accounts: ComputedAccount[] | null;

  getDisplayableRoles(account: ComputedAccount): string[] {
    return account.roles
      .filter((role) => role.ervuRole)
      .map((role) => role.displayName ?? '');
  }
}
</script>

<style lang="scss" scoped>
.ProfileAccounts {
  border-radius: 8px;
  padding: 16px 20px;
  border: 1px solid rgba(1, 72, 116, 0.11);
  display: flex;
  background-color: #edf2f7;

  &__BlockTitle {
    width: 25%;
    flex-shrink: 1;
  }

  &__Title {
    position: sticky;
    top: 16px;
    padding: 16px 0;
    margin-bottom: 16px;
    z-index: 1;
    font-family: 'Golos Text';
    font-style: normal;
    font-weight: 500;
    font-size: 20px;
    line-height: 24px;
    letter-spacing: -0.015em;
    color: rgba(2, 12, 18, 0.9);
  }

  &__BlockData {
    flex-grow: 1;
    max-width: 75%;

    .BlockData {
      &__Header {
        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;
        color: rgba(2, 12, 18, 0.58);
        align-self: center;
        margin-bottom: 10px;
        overflow: hidden;
        display: grid;
        grid-template-columns: 1fr 1fr;
      }

      &__HeaderElement {
        margin: 0;
        padding-left: 12px;
      }

      &__Content {
        background: #fff;
        border-radius: 8px;
        transition: 0.5s;
        cursor: pointer;
        overflow: hidden;
        display: grid;
        grid-template-columns: 1fr 1fr;
      }

      &__ContentElement {
        align-self: center;
        padding: 7px 0 7px 12px;
        margin: 4px 0;
        border-left: 1px solid rgba(1, 72, 116, 0.11);
        height: 100%;
        display: flex;
        align-items: center;
        font-size: 14px;

        &:first-child {
          border-left: none;
        }

        &_Roles {
          display: flex;
          flex-wrap: wrap;
          gap: 4px;

          .ContentElement__Role {
            padding: 0.16667em 0.66667em;
            background: rgba(15, 32, 160, 0.08);
            border-radius: 1em;
            font-family: Golos UI;
            white-space: nowrap;
            font-size: 12px;
            line-height: 1.16667em;
          }
        }
      }
    }
  }
}
</style>
