<div>
    <x-layouts.app title="{{ __('Partners') }}">
        <div class="page-header d-flex justify-content-between align-items-center">
            <div>
                <h1 class="page-title">{{ __('Partners') }}</h1>
                <p class="text-muted mb-0">{{ __('Manage customers and suppliers') }}</p>
            </div>
            <div>
                <button wire:click="export" class="btn btn-outline-success me-2">
                    <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>
                <button wire:click="openImportModal" class="btn btn-outline-primary me-2">
                    <i class="fas fa-file-import me-2"></i>{{ __('Import') }}
                </button>
                <a href="{{ route('partners.create') }}" class="btn btn-primary">
                    <i class="fas fa-plus me-2"></i>{{ __('New Partner') }}
                </a>
            </div>
        </div>

        <!-- Filters -->
        <div class="card mb-3">
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-4">
                        <input type="text" class="form-control" wire:model.live.debounce.300ms="search" 
                               placeholder="{{ __('Search partners...') }}">
                    </div>
                    <div class="col-md-3">
                        <select class="form-select" wire:model.live="typeFilter">
                            <option value="">{{ __('All Types') }}</option>
                            @foreach($partnerTypes as $type)
                            <option value="{{ $type->id }}">{{ $type->name }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <!-- Table -->
        <div class="card">
            <div class="table-responsive">
                <table class="table table-hover 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>{{ __('Name') }}</th>
                            <th>{{ __('Type') }}</th>
                            <!-- <th>{{ __('Category') }}</th> -->
                            <th>{{ __('Contact') }}</th>
                            <th class="text-end">{{ __('Balance') }}</th>
                            <th class="text-end">{{ __('Actions') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($partners as $partner)
                            <tr wire:key="partner-{{ $partner->id }}" class="{{ in_array($partner->id, $selectedRows) ? 'table-primary' : '' }}">
                                <td>
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" wire:model.live="selectedRows" value="{{ $partner->id }}">
                                    </div>
                                </td>
                                <td>
                                    <div class="fw-bold">{{ $partner->name }}</div>
                                    @if($partner->tax_number)
                                        <small class="text-muted">{{ __('Tax ID:') }} {{ $partner->tax_number }}</small>
                                    @endif
                                </td>
                                <td>
                                    <span class="badge bg-secondary">
                                        {{ $partner->type->name ?? '-' }}
                                    </span>
                                </td>
                                <!-- <td>{{ $partner->category->name ?? '-' }}</td> -->
                                <td>
                                    <div>{{ $partner->email }}</div>
                                    <div class="text-muted small">{{ $partner->phone }}</div>
                                </td>
                                <td class="text-end">
                                    <span class="{{ $partner->current_balance > 0 ? 'text-success' : ($partner->current_balance < 0 ? 'text-danger' : '') }}">
                                        {{ number_format($partner->current_balance, 2) }}
                                    </span>
                                </td>
                                <td class="text-end">
                                    <a href="{{ route('partners.edit', $partner) }}" class="btn btn-sm btn-outline-primary">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <button class="btn btn-sm btn-outline-danger" 
                                            wire:click="delete({{ $partner->id }})"
                                            wire:confirm="{{ __('Are you sure?') }}">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="6" class="text-center py-4 text-muted">{{ __('No partners found.') }}</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            @if($partners->hasPages())
                <div class="card-footer">{{ $partners->links() }}</div>
            @endif
        </div>
        <!-- Import Modal -->
        @if($showImportModal)
            <div class="modal fade show d-block" tabindex="-1" style="background: rgba(0,0,0,0.5);">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title">{{ __('Import Partners') }}</h5>
                            <button type="button" class="btn-close" wire:click="$set('showImportModal', false)"></button>
                        </div>
                        <div class="modal-body">
                            <div class="mb-3">
                                <label class="form-label">{{ __('Excel File') }} <span class="text-danger">*</span></label>
                                <input type="file" class="form-control @error('importFile') is-invalid @enderror" wire:model="importFile" accept=".xlsx,.xls,.csv">
                                @error('importFile') <div class="invalid-feedback">{{ $message }}</div> @enderror
                                <div wire:loading wire:target="importFile" class="text-muted mt-1 small">
                                    <i class="fas fa-spinner fa-spin me-1"></i>{{ __('Uploading...') }}
                                </div>
                            </div>

                            <div class="mt-4 pt-3 border-top">
                                <h6>{{ __('Instructions') }}</h6>
                                <ul class="small text-muted ps-3">
                                    <li>{{ __('File must contain column: Name') }}</li>
                                    <li>{{ __('Optional: Type, Email, Phone, Address, Tax Number') }}</li>
                                </ul>
                                <button type="button" class="btn btn-sm btn-link p-0 text-decoration-none" wire:click="downloadTemplate">
                                    <i class="fas fa-download me-1"></i>{{ __('Download Sample Template') }}
                                </button>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" wire:click="$set('showImportModal', false)">
                                {{ __('Cancel') }}
                            </button>
                            <button type="button" class="btn btn-primary" wire:click="import" wire:loading.attr="disabled">
                                <span wire:loading.remove wire:target="import"><i class="fas fa-file-import me-2"></i>{{ __('Import') }}</span>
                                <span wire:loading wire:target="import"><i class="fas fa-spinner fa-spin me-2"></i>{{ __('Importing...') }}</span>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        @endif
    </x-layouts.app>
</div>
