{#
  Variables
    - searchValue
    - leadCounts
    - items
    - page
    - limit
    - permissions
    - tmpl
    - totalItems
#}
{% set isIndex = 'index' == tmpl ? true : false %}
{% set tmpl = 'list' %}
{% extends isIndex ? '@MauticCore/Default/content.html.twig' : '@MauticCore/Default/raw_output.html.twig' %}

{% block mauticContent %}company{% endblock %}

{% block headerTitle %}{{ 'mautic.companies.menu.root'|trans }}{% endblock %}



{% block content %}
  {% if isIndex %}
    <div id="page-list-wrapper" class="{% if items|length > 0 or searchValue is not empty %}panel {% endif %}panel-default">
      {%- set pageButtons = [] %}
{% if permissions['lead:leads:create'] %}
    {% set pageButtons = pageButtons|merge([
        {
            'attr': {
                'href': path('mautic_import_action', {'object': 'companies', 'objectAction': 'new'}),
            },
            'iconClass': 'ri-import-line',
            'btnText': 'mautic.lead.lead.import',
        },
        {
            'attr': {
                'href': path('mautic_import_index', {'object': 'companies'}),
            },
            'iconClass': 'ri-history-line',
            'btnText': 'mautic.lead.lead.import.index',
        }
    ]) %}
{% endif %}

{{- include('@MauticCore/Helper/list_toolbar.html.twig', {
    'searchValue': searchValue,
    'searchHelp': 'mautic.company.help.searchcommands',
    'action': currentRoute,
    'page_actions': {
        'templateButtons': {'new': permissions['lead:leads:create']},
        'routeBase': 'company',
        'customButtons': pageButtons,
    },
    'bulk_actions': {
        'routeBase': 'company',
        'templateButtons': {
            'delete': permissions['lead:leads:deleteother']
        },
    },
    'quickFilters': [
        {
            'search': 'mautic.core.searchcommand.ismine',
            'label': 'mautic.core.searchcommand.ismine.label',
            'tooltip': 'mautic.core.searchcommand.ismine.description',
            'icon': 'ri-user-line'
        }
    ],
    toolbarOptionsLabel: 'mautic.lead.import_export',
    toolbarOptionsIcon: 'ri-arrow-up-down-fill ri-1x'
}) -}}
    <div class="page-list">
  {% endif %}

  {% if items|length > 0 %}
      <div class="table-responsive page-list">
          <table class="table table-hover company-list" id="companyTable">
              <thead>
              <tr>
                {{ include('@MauticCore/Helper/tableheader.html.twig', {
                    'checkall': 'true',
                    'target': '#companyTable',
                }) }}
                  {{ include('@MauticCore/Helper/tableheader.html.twig', {
                          'sessionVar': 'company',
                          'text': 'mautic.company.name',
                          'class': 'col-company-name',
                          'orderBy': 'comp.companyname',
                  }) }}
                  {{ include('@MauticCore/Helper/tableheader.html.twig', {
                          'sessionVar': 'company',
                          'text': 'mautic.company.email',
                          'class': 'visible-md visible-lg col-company-category',
                          'orderBy': 'comp.companyemail',
                  }) }}
                  {{ include('@MauticCore/Helper/tableheader.html.twig', {
                          'sessionVar': 'company',
                          'text': 'mautic.company.website',
                          'class': 'visible-md visible-lg col-company-website',
                          'orderBy': 'comp.companywebsite',
                  }) }}
                  {{ include('@MauticCore/Helper/tableheader.html.twig', {
                          'sessionVar': 'company',
                          'text': 'mautic.company.score',
                          'class': 'visible-md visible-lg col-company-score',
                          'orderBy': 'comp.score',
                  }) }}
                  {{ include('@MauticCore/Helper/tableheader.html.twig', {
                          'sessionVar': 'company',
                          'text': 'mautic.lead.list.thead.leadcount',
                          'class': 'visible-md visible-lg col-leadlist-leadcount',
                  }) }}
                  {{ include('@MauticCore/Helper/tableheader.html.twig', {
                          'sessionVar': 'company',
                          'orderBy': 'comp.id',
                          'text': 'mautic.core.id',
                          'class': 'visible-md visible-lg col-company-id',
                  }) }}
              </tr>
              </thead>
              <tbody>
              {% for item in items %}
                  {% set fields = item.fields %}
                  <tr>
                      <td>
                          {{ include('@MauticCore/Helper/list_actions.html.twig', {
                                  'item': item,
                                  'templateButtons': {
                                      'edit': permissions['lead:leads:editother'],
                                      'clone': permissions['lead:leads:create'],
                                      'delete': permissions['lead:leads:deleteother'],
                                  },
                                  'routeBase': 'company',
                          }) }}
                      </td>
                      <td>
                          <div>
                          {% if securityHasEntityAccess(permissions['lead:leads:editown'], permissions['lead:leads:editother'], item.createdBy) %}
                              <a href="{{ path('mautic_company_action', {'objectAction': 'view', 'objectId': item.id}) }}" data-toggle="ajax">
                                {% if fields.core.companyname is defined %}
                                  {{ fields.core.companyname.value|purify }}
                                {% endif %}
                              </a>
                          {% else %}
                              {% if fields.core.companyname is defined %}
                                {{ fields.core.companyname.value|purify }}
                              {% endif %}
                          {% endif %}
                          {{ include('@MauticProject/Modules/projects.html.twig') }}
                          </div>
                      </td>
                      <td>
                          {% if fields.core.companyemail is defined %}
                            {{ fields.core.companyemail.value|purify }}
                          {% endif %}
                      </td>

                      <td class="visible-md visible-lg">
                          {% if fields.core.companywebsite is defined %}
                            {{ inputUrl(fields.core.companywebsite.value) }}
                          {% endif %}
                      </td>
                      <td class="visible-md visible-lg">
                          <span size="sm" class="label label-gray">{{ item.score }}</span>
                      </td>
                      <td class="visible-md visible-lg">
                          <a size="sm" class="label label-gray"
                             href="{{ path('mautic_contact_index', {'search': 'mautic.lead.lead.searchcommand.company_id'|trans~':'~item.id}) }}"
                             data-toggle="ajax"
                             {% if 0 == leadCounts[item.id] %}disabled="disabled"{% endif %}>
                                  {{ 'mautic.lead.company.viewleads_count'|trans({'%count%': leadCounts[item.id]}) }}
                          </a>
                      </td>
                      <td class="visible-md visible-lg">{{ item.id }}</td>
                  </tr>
              {% endfor %}
              </tbody>
          </table>
      </div>
      <div class="panel-footer">
          {{ include('@MauticCore/Helper/pagination.html.twig', {
                  'totalItems': totalItems,
                  'page': page,
                  'limit': limit,
                  'menuLinkId': 'mautic_company_index',
                  'baseUrl': path('mautic_company_index'),
                  'sessionVar': 'company',
          }) }}
      </div>
  {% else %}
    {% if searchValue is not empty %}
      {{ include('@MauticCore/Helper/noresults.html.twig') }}
    {% else %}
      <div class="mt-80 col-md-offset-2 col-lg-offset-3 col-md-8 col-lg-5 height-auto">
        {% set childContainer %}
          <div class="mb-32">
              {% include '@MauticCore/Components/pictogram.html.twig' with {
                  'pictogram': 'organization',
                  'size': '80'
              } %}
          </div>
          <div class="type-body-02">
              {% include '@MauticCore/Components/list--styled.html.twig' with {
                  'type': 'unordered',
                  'style': 'checkmark',
                  'items': [
                      'mautic.company.empty.feature.segment_contacts',
                      'mautic.company.empty.feature.custom_fields',
                      'mautic.company.empty.feature.adjust_scores',
                      'mautic.company.empty.feature.identify',
                      'mautic.company.empty.feature.assign_contacts',
                  ]
              } %}
          </div>
        {% endset %}

        {{ include('@MauticCore/Components/content-block.html.twig', {
            heading: 'mautic.company.empty.heading',
            subheading: 'mautic.company.empty.subheading',
            childContainer: childContainer,
            cta: permissions['lead:leads:create'] ? {
                'label': 'mautic.company.action.add',
                'link': path('mautic_import_action', {'object': 'companies', 'objectAction': 'new'}),
                'attributes': {'data-toggle': 'ajax'}
            } : null
        }) }}
      </div>
    {% endif %}
  {% endif %}

  {% if isIndex %}
        </div>
    </div>
  {% endif %}
{% endblock %}
