@php
    $perpetratorRows = isset($perpetrators) && $perpetrators->count() ? $perpetrators : collect([new \App\Models\Perpetrator()]);
@endphp
<div class="ngbvd-repeatable-section" data-repeatable-section="perpetrator">
    <div class="ngbvd-section-header-row">
        <div><h4>Perpetrator Information</h4><p>Add one or more perpetrators connected to this incident.</p></div>
        <button type="button" class="btn ngbvd-btn-primary" data-add-repeatable="perpetrator"><i class="ti-plus"></i> Add Perpetrator</button>
    </div>
    <input type="hidden" id="criminals" name="criminals" value="{{ max(1, $perpetratorRows->count()) }}">
    <div id="perpetratorRows">
        @foreach($perpetratorRows as $i => $perpetrator)
            <div class="ngbvd-repeat-card" data-repeatable-row="perpetrator">
                <input type="hidden" name="perpetrator_id_{{ $i }}" value="{{ $perpetrator->id }}">
                <div class="ngbvd-repeat-title"><strong>Perpetrator <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-4 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)$perpetrator->gender === '1' ? 'checked' : '' }} required> Male</label><label><input type="radio" name="sex_{{ $i }}" value="2" {{ (string)$perpetrator->gender === '2' ? 'checked' : '' }} required> Female</label></div></div>
                    <div class="col-md-4 form-group"><label>Phone</label><input type="text" class="form-control" name="phone_{{ $i }}" value="{{ $perpetrator->phone }}" placeholder="Phone number"></div>
                    <div class="col-md-4 form-group"><label>Age <span class="text-danger">*</span></label><input type="number" class="form-control" name="agegroup_{{ $i }}" value="{{ $perpetrator->agegroup }}" required></div>
                    <div class="col-md-4 form-group"><label>Relationship to Survivor</label><select class="form-control search-select" name="relation_{{ $i }}"><option value="">Choose One</option>@foreach($relations as $relation)<option value="{{ $relation->id }}" {{ (string)$perpetrator->relation === (string)$relation->id ? 'selected' : '' }}>{{ $relation->name }}</option>@endforeach</select></div>
                    <div class="col-md-4 form-group"><label>Nationality</label><select class="form-control search-select" name="nationality_{{ $i }}"><option value="">Choose One</option>@foreach($nationality as $country)<option value="{{ $country->id }}" {{ (string)$perpetrator->nationality === (string)$country->id || (!$perpetrator->exists && $country->name == 'Uganda') ? 'selected' : '' }}>{{ $country->name }}</option>@endforeach</select></div>
                    <div class="col-md-4 form-group"><label>Nationality Status</label><select class="form-control search-select" name="national_status_{{ $i }}"><option value="">Choose One</option>@foreach($status as $state)<option value="{{ $state->id }}" {{ (string)$perpetrator->national_status === (string)$state->id ? 'selected' : '' }}>{{ $state->name }}</option>@endforeach</select></div>
                    <div class="col-md-4 form-group"><label>Occupation</label><select class="form-control search-select" name="occupation_{{ $i }}"><option value="">Choose One</option>@foreach($occupation as $work)<option value="{{ $work->id }}" {{ (string)$perpetrator->occupation === (string)$work->id ? 'selected' : '' }}>{{ $work->name }}</option>@endforeach</select></div>
                    <div class="col-md-4 form-group"><label>Education</label><select class="form-control search-select" name="education_{{ $i }}"><option value="">Choose One</option>@foreach($education as $school)<option value="{{ $school->id }}" {{ (string)$perpetrator->education === (string)$school->id ? 'selected' : '' }}>{{ $school->name }}</option>@endforeach</select></div>
                    <div class="col-md-4 form-group"><label>Continuing Threat <span class="text-danger">*</span></label><div class="ngbvd-radio-row"><label><input type="radio" name="threat_{{ $i }}" value="1" {{ (string)$perpetrator->threat === '1' ? 'checked' : '' }} required> Yes</label><label><input type="radio" name="threat_{{ $i }}" value="0" {{ (string)$perpetrator->threat === '0' ? 'checked' : '' }} required> No</label></div></div>
                </div>
            </div>
        @endforeach
    </div>
    <div class="form-group mt-3"><label>Further Details</label><textarea rows="4" class="form-control" name="notes" placeholder="Further details about the perpetrator(s)">{{ optional($perpetratorRows->first())->notes }}</textarea></div>
</div>
