<!--
// Copyright © 2020 Anticrm Platform Contributors.
// 
// 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 type { Ref } from '@anticrm/core'
  import type { Customer, Lead } from '@anticrm/lead'
  import { createQuery } from '@anticrm/presentation'
  import task from '@anticrm/task'
  import { CircleButton, IconAdd, Label, showPopup } from '@anticrm/ui'
  import { Table } from '@anticrm/view-resources'
  import lead from '../plugin'
  import CreateLead from './CreateLead.svelte'

  export let objectId: Ref<Customer>

  let leads: Lead[] = []

  const query = createQuery()
  $: query.query(lead.class.Lead, { attachedTo: objectId }, result => { leads = result })

  const createLead = (ev: MouseEvent): void =>
    showPopup(CreateLead, { candidate: objectId, preserveCandidate: true }, ev.target as HTMLElement)
</script>

<div class="applications-container">
  <div class="flex-row-center">
    <div class="title">Leads</div>
    <CircleButton icon={IconAdd} size={'small'} selected on:click={createLead} />
  </div>
  {#if leads.length > 0}
    <Table 
      _class={lead.class.Lead}
      config={['', '$lookup.state']}
      options={
        {
          lookup: {
            state: task.class.State
          }
        }
      }
      query={ { attachedTo: objectId } }
    />
  {:else}
    <div class="flex-col-center mt-5 createapp-container">
      <div class="small-text content-dark-color mt-2">
        <Label label={lead.string.NoLeadsForDocument} />
      </div>
      <div class="small-text">
        <a href={'#'} on:click={createLead}><Label label={lead.string.CreateLead} /></a>
      </div>
    </div>
  {/if}
</div>

<style lang="scss">
  .applications-container {
    display: flex;
    flex-direction: column;

    .title {
      margin-right: .75rem;
      font-weight: 500;
      font-size: 1.25rem;
      color: var(--theme-caption-color);
    }
  }

  .createapp-container {
    padding: 1rem;
    color: var(--theme-caption-color);
    background: var(--theme-bg-accent-color);
    border: 1px solid var(--theme-bg-accent-color);
    border-radius: .75rem;
  }
</style>
