@php
    $regularization = $regularization ?? null;
    $canRequest = (bool) ($canRequest ?? false);
    $canApprove = $regularization && auth()->user()?->canApproveRegularization($regularization);
    $isPending = $regularization && $regularization->status === \App\Models\AttendanceRegularization::STATUS_PENDING;
    $reqClockIn = $regularization?->requested_clock_in?->format('H:i') ?? $defaultClockIn ?? '';
    $reqClockOut = $regularization?->requested_clock_out?->format('H:i') ?? $defaultClockOut ?? '';
@endphp

<div class="rounded-xl border border-slate-200 bg-white p-4">
    <p class="mb-3 text-sm font-semibold text-slate-900">Request Regularization</p>

    @if($regularization)
        <div class="mb-4 rounded-lg border border-slate-200 bg-slate-50 p-3 text-sm">
            <div class="flex flex-wrap items-center justify-between gap-2">
                <span class="font-medium text-slate-800">Existing request</span>
                <span class="rounded-full px-2 py-0.5 text-xs
                    @if($regularization->status === 'approved') bg-emerald-100 text-emerald-700
                    @elseif($regularization->status === 'rejected') bg-red-100 text-red-700
                    @else bg-amber-100 text-amber-700 @endif">
                    {{ ucfirst($regularization->status) }}
                </span>
            </div>
            <p class="mt-2 text-slate-600">
                Requested:
                {{ $regularization->requested_clock_in ? $regularization->requested_clock_in->format('h:i A') : '—' }}
                –
                {{ $regularization->requested_clock_out ? $regularization->requested_clock_out->format('h:i A') : '—' }}
            </p>
            <p class="mt-1 text-slate-600">{{ $regularization->reason }}</p>
            @if($regularization->review_remarks)
                <p class="mt-2 text-xs text-slate-500">Reviewer remarks: {{ $regularization->review_remarks }}</p>
            @endif
        </div>
    @endif

    @if($canApprove && $isPending)
        <div class="mb-4 space-y-3 border-t border-slate-200 pt-4">
            <p class="text-sm font-medium text-slate-800">Manager approval</p>
            <form action="{{ route('attendance-regularizations.approve', $regularization) }}" method="POST" class="space-y-2">
                @csrf
                <input type="hidden" name="redirect_to" value="attendance">
                <input type="hidden" name="year" value="{{ $selectedYear }}">
                <input type="hidden" name="month" value="{{ $selectedMonth }}">
                <input type="hidden" name="employee_id" value="{{ $selectedEmployeeId }}">
                <input type="text" name="review_remarks" placeholder="Approval remarks (optional)" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                <button type="submit" class="inline-flex w-full items-center justify-center rounded-lg bg-emerald-600 px-4 py-2 text-sm font-medium text-white hover:bg-emerald-700">Approve</button>
            </form>
            <form action="{{ route('attendance-regularizations.reject', $regularization) }}" method="POST" class="space-y-2">
                @csrf
                <input type="hidden" name="redirect_to" value="attendance">
                <input type="hidden" name="year" value="{{ $selectedYear }}">
                <input type="hidden" name="month" value="{{ $selectedMonth }}">
                <input type="hidden" name="employee_id" value="{{ $selectedEmployeeId }}">
                <input type="text" name="review_remarks" placeholder="Rejection reason (optional)" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                <button type="submit" class="inline-flex w-full items-center justify-center rounded-lg border border-red-300 bg-white px-4 py-2 text-sm font-medium text-red-700 hover:bg-red-50">Reject</button>
            </form>
        </div>
    @endif

    @if($canRequest && ! $isPending)
        <form action="{{ route('attendance-regularizations.store') }}" method="POST" class="space-y-3">
            @csrf
            <input type="hidden" name="date" value="{{ $date }}">
            <input type="hidden" name="redirect_to" value="attendance">
            <input type="hidden" name="year" value="{{ $selectedYear }}">
            <input type="hidden" name="month" value="{{ $selectedMonth }}">
            <input type="hidden" name="employee_id" value="{{ $selectedEmployeeId }}">
            <p class="text-xs text-slate-500">Update the times you want corrected. Your manager (and higher approvers) will be notified.</p>
            <div class="grid gap-3 sm:grid-cols-2">
                <div>
                    <label class="mb-1 block text-xs font-medium uppercase text-slate-500">Requested clock in</label>
                    <input type="time" name="requested_clock_in_time" step="60" value="{{ old('requested_clock_in_time', $reqClockIn) }}" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                </div>
                <div>
                    <label class="mb-1 block text-xs font-medium uppercase text-slate-500">Requested clock out</label>
                    <input type="time" name="requested_clock_out_time" step="60" value="{{ old('requested_clock_out_time', $reqClockOut) }}" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                </div>
            </div>
            <div>
                <label class="mb-1 block text-xs font-medium uppercase text-slate-500">Reason</label>
                <textarea name="reason" rows="3" required class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm" placeholder="Why is this correction needed?">{{ old('reason') }}</textarea>
            </div>
            <button type="submit" class="inline-flex w-full items-center justify-center rounded-lg bg-slate-800 px-4 py-2 text-sm font-medium text-white hover:bg-slate-700">
                Submit for approval
            </button>
        </form>
    @elseif($canRequest && $isPending)
        <p class="text-sm text-amber-700">Your request is pending manager approval.</p>
    @elseif(! $canRequest && ! $regularization)
        <p class="text-sm text-slate-500">Only the employee can submit a regularization request from this calendar.</p>
    @endif
</div>
