@extends('layouts.app')

@section('title', 'Data Import')
@section('page-title', 'Data Import')
@section('page-subtitle', 'Bring existing members, savings, and loans into the system with CSV files')

@section('content')
<div class="row g-4">
    <div class="col-lg-5">
        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <h2 class="h5 mb-3">Import legacy data</h2>
                <form method="POST" action="{{ route('tenant.data-imports.store') }}" enctype="multipart/form-data" class="vstack gap-3">
                    @csrf
                    <div>
                        <label class="form-label">Data type</label>
                        <select name="import_type" class="form-select" required>
                            @foreach($types as $value => $label)
                                <option value="{{ $value }}">{{ $label }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div>
                        <label class="form-label">CSV file</label>
                        <input name="file" type="file" accept=".csv,.txt" class="form-control" required>
                        <div class="form-text">Upload one file at a time. Best order: branches, members, savings accounts, loans, then transaction/payment history.</div>
                    </div>
                    <button class="btn btn-primary"><i class="bi bi-upload me-1"></i> Import data</button>
                </form>
            </div>
        </div>

        <div class="card border-0 shadow-sm mt-4">
            <div class="card-body">
                <h2 class="h5 mb-3">Download templates</h2>
                <div class="vstack gap-2">
                    @foreach($types as $value => $label)
                        <a class="btn btn-outline-primary text-start" href="{{ route('tenant.data-imports.template', $value) }}">
                            <i class="bi bi-filetype-csv me-1"></i> {{ $label }} template
                        </a>
                    @endforeach
                </div>
            </div>
        </div>
    </div>

    <div class="col-lg-7">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white border-0">
                <h2 class="h5 mb-0">Import history</h2>
            </div>
            <div class="table-responsive">
                <table class="table align-middle mb-0">
                    <thead><tr><th>Date</th><th>Type</th><th>Status</th><th class="text-end">Success</th><th class="text-end">Failed</th></tr></thead>
                    <tbody>
                        @forelse($batches as $batch)
                            <tr>
                                <td>{{ app_datetime($batch->created_at) }}<div class="small text-muted">{{ $batch->original_filename }}</div></td>
                                <td>{{ $types[$batch->import_type] ?? ucfirst(str_replace('_', ' ', $batch->import_type)) }}</td>
                                <td><span class="badge {{ str_contains($batch->status, 'failed') ? 'text-bg-danger' : (str_contains($batch->status, 'error') ? 'text-bg-warning' : 'text-bg-success') }}">{{ ucfirst(str_replace('_', ' ', $batch->status)) }}</span></td>
                                <td class="text-end">{{ number_format($batch->successful_rows) }}</td>
                                <td class="text-end">{{ number_format($batch->failed_rows) }}</td>
                            </tr>
                            @if($batch->errors)
                                <tr><td colspan="5"><pre class="small bg-light border rounded p-2 mb-0">{{ $batch->errors }}</pre></td></tr>
                            @endif
                        @empty
                            <tr><td colspan="5" class="text-center text-muted py-5">No imports yet.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            <div class="p-3">{{ $batches->links() }}</div>
        </div>
    </div>
</div>
@endsection
