@extends('layouts.app')
@section('title','HR Dashboard')
@section('page-title',auth()->user()->isDeptHead() ? 'Department Overview' : 'HR Dashboard')

@push('styles')
<style>
.dash-card { border:none;border-radius:16px;box-shadow:0 4px 20px rgba(0,0,0,.18);overflow:hidden; }
.dash-card-dark { background:linear-gradient(145deg,#0a1628,#0f2140);color:#fff; }
.dash-card-mid  { background:linear-gradient(145deg,#0d1b2e,#122040);color:#fff; }
.dch {
    background:rgba(255,255,255,.06);border-bottom:1px solid rgba(255,255,255,.08);
    padding:.9rem 1.25rem;font-weight:700;font-size:.88rem;color:rgba(255,255,255,.92);
    display:flex;align-items:center;justify-content:space-between;
}
.dcb { padding:1.1rem 1.25rem; }
.row-item {
    display:flex;align-items:center;gap:.85rem;
    padding:.65rem 1.25rem;border-bottom:1px solid rgba(255,255,255,.06);
}
.row-item:last-child { border-bottom:none; }
.row-item .ri-name { font-weight:700;font-size:.84rem;color:rgba(255,255,255,.9); }
.row-item .ri-sub  { font-size:.72rem;color:rgba(255,255,255,.45); }
/* dark table */
.dark-table thead th {
    background:rgba(255,255,255,.05)!important;border-bottom:1px solid rgba(255,255,255,.1)!important;
    color:rgba(255,255,255,.5)!important;font-size:.68rem;text-transform:uppercase;letter-spacing:.07em;
}
.dark-table tbody td {
    border-bottom:1px solid rgba(255,255,255,.06)!important;
    color:rgba(255,255,255,.82)!important;background:transparent!important;
}
.dark-table tbody tr:hover td { background:rgba(255,255,255,.04)!important; }
.dark-table tbody tr:last-child td { border-bottom:none!important; }
</style>
@endpush

@section('content')
<div class="page-header">
    <div class="d-flex align-items-start justify-content-between flex-wrap gap-2">
        <div>
            <h1>{{ auth()->user()->isDeptHead() ? 'Department Overview' : 'HR Dashboard' }}</h1>
            <nav aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item active">Overview</li></ol></nav>
        </div>
        <div style="font-size:.82rem;color:var(--text-2);" id="hrDate"></div>
    </div>
</div>

{{-- Stat cards --}}
<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-people-fill"></i></div>
            <div class="stat-value">{{ $total_employees }}</div><div class="stat-label">Active Staff</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_leaves }}</div>
            <div class="stat-label">{{ auth()->user()->isDeptHead() ? 'Pending (Your Dept)' : 'Pending HR Review' }}</div></div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card sc-danger"><div class="stat-icon"><i class="bi bi-shield-exclamation"></i></div>
            <div class="stat-value">{{ $discipline_open }}</div><div class="stat-label">Open Cases</div></div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card sc-purple"><div class="stat-icon"><i class="bi bi-bar-chart-steps"></i></div>
            <div class="stat-value">{{ $evaluations_pending }}</div><div class="stat-label">Evals Pending</div></div>
    </div>
</div>

<div class="row g-3 mb-3">
    {{-- Pending approvals --}}
    <div class="col-12 col-lg-6">
        <div class="dash-card dash-card-mid h-100">
            <div class="dch">
                <span><i class="bi bi-hourglass-split me-2" style="color:#fbbf24;"></i>
                    {{ auth()->user()->isDeptHead() ? 'Leaves Awaiting Your Approval' : 'Leaves Awaiting HR Approval' }}
                </span>
                <a href="{{ route('leaves.index') }}" style="font-size:.75rem;color:rgba(255,255,255,.5);text-decoration:none;border:1px solid rgba(255,255,255,.18);border-radius:7px;padding:.25rem .7rem;">View All</a>
            </div>
            @forelse($pending_approvals as $leave)
            <div class="row-item">
                <div class="avatar avatar-sm" style="background:linear-gradient(135deg,#1e40af,#3b82f6);color:#fff;flex-shrink:0;">
                    {{ strtoupper(substr($leave->employee->first_name,0,1)) }}
                </div>
                <div class="flex-1 overflow-hidden">
                    <div class="ri-name">{{ $leave->employee->full_name }}</div>
                    <div class="ri-sub">{{ $leave->leaveType->name }} · {{ $leave->total_days }} days</div>
                </div>
                <div class="d-flex gap-1 flex-shrink-0">
                    @if(auth()->user()->isDeptHead())
                    <form method="POST" action="{{ route('leaves.approve_dept',$leave) }}">@csrf
                        <button class="btn btn-success btn-sm btn-icon" title="Approve"><i class="bi bi-check-lg"></i></button>
                    </form>
                    @else
                    <form method="POST" action="{{ route('leaves.approve_hr',$leave) }}">@csrf
                        <button class="btn btn-success btn-sm btn-icon" title="HR Approve"><i class="bi bi-check-lg"></i></button>
                    </form>
                    @endif
                    <a href="{{ route('leaves.show',$leave) }}" class="btn btn-sm btn-icon"
                       style="background:rgba(255,255,255,.1);border:none;color:rgba(255,255,255,.7);">
                        <i class="bi bi-eye"></i>
                    </a>
                </div>
            </div>
            @empty
            <div class="text-center py-4">
                <i class="bi bi-check-circle d-block mb-1" style="font-size:1.8rem;color:#4ade80;"></i>
                <span style="font-size:.82rem;color:rgba(255,255,255,.4);">No pending approvals</span>
            </div>
            @endforelse
        </div>
    </div>

    {{-- New hires this month --}}
    <div class="col-12 col-lg-6">
        <div class="dash-card dash-card-dark h-100">
            <div class="dch">
                <span><i class="bi bi-person-plus me-2" style="color:#4ade80;"></i>New Hires — {{ now()->format('F Y') }}</span>
                @if(auth()->user()->isHrOrAdmin())
                <a href="{{ route('employees.create') }}" style="font-size:.75rem;color:rgba(255,255,255,.5);text-decoration:none;border:1px solid rgba(255,255,255,.18);border-radius:7px;padding:.25rem .7rem;">+ Add</a>
                @endif
            </div>
            @forelse($new_employees as $emp)
            <div class="row-item">
                <div class="avatar avatar-sm" style="background:linear-gradient(135deg,#065f46,#059669);color:#fff;flex-shrink:0;">
                    {{ strtoupper(substr($emp->first_name,0,1).substr($emp->last_name,0,1)) }}
                </div>
                <div class="flex-1">
                    <div class="ri-name">{{ $emp->full_name }}</div>
                    <div class="ri-sub">{{ $emp->position }} · {{ $emp->department->name ?? '' }} · {{ $emp->date_of_employment?->format('M j') }}</div>
                </div>
                <a href="{{ route('employees.show',$emp) }}" class="btn btn-sm btn-icon"
                   style="background:rgba(255,255,255,.1);border:none;color:rgba(255,255,255,.7);" title="View">
                    <i class="bi bi-eye"></i>
                </a>
            </div>
            @empty
            <div class="text-center py-4" style="font-size:.83rem;color:rgba(255,255,255,.4);">No new hires this month</div>
            @endforelse
        </div>
    </div>
</div>

{{-- Department overview table --}}
@if($dept_summary->count())
<div class="dash-card dash-card-mid">
    <div class="dch"><span><i class="bi bi-building me-2" style="color:#60a5fa;"></i>Department Overview</span></div>
    <div class="table-responsive">
        <table class="table mb-0 dark-table">
            <thead><tr>
                <th>Department</th>
                <th class="text-center">Active</th>
                <th class="text-center">On Leave</th>
                <th class="text-center d-none d-md-table-cell">Pending Leaves</th>
                <th class="text-center d-none d-lg-table-cell">Open Cases</th>
            </tr></thead>
            <tbody>
            @foreach($dept_summary as $dept)
            <tr>
                <td class="fw-semibold">{{ $dept->name }}</td>
                <td class="text-center fw-bold" style="color:#4ade80;">{{ $dept->active_count ?? 0 }}</td>
                <td class="text-center fw-bold" style="color:#60a5fa;">{{ $dept->on_leave_count ?? 0 }}</td>
                <td class="text-center fw-bold d-none d-md-table-cell" style="color:#fbbf24;">{{ $dept->pending_leaves ?? 0 }}</td>
                <td class="text-center fw-bold d-none d-lg-table-cell" style="color:#f87171;">{{ $dept->open_cases ?? 0 }}</td>
            </tr>
            @endforeach
            </tbody>
        </table>
    </div>
</div>
@endif
@endsection

@push('scripts')
<script>
document.getElementById('hrDate').textContent =
    new Date().toLocaleDateString('en-US',{weekday:'long',year:'numeric',month:'long',day:'numeric'});
</script>
@endpush
