{% extends "ui/templates/base/layout.html" %}
{% set f = form if form else {} %}
{% block content %}
<div class="space-y-6">
  <div>
    <h1 class="text-2xl font-semibold text-slate-900">{{ title }}</h1>
    <p class="mt-1 text-sm text-slate-500">Create or update a domain mapping. Runtime resolver will use this table in Phase 7T.2.</p>
  </div>

  {% if errors %}
  <div class="rounded-2xl border border-rose-200 bg-rose-50 p-4 text-sm text-rose-800">
    <div class="font-semibold">Please fix the following:</div>
    <ul class="mt-2 list-disc pl-5">{% for e in errors %}<li>{{ e }}</li>{% endfor %}</ul>
  </div>
  {% endif %}

  <form method="post" class="max-w-5xl rounded-2xl border bg-white p-5 shadow-soft">
    <input type="hidden" name="csrf_token" value="{{ csrf_token }}" />
    <div class="grid grid-cols-1 gap-4 md:grid-cols-2">
      <label class="grid gap-1 md:col-span-2">
        <span class="text-sm text-slate-600">Domain Name</span>
        <input class="rounded-xl border px-3 py-2" name="domain_name" required placeholder="filingabc.com" value="{{ f.domain_name if f.domain_name is defined else (mapping.domain_name if mapping else '') }}" />
      </label>

      <label class="grid gap-1">
        <span class="text-sm text-slate-600">Domain Type</span>
        {% set selected_type = f.domain_type if f.domain_type is defined else (mapping.domain_type if mapping else 'marketplace') %}
        <select class="rounded-xl border px-3 py-2" name="domain_type" required>
          {% for code, label in domain_types %}<option value="{{ code }}" {% if selected_type == code %}selected{% endif %}>{{ label }}</option>{% endfor %}
        </select>
      </label>

      <label class="grid gap-1">
        <span class="text-sm text-slate-600">Status</span>
        {% set selected_status = f.status if f.status is defined else (mapping.status if mapping else 'draft') %}
        <select class="rounded-xl border px-3 py-2" name="status">
          {% for code, label in statuses %}<option value="{{ code }}" {% if selected_status == code %}selected{% endif %}>{{ label }}</option>{% endfor %}
        </select>
      </label>

      <label class="grid gap-1">
        <span class="text-sm text-slate-600">Audit Firm</span>
        {% set selected_tenant = f.tenant_id if f.tenant_id is defined else (mapping.tenant_id if mapping else '') %}
        <select class="rounded-xl border px-3 py-2" name="tenant_id">
          <option value="">Not applicable</option>
          {% for t in tenants %}<option value="{{ t.id }}" {% if selected_tenant == t.id %}selected{% endif %}>{{ t.display_name or t.name }} ({{ t.code }})</option>{% endfor %}
        </select>
      </label>

      <label class="grid gap-1">
        <span class="text-sm text-slate-600">Branch</span>
        {% set selected_branch = f.branch_id if f.branch_id is defined else (mapping.branch_id if mapping else '') %}
        <select class="rounded-xl border px-3 py-2" name="branch_id">
          <option value="">Not applicable</option>
          {% for b in branches %}<option value="{{ b.id }}" {% if selected_branch == b.id %}selected{% endif %}>{{ b.name }} (Firm {{ b.tenant_id }})</option>{% endfor %}
        </select>
      </label>

      <label class="grid gap-1">
        <span class="text-sm text-slate-600">Consultant</span>
        {% set selected_consultant = f.consultant_id if f.consultant_id is defined else (mapping.consultant_id if mapping else '') %}
        <select class="rounded-xl border px-3 py-2" name="consultant_id">
          <option value="">Not applicable</option>
          {% for c in consultants %}<option value="{{ c.id }}" {% if selected_consultant == c.id %}selected{% endif %}>{{ c.contact_person }}{% if c.firm_name %} - {{ c.firm_name }}{% endif %}</option>{% endfor %}
        </select>
      </label>

      <label class="grid gap-1">
        <span class="text-sm text-slate-600">Parent Firm for Consultant Domain</span>
        {% set selected_parent = f.parent_tenant_id if f.parent_tenant_id is defined else (mapping.parent_tenant_id if mapping else '') %}
        <select class="rounded-xl border px-3 py-2" name="parent_tenant_id">
          <option value="">Not applicable</option>
          {% for t in tenants %}<option value="{{ t.id }}" {% if selected_parent == t.id %}selected{% endif %}>{{ t.display_name or t.name }} ({{ t.code }})</option>{% endfor %}
        </select>
      </label>

      <label class="grid gap-1">
        <span class="text-sm text-slate-600">SSL Mode</span>
        {% set selected_ssl = f.ssl_mode if f.ssl_mode is defined else (mapping.ssl_mode if mapping else 'manual') %}
        <select class="rounded-xl border px-3 py-2" name="ssl_mode">
          {% for code, label in ssl_modes %}<option value="{{ code }}" {% if selected_ssl == code %}selected{% endif %}>{{ label }}</option>{% endfor %}
        </select>
      </label>

      <div class="flex items-center gap-6 rounded-xl border bg-slate-50 px-4 py-3">
        {% set primary_checked = f.is_primary if f.is_primary is defined else (mapping.is_primary if mapping else False) %}
        {% set verified_checked = f.is_verified if f.is_verified is defined else (mapping.is_verified if mapping else False) %}
        <label class="flex items-center gap-2 text-sm"><input type="checkbox" name="is_primary" {% if primary_checked %}checked{% endif %}> Primary</label>
        <label class="flex items-center gap-2 text-sm"><input type="checkbox" name="is_verified" {% if verified_checked %}checked{% endif %}> Verified manually</label>
      </div>

      <label class="grid gap-1 md:col-span-2">
        <span class="text-sm text-slate-600">Notes</span>
        <textarea class="rounded-xl border px-3 py-2" name="notes" rows="4">{{ f.notes if f.notes is defined else (mapping.notes if mapping else '') }}</textarea>
      </label>
    </div>

    <div class="mt-5 flex gap-3">
      <button class="rounded-xl bg-slate-900 px-4 py-2 text-sm font-medium text-white">Save Domain</button>
      <a href="/domains" class="rounded-xl border px-4 py-2 text-sm text-slate-700">Cancel</a>
    </div>
  </form>
</div>
{% endblock %}
