@php
    $witnessRows = isset($witnesses) && $witnesses->count() ? $witnesses : collect([new \App\Models\Witness()]);
@endphp
<div class="ngbvd-repeatable-section" data-repeatable-section="witness">
    <div class="ngbvd-section-header-row"><div><h4>Witness Information</h4><p>Add one or more witnesses if available.</p></div><button type="button" class="btn ngbvd-btn-primary" data-add-repeatable="witness"><i class="ti-plus"></i> Add Witness</button></div>
    <input type="hidden" id="witnessess" name="witnessess" value="{{ max(1, $witnessRows->count()) }}">
    <div id="witnessRows">
        @foreach($witnessRows as $i => $witness)
            <div class="ngbvd-repeat-card" data-repeatable-row="witness">
                <input type="hidden" name="witness_id_{{ $i }}" value="{{ $witness->id }}">
                <div class="ngbvd-repeat-title"><strong>Witness <span data-row-number>{{ $i + 1 }}</span></strong><button type="button" class="btn btn-sm btn-outline-danger" data-remove-repeatable>Remove</button></div>
                <div class="row">
                    <div class="col-md-3 form-group"><label>Name</label><input type="text" class="form-control" name="name_{{ $i }}" value="{{ $witness->name }}" placeholder="Witness name"></div>
                    <div class="col-md-3 form-group"><label>Relationship</label><select class="form-control search-select" name="relation_{{ $i }}"><option value="">Choose One</option>@foreach($relations as $relation)<option value="{{ $relation->id }}" {{ (string)$witness->relation === (string)$relation->id ? 'selected' : '' }}>{{ $relation->name }}</option>@endforeach</select></div>
                    <div class="col-md-3 form-group"><label>Age <span class="text-danger">*</span></label><input type="number" class="form-control" name="agegroup_{{ $i }}" value="{{ $witness->agegroup }}" required></div>
                    <div class="col-md-3 form-group"><label>Sex <span class="text-danger">*</span></label><div class="ngbvd-radio-row"><label><input type="radio" name="sex_{{ $i }}" value="1" {{ (string)$witness->gender === '1' ? 'checked' : '' }} required> Male</label><label><input type="radio" name="sex_{{ $i }}" value="2" {{ (string)$witness->gender === '2' ? 'checked' : '' }} required> Female</label></div></div>
                </div>
            </div>
        @endforeach
    </div>
</div>
