@extends('layouts.admin')

@section('title', 'Product Import Reports')

@section('content')
<div class="admin-page-header">
    <h1>Product Import Reports</h1>
    <div class="d-flex gap-2">
        <a href="{{ route('admin.products.import.show') }}" class="admin-btn-primary">
            <i class="bi bi-upload"></i> New Import
        </a>
        <a href="{{ route('admin.products.index') }}" class="admin-btn-primary">Back to Products</a>
    </div>
</div>

<div class="admin-card">
    <div class="card-body">
        <p class="text-muted mb-3">
            <i class="bi bi-info-circle"></i> Imports run in the background. Refresh this page to see updated status and reports.
        </p>
        <div class="table-responsive">
            <table class="table table-hover align-middle">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>File</th>
                        <th>Status</th>
                        <th>Message</th>
                        <th>Started</th>
                        <th>Completed</th>
                        <th>Created</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($imports as $imp)
                    <tr>
                        <td>{{ $imp->id }}</td>
                        <td><span title="{{ $imp->original_name }}">{{ Str::limit($imp->original_name ?? $imp->file_path, 30) }}</span></td>
                        <td>
                            @if($imp->status === 'pending')
                                <span class="badge bg-secondary">Pending</span>
                            @elseif($imp->status === 'processing')
                                <span class="badge bg-info">Processing</span>
                            @elseif($imp->status === 'completed')
                                <span class="badge bg-success">Completed</span>
                            @else
                                <span class="badge bg-danger">Failed</span>
                            @endif
                        </td>
                        <td>{{ Str::limit($imp->message, 50) }}</td>
                        <td>{{ $imp->started_at ? $imp->started_at->format('M j, H:i') : '–' }}</td>
                        <td>{{ $imp->completed_at ? $imp->completed_at->format('M j, H:i') : '–' }}</td>
                        <td>{{ $imp->created_at->format('M j, H:i') }}</td>
                        <td>
                            <a href="{{ route('admin.products.import.report.show', $imp) }}" class="btn btn-sm btn-outline-primary">
                                <i class="bi bi-eye"></i> Report
                            </a>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="8" class="text-center text-muted py-4">No import reports yet. <a href="{{ route('admin.products.import.show') }}">Start an import</a>.</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <!-- Pagination - Right Aligned -->
        <div class="pagination-wrapper">
            <div class="d-flex justify-content-end align-items-center gap-3 flex-wrap">
                <div class="pagination-info-text">
                    Showing <strong>{{ $imports->firstItem() ?? 0 }}</strong>–<strong>{{ $imports->lastItem() ?? 0 }}</strong> 
                    of <strong>{{ $imports->total() }}</strong> import reports
                </div>
                @if($imports->hasPages())
                <div class="pagination-controls">
                    {{ $imports->links('vendor.pagination.admin-categories') }}
                </div>
                @endif
            </div>
        </div>
    </div>
</div>
@endsection
