@extends('layouts.app')

@section('title', 'Manual mapping – ' . $batch->file_name)

@section('content')
    <div class="page-actions">
        <a href="{{ route('imports.show', $batch) }}" class="btn btn-ghost">← Back to batch</a>
        <form action="{{ route('imports.match', $batch) }}" method="POST" style="display:inline-block;">
            @csrf
            <button type="submit" class="btn btn-primary">Run auto-match</button>
        </form>
    </div>
    <h1>Manual mapping</h1>
    <p class="muted">Approve suggested doctors, pick another, create new doctor, or mark as exception.</p>

    <div class="card mt-1">
        <form method="GET" action="{{ route('imports.mapping', $batch) }}" class="filter-bar">
            <label class="inline">Confidence</label>
            <select name="confidence" onchange="this.form.submit()">
                <option value="">All</option>
                <option value="low" {{ request('confidence') === 'low' ? 'selected' : '' }}>Low (&lt;70%)</option>
                <option value="high" {{ request('confidence') === 'high' ? 'selected' : '' }}>High (≥70%)</option>
            </select>
            <label class="inline">MR</label>
            <select name="mr_id" onchange="this.form.submit()">
                <option value="">All</option>
                @foreach ($mrs as $mr)
                    <option value="{{ $mr->id }}" {{ request('mr_id') == $mr->id ? 'selected' : '' }}>{{ $mr->name }}</option>
                @endforeach
            </select>
            <label class="inline">
                <input type="checkbox" name="unmapped" value="1" {{ request('unmapped') ? 'checked' : '' }} onchange="this.form.submit()"> Unmapped only
            </label>
        </form>
    </div>

    <div class="card mt-1">
        <div class="table-wrap"><table>
            <thead>
                <tr>
                    <th>Row</th>
                    <th>MR</th>
                    <th>Date</th>
                    <th>Raw doctor</th>
                    <th>Suggested</th>
                    <th>Confidence</th>
                    <th>Status</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                @foreach ($rows as $row)
                    <tr>
                        <td>{{ $row->row_index }}</td>
                        <td>{{ $row->resolvedMr?->name ?? '–' }}</td>
                        <td>{{ $row->resolved_visit_date?->format('Y-m-d') ?? '–' }}</td>
                        <td>{{ \Illuminate\Support\Arr::get($row->raw_data, 'doctor_name', \Illuminate\Support\Arr::get($row->raw_data, 'doctor', '–')) }}</td>
                        <td>{{ $row->suggestedDoctor?->name ?? '–' }}</td>
                        <td>{{ $row->match_confidence !== null ? $row->match_confidence . '%' : '–' }}</td>
                        <td>{{ $row->visit ? 'Committed' : 'Pending' }}</td>
                        <td>
                            @if (! $row->visit)
                                <div class="mapping-actions">
                                    <form action="{{ route('mapping.approve') }}" method="POST" class="mapping-form">
                                        @csrf
                                        <input type="hidden" name="staging_row_id" value="{{ $row->id }}">
                                        <select name="doctor_id">
                                            <option value="">– New doctor –</option>
                                            @foreach ($row->match_candidates ?? [] as $c)
                                                @php $doc = $doctors->firstWhere('id', $c['doctor_id']); @endphp
                                                <option value="{{ $c['doctor_id'] }}">{{ $doc ? $doc->name : \App\Models\Doctor::find($c['doctor_id'])?->name }} ({{ $c['score'] ?? 0 }})</option>
                                            @endforeach
                                            @foreach ($doctors->take(50) as $d)
                                                <option value="{{ $d->id }}">{{ $d->name }}</option>
                                            @endforeach
                                        </select>
                                        <input type="text" name="new_doctor_name" placeholder="New doctor name" />
                                        <button type="submit" class="btn btn-primary btn-sm">Approve</button>
                                    </form>
                                    <form action="{{ route('mapping.exception') }}" method="POST" style="display:inline;">
                                        @csrf
                                        <input type="hidden" name="staging_row_id" value="{{ $row->id }}">
                                        <button type="submit" class="btn btn-ghost btn-sm">Exception</button>
                                    </form>
                                </div>
                            @else
                                –
                            @endif
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table></div>
        <div class="mt-1">{{ $rows->withQueryString()->links() }}</div>
    </div>
@endsection
