@extends('layouts.app')

@section('title', 'Customer Management')

@section('breadcrumb')
    @include('components.breadcrumb', ['items' => ['Parties' => null, 'Customers' => null]])
@endsection

@section('content')
<div>
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 mb-6">
        <div>
            <h1 class="text-2xl font-bold text-slate-900 dark:text-white">Customer Management</h1>
            <p class="text-sm text-slate-500 dark:text-slate-400 mt-1">{{ $customers->total() ?? 0 }} customers registered</p>
        </div>
        <x-button variant="success" icon="user-plus">Add Customer</x-button>
    </div>

    <x-card :padding="false">
        <div class="overflow-x-auto">
            <table class="w-full text-sm">
                <thead>
                    <tr class="bg-slate-50 dark:bg-slate-900/50 border-b border-slate-200 dark:border-slate-800">
                        <th class="px-4 py-3 text-left text-[11px] font-bold uppercase tracking-wider text-slate-500">Customer</th>
                        <th class="px-4 py-3 text-left text-[11px] font-bold uppercase tracking-wider text-slate-500">Contact</th>
                        <th class="px-4 py-3 text-right text-[11px] font-bold uppercase tracking-wider text-slate-500">Total Sales</th>
                        <th class="px-4 py-3 text-right text-[11px] font-bold uppercase tracking-wider text-slate-500">Received</th>
                        <th class="px-4 py-3 text-right text-[11px] font-bold uppercase tracking-wider text-slate-500">Due</th>
                        <th class="px-4 py-3 text-center text-[11px] font-bold uppercase tracking-wider text-slate-500">Status</th>
                        <th class="px-4 py-3 text-center text-[11px] font-bold uppercase tracking-wider text-slate-500">Action</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
                    @forelse($customers ?? [] as $c)
                        @php $balance = $c->balance; @endphp
                        <tr class="hover:bg-slate-50 dark:hover:bg-slate-800/50">
                            <td class="px-4 py-3.5">
                                <div class="flex items-center gap-3">
                                    <div class="w-9 h-9 rounded-lg bg-emerald-100 dark:bg-emerald-500/15 flex items-center justify-center font-bold text-emerald-700 text-xs">
                                        {{ strtoupper(substr($c->name, 0, 2)) }}
                                    </div>
                                    <div>
                                        <p class="text-sm font-semibold text-slate-900 dark:text-white">{{ $c->name }}</p>
                                        <p class="text-xs text-slate-500">{{ $c->code }}</p>
                                    </div>
                                </div>
                            </td>
                            <td class="px-4 py-3.5 text-xs text-slate-500">{{ $c->phone }}</td>
                            <td class="px-4 py-3.5 text-right text-sm font-semibold counter-num">৳{{ number_format($balance['sales']) }}</td>
                            <td class="px-4 py-3.5 text-right text-sm text-emerald-600 counter-num">৳{{ number_format($balance['paid']) }}</td>
                            <td class="px-4 py-3.5 text-right text-sm font-bold {{ ($balance['sales'] - $balance['paid']) > 0 ? 'text-rose-600' : 'text-slate-400' }} counter-num">
                                ৳{{ number_format(max(0, $balance['sales'] - $balance['paid'])) }}
                            </td>
                            <td class="px-4 py-3.5 text-center">
                                <x-badge :variant="$c->is_active ? 'success' : 'gray'">{{ $c->is_active ? 'Active' : 'Inactive' }}</x-badge>
                            </td>
                            <td class="px-4 py-3.5 text-center">
                                <div class="flex items-center justify-center gap-1">
                                    <a href="{{ route('reports.customer', ['party_id' => $c->id]) }}" class="w-8 h-8 flex items-center justify-center rounded-lg hover:bg-slate-100 dark:hover:bg-slate-800" title="View Ledger">
                                        <i data-lucide="scroll-text" class="w-4 h-4"></i>
                                    </a>
                                    <a href="{{ route('sales.create', ['party_id' => $c->id]) }}" class="w-8 h-8 flex items-center justify-center rounded-lg hover:bg-slate-100 dark:hover:bg-slate-800" title="New Sale">
                                        <i data-lucide="receipt" class="w-4 h-4"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="7" class="text-center py-8 text-sm text-slate-500">No customers found.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if(isset($customers) && method_exists($customers, 'hasPages') && $customers->hasPages())
        <div class="px-4 py-3 border-t border-slate-200 dark:border-slate-800">
            {{ $customers->withQueryString()->links() }}
        </div>
        @endif
    </x-card>
</div>
@endsection
