<!--
// Copyright © 2022 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
-->
<script lang="ts">
  import contact, { EmployeeAccount, formatName } from '@hcengineering/contact'
  import { Ref } from '@hcengineering/core'
  import { createQuery } from '@hcengineering/presentation'
  import { Request } from '@hcengineering/request'
  import { Label, TimeSince } from '@hcengineering/ui'
  import { ObjectPresenter } from '@hcengineering/view-resources'
  import request from '../plugin'
  import RequestActions from './RequestActions.svelte'
  import RequestPresenter from './RequestPresenter.svelte'

  export let value: Request

  let employee: EmployeeAccount | undefined

  const query = createQuery()

  $: query.query(
    contact.class.EmployeeAccount,
    { _id: value.tx.modifiedBy as Ref<EmployeeAccount> },
    (account) => {
      ;[employee] = account
    },
    { limit: 1 }
  )
</script>

<div class="container">
  <div class="flex-between">
    <div class="label">
      <div class="bold">
        {#if employee}
          {formatName(employee.name)}
        {/if}
      </div>
      <span class="lower">
        <Label label={request.string.CreatedRequest} />
        <Label label={request.string.For} />
      </span>
      <ObjectPresenter objectId={value.tx.objectId} _class={value.tx.objectClass} />
    </div>
    <div class="time"><TimeSince value={value.tx.modifiedOn} /></div>
  </div>
  <RequestPresenter {value} />

  <RequestActions {value} />
</div>

<style lang="scss">
  .container {
    background-color: var(--body-color);
    padding: 0.5rem 0.75rem 0.75rem;
    min-height: 0;
    border-radius: 0.75rem;
    border: 1px solid var(--divider-color);

    &:not(:last-child) {
      margin-bottom: 0.75rem;
    }
  }

  .label {
    display: flex;
    align-items: center;
    flex-wrap: wrap;

    & > * {
      margin-right: 0.25rem;
    }
    & > *:last-child {
      margin-right: 0;
    }
  }

  .lower {
    text-transform: lowercase;
  }

  .bold {
    font-weight: 500;
    color: var(--caption-color);
  }

  .time {
    align-self: baseline;
    margin-left: 1rem;
    color: var(--dark-color);
  }
</style>
