@extends('layouts.app')

@section('title', 'سجلات الحضور والانصراف')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">سجلات الحضور والانصراف</h3>
                    <div class="card-tools">
                        <form action="{{ route('attendance.index') }}" method="GET" class="form-inline">
                            <div class="input-group input-group-sm">
                                <input type="date" name="date" class="form-control" value="{{ $date }}">
                                <div class="input-group-append">
                                    <button type="submit" class="btn btn-default">
                                        <i class="fas fa-search"></i>
                                    </button>
                                </div>
                            </div>
                        </form>
                        @can('hr.attendance.create')
                        <a href="{{ route('attendance.create') }}" class="btn btn-primary btn-sm mt-2">
                            <i class="fas fa-plus"></i> تسجيل حضور جديد
                        </a>
                        @endcan
                        @can('hr.attendance.bulk-create')
                        <a href="{{ route('attendance.bulk-create') }}" class="btn btn-success btn-sm mt-2">
                            <i class="fas fa-list"></i> تسجيل حضور جماعي
                        </a>
                        @endcan
                    </div>
                </div>
                <div class="card-body">
                    @if(session('success'))
                    <div class="alert alert-success">
                        {{ session('success') }}
                    </div>
                    @endif

                    @if(session('error'))
                    <div class="alert alert-danger">
                        {{ session('error') }}
                    </div>
                    @endif

                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>الموظف</th>
                                    <th>التاريخ</th>
                                    <th>وقت الحضور</th>
                                    <th>وقت الانصراف</th>
                                    <th>ساعات العمل</th>
                                    <th>الحالة</th>
                                    <th>ملاحظات</th>
                                    <th>الإجراءات</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($attendances as $attendance)
                                <tr>
                                    <td>{{ $loop->iteration }}</td>
                                    <td>{{ $attendance->employee->user->name }}</td>
                                    <td>{{ $attendance->date }}</td>
                                    <td>{{ $attendance->check_in }}</td>
                                    <td>{{ $attendance->check_out ?? '-' }}</td>
                                    <td>
                                        @if($attendance->check_in && $attendance->check_out)
                                            @php
                                                $checkIn = \Carbon\Carbon::parse($attendance->check_in);
                                                $checkOut = \Carbon\Carbon::parse($attendance->check_out);
                                                $hours = $checkOut->diffInHours($checkIn);
                                                $minutes = $checkOut->diffInMinutes($checkIn) % 60;
                                                echo $hours . ' ساعة و ' . $minutes . ' دقيقة';
                                            @endphp
                                        @else
                                            -
                                        @endif
                                    </td>
                                    <td>
                                        @if($attendance->status == 'present')
                                            <span class="badge badge-success">حاضر</span>
                                        @elseif($attendance->status == 'absent')
                                            <span class="badge badge-danger">غائب</span>
                                        @elseif($attendance->status == 'late')
                                            <span class="badge badge-warning">متأخر</span>
                                        @elseif($attendance->status == 'early_leave')
                                            <span class="badge badge-info">مغادرة مبكرة</span>
                                        @endif
                                    </td>
                                    <td>{{ $attendance->notes ?? '-' }}</td>
                                    <td>
                                        @can('hr.attendance.edit')
                                        <a href="{{ route('attendance.edit', $attendance) }}" class="btn btn-primary btn-sm">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        @endcan

                                        @can('hr.attendance.delete')
                                        <form action="{{ route('attendance.destroy', $attendance) }}" method="POST" class="d-inline">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('هل أنت متأكد من حذف هذا السجل؟')">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        </form>
                                        @endcan
                                    </td>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="9" class="text-center">لا توجد سجلات حضور لهذا اليوم</td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>

                    <div class="mt-3">
                        {{ $attendances->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
