{% extends "ui/templates/base/layout.html" %}
{% block content %}
<div class="space-y-6">
  <div>
    <h1 class="text-2xl font-semibold">Edit Branch</h1>
    <p class="mt-1 text-sm text-slate-500">
      Branch remains mapped to its audit firm. Firm Admin can edit only branches of their own audit firm.
    </p>
  </div>

  {% if flash %}
  <div class="rounded-2xl border border-amber-200 bg-amber-50 px-4 py-3 text-sm text-amber-900">
    {{ flash }}
  </div>
  {% endif %}

  <form class="rounded-2xl border bg-white p-5 shadow-soft max-w-5xl" method="post">
    <input type="hidden" name="csrf_token" value="{{ csrf_token }}" />

    <div class="grid gap-5">
      <div class="grid grid-cols-1 gap-3 md:grid-cols-2">
        <label class="grid gap-1">
          <span class="text-sm text-slate-600">Firm</span>
          <div class="rounded-xl border bg-slate-50 px-3 py-2 text-sm text-slate-700">
            {{ branch.tenant.name if branch.tenant else ('Audit Firm ' ~ branch.tenant_id) }}
          </div>
        </label>

        <label class="grid gap-1">
          <span class="text-sm text-slate-600">Branch Code</span>
          <input class="border rounded-xl bg-slate-50 px-3 py-2" value="{{ branch.code }}" readonly />
        </label>
      </div>

      <div class="grid grid-cols-1 gap-3 md:grid-cols-2">
        <label class="grid gap-1">
          <span class="text-sm text-slate-600">Branch Name</span>
          <input class="border rounded-xl px-3 py-2" name="name" value="{{ branch.name or '' }}" required />
        </label>

        <label class="grid gap-1">
          <span class="text-sm text-slate-600">Timezone</span>
          <input class="border rounded-xl px-3 py-2" name="timezone" value="{{ branch.timezone or 'Asia/Kolkata' }}" required />
        </label>
      </div>

      <div class="grid grid-cols-1 gap-3 md:grid-cols-2">
        <label class="grid gap-1">
          <span class="text-sm text-slate-600">Office Start Time (HH:MM)</span>
          <input class="border rounded-xl px-3 py-2" name="office_start_time" value="{{ branch.office_start_time.strftime('%H:%M') if branch.office_start_time else '' }}" />
        </label>
        <label class="grid gap-1">
          <span class="text-sm text-slate-600">Office End Time (HH:MM)</span>
          <input class="border rounded-xl px-3 py-2" name="office_end_time" value="{{ branch.office_end_time.strftime('%H:%M') if branch.office_end_time else '' }}" />
        </label>
      </div>

      <div class="border-t pt-4">
        <div class="font-semibold">SMTP Credentials</div>
        <div class="mt-3 grid grid-cols-1 gap-3 md:grid-cols-2">
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">SMTP Host</span>
            <input class="border rounded-xl px-3 py-2" name="smtp_host" value="{{ branch.smtp_host or '' }}" />
          </label>
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">SMTP Port</span>
            <input class="border rounded-xl px-3 py-2" name="smtp_port" value="{{ branch.smtp_port or '' }}" />
          </label>
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">SMTP Username</span>
            <input class="border rounded-xl px-3 py-2" name="smtp_username" value="{{ branch.smtp_username or '' }}" />
          </label>
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">SMTP Password</span>
            <input class="border rounded-xl px-3 py-2" name="smtp_password" value="{{ branch.smtp_password or '' }}" />
          </label>
          <label class="mt-2 flex items-center gap-2">
            <input type="checkbox" name="smtp_use_tls" {% if branch.smtp_use_tls %}checked{% endif %} />
            <span class="text-sm text-slate-700">Use TLS</span>
          </label>
        </div>
      </div>

      <div class="border-t pt-4">
        <label class="grid gap-1">
          <span class="text-sm text-slate-600">Local Storage Path</span>
          <input class="border rounded-xl px-3 py-2" name="local_storage_path" value="{{ branch.local_storage_path or '' }}" />
        </label>
      </div>

      <div class="border-t pt-4">
        <div class="font-semibold">Branch Identity (Compliance)</div>
        <div class="mt-3 grid grid-cols-1 gap-3 md:grid-cols-2">
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">GSTIN</span>
            <input class="border rounded-xl px-3 py-2" name="gstin" value="{{ settings.gstin or '' }}" />
          </label>
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">PAN</span>
            <input class="border rounded-xl px-3 py-2" name="pan" value="{{ settings.pan or '' }}" />
          </label>

          <label class="grid gap-1 md:col-span-2">
            <span class="text-sm text-slate-600">Address Line 1</span>
            <input class="border rounded-xl px-3 py-2" name="address_line1" value="{{ settings.address_line1 or '' }}" />
          </label>

          <label class="grid gap-1 md:col-span-2">
            <span class="text-sm text-slate-600">Address Line 2</span>
            <input class="border rounded-xl px-3 py-2" name="address_line2" value="{{ settings.address_line2 or '' }}" />
          </label>

          <label class="grid gap-1">
            <span class="text-sm text-slate-600">City</span>
            <input class="border rounded-xl px-3 py-2" name="city" value="{{ settings.city or '' }}" />
          </label>

          <label class="grid gap-1">
            <span class="text-sm text-slate-600">State</span>
            <input class="border rounded-xl px-3 py-2" name="state" value="{{ settings.state or '' }}" />
          </label>

          <label class="grid gap-1">
            <span class="text-sm text-slate-600">PIN Code</span>
            <input class="border rounded-xl px-3 py-2" name="pin_code" value="{{ settings.pin_code or '' }}" />
          </label>
        </div>
      </div>

      <div class="border-t pt-4">
        <div class="font-semibold">Attendance Geo/IP Controls</div>
        <p class="mt-1 text-xs text-slate-500">If geo/IP validation fails, employee punch is still saved but marked pending approval for OD/client visit review.</p>
        <div class="mt-3 grid grid-cols-1 gap-3 md:grid-cols-2">
          <label class="grid gap-1 md:col-span-2">
            <span class="text-sm text-slate-600">Geo Address / Landmark</span>
            <input class="border rounded-xl px-3 py-2" name="geo_address" value="{{ settings.geo_address or '' }}" />
          </label>
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">Latitude</span>
            <input class="border rounded-xl px-3 py-2" name="latitude" value="{{ settings.latitude if settings.latitude is not none else '' }}" />
          </label>
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">Longitude</span>
            <input class="border rounded-xl px-3 py-2" name="longitude" value="{{ settings.longitude if settings.longitude is not none else '' }}" />
          </label>
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">Allowed Radius (meters)</span>
            <input class="border rounded-xl px-3 py-2" name="attendance_geo_radius_meters" value="{{ settings.attendance_geo_radius_meters or 100 }}" />
          </label>
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">Grace Minutes</span>
            <input class="border rounded-xl px-3 py-2" name="attendance_grace_minutes" value="{{ settings.attendance_grace_minutes if settings.attendance_grace_minutes is not none else 10 }}" />
          </label>
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">Half Day After (HH:MM)</span>
            <input class="border rounded-xl px-3 py-2" name="attendance_half_day_after_time" value="{{ settings.attendance_half_day_after_time.strftime('%H:%M') if settings.attendance_half_day_after_time else '' }}" placeholder="13:00" />
          </label>
          <label class="mt-6 flex items-center gap-2">
            <input type="checkbox" name="attendance_rule_enabled" {% if settings.attendance_rule_enabled is not defined or settings.attendance_rule_enabled %}checked{% endif %} />
            <span class="text-sm text-slate-700">Enable timezone/late/half-day rules</span>
          </label>
          <label class="mt-6 flex items-center gap-2">
            <input type="checkbox" name="attendance_geo_enabled" {% if settings.attendance_geo_enabled %}checked{% endif %} />
            <span class="text-sm text-slate-700">Enable geo-fenced attendance</span>
          </label>
          <label class="grid gap-1 md:col-span-2">
            <span class="text-sm text-slate-600">Allowed Office IP / CIDR List</span>
            <textarea class="border rounded-xl px-3 py-2" name="attendance_allowed_ip_csv" rows="2">{{ settings.attendance_allowed_ip_csv or '' }}</textarea>
          </label>
          <label class="flex items-center gap-2">
            <input type="checkbox" name="attendance_ip_enabled" {% if settings.attendance_ip_enabled %}checked{% endif %} />
            <span class="text-sm text-slate-700">Enable optional IP/LAN validation</span>
          </label>
        </div>
      </div>

      <div class="flex gap-3">
        <button class="rounded-xl bg-slate-900 px-4 py-2 text-sm text-white" type="submit">Save Branch</button>
        <a class="rounded-xl border px-4 py-2 text-sm" href="/system-settings/branches">Cancel</a>
      </div>
    </div>
  </form>
</div>
{% endblock %}