@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.store') }}" method="POST">
                        @csrf
                        
                        <div class="form-group">
                            <label for="employee_id">الموظف</label>
                            <select name="employee_id" id="employee_id" class="form-control @error('employee_id') is-invalid @enderror" required>
                                <option value="">-- اختر الموظف --</option>
                                @foreach($employees as $employee)
                                <option value="{{ $employee->id }}" {{ old('employee_id') == $employee->id ? 'selected' : '' }}>
                                    {{ $employee->user->name }} ({{ $employee->employee_number }})
                                </option>
                                @endforeach
                            </select>
                            @error('employee_id')
                            <span class="invalid-feedback">{{ $message }}</span>
                            @enderror
                        </div>

                        <div class="form-group">
                            <label for="date">التاريخ</label>
                            <input type="date" name="date" id="date" class="form-control @error('date') is-invalid @enderror" value="{{ old('date', now()->toDateString()) }}" required>
                            @error('date')
                            <span class="invalid-feedback">{{ $message }}</span>
                            @enderror
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="check_in">وقت الحضور</label>
                                    <input type="time" name="check_in" id="check_in" class="form-control @error('check_in') is-invalid @enderror" value="{{ old('check_in') }}">
                                    @error('check_in')
                                    <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="check_out">وقت الانصراف</label>
                                    <input type="time" name="check_out" id="check_out" class="form-control @error('check_out') is-invalid @enderror" value="{{ old('check_out') }}">
                                    @error('check_out')
                                    <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="status">الحالة</label>
                            <select name="status" id="status" class="form-control @error('status') is-invalid @enderror" required>
                                <option value="present" {{ old('status') == 'present' ? 'selected' : '' }}>حاضر</option>
                                <option value="absent" {{ old('status') == 'absent' ? 'selected' : '' }}>غائب</option>
                                <option value="late" {{ old('status') == 'late' ? 'selected' : '' }}>متأخر</option>
                                <option value="early_leave" {{ old('status') == 'early_leave' ? 'selected' : '' }}>مغادرة مبكرة</option>
                            </select>
                            @error('status')
                            <span class="invalid-feedback">{{ $message }}</span>
                            @enderror
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="late_minutes">دقائق التأخير</label>
                                    <input type="number" name="late_minutes" id="late_minutes" class="form-control @error('late_minutes') is-invalid @enderror" value="{{ old('late_minutes', 0) }}" min="0">
                                    @error('late_minutes')
                                    <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="overtime_minutes">دقائق العمل الإضافي</label>
                                    <input type="number" name="overtime_minutes" id="overtime_minutes" class="form-control @error('overtime_minutes') is-invalid @enderror" value="{{ old('overtime_minutes', 0) }}" min="0">
                                    @error('overtime_minutes')
                                    <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="notes">ملاحظات</label>
                            <textarea name="notes" id="notes" class="form-control @error('notes') is-invalid @enderror" rows="3">{{ old('notes') }}</textarea>
                            @error('notes')
                            <span class="invalid-feedback">{{ $message }}</span>
                            @enderror
                        </div>

                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">حفظ</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
