@extends('layouts.app')
@section('title','Leave Management')
@section('page-title',auth()->user()->isEmployee() ? 'My Leaves' : 'Leave Management')

@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 Leave Requests' : 'Leave Management' }}</h1>
            <nav aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item active">All Records</li></ol></nav>
        </div>
        <a href="{{ route('leaves.create') }}" class="btn btn-primary">
            <i class="bi bi-calendar-plus me-1"></i>{{ auth()->user()->isEmployee() ? 'Request Leave' : 'New Request' }}
        </a>
    </div>
</div>

{{-- Summary cards --}}
<div class="row g-3 mb-4">
    <div class="col-6 col-md-3">
        <div class="card p-3 text-center" style="border-left:4px solid #f59e0b;">
            <div class="fw-black" style="font-size:1.8rem;color:#f59e0b;">{{ $counts['pending'] ?? 0 }}</div>
            <div style="font-size:.72rem;color:var(--text-2);text-transform:uppercase;letter-spacing:.04em;">Pending</div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="card p-3 text-center" style="border-left:4px solid var(--ul-primary);">
            <div class="fw-black" style="font-size:1.8rem;color:var(--ul-primary);">{{ ($counts['dept_approved'] ?? 0) + ($counts['hr_approved'] ?? 0) }}</div>
            <div style="font-size:.72rem;color:var(--text-2);text-transform:uppercase;letter-spacing:.04em;">In Review</div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="card p-3 text-center" style="border-left:4px solid #059669;">
            <div class="fw-black" style="font-size:1.8rem;color:#059669;">{{ $counts['approved'] ?? 0 }}</div>
            <div style="font-size:.72rem;color:var(--text-2);text-transform:uppercase;letter-spacing:.04em;">Approved</div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="card p-3 text-center" style="border-left:4px solid #dc2626;">
            <div class="fw-black" style="font-size:1.8rem;color:#dc2626;">{{ $counts['rejected'] ?? 0 }}</div>
            <div style="font-size:.72rem;color:var(--text-2);text-transform:uppercase;letter-spacing:.04em;">Rejected</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">
            @if(!auth()->user()->isEmployee())
            <div class="col-12 col-sm-4 col-md-3">
                <input type="search" name="search" class="form-control" placeholder="Search name or leave #…" value="{{ request('search') }}">
            </div>
            @endif
            <div class="col-6 col-sm-4 col-md-2">
                <select name="status" class="form-select">
                    <option value="">All Status</option>
                    <option value="pending" {{ request('status')=='pending'?'selected':'' }}>Pending</option>
                    <option value="dept_approved" {{ request('status')=='dept_approved'?'selected':'' }}>Dept Approved</option>
                    <option value="hr_approved" {{ request('status')=='hr_approved'?'selected':'' }}>HR Approved</option>
                    <option value="approved" {{ request('status')=='approved'?'selected':'' }}>Approved</option>
                    <option value="rejected" {{ request('status')=='rejected'?'selected':'' }}>Rejected</option>
                </select>
            </div>
            <div class="col-6 col-md-2">
                <select name="leave_type_id" class="form-select">
                    <option value="">All Types</option>
                    @foreach($leaveTypes as $lt)
                    <option value="{{ $lt->id }}" {{ request('leave_type_id')==$lt->id?'selected':'' }}>{{ $lt->name }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-6 col-md-2">
                <input type="month" name="month" class="form-control" value="{{ request('month') }}">
            </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('leaves.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>{{ $leaves->total() }} Records</span>
        <div class="d-flex gap-2">
            @if(!auth()->user()->isEmployee())
            <button class="btn btn-sm btn-outline-success"><i class="bi bi-file-earmark-excel me-1"></i>Export</button>
            @endif
        </div>
    </div>
    <div class="table-responsive">
        <table class="table mb-0">
            <thead>
                <tr>
                    <th>Leave #</th>
                    @if(!auth()->user()->isEmployee())<th>Employee</th>@endif
                    <th>Type</th>
                    <th class="d-none d-md-table-cell">From</th>
                    <th class="d-none d-md-table-cell">To</th>
                    <th class="d-none d-sm-table-cell">Days</th>
                    <th>Status</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
            @forelse($leaves as $leave)
            <tr>
                <td class="fw-semibold" style="color:var(--ul-primary);">{{ $leave->leave_number }}</td>
                @if(!auth()->user()->isEmployee())
                <td>
                    <div class="d-flex align-items-center gap-2">
                        <div class="avatar avatar-sm" style="background:var(--nav-active-bg);color:#fff;flex-shrink:0;">
                            {{ strtoupper(substr($leave->employee->first_name??'?',0,1)) }}
                        </div>
                        <div class="d-none d-sm-block">
                            <div class="fw-semibold" style="font-size:.83rem;">{{ $leave->employee->full_name ?? '—' }}</div>
                            <div style="font-size:.71rem;color:var(--text-2);">{{ $leave->employee->department->name ?? '' }}</div>
                        </div>
                    </div>
                </td>
                @endif
                <td>
                    <span class="badge" style="background:{{ $leave->leaveType->color ?? '#6c757d' }};color:#fff;">
                        {{ $leave->leaveType->name }}
                    </span>
                </td>
                <td class="d-none d-md-table-cell" style="font-size:.82rem;">{{ $leave->date_from->format('M d, Y') }}</td>
                <td class="d-none d-md-table-cell" style="font-size:.82rem;">{{ $leave->date_to->format('M d, Y') }}</td>
                <td class="d-none d-sm-table-cell fw-semibold">{{ $leave->total_days }}</td>
                <td>
                    <span class="badge bg-{{ $leave->status_badge }}">{{ $leave->status_label }}</span>
                </td>
                <td class="text-end">
                    <div class="d-flex gap-1 justify-content-end">
                        {{-- Role-specific approval buttons --}}
                        @if(auth()->user()->isPresident() && $leave->status === 'hr_approved')
                        <form method="POST" action="{{ route('leaves.approve_president', $leave) }}">
                            @csrf<button class="btn btn-icon btn-sm btn-success" title="Final Approve"><i class="bi bi-check-lg"></i></button>
                        </form>
                        @elseif(auth()->user()->isHrOrAdmin() && $leave->status === 'dept_approved')
                        <form method="POST" action="{{ route('leaves.approve_hr', $leave) }}">
                            @csrf<button class="btn btn-icon btn-sm btn-success" title="HR Approve"><i class="bi bi-check-lg"></i></button>
                        </form>
                        @elseif(auth()->user()->isDeptHead() && $leave->status === 'pending')
                        <form method="POST" action="{{ route('leaves.approve_dept', $leave) }}">
                            @csrf<button class="btn btn-icon btn-sm btn-success" title="Dept Approve"><i class="bi bi-check-lg"></i></button>
                        </form>
                        @elseif(auth()->user()->isEmployee() && in_array($leave->status,['pending','dept_approved']))
                        <form method="POST" action="{{ route('leaves.cancel', $leave) }}" onsubmit="return confirm('Cancel this request?')">
                            @csrf<button class="btn btn-icon btn-sm btn-outline-danger" title="Cancel"><i class="bi bi-x-lg"></i></button>
                        </form>
                        @endif
                        <a href="{{ route('leaves.show', $leave) }}" class="btn btn-icon btn-sm btn-outline-secondary" title="View"><i class="bi bi-eye"></i></a>
                    </div>
                </td>
            </tr>
            @empty
            <tr><td colspan="9" class="text-center py-4" style="color:var(--text-2);">No leave records found</td></tr>
            @endforelse
            </tbody>
        </table>
    </div>
    @if($leaves->hasPages())
    <div class="card-body pt-0">{{ $leaves->links('pagination::bootstrap-5') }}</div>
    @endif
</div>
@endsection
