<x-app-layout>
    <x-slot name="header">{{ __('Vendors') }}</x-slot>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <div class="card shadow-sm">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="mb-0"><i class="bi bi-building me-2"></i>{{ __('Vendor List') }}</h5>
            <a href="{{ route('vendors.create') }}" class="btn btn-primary btn-sm">
                <i class="bi bi-plus-lg me-1"></i>{{ __('Add Vendor') }}
            </a>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>#</th>
                            <th>{{ __('Name') }}</th>
                            <th>{{ __('Phone') }}</th>
                            <th>{{ __('Address') }}</th>
                            <th class="text-end">{{ __('Due Balance') }}</th>
                            <th>{{ __('Status') }}</th>
                            <th class="text-end">{{ __('Actions') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($vendors as $vendor)
                            <tr>
                                <td>{{ $loop->iteration }}</td>
                                <td><strong>{{ $vendor->name }}</strong></td>
                                <td>{{ $vendor->phone ?? '-' }}</td>
                                <td>{{ $vendor->address ?? '-' }}</td>
                                <td class="text-end">
                                    <span class="{{ $vendor->current_due > 0 ? 'text-danger fw-semibold' : 'text-success' }}">
                                        {{ number_format($vendor->current_due, 2) }}
                                    </span>
                                </td>
                                <td>
                                    @if($vendor->is_active)
                                        <span class="badge bg-success">{{ __('Active') }}</span>
                                    @else
                                        <span class="badge bg-secondary">{{ __('Inactive') }}</span>
                                    @endif
                                </td>
                                <td class="text-end">
                                    <a href="{{ route('vendors.ledger', $vendor) }}" class="btn btn-outline-info btn-sm">
                                        <i class="bi bi-journal-text me-1"></i>{{ __('Ledger') }}
                                    </a>
                                    <a href="{{ route('vendors.edit', $vendor) }}" class="btn btn-outline-secondary btn-sm">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                    <form method="POST" action="{{ route('vendors.destroy', $vendor) }}" class="d-inline"
                                          onsubmit="return confirm('{{ __('Delete this vendor?') }}')">
                                        @csrf @method('DELETE')
                                        <button class="btn btn-outline-danger btn-sm"><i class="bi bi-trash"></i></button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="7" class="text-center text-muted py-4">{{ __('No vendors found.') }}</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @if($vendors->hasPages())
            <div class="card-footer">{{ $vendors->links() }}</div>
        @endif
    </div>
</x-app-layout>
