<div>
    <x-layouts.app title="{{ __('Sales Returns') }}">
        <div class="row mb-4">
            <div class="col-12 d-flex justify-content-between align-items-center">
                <div>
                    <h1 class="page-title">{{ __('Sales Returns') }}</h1>
                    <p class="text-muted mb-0">{{ __('Manage and track all sales returns') }}</p>
                </div>
                <div>
                    <button wire:click="export" class="btn btn-outline-success">
                        <i class="fas fa-file-excel me-2"></i>{{ __('Export') }}
                        @if(count($selectedRows) > 0)
                            <span class="badge bg-success ms-1">{{ count($selectedRows) }}</span>
                        @endif
                    </button>
                </div>
            </div>
        </div>

        <div class="card mb-4">
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-4">
                        <label class="form-label">{{ __('Search') }}</label>
                        <input type="text" class="form-control" wire:model.live.debounce.300ms="search" placeholder="{{ __('Search by Return #, Sale #, or Customer') }}">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">{{ __('Date From') }}</label>
                        <input type="date" class="form-control" wire:model.live="dateFrom">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">{{ __('Date To') }}</label>
                        <input type="date" class="form-control" wire:model.live="dateTo">
                    </div>
                </div>
            </div>
        </div>

        <div class="card">
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead class="table-light">
                            <tr>
                                <th style="width: 40px;">
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" wire:model.live="selectAll">
                                    </div>
                                </th>
                                <th>{{ __('Return #') }}</th>
                                <th>{{ __('Date') }}</th>
                                <th>{{ __('Original Sale') }}</th>
                                <th>{{ __('Customer') }}</th>
                                <th>{{ __('Amount') }}</th>
                                <th>{{ __('Reason') }}</th>
                                <th>{{ __('Status') }}</th>
                                <th>{{ __('Processed By') }}</th>
                                <th class="text-end">{{ __('Actions') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($returns as $return)
                                <tr class="{{ in_array($return->id, $selectedRows) ? 'table-primary' : '' }}">
                                    <td>
                                        <div class="form-check">
                                            <input class="form-check-input" type="checkbox" wire:model.live="selectedRows" value="{{ $return->id }}">
                                        </div>
                                    </td>
                                    <td class="fw-bold">{{ $return->return_number }}</td>
                                    <td>{{ $return->created_at->format('Y-m-d H:i') }}</td>
                                    <td>
                                        <a href="#" wire:click.prevent="$dispatch('view-sale', {id: {{ $return->sale_id }}})">
                                            {{ $return->sale->sale_number }}
                                        </a>
                                    </td>
                                    <td>
                                        @if($return->sale->customer)
                                            {{ $return->sale->customer->name }}
                                        @else
                                            <span class="text-muted">{{ __('Walk-in Customer') }}</span>
                                        @endif
                                    </td>
                                    <td class="fw-bold text-danger">
                                        E£ {{ number_format($return->total, 2) }}
                                    </td>
                                    <td>{{ Str::limit($return->reason, 20) }}</td>
                                    <td>
                                        @if($return->status === 'completed')
                                            <span class="badge bg-success">{{ __('Completed') }}</span>
                                        @else
                                            <span class="badge bg-secondary">{{ ucfirst($return->status) }}</span>
                                        @endif
                                    </td>
                                    <td>{{ $return->user->name ?? 'N/A' }}</td>
                                    <td class="text-end">
                                        <button class="btn btn-sm btn-outline-primary" wire:click="viewDetails({{ $return->id }})">
                                            <i class="fas fa-eye"></i>
                                        </button>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="9" class="text-center py-5 text-muted">
                                        <i class="fas fa-undo fa-3x mb-3 opacity-50"></i>
                                        <p class="mb-0">{{ __('No returns found matching your criteria.') }}</p>
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
            @if($returns->hasPages())
                <div class="card-footer border-top-0">
                    {{ $returns->links() }}
                </div>
            @endif
        </div>

        <!-- Details Modal -->
        @if($showDetailsModal && $selectedReturn)
            <div class="modal fade show" tabindex="-1" style="display: block; background: rgba(0,0,0,0.5);">
                <div class="modal-dialog modal-lg modal-dialog-centered">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title">
                                {{ __('Return Details') }} <small class="text-muted">#{{ $selectedReturn->return_number }}</small>
                            </h5>
                            <button type="button" class="btn-close" wire:click="closeDetails"></button>
                        </div>
                        <div class="modal-body">
                            <div class="row mb-3">
                                <div class="col-md-6">
                                    <p class="mb-1"><strong>{{ __('Original Sale') }}:</strong> {{ $selectedReturn->sale->sale_number }}</p>
                                    <p class="mb-1"><strong>{{ __('Date') }}:</strong> {{ $selectedReturn->created_at->format('Y-m-d H:i') }}</p>
                                    <p class="mb-1"><strong>{{ __('Processed By') }}:</strong> {{ $selectedReturn->user->name }}</p>
                                </div>
                                <div class="col-md-6 text-end">
                                    <p class="mb-1"><strong>{{ __('Status') }}:</strong> {{ ucfirst($selectedReturn->status) }}</p>
                                    <p class="mb-1"><strong>{{ __('Refund Type') }}:</strong> {{ ucfirst($selectedReturn->refund_type) }}</p>
                                </div>
                            </div>
                            
                            <table class="table table-bordered table-sm">
                                <thead class="table-light">
                                    <tr>
                                        <th>{{ __('Product') }}</th>
                                        <th class="text-center">{{ __('Qty Returned') }}</th>
                                        <th class="text-end">{{ __('Refund Amount') }}</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($selectedReturn->items as $item)
                                        <tr>
                                            <td>{{ $item->product->name }}</td>
                                            <td class="text-center">{{ $item->quantity }}</td>
                                            <td class="text-end">E£ {{ number_format($item->total, 2) }}</td>
                                        </tr>
                                    @endforeach
                                </tbody>
                                <tfoot class="table-light fw-bold">
                                    <tr>
                                        <td colspan="2" class="text-end">{{ __('Total Refunded') }}</td>
                                        <td class="text-end">E£ {{ number_format($selectedReturn->total, 2) }}</td>
                                    </tr>
                                </tfoot>
                            </table>
                            
                            @if($selectedReturn->notes)
                                <div class="alert alert-light border mt-3 mb-0">
                                    <strong>{{ __('Notes') }}:</strong> {{ $selectedReturn->notes }}
                                </div>
                            @endif
                        </div>
                        <div class="modal-footer">
                            <a href="{{ route('pdf.return.receipt', ['return' => $selectedReturn->id]) }}" class="btn btn-outline-info">
                                <i class="fas fa-download me-2"></i>{{ __('Download PDF') }}
                            </a>
                            <a href="{{ route('pdf.return.receipt', ['return' => $selectedReturn->id, 'preview' => true]) }}" target="_blank" class="btn btn-primary">
                                <i class="fas fa-print me-2"></i>{{ __('Print') }}
                            </a>
                            <button type="button" class="btn btn-secondary" wire:click="closeDetails">{{ __('Close') }}</button>
                        </div>
                    </div>
                </div>
            </div>
        @endif
    </x-layouts.app>
</div>
