{% include 'mixins/partials/notify.html' %}
<table
  class="table is-fullwidth is-hoverable"
  hx-target="#{{ context_object_name }}-table"
  id="{{ context_object_name }}-table"
  hx-swap="outerHTML"
  hx-trigger="{{ context_object_name_singular }}Event from:body"
  hx-get="{{ list_url }}">
  <thead>
    <th>number</th>
    <th>uuid</th>
    <th>account</th>
    <th>name</th>
    <th>person</th>
    <th>availability</th>
    <th>actions</th>
  </thead>
  {% for item in object_list %}
    <tr>
      <td>{% if item.chat %}{{ item.chat.source_number }}{% endif %}</td>
      <td>
        {% if item.chat %}
          <a
            class="has-text-grey button nowrap-child"
            onclick="window.prompt('Copy to clipboard: Ctrl+C, Enter', '{{ item.chat.source_uuid }}');">
            <span class="icon" title="Copy to clipboard" aria-label="Copy to clipboard">
              <i class="fa-solid fa-copy" aria-hidden="true"></i>
            </span>
          </a>
        {% endif %}
      </td>
      <td>{% if item.chat %}{{ item.chat.account }}{% endif %}</td>
      <td>
        {% if item.is_group %}
          <span class="tag is-info is-light is-small mr-1"><i class="fa-solid fa-users"></i></span>
        {% endif %}
        {% if item.chat %}{{ item.chat.source_name }}{% else %}{{ item.name }}{% endif %}
      </td>
      <td>{{ item.person_name|default:"-" }}</td>
      <td>
        {% if item.availability_label %}
          <span class="tag is-light">{{ item.availability_label }}</span>
        {% else %}
          -
        {% endif %}
      </td>
      <td>
        <div class="buttons">
          {% if not item.is_group %}
            <button
              hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
              hx-delete="{# url 'account_delete' type=type pk=item.id #}"
              hx-trigger="click"
              hx-target="#modals-here"
              hx-swap="innerHTML"
              hx-confirm="Are you sure you wish to unlink {{ item.chat }}?"
              class="button">
              <span class="icon-text">
                <span class="icon">
                  <i class="fa-solid fa-xmark"></i>
                </span>
              </span>
            </button>
          {% endif %}
          {% if type == 'page' %}
            {% if item.can_compose %}
              <a href="{{ item.compose_page_url }}"><button
                class="button"
                title="Manual text mode">
                <span class="icon-text">
                  <span class="icon">
                    <i class="{{ item.manual_icon_class }}"></i>
                  </span>
                </span>
              </button>
              </a>
            {% else %}
              <button class="button" disabled title="No identifier available for manual send">
                <span class="icon-text">
                  <span class="icon">
                    <i class="{{ item.manual_icon_class }}"></i>
                  </span>
                </span>
              </button>
            {% endif %}
            {% if not item.is_group %}
              <a href="{{ item.match_url }}"><button
                class="button"
                title="Match identifier to person">
                <span class="icon-text">
                  <span class="icon">
                    <i class="fa-solid fa-link"></i>
                  </span>
                </span>
              </button>
              </a>
            {% endif %}
            <a href="{{ item.ai_url }}"><button
              class="button"
              title="Open AI workspace">
              <span class="icon-text">
                <span class="icon">
                  <i class="fa-solid fa-brain-circuit"></i>
                </span>
              </span>
            </button>
            </a>
          {% else %}
            {% if item.can_compose %}
              <button
                hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
                hx-get="{{ item.compose_widget_url }}"
                hx-trigger="click"
                hx-target="#widgets-here"
                hx-swap="beforeend"
                class="button">
                <span class="icon-text">
                  <span class="icon">
                    <i class="{{ item.manual_icon_class }}"></i>
                  </span>
                </span>
              </button>
            {% else %}
              <button class="button" disabled title="No identifier available for manual send">
                <span class="icon-text">
                  <span class="icon">
                    <i class="{{ item.manual_icon_class }}"></i>
                  </span>
                </span>
              </button>
            {% endif %}
            {% if not item.is_group %}
              <a href="{{ item.match_url }}"><button class="button" title="Match identifier to person">
                <span class="icon-text">
                  <span class="icon">
                    <i class="fa-solid fa-link"></i>
                  </span>
                </span>
              </button></a>
            {% endif %}
            <a href="{{ item.ai_url }}"><button class="button">
              <span class="icon-text">
                <span class="icon">
                  <i class="fa-solid fa-brain-circuit"></i>
                </span>
              </span>
            </button></a>
          {% endif %}
        </div>
      </td>
    </tr>
  {% endfor %}

</table>
