<div>
    <div class="row">
        <div class="col-md-6 d-flex justify-content-between mt-2 mb-2">
            <h4 class="fw-bold "><span class="text-muted fw-light"></span> Pre Claim</h4>
        </div>

        <div class="col-md-6 d-flex justify-content-end">
            <div class="col-md-9 mb-4">
                <input type="text" wire:model.live.debounce.300ms="searchBatches" value="" name="searchBatches" placeholder="Search batches" class="form-control search">
            </div>
        </div>
    </div>
    <!-- Striped Rows -->
    <div class="card">
        <div class="card-body">
            <div class="d-flex justify-content-end mb-2 mt-0">
                <button
                    type="button"
                    class="btn btn-primary"
                    data-bs-toggle="modal"
                    data-bs-target="#importModal"
                    data-bs-backdrop="static" data-bs-keyboard="false"
                    style="margin-left: 10px;"
                    ><strong><i class="bx bx-up-arrow-alt"></i>
                    Import</strong>
                </button>
                </a>
            </div>

            <div class="table-responsive text-nowrap">
                <table class="table table-hover">
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>Batch #</th>
                        <th>File Name</th>
                        <th>File Path</th>
                        <th>Status</th>
                        <th>Created</th>
                        <th>Action</th>
                    </tr>
                    </thead>
                    <tbody class="table-border-bottom-0">
                    @forelse ($batches as $batch)
                        <tr wire:key="batch-row-{{ $batch->id }}">
                            <td>{{ $batch->id }}</td>
                            <td><strong>{{ $batch->batch_number ?? '-' }}</strong></td>
                            <td>{{ $batch->file_name }}</td>
                            <td><code>{{ $batch->file_path }}</code></td>
                            <td>
                                <span class="badge {{ (int) $batch->completed === 1 ? 'bg-label-success' : 'bg-label-warning' }}">
                                    {{ (int) $batch->completed === 1 ? 'Completed' : 'Processing' }}
                                </span>
                            </td>
                            <td>{{ optional($batch->created_at)->format('Y-m-d H:i') }}</td>
                            <td>
                                <a class="btn btn-sm btn-primary"
                                   href="{{ route('admin.preclaim-data', $batch->id) }}">
                                    View Rows
                                </a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="text-center">No batches found</td>
                        </tr>
                    @endforelse
                    </tbody>
                </table>

                <div class="mt-2">
                    {{ $batches->links('pagination::bootstrap-5') }}
                </div>
            </div>
        </div>
    </div>

    <!-- Import Modal -->
    <div class="modal fade" id="importModal"  tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-lg" role="document">
            <form action="{{route('admin.import-pre-claim')}}" method="POST" enctype="multipart/form-data">
                @csrf
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="modalTitle">Import Pre Claim</h5>
                        <button
                            type="button"
                            class="btn-close"
                            data-bs-dismiss="modal"
                            aria-label="Close"
                        ></button>
                    </div>
                    <div class="modal-body">
                        @csrf

                        <div class="row">
                            <div class="col mb-2">
                                <label for="description" class="form-label">File</label>
                                <input type="file" name="file" class="form-control @error('file') is-invalid @enderror" accept="csv, xlsx"/>
                                @error('file')
                                    <span class="invalid-feedback"><strong>{{$message}}</strong></span>
                                @enderror
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary">Submit</button>
                    </div>
                </div>
            </form>
        </div>
    </div>


</div>

@section('script')
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
    $(document).ready(function(){
        function confirmDelete(id) {
            Swal.fire({
                title: 'Are you sure?',
                text: "This may effect on product listings",
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, delete it!'
            }).then((result) => {
                if (result.isConfirmed) {
                    @this.call('delete', id)
                }
            });
        }

        $(document).on('click', '.delete', function(e) {
            e.preventDefault();

            // If icon is clicked instead of <a>, this ensures we still get correct data-id
            const id = $(e.target).closest('.delete').data('id');
            confirmDelete(id);
        });
    });

    $(document).ready(function(){
        $("#importModal").modal({
            backdrop: "static",
            keyboard: false,
        });
    });

    function copyToClipboard() {
        const text = $("#imagePath").text();
        console.log(text);
        navigator.clipboard.writeText(text).then(() => {
            document.getElementById("copyStatus").textContent = "Copied!";
        }).catch(err => {
            document.getElementById("copyStatus").textContent = "Failed to copy!";
            console.error("Failed to copy: ", err);
        });
    }

</script>

@if($errors->any())
    <script type="text/javascript">
        $(document).ready(function(){
            $("#importModal").modal('show');
        });
    </script>
@endif
@endsection