@extends('layouts.app')
@section('title', $row->exists ? 'Edit Finding' : 'New Finding')

@section('content')
<section class="content-header">
    <h1>{{ $row->exists ? 'Edit' : 'New' }} Finding
        <small>{{ ucwords(str_replace('_', ' ', $row->system_key)) }}</small></h1>
    <ol class="breadcrumb">
        <li><a href="{{ route('clinic.system-findings.index') }}">System Findings</a></li>
        <li><a href="{{ route('clinic.system-findings.list', $row->system_key) }}">{{ ucwords(str_replace('_', ' ', $row->system_key)) }}</a></li>
        <li class="active">{{ $row->exists ? 'Edit' : 'New' }}</li>
    </ol>
</section>

<section class="content">
    <form method="POST"
          action="{{ $row->exists ? route('clinic.system-findings.update', $row->id) : route('clinic.system-findings.store') }}">
        @csrf
        @if(!$row->exists)
            <input type="hidden" name="system_key" value="{{ $row->system_key }}">
        @endif
        @component('components.widget', ['class' => 'box-primary'])
            <div class="row">
                <div class="col-md-4 form-group">
                    <label>Value (machine key) <span class="text-danger">*</span></label>
                    <input type="text" name="value" required class="form-control"
                           value="{{ $row->value }}" placeholder="e.g. murmur_grade2"
                           {{ $row->exists ? 'readonly' : '' }}>
                    <small class="text-muted">
                        Lowercase, snake_case. To override a system default, use the same value.
                    </small>
                </div>
                <div class="col-md-8 form-group">
                    <label>Label <span class="text-danger">*</span></label>
                    <input type="text" name="label" required class="form-control"
                           value="{{ $row->label }}" placeholder="What the doctor sees in the checklist">
                </div>
            </div>

            <div class="row">
                <div class="col-md-4 form-group">
                    <label>Sort order</label>
                    <input type="number" name="sort_order" class="form-control" min="0" max="999"
                           value="{{ $row->sort_order ?? 99 }}">
                </div>
                <div class="col-md-4 form-group">
                    <label>Status</label>
                    <select name="status" class="form-control">
                        <option value="active" {{ ($row->status ?? 'active') === 'active' ? 'selected' : '' }}>Active</option>
                        <option value="inactive" {{ ($row->status ?? '') === 'inactive' ? 'selected' : '' }}>Inactive</option>
                    </select>
                </div>
            </div>

            <h4 style="border-bottom:1px solid #e2e8f0;padding-bottom:6px;">Per-finding flags</h4>

            <div class="form-group">
                <label style="font-weight:normal;cursor:pointer;display:block;">
                    <input type="checkbox" name="supports_severity" value="1"
                           {{ $row->supports_severity ? 'checked' : '' }}>
                    <strong>Show severity radio</strong> (Mild / Moderate / Severe)
                </label>
                <label style="font-weight:normal;cursor:pointer;display:block;">
                    <input type="checkbox" name="supports_lr" value="1"
                           {{ $row->supports_lr ? 'checked' : '' }}>
                    <strong>Show lateralisation toggle</strong> (L / R)
                </label>
                <label style="font-weight:normal;cursor:pointer;display:block;">
                    <input type="checkbox" name="is_critical" value="1"
                           {{ $row->is_critical ? 'checked' : '' }}>
                    <strong>Critical</strong> — any positive selection feeds the Medical Critical Check report
                </label>
            </div>

            <div class="form-group" style="margin-top:14px;">
                <button type="submit" class="btn btn-primary"><i class="fa fa-save"></i> Save</button>
                <a href="{{ route('clinic.system-findings.list', $row->system_key) }}" class="btn btn-default">
                    <i class="fa fa-times"></i> Cancel
                </a>
            </div>
        @endcomponent
    </form>
</section>
@endsection
