@php
    $statusRows = isset($incidentStatuses) && $incidentStatuses->count() ? $incidentStatuses : collect([new \App\Models\IncidentStatus()]);
@endphp
<div class="ngbvd-repeatable-section" data-repeatable-section="status" data-pending-case-status-ids='@json($pendingCaseStatusIds ?? ['1'])' data-pending-point-options='@json(($pendingPoints ?? collect())->map(function ($p) { return ['id' => (string) $p->id, 'name' => $p->name]; })->values())'>
    <div class="ngbvd-section-header-row"><div><h4>Case Status</h4><p>Record the current status. Status history can be updated progressively over time.</p></div><button type="button" class="btn ngbvd-btn-primary" data-add-repeatable="status"><i class="ti-plus"></i> Add Status Update</button></div>
    <input type="hidden" id="statuses_count" name="statuses_count" value="{{ max(1, $statusRows->count()) }}">
    <div id="statusRows">
        @foreach($statusRows as $i => $incidentStatus)
            <div class="ngbvd-repeat-card" data-repeatable-row="status">
                <input type="hidden" name="status_id_{{ $i }}" value="{{ $incidentStatus->id }}">
                <input type="hidden" data-existing-pendingpoint value="{{ $incidentStatus->pendingpoint }}">
                <div class="ngbvd-repeat-title"><strong>Status Update <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>Date <span class="text-danger">*</span></label><input type="text" class="form-control datepicker" name="date_reported_{{ $i }}" value="{{ $incidentStatus->date_logged }}" required autocomplete="off"></div>
                    <div class="col-md-4 form-group"><label>Case Status <span class="text-danger">*</span></label><select class="form-control search-select" name="reportedto_{{ $i }}" data-status-select required><option value="">Choose One</option>@foreach($statuses as $status)<option value="{{ $status->id }}" {{ (string)$incidentStatus->incident_status === (string)$status->id ? 'selected' : '' }}>{{ $status->name }}</option>@endforeach</select></div>
                    <div class="col-md-4 form-group"><label>Details</label><textarea rows="3" class="form-control" name="notes_{{ $i }}" placeholder="Status details">{{ $incidentStatus->details }}</textarea></div>
                </div>
                <div class="row">
                    <div class="col-md-6" data-statuses-target>
                        @if(in_array((string) $incidentStatus->incident_status, $pendingCaseStatusIds ?? ['1'], true))
                            <label>Pending Points <span class="text-danger">*</span></label>
                            <select class="form-control search-select" name="pendingpoint_{{ $i }}" required>
                                <option value="">Choose One</option>
                                @foreach(($pendingPoints ?? collect()) as $pendingStatus)
                                    <option value="{{ $pendingStatus->id }}" {{ (string)$incidentStatus->pendingpoint === (string)$pendingStatus->id ? 'selected' : '' }}>{{ $pendingStatus->name }}</option>
                                @endforeach
                            </select>
                        @endif
                    </div>
                    <div class="col-md-6" data-satisfied-target></div>
                </div>
            </div>
        @endforeach
    </div>
</div>
