{% extends "ui/templates/base/layout.html" %}
{% block content %}
<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">Employees</h2>
      <p class="text-sm text-slate-500">Employee master, user linkage, department, designation, reporting manager and status.</p>
    </div>
    {% if can_import_employee_hr(current_user, current_user_permissions, current_user_roles) %}
    <a href="/employees/imports" class="inline-flex rounded-xl border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">HR Imports</a>
    {% endif %}
    {% if can_manage_employees(current_user, current_user_permissions, current_user_roles) %}
    <a href="/employees/new" class="inline-flex rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white shadow-soft hover:bg-brand-700">Add Employee</a>
    {% endif %}
  </div>

  <form method="get" class="rounded-2xl border border-slate-200 bg-white p-4 shadow-soft">
    <div class="grid gap-3 md:grid-cols-[1fr_auto_auto_auto]">
      <input name="q" value="{{ q or '' }}" placeholder="Search by code, name, email, mobile, PAN, department or designation" class="rounded-xl border border-slate-300 px-3 py-2 text-sm">
      <select name="link_status" class="rounded-xl border border-slate-300 px-3 py-2 text-sm text-slate-700">
        <option value="all" {% if link_status == 'all' %}selected{% endif %}>All login link status</option>
        <option value="linked" {% if link_status == 'linked' %}selected{% endif %}>Linked to login user</option>
        <option value="unlinked" {% if link_status == 'unlinked' %}selected{% endif %}>Not linked to login user</option>
      </select>
      <label class="inline-flex items-center gap-2 rounded-xl border border-slate-300 px-3 py-2 text-sm text-slate-700">
        <input type="checkbox" name="include_inactive" value="1" {% if include_inactive %}checked{% endif %}> Include inactive
      </label>
      <button class="rounded-xl border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Filter</button>
    </div>
  </form>

  <div class="grid gap-4 md:grid-cols-5">
    <div class="rounded-2xl border border-slate-200 bg-white p-4 shadow-soft"><div class="text-xs font-semibold uppercase text-slate-500">Visible Employees</div><div class="mt-1 text-2xl font-semibold">{{ rows|length }}</div></div>
    <div class="rounded-2xl border border-emerald-200 bg-emerald-50 p-4 shadow-soft"><div class="text-xs font-semibold uppercase text-emerald-700">Linked Users</div><div class="mt-1 text-2xl font-semibold text-emerald-800">{{ link_summary.linked if link_summary else 0 }}</div></div>
    <div class="rounded-2xl border border-amber-200 bg-amber-50 p-4 shadow-soft"><div class="text-xs font-semibold uppercase text-amber-700">Unlinked Employees</div><div class="mt-1 text-2xl font-semibold text-amber-800">{{ link_summary.unlinked if link_summary else 0 }}</div></div>
    <div class="rounded-2xl border border-slate-200 bg-white p-4 shadow-soft"><div class="text-xs font-semibold uppercase text-slate-500">Active Scope Branch</div><div class="mt-1 text-lg font-semibold">{{ scope.branch_id if scope.branch_id else 'All' }}</div></div>
    <div class="rounded-2xl border border-slate-200 bg-white p-4 shadow-soft"><div class="text-xs font-semibold uppercase text-slate-500">Access</div><div class="mt-1 text-sm font-semibold">{{ 'Cross Audit Firm' if scope.allow_cross_tenant else 'Audit Firm Scoped' }} / {{ 'Cross Branch' if scope.allow_cross_branch else 'Branch Scoped' }}</div></div>
  </div>

  {% if link_summary and link_summary.unlinked %}
  <div class="rounded-2xl border border-amber-200 bg-amber-50 p-4 text-sm text-amber-900">
    <div class="font-semibold">{{ link_summary.unlinked }} employee(s) are not linked to login users.</div>
    <p class="mt-1">Employee self-service pages such as My Workspace, attendance, leave, documents and payslips work fully only after the employee master is linked to an IAM user.</p>
    <a href="/employees?link_status=unlinked{% if include_inactive %}&include_inactive=1{% endif %}" class="mt-2 inline-flex rounded-lg border border-amber-300 px-3 py-1.5 text-xs font-semibold text-amber-900 hover:bg-amber-100">Show unlinked employees</a>
  </div>
  {% endif %}

  <div class="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-soft">
    <table class="min-w-full divide-y divide-slate-200 text-sm">
      <thead class="bg-slate-50 text-left text-xs font-semibold uppercase tracking-wide text-slate-500">
        <tr>
          <th class="px-4 py-3">Employee</th>
          <th class="px-4 py-3">Department</th>
          <th class="px-4 py-3">Contact</th>
          <th class="px-4 py-3">Joining</th>
          <th class="px-4 py-3">Status</th>
          <th class="px-4 py-3 text-right">Action</th>
        </tr>
      </thead>
      <tbody class="divide-y divide-slate-100">
        {% for row in rows %}
        <tr class="hover:bg-slate-50">
          <td class="px-4 py-3">
            <div class="font-semibold text-slate-900">{{ row.full_name }}</div>
            <div class="text-xs text-slate-500">{{ row.employee_code }}{% if row.user_id %} • User #{{ row.user_id }}{% endif %}</div>
            {% if row.user_id %}
              <span class="mt-1 inline-flex rounded-full bg-emerald-50 px-2 py-0.5 text-[11px] font-semibold text-emerald-700">Login linked</span>
            {% else %}
              <span class="mt-1 inline-flex rounded-full bg-amber-50 px-2 py-0.5 text-[11px] font-semibold text-amber-700">Login not linked</span>
            {% endif %}
          </td>
          <td class="px-4 py-3 text-slate-600"><div>{{ row.department or '-' }}</div><div class="text-xs">{{ row.designation or '-' }}</div></td>
          <td class="px-4 py-3 text-slate-600"><div>{{ row.email or '-' }}</div><div class="text-xs">{{ row.mobile or '-' }}</div></td>
          <td class="px-4 py-3 text-slate-600">{{ row.date_of_joining or '-' }}</td>
          <td class="px-4 py-3"><span class="rounded-full px-2 py-1 text-xs font-semibold {% if row.status == 'active' %}bg-emerald-50 text-emerald-700{% elif row.status == 'relieved' %}bg-amber-50 text-amber-700{% else %}bg-slate-100 text-slate-600{% endif %}">{{ row.status.replace('_',' ').title() }}</span></td>
          <td class="px-4 py-3 text-right"><a href="/employees/{{ row.id }}" class="rounded-lg border border-slate-300 px-3 py-1.5 text-xs font-semibold text-slate-700 hover:bg-slate-50">Open</a></td>
        </tr>
        {% else %}
        <tr><td colspan="6" class="px-4 py-8 text-center text-slate-500">No employees found.</td></tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
</div>
{% endblock %}
