@extends('layouts.app')
@section('title','Employees')
@section('page-title','Employee Management')

@section('content')
@if(!auth()->user()->canAccess('employees'))
<div class="alert alert-danger"><i class="bi bi-lock-fill me-2"></i>You do not have permission to view employee records.</div>
@else
<div class="page-header">
    <div class="d-flex align-items-start justify-content-between flex-wrap gap-2">
        <div>
            <h1>Employees</h1>
            <nav aria-label="breadcrumb"><ol class="breadcrumb">
                <li class="breadcrumb-item active">
                    {{ auth()->user()->isDeptHead() ? 'Your department staff' : 'All employees' }}
                </li>
            </ol></nav>
        </div>
        @if(auth()->user()->isHrOrAdmin())
        <a href="{{ route('employees.create') }}" class="btn btn-primary">
            <i class="bi bi-person-plus me-1"></i>Add Employee
        </a>
        @endif
    </div>
</div>

<div class="card mb-3">
    <div class="card-body py-3">
        <form method="GET" class="row g-2 align-items-end">
            <div class="col-12 col-sm-6 col-md-3">
                <input type="search" name="search" class="form-control" placeholder="Search name, ID, position…" value="{{ request('search') }}">
            </div>
            @if(!auth()->user()->isDeptHead())
            <div class="col-12 col-sm-6 col-md-3">
                <select name="department_id" class="form-select">
                    <option value="">All Departments</option>
                    @foreach($departments as $d)
                    <option value="{{ $d->id }}" {{ request('department_id')==$d->id?'selected':'' }}>{{ $d->name }}</option>
                    @endforeach
                </select>
            </div>
            @endif
            <div class="col-6 col-md-2">
                <select name="status" class="form-select">
                    <option value="">All Status</option>
                    <option value="active" {{ request('status')=='active'?'selected':'' }}>Active</option>
                    <option value="on_leave" {{ request('status')=='on_leave'?'selected':'' }}>On Leave</option>
                    <option value="suspended" {{ request('status')=='suspended'?'selected':'' }}>Suspended</option>
                    <option value="terminated" {{ request('status')=='terminated'?'selected':'' }}>Terminated</option>
                </select>
            </div>
            <div class="col-6 col-md-2 d-flex gap-2">
                <button type="submit" class="btn btn-primary flex-fill"><i class="bi bi-search me-1"></i>Filter</button>
                <a href="{{ route('employees.index') }}" class="btn btn-outline-secondary"><i class="bi bi-x-lg"></i></a>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <span>{{ $employees->total() }} Employees
            @if(auth()->user()->isDeptHead())
            <span class="badge bg-secondary ms-1">Your Dept.</span>
            @endif
        </span>
        @if(auth()->user()->isHrOrAdmin())
        <button class="btn btn-sm btn-outline-success"><i class="bi bi-file-earmark-excel me-1"></i>Export</button>
        @endif
    </div>
    <div class="table-responsive">
        <table class="table mb-0">
            <thead>
                <tr>
                    <th>Employee</th>
                    <th class="d-none d-md-table-cell">ID No.</th>
                    @if(!auth()->user()->isDeptHead())
                    <th class="d-none d-lg-table-cell">Department</th>
                    @endif
                    <th class="d-none d-md-table-cell">Position</th>
                    <th>Status</th>
                    <th class="d-none d-xl-table-cell">Hired</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
            @forelse($employees as $emp)
            @php
                $hue = abs(crc32($emp->first_name)) % 360;
            @endphp
            <tr>
                <td>
                    <div class="d-flex align-items-center gap-2">
                        <div class="avatar avatar-sm" style="background:hsl({{ $hue }},55%,40%);color:#fff;flex-shrink:0;">
                            {{ strtoupper(substr($emp->first_name,0,1).substr($emp->last_name,0,1)) }}
                        </div>
                        <div>
                            <div class="fw-semibold" style="font-size:.85rem;">{{ $emp->full_name }}</div>
                            <div style="font-size:.71rem;color:var(--text-2);">{{ $emp->user->email ?? '' }}</div>
                        </div>
                    </div>
                </td>
                <td class="d-none d-md-table-cell fw-semibold" style="color:var(--ul-primary);font-size:.82rem;">{{ $emp->employee_number }}</td>
                @if(!auth()->user()->isDeptHead())
                <td class="d-none d-lg-table-cell" style="font-size:.83rem;">{{ $emp->department->name ?? '—' }}</td>
                @endif
                <td class="d-none d-md-table-cell" style="font-size:.83rem;max-width:160px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{ $emp->position }}</td>
                <td>
                    <span class="badge bg-{{ $emp->status_badge ?? 'secondary' }}">
                        {{ ucfirst(str_replace('_',' ',$emp->status)) }}
                    </span>
                </td>
                <td class="d-none d-xl-table-cell" style="font-size:.78rem;color:var(--text-2);">{{ $emp->date_of_employment?->format('M Y') }}</td>
                <td class="text-end">
                    <div class="d-flex gap-1 justify-content-end">
                        <a href="{{ route('employees.show',$emp) }}" class="btn btn-sm btn-icon btn-outline-primary" title="View"><i class="bi bi-eye"></i></a>
                        @if(auth()->user()->isHrOrAdmin())
                        <a href="{{ route('employees.edit',$emp) }}" class="btn btn-sm btn-icon btn-outline-secondary" title="Edit"><i class="bi bi-pencil"></i></a>
                        <form method="POST" action="{{ route('employees.destroy',$emp) }}" onsubmit="return confirm('Deactivate this employee?')">
                            @csrf @method('DELETE')
                            <button class="btn btn-sm btn-icon btn-outline-danger" title="Deactivate"><i class="bi bi-person-x"></i></button>
                        </form>
                        @endif
                    </div>
                </td>
            </tr>
            @empty
            <tr><td colspan="7" class="text-center py-4" style="color:var(--text-2);">No employees found</td></tr>
            @endforelse
            </tbody>
        </table>
    </div>
    @if($employees->hasPages())
    <div class="card-body pt-0">{{ $employees->links('pagination::bootstrap-5') }}</div>
    @endif
</div>
@endif
@endsection
