<?php $__env->startSection('title', 'Manual mapping – ' . $batch->file_name); ?>

<?php $__env->startSection('content'); ?>
    <div class="page-actions">
        <a href="<?php echo e(route('imports.show', $batch)); ?>" class="btn btn-ghost">← Back to batch</a>
        <form action="<?php echo e(route('imports.match', $batch)); ?>" method="POST" style="display:inline-block;">
            <?php echo csrf_field(); ?>
            <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="<?php echo e(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" <?php echo e(request('confidence') === 'low' ? 'selected' : ''); ?>>Low (&lt;70%)</option>
                <option value="high" <?php echo e(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>
                <?php $__currentLoopData = $mrs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $mr): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <option value="<?php echo e($mr->id); ?>" <?php echo e(request('mr_id') == $mr->id ? 'selected' : ''); ?>><?php echo e($mr->name); ?></option>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </select>
            <label class="inline">
                <input type="checkbox" name="unmapped" value="1" <?php echo e(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>
                <?php $__currentLoopData = $rows; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <tr>
                        <td><?php echo e($row->row_index); ?></td>
                        <td><?php echo e($row->resolvedMr?->name ?? '–'); ?></td>
                        <td><?php echo e($row->resolved_visit_date?->format('Y-m-d') ?? '–'); ?></td>
                        <td><?php echo e(\Illuminate\Support\Arr::get($row->raw_data, 'doctor_name', \Illuminate\Support\Arr::get($row->raw_data, 'doctor', '–'))); ?></td>
                        <td><?php echo e($row->suggestedDoctor?->name ?? '–'); ?></td>
                        <td><?php echo e($row->match_confidence !== null ? $row->match_confidence . '%' : '–'); ?></td>
                        <td><?php echo e($row->visit ? 'Committed' : 'Pending'); ?></td>
                        <td>
                            <?php if(! $row->visit): ?>
                                <div class="mapping-actions">
                                    <form action="<?php echo e(route('mapping.approve')); ?>" method="POST" class="mapping-form">
                                        <?php echo csrf_field(); ?>
                                        <input type="hidden" name="staging_row_id" value="<?php echo e($row->id); ?>">
                                        <select name="doctor_id">
                                            <option value="">– New doctor –</option>
                                            <?php $__currentLoopData = $row->match_candidates ?? []; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $c): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <?php $doc = $doctors->firstWhere('id', $c['doctor_id']); ?>
                                                <option value="<?php echo e($c['doctor_id']); ?>"><?php echo e($doc ? $doc->name : \App\Models\Doctor::find($c['doctor_id'])?->name); ?> (<?php echo e($c['score'] ?? 0); ?>)</option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                            <?php $__currentLoopData = $doctors->take(50); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $d): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($d->id); ?>"><?php echo e($d->name); ?></option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </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="<?php echo e(route('mapping.exception')); ?>" method="POST" style="display:inline;">
                                        <?php echo csrf_field(); ?>
                                        <input type="hidden" name="staging_row_id" value="<?php echo e($row->id); ?>">
                                        <button type="submit" class="btn btn-ghost btn-sm">Exception</button>
                                    </form>
                                </div>
                            <?php else: ?>
                                –
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </tbody>
        </table></div>
        <div class="mt-1"><?php echo e($rows->withQueryString()->links()); ?></div>
    </div>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\xampp\htdocs\gap-finder-analytics\resources\views/mapping/index.blade.php ENDPATH**/ ?>