@extends('layouts.app')
@section('title', auth()->user()->isEmployee() ? 'My Evaluations' : 'Performance Evaluation')
@section('page-title', auth()->user()->isEmployee() ? 'My Evaluations' : 'Performance Evaluations')

@section('content')
<div class="page-header">
    <div class="d-flex align-items-start justify-content-between flex-wrap gap-2">
        <div>
            <h1>{{ auth()->user()->isEmployee() ? 'My Performance Evaluations' : 'Performance Evaluations' }}</h1>
            <nav aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item active">
                {{ auth()->user()->isEmployee() ? 'Your results across all periods' : 'All evaluations' }}
            </li></ol></nav>
        </div>
        @if(!auth()->user()->isEmployee())
        <a href="{{ route('performance.create') }}" class="btn btn-primary">
            <i class="bi bi-plus-lg me-1"></i>New Evaluation
        </a>
        @endif
    </div>
</div>

<div class="row g-3 mb-4">
    <div class="col-6 col-md-3">
        <div class="stat-card sc-primary"><div class="stat-icon"><i class="bi bi-clipboard-check"></i></div>
            <div class="stat-value">{{ $total }}</div><div class="stat-label">Total</div></div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card sc-warning"><div class="stat-icon"><i class="bi bi-hourglass-split"></i></div>
            <div class="stat-value">{{ $pending }}</div><div class="stat-label">{{ auth()->user()->isEmployee() ? 'Pending Ack.' : 'Pending Review' }}</div></div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card sc-success"><div class="stat-icon"><i class="bi bi-check-circle"></i></div>
            <div class="stat-value">{{ $completed }}</div><div class="stat-label">Completed</div></div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card sc-gold"><div class="stat-icon"><i class="bi bi-trophy"></i></div>
            <div class="stat-value">{{ $avg_score ? round($avg_score).'%' : '—' }}</div>
            <div class="stat-label">{{ auth()->user()->isEmployee() ? 'Your Avg.' : 'Dept. Avg.' }}</div></div>
    </div>
</div>

