{% import "macros/buttons.html" as buttons %}
{% import "macros/icons.html" as icons %}
{% import "macros/datatable.html" as datatable %}

{% macro email_column(user) %}
<a
  class="font-medium text-slate-800 hover:text-slate-900"
  href="{{ url_for('dashboard.users:get', id=user.id) }}"
  hx-boost="true"
  hx-target="#aside"
  _="install OpenAside"
>
  {{ user.email }}
</a>
{% endmacro %}

{% macro created_at_column(user) %}
{{ user.created_at.strftime('%x %X') }}
{% endmacro %}

{% macro updated_at_column(user) %}
{{ user.updated_at.strftime('%x %X') }}
{% endmacro %}

{% macro tenant_column(user) %}
{{ user.tenant.name }}
{% endmacro %}

{% macro id_column(user) %}
{{ user.id }}
{% endmacro %}

{% macro user_field_value_column(user, user_field) %}
  {% set value = user.get_user_field_value(user_field).value %}
  {% if value %}
    {% if user_field.type == "BOOLEAN" %}
      {% if value %}
        {{ icons.check('w-4 h-4') }}
      {% else %}
        {{ icons.x_mark('w-4 h-4') }}
      {% endif %}
    {% elif user_field.type == "DATE" %}
      {{ value.strftime('%x') }}
    {% elif user_field.type == "DATETIME" %}
      {{ value.strftime('%x %X') }}
    {% elif user_field.type == "CHOICE" %}
      {{ user_field.get_choice_label(value) }}
    {% elif user_field.type == "ADDRESS" %}
      <span>{{ value.line1 }}{% if value.line2 %} {{ value.line2 }}{% endif %}</span>
      <span>, </span>
      <span>{{ value.postal_code }} </span>
      <span>{{ value.city }}, </span>
      {% if value.state %}
        <span>{{ value.state }}, </span>
      {% endif %}
      <span>{{ value.country }}</span>
    {% else %}
      {{ value }}
    {% endif %}
  {% endif %}
{% endmacro %}

{{
  datatable.datatable(
    users,
    count,
    datatable_query_parameters,
    "Users",
    columns | map("get_column_macro") | list,
  )
}}
