@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">
                        <a href="{{ route('attendance.index') }}" class="btn btn-default btn-sm">
                            <i class="fas fa-arrow-right"></i> العودة لقائمة الحضور
                        </a>
                    </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

                    <form action="{{ route('attendance.report') }}" method="GET" class="mb-4">
                        <div class="row">
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label for="start_date">من تاريخ</label>
                                    <input type="date" name="start_date" id="start_date" class="form-control" value="{{ $startDate }}">
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label for="end_date">إلى تاريخ</label>
                                    <input type="date" name="end_date" id="end_date" class="form-control" value="{{ $endDate }}">
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="employee_id">الموظف</label>
                                    <select name="employee_id" id="employee_id" class="form-control">
                                        <option value="">جميع الموظفين</option>
                                        @foreach($employees as $employee)
                                        <option value="{{ $employee->id }}" {{ $employeeId == $employee->id ? 'selected' : '' }}>
                                            {{ $employee->user->name }} ({{ $employee->employee_number }})
                                        </option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-2">
                                <div class="form-group">
                                    <label class="d-block">&nbsp;</label>
                                    <button type="submit" class="btn btn-primary">عرض التقرير</button>
                                </div>
                            </div>
                        </div>
                    </form>

                    <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>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($reportData as $employeeData)
                                <tr>
                                    <td>{{ $employeeData['employee_name'] }}</td>
                                    <td>{{ $employeeData['present_days'] }}</td>
                                    <td>{{ $employeeData['absent_days'] }}</td>
                                    <td>{{ $employeeData['late_days'] }}</td>
                                    <td>{{ $employeeData['leave_days'] }}</td>
                                    <td>{{ $employeeData['total_late_minutes'] }} دقيقة</td>
                                    <td>{{ $employeeData['total_overtime_minutes'] }} دقيقة</td>
                                    <td>
                                        <button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#employee-details-{{ $employeeData['employee_id'] }}">
                                            <i class="fas fa-eye"></i> عرض التفاصيل
                                        </button>
                                    </td>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="8" class="text-center">لا توجد بيانات للعرض</td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- نوافذ تفاصيل الموظفين -->
@foreach($reportData as $employeeData)
<div class="modal fade" id="employee-details-{{ $employeeData['employee_id'] }}" tabinde="-1" role="dialog" aria-labelledby="employee-details-label-{{ $employeeData['employee_id'] }}" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="employee-details-label-{{ $employeeData['employee_id'] }}">تفاصيل حضور: {{ $employeeData['employee_name'] }}</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <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>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($employeeData['attendance_records'] as $record)
                            <tr>
                                <td>{{ $record->date }}</td>
                                <td>{{ $record->check_in ? \Carbon\Carbon::parse($record->check_in)->format('H:i') : '-' }}</td>
                                <td>{{ $record->check_out ? \Carbon\Carbon::parse($record->check_out)->format('H:i') : '-' }}</td>
                                <td>
                                    @if($record->status == 'present')
                                        <span class="badge badge-success">حاضر</span>
                                    @elseif($record->status == 'absent')
                                        <span class="badge badge-danger">غائب</span>
                                    @elseif($record->status == 'late')
                                        <span class="badge badge-warning">متأخر</span>
                                    @elseif($record->status == 'early_leave')
                                        <span class="badge badge-info">مغادرة مبكرة</span>
                                    @elseif($record->status == 'leave')
                                        <span class="badge badge-secondary">إجازة</span>
                                    @endif
                                </td>
                                <td>{{ $record->late_minutes ?? '0' }}</td>
                                <td>{{ $record->overtime_minutes ?? '0' }}</td>
                                <td>{{ $record->notes ?? '-' }}</td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">إغلاق</button>
            </div>
        </div>
    </div>
</div>
@endforeach
@endsection
