{% extends "base/layout.html" %}
{% block content %}
  <h1 class="text-2xl font-semibold">Edit Branch</h1>

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

    <div class="grid gap-5">
      <div class="grid grid-cols-1 md:grid-cols-2 gap-3">
        <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 }}" 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 }}" />
          {% if settings.timezone_locked %}
            <span class="text-xs text-amber-700">Timezone is locked by policy.</span>
          {% endif %}
        </label>
      </div>

      <div class="grid grid-cols-1 md:grid-cols-2 gap-3">
        <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 or '' }}" placeholder="09:30" />
        </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 or '' }}" placeholder="18:30" />
        </label>
      </div>

      <div class="border-t pt-4">
        <div class="font-semibold">SMTP Credentials</div>
        <div class="grid grid-cols-1 md:grid-cols-2 gap-3 mt-3">
          <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="flex items-center gap-2 mt-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">
        <div class="font-semibold">Local Storage Root</div>
        <label class="grid gap-1 mt-3">
          <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 '' }}" placeholder="D:\AuditFirm\AuditFirm\Branch" />
        </label>
      </div>

      <div class="border-t pt-4">
        <div class="font-semibold">Branch Identity (Compliance)</div>
        <div class="grid grid-cols-1 md:grid-cols-2 gap-3 mt-3">
          <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 class="grid grid-cols-1 md:grid-cols-2 gap-3 mt-4">
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">Letterhead Logo Path</span>
            <input class="border rounded-xl px-3 py-2" name="letterhead_logo_path" value="{{ settings.letterhead_logo_path or '' }}" placeholder="D:\AuditFirm\Assets\logo.png" />
          </label>
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">Signature Image Path</span>
            <input class="border rounded-xl px-3 py-2" name="letterhead_signature_path" value="{{ settings.letterhead_signature_path or '' }}" placeholder="D:\AuditFirm\Assets\sign.png" />
          </label>
          <label class="grid gap-1 md:col-span-2">
            <span class="text-sm text-slate-600">Stamp Image Path</span>
            <input class="border rounded-xl px-3 py-2" name="letterhead_stamp_path" value="{{ settings.letterhead_stamp_path or '' }}" placeholder="D:\AuditFirm\Assets\stamp.png" />
          </label>
        </div>
      </div>

      <div class="border-t pt-4">
        <div class="font-semibold">Working Days & Holidays</div>
        <div class="grid grid-cols-1 md:grid-cols-2 gap-3 mt-3">
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">Working Days CSV</span>
            <input class="border rounded-xl px-3 py-2" name="working_days_csv" value="{{ settings.working_days_csv }}" />
          </label>
          <label class="flex items-center gap-2 mt-7">
            <input type="checkbox" name="timezone_locked" {% if settings.timezone_locked %}checked{% endif %} />
            <span class="text-sm text-slate-700">Lock Timezone</span>
          </label>
          <label class="grid gap-1 md:col-span-2">
            <span class="text-sm text-slate-600">Holidays JSON</span>
            <textarea class="border rounded-xl px-3 py-2 h-28" name="holidays_json">{{ settings.holidays_json }}</textarea>
          </label>
        </div>
      </div>

      <div class="border-t pt-4">
        <div class="font-semibold">Email Policy</div>
        <div class="grid grid-cols-1 md:grid-cols-2 gap-3 mt-3">
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">From Name</span>
            <input class="border rounded-xl px-3 py-2" name="email_from_name" value="{{ settings.email_from_name or '' }}" />
          </label>
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">From Email</span>
            <input class="border rounded-xl px-3 py-2" name="email_from_email" value="{{ settings.email_from_email or '' }}" />
          </label>
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">Reply-To</span>
            <input class="border rounded-xl px-3 py-2" name="email_reply_to" value="{{ settings.email_reply_to or '' }}" />
          </label>
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">Default CC (CSV)</span>
            <input class="border rounded-xl px-3 py-2" name="default_cc_csv" value="{{ settings.default_cc_csv or '' }}" />
          </label>
          <label class="grid gap-1 md:col-span-2">
            <span class="text-sm text-slate-600">Default BCC (CSV)</span>
            <input class="border rounded-xl px-3 py-2" name="default_bcc_csv" value="{{ settings.default_bcc_csv or '' }}" />
          </label>
          <label class="grid gap-1 md:col-span-2">
            <span class="text-sm text-slate-600">Email Signature (HTML)</span>
            <textarea class="border rounded-xl px-3 py-2 h-28" name="email_signature_html">{{ settings.email_signature_html or '' }}</textarea>
          </label>
        </div>
      </div>

      <div class="border-t pt-4">
        <div class="font-semibold">Storage Policy</div>
        <div class="grid grid-cols-1 md:grid-cols-2 gap-3 mt-3">
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">Storage Mode</span>
            <select class="border rounded-xl px-3 py-2" name="storage_mode">
              {% set sm = settings.storage_mode %}
              <option value="local_only" {% if sm == "local_only" %}selected{% endif %}>local_only</option>
              <option value="cloud_only" {% if sm == "cloud_only" %}selected{% endif %}>cloud_only</option>
              <option value="hybrid" {% if sm == "hybrid" %}selected{% endif %}>hybrid</option>
            </select>
          </label>
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">Max File Size (MB)</span>
            <input class="border rounded-xl px-3 py-2" name="max_file_mb" value="{{ settings.max_file_mb }}" />
          </label>
          <label class="grid gap-1 md:col-span-2">
            <span class="text-sm text-slate-600">Allowed Extensions (CSV)</span>
            <input class="border rounded-xl px-3 py-2" name="allowed_ext_csv" value="{{ settings.allowed_ext_csv }}" />
          </label>
          <label class="grid gap-1 md:col-span-2">
            <span class="text-sm text-slate-600">Folder Template</span>
            <input class="border rounded-xl px-3 py-2" name="folder_template" value="{{ settings.folder_template }}" />
            <span class="text-xs text-slate-500">Tokens: {root}, {client_code}, {fy}, {service}</span>
          </label>
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">Retention Years</span>
            <input class="border rounded-xl px-3 py-2" name="retention_years" value="{{ settings.retention_years }}" />
          </label>
        </div>
      </div>

      <div class="border-t pt-4">
        <div class="font-semibold">Security Policy</div>
        <div class="grid grid-cols-1 md:grid-cols-2 gap-3 mt-3">
          <label class="grid gap-1 md:col-span-2">
            <span class="text-sm text-slate-600">OTP Required Roles (CSV)</span>
            <input class="border rounded-xl px-3 py-2" name="otp_required_roles_csv" value="{{ settings.otp_required_roles_csv }}" />
          </label>
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">Session Duration (minutes)</span>
            <input class="border rounded-xl px-3 py-2" name="session_duration_minutes" value="{{ settings.session_duration_minutes }}" />
          </label>
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">Lockout Attempts</span>
            <input class="border rounded-xl px-3 py-2" name="lockout_attempts" value="{{ settings.lockout_attempts }}" />
          </label>
          <label class="grid gap-1">
            <span class="text-sm text-slate-600">Lockout Minutes</span>
            <input class="border rounded-xl px-3 py-2" name="lockout_minutes" value="{{ settings.lockout_minutes }}" />
          </label>
        </div>
      </div>

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