{% extends "ui/templates/base/layout.html" %}
{% block content %}
<div class="mb-4 flex flex-wrap gap-3">
    <form method="post" action="/employees/{{ employee.id }}/onboarding/generate" class="inline-block">
      <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
      <button class="rounded-xl bg-indigo-700 px-4 py-2 text-sm font-semibold text-white">Generate Onboarding</button>
    </form>
    <form method="post" action="/employees/{{ employee.id }}/offboarding/initiate" class="inline-grid gap-2 rounded-2xl border bg-white p-3 align-top shadow-sm md:grid-cols-4">
      <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
      <select name="request_type" class="rounded-lg border px-2 py-1 text-sm"><option value="resignation">Resignation</option><option value="termination">Termination</option><option value="contract_end">Contract End</option></select>
      <input type="date" name="requested_relieving_date" class="rounded-lg border px-2 py-1 text-sm">
      <input name="reason" placeholder="Reason" class="rounded-lg border px-2 py-1 text-sm">
      <button class="rounded-lg bg-rose-700 px-3 py-1 text-sm font-semibold text-white">Initiate Offboarding</button>
    </form>
</div>
<div class="space-y-6">
  <div class="flex flex-wrap items-center justify-between gap-3">
    <div>
      <h2 class="text-xl font-semibold text-slate-900">{{ employee.full_name }}</h2>
      <p class="text-sm text-slate-500">Employee Code: {{ employee.employee_code }} • Audit Firm {{ employee.tenant_id }} • Branch {{ employee.branch_id }}</p>
    </div>
    <div class="flex gap-2">
      <a href="/employees" class="rounded-xl border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Back</a>
      {% if can_manage_employees(current_user, current_user_permissions, current_user_roles) %}<a href="/employees/{{ employee.id }}/edit" class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-700">Edit</a>{% endif %}
    </div>
  </div>

  {% if link_error %}
  <div class="rounded-2xl border border-red-200 bg-red-50 p-4 text-sm text-red-800">Unable to update login linkage. Please select an active user from the same audit firm and branch who is not already linked to another employee.</div>
  {% endif %}

  <div class="rounded-2xl border {% if employee.user_id %}border-emerald-200 bg-emerald-50{% else %}border-amber-200 bg-amber-50{% endif %} p-4 shadow-soft">
    <div class="flex flex-wrap items-start justify-between gap-3">
      <div>
        <h3 class="font-semibold {% if employee.user_id %}text-emerald-900{% else %}text-amber-900{% endif %}">Login user linkage</h3>
        {% if employee.user_id %}
        <p class="mt-1 text-sm text-emerald-800">This employee is linked to {% if employee.user %}{{ employee.user.full_name or employee.user.email }}{% else %}User #{{ employee.user_id }}{% endif %}. Employee self-service pages will work for this login.</p>
        {% else %}
        <p class="mt-1 text-sm text-amber-900">This employee is not linked to a login user yet. Attendance, My Workspace, profile, leave, documents and payslip self-service need this link.</p>
        {% endif %}
      </div>
      {% if can_manage_employees(current_user, current_user_permissions, current_user_roles) %}
      <form method="post" action="/employees/{{ employee.id }}/link-user" class="flex flex-wrap items-center gap-2">
        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
        <select name="user_id" class="min-w-64 rounded-xl border border-slate-300 bg-white px-3 py-2 text-sm">
          <option value="">Unlink / no login user</option>
          {% for u in linkable_users %}
          <option value="{{ u.id }}" {% if employee.user_id == u.id %}selected{% endif %}>{{ u.full_name or u.email }} — {{ u.email }}</option>
          {% endfor %}
        </select>
        <button class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-700">Update Link</button>
      </form>
      {% endif %}
    </div>
  </div>

  <div class="grid gap-6 lg:grid-cols-3">
    <div class="space-y-6 lg:col-span-2">
      <div class="rounded-2xl border border-slate-200 bg-white p-6 shadow-soft">
        <h3 class="font-semibold text-slate-900">Basic Details</h3>
        <dl class="mt-4 grid gap-4 md:grid-cols-2 text-sm">
          <div><dt class="text-slate-500">Email</dt><dd class="font-medium text-slate-900">{{ employee.email or '-' }}</dd></div>
          <div><dt class="text-slate-500">Mobile</dt><dd class="font-medium text-slate-900">{{ employee.mobile or '-' }}</dd></div>
          <div><dt class="text-slate-500">Department</dt><dd class="font-medium text-slate-900">{{ employee.department or '-' }}</dd></div>
          <div><dt class="text-slate-500">Designation</dt><dd class="font-medium text-slate-900">{{ employee.designation or '-' }}</dd></div>
          <div><dt class="text-slate-500">Employment Type</dt><dd class="font-medium text-slate-900">{{ employee.employment_type.replace('_',' ').title() }}</dd></div>
          <div><dt class="text-slate-500">Date of Joining</dt><dd class="font-medium text-slate-900">{{ employee.date_of_joining or '-' }}</dd></div>
          <div><dt class="text-slate-500">Date of Leaving</dt><dd class="font-medium text-slate-900">{{ employee.date_of_leaving or '-' }}</dd></div>
          <div><dt class="text-slate-500">Linked User</dt><dd class="font-medium text-slate-900">{% if employee.user %}{{ employee.user.full_name or employee.user.email }} <span class="text-xs text-slate-500">({{ employee.user.email }})</span>{% else %}<span class="text-amber-700">Not linked</span>{% endif %}</dd></div>
        </dl>
      </div>

      <div class="rounded-2xl border border-slate-200 bg-white p-6 shadow-soft">
        <h3 class="font-semibold text-slate-900">Statutory & Bank Details</h3>
        <dl class="mt-4 grid gap-4 md:grid-cols-2 text-sm">
          <div><dt class="text-slate-500">PAN</dt><dd class="font-medium text-slate-900">{{ employee.pan or '-' }}</dd></div>
          <div><dt class="text-slate-500">UAN</dt><dd class="font-medium text-slate-900">{{ employee.uan or '-' }}</dd></div>
          <div><dt class="text-slate-500">ESI No</dt><dd class="font-medium text-slate-900">{{ employee.esi_no or '-' }}</dd></div>
          <div><dt class="text-slate-500">PF No</dt><dd class="font-medium text-slate-900">{{ employee.pf_no or '-' }}</dd></div>
          <div><dt class="text-slate-500">Bank Name</dt><dd class="font-medium text-slate-900">{{ employee.bank_name or '-' }}</dd></div>
          <div><dt class="text-slate-500">Bank Account</dt><dd class="font-medium text-slate-900">{{ employee.bank_account_no or '-' }}</dd></div>
          <div><dt class="text-slate-500">IFSC</dt><dd class="font-medium text-slate-900">{{ employee.bank_ifsc or '-' }}</dd></div>
        </dl>
      </div>
    </div>

    <div class="space-y-6">
      <div class="rounded-2xl border border-slate-200 bg-white p-6 shadow-soft">
        <h3 class="font-semibold text-slate-900">Status</h3>
        <div class="mt-3"><span class="rounded-full px-3 py-1 text-sm font-semibold {% if employee.status == 'active' %}bg-emerald-50 text-emerald-700{% elif employee.status == 'relieved' %}bg-amber-50 text-amber-700{% else %}bg-slate-100 text-slate-600{% endif %}">{{ employee.status.replace('_',' ').title() }}</span></div>
        {% if can_change_employee_status(current_user, current_user_permissions, current_user_roles) %}
        <form method="post" action="/employees/{{ employee.id }}/status" class="mt-5 space-y-3">
          <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
          <div><label class="mb-1 block text-sm font-medium text-slate-700">Change Status</label><select name="status" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">{% for st in employee_statuses %}<option value="{{ st }}" {% if employee.status == st %}selected{% endif %}>{{ st.title() }}</option>{% endfor %}</select></div>
          <div><label class="mb-1 block text-sm font-medium text-slate-700">Date of Leaving</label><input type="date" name="date_of_leaving" value="{{ employee.date_of_leaving or '' }}" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm"></div>
          <button class="w-full rounded-xl border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Update Status</button>
        </form>
        {% endif %}
      </div>

      <div class="rounded-2xl border border-slate-200 bg-white p-6 shadow-soft">
        <h3 class="font-semibold text-slate-900">Emergency / Notes</h3>
        <dl class="mt-4 space-y-3 text-sm"><div><dt class="text-slate-500">Emergency Contact</dt><dd class="font-medium text-slate-900">{{ employee.emergency_contact_name or '-' }} {{ employee.emergency_contact_mobile or '' }}</dd></div><div><dt class="text-slate-500">Address</dt><dd class="whitespace-pre-line font-medium text-slate-900">{{ employee.address or '-' }}</dd></div><div><dt class="text-slate-500">Notes</dt><dd class="whitespace-pre-line font-medium text-slate-900">{{ employee.notes or '-' }}</dd></div></dl>
      </div>
    </div>
  </div>
</div>
{% endblock %}
