<?php $__env->startSection('title', 'Dashboard – Gap Finder Analytics'); ?>

<?php $__env->startSection('content'); ?>
    <h1>Dashboard</h1>
    <p class="muted">Overview of territories, MRs, doctors, and imports.</p>

    <div class="grid grid-3 mt-2">
        <div class="card mb-0">
            <div class="card-title">Territories (Areas)</div>
            <div class="card-value"><?php echo e($stats['areas']); ?></div>
        </div>
        <div class="card mb-0">
            <div class="card-title">Medical Representatives</div>
            <div class="card-value"><?php echo e($stats['mrs']); ?></div>
        </div>
        <div class="card mb-0">
            <div class="card-title">Doctors</div>
            <div class="card-value"><?php echo e($stats['doctors']); ?></div>
        </div>
        <div class="card mb-0">
            <div class="card-title">MR–Area Assignments</div>
            <div class="card-value"><?php echo e($stats['assignments']); ?></div>
        </div>
        <div class="card mb-0">
            <div class="card-title">Visits</div>
            <div class="card-value"><?php echo e($stats['visits']); ?></div>
        </div>
        <div class="card mb-0">
            <div class="card-title">Import Batches</div>
            <div class="card-value"><?php echo e($stats['import_batches']); ?></div>
        </div>
    </div>

    <div class="grid grid-3 mt-2" style="grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));">
        <div class="card">
            <h2>Recent Territories</h2>
            <?php if($recentAreas->count()): ?>
                <div class="table-wrap"><table>
                    <thead>
                        <tr><th>Code / Name</th><th>Doctors</th></tr>
                    </thead>
                    <tbody>
                        <?php $__currentLoopData = $recentAreas; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $a): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <tr>
                                <td><?php echo e($a->code ?? $a->name); ?></td>
                                <td><?php echo e($a->doctors_count); ?></td>
                            </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </tbody>
                </table></div>
            <?php else: ?>
                <p class="muted">No territories yet. Import a location sheet.</p>
            <?php endif; ?>
        </div>
        <div class="card">
            <h2>Recent MRs</h2>
            <?php if($recentMrs->count()): ?>
                <div class="table-wrap"><table>
                    <thead>
                        <tr><th>Name</th><th>Assignments</th></tr>
                    </thead>
                    <tbody>
                        <?php $__currentLoopData = $recentMrs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $mr): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <tr>
                                <td><?php echo e($mr->name); ?></td>
                                <td><?php echo e($mr->area_assignments_count); ?></td>
                            </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </tbody>
                </table></div>
            <?php else: ?>
                <p class="muted">No MRs yet. Import a location sheet.</p>
            <?php endif; ?>
        </div>
        <div class="card">
            <h2>Recent Doctors</h2>
            <?php if($recentDoctors->count()): ?>
                <div class="table-wrap"><table>
                    <thead>
                        <tr><th>Name</th><th>Area</th></tr>
                    </thead>
                    <tbody>
                        <?php $__currentLoopData = $recentDoctors; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $d): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <tr>
                                <td><?php echo e($d->name); ?></td>
                                <td><?php echo e($d->area->name ?? '–'); ?></td>
                            </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </tbody>
                </table></div>
            <?php else: ?>
                <p class="muted">No doctors yet. Import a location sheet.</p>
            <?php endif; ?>
        </div>
    </div>

    <div class="card mt-2">
        <h2>Recent Import Batches</h2>
        <?php if($recentBatches->count()): ?>
            <div class="table-wrap"><table>
                <thead>
                    <tr><th>File</th><th>Status</th><th>Rows</th><th>Date</th><th></th></tr>
                </thead>
                <tbody>
                    <?php $__currentLoopData = $recentBatches; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $b): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <tr>
                            <td><?php echo e($b->file_name); ?></td>
                            <td><?php echo e($b->status); ?></td>
                            <td><?php echo e($b->staging_rows_count ?? 0); ?></td>
                            <td><?php echo e($b->created_at->format('M j, Y H:i')); ?></td>
                            <td><a href="<?php echo e(route('imports.show', $b)); ?>">View</a></td>
                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </tbody>
            </table>
        <?php else: ?>
            <p class="muted">No import batches yet. Go to Imports to upload visit or location sheets.</p>
        <?php endif; ?>
        <p style="margin-top: 1rem;"><a href="<?php echo e(route('imports.index')); ?>" class="btn btn-ghost">All imports →</a></p>
    </div>

    <?php if(!empty($deviationAlerts)): ?>
        <div class="card mt-2">
            <h2>Deviation alerts (last 30 days)</h2>
            <p class="muted">MRs with compliance score below “high” (<?php echo e($periodFrom); ?> – <?php echo e($periodTo); ?>). Run <code>php artisan visits:classify</code> to refresh territory classification.</p>
            <div class="table-wrap"><table>
                <thead>
                    <tr><th>MR</th><th>Score</th><th>Band</th><th>Visits</th><th>Outside</th><th>Reasons</th><th></th></tr>
                </thead>
                <tbody>
                    <?php $__currentLoopData = $deviationAlerts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $mrId => $s): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <?php $mr = \App\Models\Mr::find($mrId); ?>
                        <tr>
                            <td><?php echo e($mr?->name ?? 'MR #'.$mrId); ?></td>
                            <td><?php echo e($s['score']); ?></td>
                            <td><span class="badge badge-<?php echo e($s['band'] === 'low' ? 'error' : 'warning'); ?>"><?php echo e($s['band']); ?></span></td>
                            <td><?php echo e($s['total_visits']); ?></td>
                            <td><?php echo e($s['outside']); ?></td>
                            <td><?php echo e(collect($s['reasons'] ?? [])->pluck('code')->join(', ') ?: '–'); ?></td>
                            <td><a href="<?php echo e(route('compliance.show', ['mr' => $mrId, 'from' => $periodFrom, 'to' => $periodTo])); ?>">Details</a></td>
                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </tbody>
            </table></div>
            <p class="mt-1"><a href="<?php echo e(route('compliance.index')); ?>" class="btn btn-ghost">Compliance overview →</a></p>
        </div>
    <?php endif; ?>
<?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/dashboard.blade.php ENDPATH**/ ?>