@php
    $actionRows = isset($actions) && $actions->count() ? $actions : collect([new \App\Models\Action()]);
@endphp
<div class="ngbvd-repeatable-section" data-repeatable-section="action">
    <div class="ngbvd-section-header-row"><div><h4>Action Taken</h4><p>Record one or more actions, referrals or services provided over time.</p></div><button type="button" class="btn ngbvd-btn-primary" data-add-repeatable="action"><i class="ti-plus"></i> Add Action</button></div>
    <input type="hidden" id="actions_count" name="actions_count" value="{{ max(1, $actionRows->count()) }}">
    <div id="actionRows">
        @foreach($actionRows as $i => $action)
            @php $selectedServices = collect(explode(',', (string)$action->services))->filter()->map(fn($v) => trim($v))->toArray(); @endphp
            <div class="ngbvd-repeat-card" data-repeatable-row="action">
                <input type="hidden" name="action_id_{{ $i }}" value="{{ $action->id }}">
                <div class="ngbvd-repeat-title"><strong>Action <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>Reported To <span class="text-danger">*</span></label><select class="form-control search-select" name="reportedto_{{ $i }}" required><option value="">Choose One</option>@foreach($authorities as $authority)<option value="{{ $authority->id }}" {{ (string)$action->reported_to === (string)$authority->id ? 'selected' : '' }}>{{ $authority->name }}</option>@endforeach</select></div>
                    <div class="col-md-4 form-group"><label>Date Reported <span class="text-danger">*</span></label><input type="text" class="form-control datepicker" name="date_reported_{{ $i }}" value="{{ $action->report_date }}" required autocomplete="off"></div>
                    <div class="col-md-4 form-group"><label>Referred To</label><select class="form-control search-select" name="refferedto_{{ $i }}"><option value="">Choose One</option>@foreach($authorities as $authority)<option value="{{ $authority->id }}" {{ (string)$action->refferedto === (string)$authority->id ? 'selected' : '' }}>{{ $authority->name }}</option>@endforeach</select></div>
                    <div class="col-md-4 form-group"><label>Date Referred</label><input type="text" class="form-control datepicker" name="date_reffered_{{ $i }}" value="{{ $action->date_reffered }}" autocomplete="off"></div>
                    <div class="col-md-8 form-group"><label>Details</label><textarea rows="3" class="form-control" name="notes_{{ $i }}" placeholder="Details of the action taken">{{ $action->notes }}</textarea></div>
                </div>
                <div class="ngbvd-check-card mt-3"><h5>Services Offered To The Survivor</h5><div class="row">@foreach($services as $service)<div class="col-md-4"><label class="ngbvd-checkbox-card"><input type="checkbox" value="{{ $service->id }}" name="serviceid_{{ $i }}[]" {{ in_array((string)$service->id, $selectedServices, true) ? 'checked' : '' }}> <span>{{ $service->name }}</span></label></div>@endforeach</div></div>
            </div>
        @endforeach
    </div>
</div>
