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

@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 Attendance' : 'Attendance Records' }}</h1>
            <nav aria-label="breadcrumb"><ol class="breadcrumb">
                <li class="breadcrumb-item active">{{ \Carbon\Carbon::parse($month.'-01')->format('F Y') }}</li>
            </ol></nav>
        </div>
        <div class="d-flex gap-2 flex-wrap">
            @if(!auth()->user()->isEmployee())
            <a href="{{ route('attendance.summary') }}" class="btn btn-sm btn-outline-primary">
                <i class="bi bi-table me-1"></i>Monthly Summary
            </a>
            @endif
            <form method="POST" action="{{ route('attendance.clock') }}">
                @csrf
                <button type="submit" class="btn btn-primary btn-sm">
                    <i class="bi bi-fingerprint me-1"></i>{{ auth()->user()->isEmployee() ? 'Clock In / Out' : 'Record Attendance' }}
                </button>
            </form>
        </div>
    </div>
</div>

{{-- Employee quick stats --}}
@if(auth()->user()->isEmployee() && isset($my_summary))
<div class="row g-3 mb-4">
    <div class="col-6 col-md-3">
        <div class="stat-card sc-success"><div class="stat-icon"><i class="bi bi-person-check"></i></div>
            <div class="stat-value">{{ $my_summary['present'] ?? 0 }}</div><div class="stat-label">Present</div></div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card sc-danger"><div class="stat-icon"><i class="bi bi-person-x"></i></div>
            <div class="stat-value">{{ $my_summary['absent'] ?? 0 }}</div><div class="stat-label">Absent</div></div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card sc-warning"><div class="stat-icon"><i class="bi bi-clock-history"></i></div>
            <div class="stat-value">{{ $my_summary['late'] ?? 0 }}</div><div class="stat-label">Late</div></div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card sc-info"><div class="stat-icon"><i class="bi bi-calendar-check"></i></div>
            <div class="stat-value">{{ $my_summary['on_leave'] ?? 0 }}</div><div class="stat-label">On Leave</div></div>
    </div>
</div>
@endif

{{-- 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-2">
                <label class="form-label" style="font-size:.74rem;">Month</label>
                <input type="month" name="month" class="form-control" value="{{ $month }}">
            </div>
            @if($employees && !auth()->user()->isEmployee())
            <div class="col-12 col-sm-5 col-md-3">
                <label class="form-label" style="font-size:.74rem;">Employee</label>
                <select name="employee_id" class="form-select">
                    <option value="">All Employees</option>
                    @foreach($employees as $emp)
                    <option value="{{ $emp->id }}" {{ request('employee_id')==$emp->id?'selected':'' }}>{{ $emp->full_name }}</option>
                    @endforeach
                </select>
            </div>
            @endif
            <div class="col-6 col-md-2">
                <label class="form-label" style="font-size:.74rem;">Status</label>
                <select name="status" class="form-select">
                    <option value="">All Status</option>
                    <option value="present" {{ request('status')=='present'?'selected':'' }}>Present</option>
                    <option value="absent" {{ request('status')=='absent'?'selected':'' }}>Absent</option>
                    <option value="late" {{ request('status')=='late'?'selected':'' }}>Late</option>
                    <option value="on_leave" {{ request('status')=='on_leave'?'selected':'' }}>On Leave</option>
                </select>
            </div>
            <div class="col-6 col-md-2 d-flex gap-2" style="align-self:flex-end;">
                <button type="submit" class="btn btn-primary flex-fill"><i class="bi bi-search me-1"></i>Filter</button>
                <a href="{{ route('attendance.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>{{ $records->total() }} Records
            @if(auth()->user()->isEmployee())<span class="badge bg-secondary ms-1">My Records Only</span>@endif
        </span>
        @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 class="table-responsive">
        <table class="table mb-0">
            <thead>
                <tr>
                    <th>Date</th>
                    @if(!auth()->user()->isEmployee())<th>Employee</th>@endif
                    <th>Time In</th>
                    <th>Time Out</th>
                    <th class="d-none d-md-table-cell">Hours</th>
                    <th>Status</th>
                    <th class="d-none d-lg-table-cell">Notes</th>
                </tr>
            </thead>
            <tbody>
            @forelse($records as $rec)
            @php
                $stBg = match($rec->status) { 'present'=>'bg-success','absent'=>'bg-danger','late'=>'bg-warning','on_leave'=>'bg-info', default=>'bg-secondary' };
            @endphp
            <tr>
                <td class="fw-semibold" style="font-size:.83rem;">
                    {{ \Carbon\Carbon::parse($rec->date)->format('D, M j') }}
                </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($rec->employee->first_name??'?',0,1)) }}
                        </div>
                        <div class="d-none d-sm-block fw-semibold" style="font-size:.82rem;">{{ $rec->employee->full_name ?? '—' }}</div>
                    </div>
                </td>
                @endif
                <td style="font-size:.82rem;color:{{ $rec->status==='absent'?'var(--text-3)':'var(--text-1)' }};">
                    {{ $rec->time_in ? \Carbon\Carbon::parse($rec->time_in)->format('h:i A') : '—' }}
                </td>
                <td style="font-size:.82rem;color:{{ $rec->status==='absent'?'var(--text-3)':'var(--text-1)' }};">
                    {{ $rec->time_out ? \Carbon\Carbon::parse($rec->time_out)->format('h:i A') : '—' }}
                </td>
                <td class="d-none d-md-table-cell fw-semibold" style="font-size:.82rem;">
                    {{ $rec->hours_worked ? number_format($rec->hours_worked,1).'h' : '—' }}
                </td>
                <td><span class="badge {{ $stBg }}">{{ ucfirst(str_replace('_',' ',$rec->status)) }}</span></td>
                <td class="d-none d-lg-table-cell" style="font-size:.75rem;color:var(--text-2);max-width:180px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">
                    {{ $rec->notes ?? '' }}
                </td>
            </tr>
            @empty
            <tr><td colspan="7" class="text-center py-4" style="color:var(--text-2);">No attendance records for this period</td></tr>
            @endforelse
            </tbody>
        </table>
    </div>
    @if($records->hasPages())
    <div class="card-body pt-0">{{ $records->links('pagination::bootstrap-5') }}</div>
    @endif
</div>
@endsection
