{% extends "ui/templates/base/layout.html" %}
{% block content %}
{% include "modules/consultants/templates/consultants/_consultant_tabs.html" %}
{% set is_dict = managed_client is mapping %}
<div class="space-y-6">
  <div>
    <h2 class="text-xl font-semibold text-slate-900">{{ title }}</h2>
    <p class="text-sm text-slate-500">Maintain your own client record in the consultant portal. This does not change the audit firm's client master.</p>
  </div>

  {% if errors %}
  <div class="rounded-2xl border border-red-200 bg-red-50 p-4 text-sm text-red-700">
    <ul class="list-disc pl-5">{% for err in errors %}<li>{{ err }}</li>{% endfor %}</ul>
  </div>
  {% endif %}

  <form method="post" class="space-y-5 rounded-2xl border border-slate-200 bg-white p-5 shadow-soft">
    <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
    <div class="grid gap-4 md:grid-cols-3">
      <div>
        <label class="mb-1 block text-sm font-medium text-slate-700">Client Code</label>
        <input name="client_code" value="{{ managed_client.client_code if managed_client and not is_dict else managed_client.get('client_code','') if managed_client else '' }}" placeholder="Auto if blank" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
      </div>
      <div class="md:col-span-2">
        <label class="mb-1 block text-sm font-medium text-slate-700">Client Name *</label>
        <input name="client_name" required value="{{ managed_client.client_name if managed_client and not is_dict else managed_client.get('client_name','') if managed_client else '' }}" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
      </div>
      <div>
        <label class="mb-1 block text-sm font-medium text-slate-700">Trade Name</label>
        <input name="trade_name" value="{{ managed_client.trade_name if managed_client and not is_dict else managed_client.get('trade_name','') if managed_client else '' }}" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
      </div>
      <div>
        <label class="mb-1 block text-sm font-medium text-slate-700">Client Type</label>
        {% set ct = managed_client.client_type if managed_client and not is_dict else managed_client.get('client_type','Other') if managed_client else 'Other' %}
        <select name="client_type" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
          {% for option in client_types %}<option value="{{ option }}" {% if ct == option %}selected{% endif %}>{{ option }}</option>{% endfor %}
        </select>
      </div>
      <div>
        <label class="mb-1 block text-sm font-medium text-slate-700">PAN</label>
        <input name="pan" value="{{ managed_client.pan if managed_client and not is_dict else managed_client.get('pan','') if managed_client else '' }}" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm uppercase">
      </div>
      <div>
        <label class="mb-1 block text-sm font-medium text-slate-700">GSTIN</label>
        <input name="gstin" value="{{ managed_client.gstin if managed_client and not is_dict else managed_client.get('gstin','') if managed_client else '' }}" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm uppercase">
      </div>
      <div>
        <label class="mb-1 block text-sm font-medium text-slate-700">TAN</label>
        <input name="tan" value="{{ managed_client.tan if managed_client and not is_dict else managed_client.get('tan','') if managed_client else '' }}" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm uppercase">
      </div>
      <div>
        <label class="mb-1 block text-sm font-medium text-slate-700">Contact Person</label>
        <input name="contact_person_name" value="{{ managed_client.contact_person_name if managed_client and not is_dict else managed_client.get('contact_person_name','') if managed_client else '' }}" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
      </div>
      <div>
        <label class="mb-1 block text-sm font-medium text-slate-700">Mobile *</label>
        <input name="mobile" value="{{ managed_client.mobile if managed_client and not is_dict else managed_client.get('mobile','') if managed_client else '' }}" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
      </div>
      <div>
        <label class="mb-1 block text-sm font-medium text-slate-700">Email *</label>
        <input name="email" type="email" value="{{ managed_client.email if managed_client and not is_dict else managed_client.get('email','') if managed_client else '' }}" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
      </div>
    </div>

    <div class="grid gap-4 md:grid-cols-3">
      <div>
        <label class="mb-1 block text-sm font-medium text-slate-700">Address Line 1</label>
        <input name="address_line_1" value="{{ managed_client.address_line_1 if managed_client and not is_dict else managed_client.get('address_line_1','') if managed_client else '' }}" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
      </div>
      <div>
        <label class="mb-1 block text-sm font-medium text-slate-700">Address Line 2</label>
        <input name="address_line_2" value="{{ managed_client.address_line_2 if managed_client and not is_dict else managed_client.get('address_line_2','') if managed_client else '' }}" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
      </div>
      <div>
        <label class="mb-1 block text-sm font-medium text-slate-700">City</label>
        <input name="city" value="{{ managed_client.city if managed_client and not is_dict else managed_client.get('city','') if managed_client else '' }}" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
      </div>
      <div>
        <label class="mb-1 block text-sm font-medium text-slate-700">State</label>
        <input name="state" value="{{ managed_client.state if managed_client and not is_dict else managed_client.get('state','') if managed_client else '' }}" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
      </div>
      <div>
        <label class="mb-1 block text-sm font-medium text-slate-700">Pincode</label>
        <input name="pincode" value="{{ managed_client.pincode if managed_client and not is_dict else managed_client.get('pincode','') if managed_client else '' }}" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
      </div>
      <div>
        <label class="mb-1 block text-sm font-medium text-slate-700">Country</label>
        <input name="country" value="{{ managed_client.country if managed_client and not is_dict else managed_client.get('country','India') if managed_client else 'India' }}" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
      </div>
    </div>

    <div class="grid gap-4 md:grid-cols-3">
      <div>
        <label class="mb-1 block text-sm font-medium text-slate-700">Relationship Stage</label>
        {% set stage = managed_client.relationship_stage if managed_client and not is_dict else managed_client.get('relationship_stage','managed') if managed_client else 'managed' %}
        <select name="relationship_stage" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
          {% for code, label in managed_client_stages %}<option value="{{ code }}" {% if stage == code %}selected{% endif %}>{{ label }}</option>{% endfor %}
        </select>
      </div>
      <div>
        <label class="mb-1 block text-sm font-medium text-slate-700">Status</label>
        {% set st = managed_client.status if managed_client and not is_dict else managed_client.get('status','active') if managed_client else 'active' %}
        <select name="status" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
          {% for code, label in managed_client_statuses %}<option value="{{ code }}" {% if st == code %}selected{% endif %}>{{ label }}</option>{% endfor %}
        </select>
      </div>
      <label class="mt-7 inline-flex items-center gap-2 text-sm text-slate-700">
        <input type="checkbox" name="is_active" {% if not managed_client or (managed_client and ((not is_dict and managed_client.is_active) or (is_dict and managed_client.get('is_active', True)))) %}checked{% endif %}> Active
      </label>
    </div>

    <div>
      <label class="mb-1 block text-sm font-medium text-slate-700">Service Interest</label>
      <textarea name="service_interest" rows="3" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">{{ managed_client.service_interest if managed_client and not is_dict else managed_client.get('service_interest','') if managed_client else '' }}</textarea>
    </div>
    <div>
      <label class="mb-1 block text-sm font-medium text-slate-700">Notes</label>
      <textarea name="notes" rows="3" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">{{ managed_client.notes if managed_client and not is_dict else managed_client.get('notes','') if managed_client else '' }}</textarea>
    </div>

    <div class="flex justify-end gap-3">
      <a href="/consultant/managed-clients" class="rounded-xl border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Cancel</a>
      <button class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-700">Save Managed Client</button>
    </div>
  </form>
</div>
{% endblock %}
