{% extends "admin/base.html" %}
{% import "macros/ui.html" as ui %}

{% block title %}{{ customer.name }}{% endblock title %}
{% block crumb %}{{ t(key="admin-customers", lang=lang | default(value='sk')) }}{% endblock crumb %}

{% block content %}
{% set L = lang | default(value='sk') %}
{% set q_enc = query | default(value='') | urlencode %}
<div class="flex items-center justify-between gap-3">
  <div>
    <h1 class="text-2xl font-bold text-on-surface-strong dark:text-on-surface-dark-strong">{{ customer.name }}</h1>
    <p class="text-sm text-on-surface/70 dark:text-on-surface-dark/70">{{ customer.email }}</p>
  </div>
  {{ ui::button(variant="outline-secondary", label=t(key="back", lang=lang | default(value='sk')), href="/admin/customers", size="px-3 py-2 text-sm") }}
</div>

{% if error %}
<div class="mt-4">{{ ui::alert_danger(message=t(key=error, lang=lang | default(value='sk'))) }}</div>
{% endif %}

<!-- assigned discount profiles -->
<section class="mt-6 rounded-radius border border-outline bg-surface p-6 dark:border-outline-dark dark:bg-surface-dark-alt">
  <h2 class="text-lg font-semibold text-on-surface-strong dark:text-on-surface-dark-strong">{{ t(key="discount-profiles", lang=lang | default(value='sk')) }}</h2>
  {% if profiles | length > 0 %}
  <form method="post" action="/admin/customers/{{ customer.id }}/profiles" class="mt-3 space-y-3">
    {{ ui::csrf_field() }}
    <div class="grid gap-2 sm:grid-cols-2">
      {% for profile in profiles %}
      <label class="flex items-center gap-2 text-sm text-on-surface dark:text-on-surface-dark">
        <input type="checkbox" name="profile_ids" value="{{ profile.id }}" {% if profile.assigned %}checked{% endif %}>
        <span>{{ profile.name }} <span class="text-on-surface/60 dark:text-on-surface-dark/60">(−{{ profile.percent }}%, {% if profile.scope_type == "all_except" %}{{ t(key="scope-all-except", lang=lang | default(value='sk')) }}{% else %}{{ t(key="scope-include", lang=lang | default(value='sk')) }}{% endif %})</span></span>
      </label>
      {% endfor %}
    </div>
    {{ ui::button(label=t(key="save", lang=lang | default(value='sk')), type="submit", size="px-4 py-2 text-sm") }}
  </form>
  {% else %}
  <p class="mt-2 text-sm text-on-surface/70 dark:text-on-surface-dark/70">
    {{ t(key="admin-no-profiles", lang=lang | default(value='sk')) }}
    <a href="/admin/catalog/discount-profiles/new" class="text-primary dark:text-primary-dark">{{ t(key="new-profile", lang=lang | default(value='sk')) }}</a>
  </p>
  {% endif %}
</section>

<div class="mt-6 flex flex-wrap items-center justify-between gap-3">
  <p class="text-sm text-on-surface/70 dark:text-on-surface-dark/70">{{ t(key="negotiated-prices-hint", lang=L) }}</p>

  <!-- product search (drafts included); keeps the active category -->
  <form method="get" action="/admin/customers/{{ customer.id }}" role="search" class="relative w-full max-w-xs">
    <input type="hidden" name="category" value="{{ selected_category }}">
    <span class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3 text-on-surface/50 dark:text-on-surface-dark/50">
      {{ ui::icon(name="search", size="size-5") }}
    </span>
    <input type="search" name="q" value="{{ query | default(value='') }}" autocomplete="off"
      placeholder="{{ t(key='search-placeholder', lang=L) }}" aria-label="{{ t(key='search-placeholder', lang=L) }}"
      class="w-full rounded-radius border border-outline bg-surface py-2 pl-10 pr-3 text-sm text-on-surface placeholder:text-on-surface/50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary dark:border-outline-dark dark:bg-surface-dark-alt dark:text-on-surface-dark dark:placeholder:text-on-surface-dark/50 dark:focus-visible:outline-primary-dark">
  </form>
</div>

{% set category_base = "/admin/customers/" ~ customer.id %}
{% set category_suffix = "&q=" ~ q_enc %}
<div class="mt-3 flex flex-col gap-6 md:flex-row md:items-start">
  {% include "admin/partials/category_filter.html" %}
  <div class="min-w-0 flex-1 {{ ui::table_wrap_cls() }}">
  {% if products | length > 0 %}
  <table class="{{ ui::table_cls() }}">
    <thead class="{{ ui::thead_cls() }}">
      <tr>
        {{ ui::th(label=t(key="product", lang=lang | default(value='sk'))) }}
        {{ ui::th(label=t(key="business-price", lang=lang | default(value='sk'))) }}
        {{ ui::th(label=t(key="effective-price", lang=lang | default(value='sk'))) }}
        {{ ui::th(label=t(key="actions", lang=lang | default(value='sk')), align="text-right") }}
      </tr>
    </thead>
    <tbody class="{{ ui::tbody_cls() }}">
      {% for product in products %}
      <tr class="{{ ui::row_cls() }}">
        <td class="px-4 py-3 font-medium text-on-surface-strong dark:text-on-surface-dark-strong">
          {{ product.name }}
          {% if product.variant_label %}<span class="block text-xs font-normal text-on-surface/60 dark:text-on-surface-dark/60">{{ product.variant_label }}</span>{% endif %}
        </td>
        <td class="px-4 py-3 tabular-nums">
          {% if product.business_reduced %}
          <span class="font-medium text-danger">{{ product.business_price }} €</span>
          <span class="ml-1 text-xs text-on-surface/50 line-through dark:text-on-surface-dark/50">{{ product.regular_price }}</span>
          {% else %}
          {{ product.business_price }} €
          {% endif %}
        </td>
        <td class="px-4 py-3 tabular-nums">
          <span class="font-medium {% if product.effective_differs %}text-primary dark:text-primary-dark{% else %}text-on-surface-strong dark:text-on-surface-dark-strong{% endif %}">{{ product.effective_price }} €</span>
          {% if product.collision %}<span class="ml-1">{{ ui::badge(label=t(key="collision", lang=lang | default(value='sk')), variant="warning") }}</span>{% endif %}
        </td>
        <td class="px-4 py-3">
          <div class="flex flex-wrap justify-end gap-2">
            {{ ui::button(variant="outline-secondary", label=t(key="set-negotiated-price", lang=lang | default(value='sk')), href="/admin/customers/" ~ customer.id ~ "/prices/" ~ product.variant_id ~ "/edit", size="px-3 py-1.5 text-xs") }}
            {% if product.has_negotiated %}
            <form method="post" action="/admin/customers/{{ customer.id }}/prices/{{ product.variant_id }}/remove"
              onsubmit="return confirm('{{ t(key="negotiated-remove-confirm", lang=lang | default(value='sk')) }}')">
              {{ ui::csrf_field() }}
              {{ ui::button(variant="outline-danger", label=t(key="remove", lang=lang | default(value='sk')), type="submit", size="px-3 py-1.5 text-xs") }}
            </form>
            {% endif %}
          </div>
        </td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
  {% else %}
  <div class="flex flex-col items-center gap-3 px-4 py-16 text-center">
    <p class="text-on-surface/70 dark:text-on-surface-dark/70">{{ t(key="admin-no-products", lang=lang | default(value='sk')) }}</p>
  </div>
  {% endif %}
  </div>
</div>
{% endblock content %}
