@props(['type', 'defaults', 'level' => 0])

@if ($type->childrenRecursive->isNotEmpty())
	<div class="hs-accordion" id="accordion-{{ $type->id }}" >
		<div class="flex justify-between items-center">
			<button
				class="hs-accordion-toggle py-0 inline-flex items-center gap-x-3 font-semibold text-start text-gray-800 transition hover:text-gray-500 dark:text-gray-200 dark:hover:text-white/80"
				aria-controls="accordion-collapse-{{ $type->id }}" type="button" aria-expanded="false">
				<code class="plus-icon mb-2 block w-3 h-3 text-gray-600">+</code>
				<code class="minus-icon mb-2 hidden w-3 h-3 text-gray-600">-</code>
				<code class="text-gray-800 dark:text-white">{{ $type->code }} - {{ $type->description }}</code>
			</button>
			<!-- Use old() helper and check if defaults exist for this insurance type -->
			<input type="text" name="commission_defaults[{{ $type->id }}]" class="branch-input p-1 border rounded w-1/3"
				data-children-container="#accordion-collapse-{{ $type->id }}" placeholder="vrednost za {{ $type->code }}"
				value="{{ old('commission_defaults.' . $type->id, $defaults[$type->id]->commission_rate ?? '') }}" />
		</div>
		<div id="accordion-collapse-{{ $type->id }}"
			class="hs-accordion-content hidden w-full overflow-hidden transition-[height] duration-300"
			aria-labelledby="accordion-{{ $type->id }}">
			<div class="hs-accordion-group ltr:pl-6 rtl:pr-6">
				@foreach ($type->childrenRecursive as $child)
					@include('backend.insurance.insurance-types.partials.insurance_type', [
						'type' => $child,
						'defaults' => $defaults,
						'level' => $level + 1,
					])
				@endforeach
			</div>
		</div>
	</div>
@else
	<div class="hs-accordion-item hover:bg-green-100">
		<div class="flex justify-between items-center">
			<div class="flex items-center gap-x-3 ">
				<code class="plus-icon mb-2 block w-3 h-3 text-gray-600">•</code>
				<code class="text-gray-800 dark:text-white">{{ $type->code }} - {{ $type->description }}</code>
			</div>
			<input type="text" name="commission_defaults[{{ $type->id }}]" class="branch-input p-1 border rounded w-1/3"
				placeholder="vrednost za {{ $type->code }}"
				value="{{ old('commission_defaults.' . $type->id, $defaults[$type->id]->commission_rate ?? '') }}" />
		</div>
	</div>
@endif
