@php
    $showAssetMeta = $showAssetMeta ?? false;
    $showAccountMeta = $showAccountMeta ?? false;
@endphp

@forelse ($sectionTasks as $task)
    <div class="border rounded-3 p-3 mb-3" style="border-color: {{ config('theme.colors.muted') }} !important;">
        <div class="d-flex flex-wrap justify-content-between gap-2 mb-2">
            <div>
                <div class="fw-semibold">
                    {{ $task->title }}
                    @if ($task->is_required)
                        <span class="badge" style="background: {{ config('theme.colors.primary') }}">{{ __('recruitment.onboarding.required') }}</span>
                    @endif
                </div>
                @if ($task->description)
                    <div class="small text-muted">{{ $task->description }}</div>
                @endif
            </div>
            <span class="badge align-self-start" style="background: {{ config('theme.colors.muted') }}">
                {{ __('recruitment.onboarding.task_status.'.$task->status) }}
            </span>
        </div>

        @can('manage', $case)
            <form method="POST" action="{{ route('hr.recruitment.onboarding.tasks.update', [$case, $task]) }}" class="row g-2 align-items-end">
                @csrf
                @method('PUT')
                <div class="col-md-3">
                    <label class="form-label small">{{ __('recruitment.onboarding.fields.status') }}</label>
                    <select name="status" class="form-select form-select-sm">
                        @foreach (['pending','in_progress','completed','skipped','cancelled'] as $st)
                            <option value="{{ $st }}" @selected($task->status === $st)>{{ __('recruitment.onboarding.task_status.'.$st) }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label small">{{ __('recruitment.onboarding.fields.assigned_to') }}</label>
                    <select name="assigned_to" class="form-select form-select-sm">
                        <option value="">—</option>
                        @foreach ($assignees as $user)
                            <option value="{{ $user->id }}" @selected((int) $task->assigned_to === (int) $user->id)>{{ $user->name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label small">{{ __('recruitment.onboarding.fields.due_date') }}</label>
                    <input type="date" name="due_date" class="form-control form-control-sm" value="{{ $task->due_date?->format('Y-m-d') }}">
                </div>
                <div class="col-md-4">
                    <label class="form-label small">{{ __('recruitment.onboarding.fields.notes') }}</label>
                    <input type="text" name="notes" class="form-control form-control-sm" value="{{ $task->notes }}">
                </div>
                @if ($showAssetMeta)
                    <div class="col-md-4">
                        <label class="form-label small">{{ __('recruitment.onboarding.fields.asset_name') }}</label>
                        <input type="text" name="meta[asset_name]" class="form-control form-control-sm" value="{{ $task->meta['asset_name'] ?? '' }}">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label small">{{ __('recruitment.onboarding.fields.asset_serial') }}</label>
                        <input type="text" name="meta[asset_serial]" class="form-control form-control-sm" value="{{ $task->meta['asset_serial'] ?? '' }}">
                    </div>
                @endif
                @if ($showAccountMeta)
                    <div class="col-md-4">
                        <label class="form-label small">{{ __('recruitment.onboarding.fields.account_username') }}</label>
                        <input type="text" name="meta[account_username]" class="form-control form-control-sm" value="{{ $task->meta['account_username'] ?? '' }}">
                    </div>
                    @if (($task->meta['account_type'] ?? null) === 'custom')
                        <div class="col-md-4">
                            <label class="form-label small">{{ __('recruitment.onboarding.fields.custom_label') }}</label>
                            <input type="text" name="meta[custom_label]" class="form-control form-control-sm" value="{{ $task->meta['custom_label'] ?? '' }}">
                        </div>
                    @endif
                @endif
                <div class="col-md-2">
                    <x-ui.buttons.primary type="submit" class="btn-sm w-100">{{ __('recruitment.common.save') }}</x-ui.buttons.primary>
                </div>
            </form>

            <form method="POST" action="{{ route('hr.recruitment.onboarding.tasks.attachments.store', [$case, $task]) }}" enctype="multipart/form-data" class="row g-2 align-items-end mt-2">
                @csrf
                <div class="col-md-8">
                    <label class="form-label small">{{ __('recruitment.onboarding.fields.attachment') }}</label>
                    <input type="file" name="attachment" class="form-control form-control-sm" required>
                </div>
                <div class="col-md-4">
                    <x-ui.buttons.secondary type="submit" class="btn-sm w-100">{{ __('recruitment.onboarding.actions.upload') }}</x-ui.buttons.secondary>
                </div>
            </form>
        @else
            <div class="small text-muted">
                {{ __('recruitment.onboarding.fields.assigned_to') }}: {{ $task->assignee?->name ?? '—' }}
                · {{ __('recruitment.onboarding.fields.due_date') }}: {{ $task->due_date?->format('Y-m-d') ?? '—' }}
                · {{ __('recruitment.onboarding.fields.completed_at') }}: {{ $task->completed_at?->format('Y-m-d H:i') ?? '—' }}
            </div>
        @endcan

        @if ($task->attachments->isNotEmpty())
            <ul class="list-unstyled small mb-0 mt-2">
                @foreach ($task->attachments as $file)
                    <li>
                        <a href="{{ route('hr.recruitment.onboarding.attachments.download', [$case, $file]) }}">{{ $file->original_name }}</a>
                    </li>
                @endforeach
            </ul>
        @endif
    </div>
@empty
    <p class="text-muted mb-0">{{ __('recruitment.onboarding.no_tasks') }}</p>
@endforelse
