@extends('admin.layout.layout')
@section('title', 'Pre Claim Data')

@section('content')
    <div class="container-xxl flex-grow-1 container-p-y">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <div>
                <h4 class="fw-bold mb-0">
                    Pre Claim Data ({{ $batch->batch_number ?? ('Batch #' . $batch->id) }})
                </h4>
                <div class="text-muted">
                    <span>Type: <strong>{{ $batch->file_type }}</strong></span>
                    <span class="ms-3">Path: <code>{{ $batch->file_path }}</code></span>
                </div>
            </div>
            <a href="{{ route('admin.preclaim') }}" class="btn btn-outline-secondary">Back</a>
        </div>

        <div class="card">
            <div class="card-body">
                <form method="GET" class="row g-2 mb-3">
                    <div class="col-md-6">
                        <input type="text" name="search" value="{{ request('search') }}" class="form-control" placeholder="Search rows">
                    </div>
                    <div class="col-md-3">
                        <button class="btn btn-primary" type="submit">Search</button>
                        <a href="{{ route('admin.preclaim-data', $batch->id) }}" class="btn btn-outline-secondary">Reset</a>
                    </div>
                </form>

                <div class="table-responsive text-nowrap">
                    <table class="table table-hover">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>Member ID</th>
                            <th>Payer ID</th>
                            <th>Name</th>
                            <th>DOB</th>
                            <th>Optum</th>
                            <th>Created</th>
                        </tr>
                        </thead>
                        <tbody class="table-border-bottom-0">
                        @forelse($rows as $row)
                            @php
                                $data = is_array($row->row_data) ? $row->row_data : (array) $row->row_data;
                                $resp = is_array($row->response_data) ? $row->response_data : (array) $row->response_data;
                                $ok = (bool) ($resp['ok'] ?? false);
                            @endphp
                            <tr>
                                <td>{{ $row->id }}</td>
                                <td>{{ $data['memberId'] ?? $data['member_id'] ?? '-' }}</td>
                                <td>{{ $data['payerId'] ?? $data['payer_id'] ?? '-' }}</td>
                                <td>{{ trim(($data['firstName'] ?? $data['first_name'] ?? '') . ' ' . ($data['lastName'] ?? $data['last_name'] ?? '')) ?: '-' }}</td>
                                <td>{{ $data['dateOfBirth'] ?? $data['date_of_birth'] ?? '-' }}</td>
                                <td>
                                    <span class="badge {{ $ok ? 'bg-label-success' : 'bg-label-danger' }}">
                                        {{ $ok ? 'OK' : 'FAILED' }}
                                    </span>
                                </td>
                                <td>{{ optional($row->created_at)->format('Y-m-d H:i') }}</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="text-center">No rows found</td>
                            </tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>

                <div class="mt-2">
                    {{ $rows->links() }}
                </div>
            </div>
        </div>
    </div>
@endsection