{{-- Filters --}}
<div class="card mb-3">
    <div class="card-body py-3">
        <form method="GET" class="row g-2 align-items-end">
            <div class="col-6 col-md-3">
                <select name="period_id" class="form-select">
                    <option value="">All Periods</option>
                    @foreach($periods as $p)
                    <option value="{{ $p->id }}" {{ request('period_id')==$p->id?'selected':'' }}>{{ $p->name }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-6 col-md-2">
                <select name="status" class="form-select">
                    <option value="">All Status</option>
                    <option value="submitted">Submitted</option>
                    <option value="acknowledged">Acknowledged</option>
                    <option value="completed">Completed</option>
                </select>
            </div>
            <div class="col-12 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('performance.index') }}" class="btn btn-outline-secondary"><i class="bi bi-x-lg"></i></a>
            </div>
        </form>
    </div>
</div>

@if(auth()->user()->isEmployee())
{{-- Employee: card grid --}}
<div class="row g-3">
    @forelse($evaluations as $eval)
    @php
        $score = $eval->weighted_score ? round($eval->weighted_score) : 0;
        $rMap  = ['excellent'=>'#059669','very_good'=>'#0055b3','satisfactory'=>'#0284c7','needs_improvement'=>'#f59e0b','unsatisfactory'=>'#dc2626'];
        $rCol  = $rMap[$eval->overall_rating ?? ''] ?? '#6c757d';
    @endphp
    <div class="col-12 col-sm-6 col-xl-4">
        <div class="card h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-start mb-3">
                    <div><div class="fw-bold">{{ $eval->period->name ?? '—' }}</div>
                    <div style="font-size:.75rem;color:var(--text-2);">{{ $eval->eval_number }}</div></div>
                    <span class="badge bg-{{ $eval->status === 'completed' ? 'success' : ($eval->status === 'submitted' ? 'warning' : 'secondary') }}">{{ ucfirst($eval->status) }}</span>
                </div>
                <div class="text-center my-3">
                    <div style="width:84px;height:84px;border-radius:50%;background:conic-gradient({{ $rCol }} {{ $score*3.6 }}deg,var(--card-border) 0);display:flex;align-items:center;justify-content:center;margin:0 auto .6rem;box-shadow:0 0 0 5px var(--card-bg);">
                        <div style="width:66px;height:66px;border-radius:50%;background:var(--card-bg);display:flex;align-items:center;justify-content:center;">
                            <div style="font-weight:900;font-size:1.15rem;color:{{ $rCol }};">{{ $score }}%</div>
                        </div>
                    </div>
                    <span class="badge" style="background:{{ $rCol }};color:#fff;">{{ $eval->rating_label }}</span>
                </div>
                <div class="d-flex justify-content-between pt-2 mt-2" style="border-top:1px solid var(--card-border);">
                    <a href="{{ route('performance.show',$eval) }}" class="btn btn-sm btn-outline-primary"><i class="bi bi-eye me-1"></i>Details</a>
                    @if($eval->status === 'submitted')
                    <form method="POST" action="{{ route('performance.acknowledge',$eval) }}">@csrf
                        <button class="btn btn-sm btn-success"><i class="bi bi-check2-circle me-1"></i>Acknowledge</button>
                    </form>
                    @endif
                </div>
            </div>
        </div>
    </div>
    @empty
    <div class="col-12">
        <div class="card p-5 text-center">
            <i class="bi bi-bar-chart d-block" style="font-size:3rem;color:var(--text-3);"></i>
            <div class="mt-2 fw-semibold" style="color:var(--text-2);">No evaluations on record yet</div>
            <div style="font-size:.82rem;color:var(--text-3);">Your evaluations will appear here once submitted by your manager</div>
        </div>
    </div>
    @endforelse
</div>
@if($evaluations->hasPages())<div class="mt-3">{{ $evaluations->links('pagination::bootstrap-5') }}</div>@endif

@else
{{-- HR / Admin / Dept Head: table --}}
<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <span>{{ $evaluations->total() }} Evaluations</span>
        <button class="btn btn-sm btn-outline-success"><i class="bi bi-file-earmark-excel me-1"></i>Export</button>
    </div>
    <div class="table-responsive">
        <table class="table mb-0">
            <thead><tr>
                <th>Eval #</th><th>Employee</th>
                <th class="d-none d-md-table-cell">Dept.</th>
                <th class="d-none d-md-table-cell">Period</th>
                <th>Score</th><th>Rating</th><th>Status</th>
                <th class="text-end">Actions</th>
            </tr></thead>
            <tbody>
            @forelse($evaluations as $eval)
            <tr>
                <td class="fw-semibold" style="color:var(--ul-primary);font-size:.82rem;">{{ $eval->eval_number }}</td>
                <td>
                    <div class="d-flex align-items-center gap-2">
                        <div class="avatar avatar-sm" style="background:var(--nav-active-bg);color:#fff;">{{ strtoupper(substr($eval->employee->first_name??'?',0,1)) }}</div>
                        <div class="d-none d-sm-block fw-semibold" style="font-size:.83rem;">{{ $eval->employee->full_name ?? '—' }}</div>
                    </div>
                </td>
                <td class="d-none d-md-table-cell" style="font-size:.79rem;color:var(--text-2);">{{ $eval->employee->department->name ?? '—' }}</td>
                <td class="d-none d-md-table-cell" style="font-size:.79rem;">{{ $eval->period->name ?? '—' }}</td>
                <td><span class="fw-bold">{{ $eval->weighted_score ? round($eval->weighted_score).'%' : '—' }}</span></td>
                <td>
                    @if($eval->overall_rating)
                    <span class="badge bg-{{ $eval->rating_badge }}">{{ $eval->rating_label }}</span>
                    @else —
                    @endif
                </td>
                <td><span class="badge bg-{{ $eval->status==='completed'?'success':($eval->status==='submitted'?'warning':'secondary') }}">{{ ucfirst($eval->status) }}</span></td>
                <td class="text-end">
                    <div class="d-flex gap-1 justify-content-end">
                        <a href="{{ route('performance.show',$eval) }}" class="btn btn-sm btn-icon btn-outline-secondary"><i class="bi bi-eye"></i></a>
                        @if(auth()->user()->isHrOrAdmin() && $eval->status === 'acknowledged')
                        <form method="POST" action="{{ route('performance.hr_review',$eval) }}">@csrf
                            <button class="btn btn-sm btn-icon btn-success" title="Complete Review"><i class="bi bi-check-lg"></i></button>
                        </form>
                        @endif
                    </div>
                </td>
            </tr>
            @empty
            <tr><td colspan="8" class="text-center py-4" style="color:var(--text-2);">No evaluations found</td></tr>
            @endforelse
            </tbody>
        </table>
    </div>
    @if($evaluations->hasPages())<div class="card-body pt-0">{{ $evaluations->links('pagination::bootstrap-5') }}</div>@endif
</div>
@endif
@endsection
