@extends('layouts.app')

@section('title', 'Imports – Gap Finder Analytics')

@section('content')
    <h1>Imports</h1>
    <p class="muted">Upload visit (field activity) or location (ground team) sheets.</p>

    <div class="card mt-2">
        <h2>1. Field activity / visits</h2>
        <form action="{{ route('imports.upload') }}" method="POST" enctype="multipart/form-data">
            @csrf
            <div class="form-row">
                <label for="file">Excel (MR, date, doctor, clinic, lat/lng):</label>
                <input type="file" name="file" id="file" accept=".xlsx,.xls,.csv" required>
                <button type="submit" class="btn btn-primary">Import visits</button>
            </div>
        </form>
    </div>

    <div class="card">
        <h2>2. Ground team / location sheet (doctor_db.xlsx format)</h2>
        <p class="muted">Expected columns: EmployeeReferenceId, EmployeeName, DesignationName, BaseTownName, TeamName, DoctorCode, DoctorName, DoctorSpecialty, pLat, pLng. Territory is derived from TeamName when not present.</p>
        <form action="{{ route('imports.location') }}" method="POST" enctype="multipart/form-data">
            @csrf
            <div class="form-row">
                <label for="location_file">Excel file:</label>
                <input type="file" name="file" id="location_file" accept=".xlsx,.xls,.csv" required>
                <button type="submit" class="btn btn-primary">Import location sheet</button>
            </div>
        </form>
    </div>

    <h2 class="mt-2">Visit import batches</h2>
    <div class="card">
        <div class="table-wrap"><table>
            <thead>
                <tr>
                    <th>File</th>
                    <th>Status</th>
                    <th>Rows</th>
                    <th>Date</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @forelse ($batches as $batch)
                    <tr>
                        <td>{{ $batch->file_name }}</td>
                        <td>{{ $batch->status }}</td>
                        <td>{{ $batch->staging_rows_count ?? 0 }}</td>
                        <td>{{ $batch->created_at->format('M j, Y H:i') }}</td>
                        <td><a href="{{ route('imports.show', $batch) }}">View</a></td>
                    </tr>
                @empty
                    <tr><td colspan="5" class="muted">No visit imports yet.</td></tr>
                @endforelse
            </tbody>
        </table></div>
        <div class="mt-1">{{ $batches->links() }}</div>
    </div>
@endsection
