@extends('layouts.aura-main')

@section('main-content')
<div class="pb-20">
    <div class="flex justify-between items-center mb-8">
        <div>
            <h1 class="text-2xl font-black text-slate-900 tracking-tight">Customers</h1>
            <p class="text-[11px] font-bold text-indigo-600">Accounts receivable & directory</p>
        </div>
        <div class="flex gap-2">
            <a href="{{ route('customers.export') }}" class="bg-white border border-slate-300 text-slate-600 px-4 py-2 rounded-sm text-[10px] font-black hover:bg-slate-50 transition shadow-sm">
                Export CSV
            </a>
            <a href="{{ route('customers.create') }}" class="bg-indigo-600 text-white px-5 py-2 rounded-sm text-[10px] font-black hover:bg-indigo-700 transition shadow-lg inline-flex items-center gap-2">
                <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M12 4v16m8-8H4"></path></svg>
                Add Customer
            </a>
        </div>
    </div>

    <div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-8">
        <div class="bg-white border border-slate-200 p-5 rounded-sm shadow-sm">
            <p class="text-[9px] font-bold text-slate-400 mb-1">Total directory</p>
            <p class="text-2xl font-black text-slate-900">{{ $customers->count() }}</p>
        </div>
        <div class="bg-white border border-slate-200 p-5 rounded-sm shadow-sm border-l-4 border-l-emerald-500">
            <p class="text-[9px] font-bold text-slate-400 mb-1">Active accounts</p>
            <p class="text-2xl font-black text-emerald-600">{{ $customers->where('status', 'active')->count() }}</p>
        </div>
        <div class="bg-white border border-slate-200 p-5 rounded-sm shadow-sm">
            <p class="text-[9px] font-bold text-slate-400 mb-1">Total debtors</p>
            <p class="text-2xl font-black text-slate-900">
                @php 
                    $totalBalance = $customers->sum(fn($c) => $c->currentBalance());
                @endphp
                K{{ number_format($totalBalance, 2) }}
            </p>
        </div>
    </div>

    <div class="bg-white border border-slate-300 rounded-sm overflow-hidden shadow-sm">
        <table class="w-full text-left border-collapse">
            <thead class="bg-slate-50 border-b border-slate-300 text-[10px] font-bold text-slate-500">
                <tr>
                    <th class="px-6 py-4 border-r w-12 text-center">#</th>
                    <th class="px-6 py-4 border-r">Customer name & TPIN</th>
                    <th class="px-6 py-4 border-r">Contact details</th>
                    <th class="px-6 py-4 border-r text-right">Current balance</th>
                    <th class="px-6 py-4 border-r text-center">Status</th>
                    <th class="px-6 py-4 text-right">Actions</th>
                </tr>
            </thead>
            <tbody class="text-xs font-medium">
                @forelse($customers as $customer)
                <tr class="border-b border-slate-200 hover:bg-slate-50/50 transition group">
                    <td class="px-6 py-4 border-r text-center text-slate-400 font-mono">{{ $loop->iteration }}</td>
                    <td class="px-6 py-4 border-r">
                        <span class="block font-black text-slate-900 tracking-tight group-hover:text-indigo-600 transition">{{ $customer->name }}</span>
                        <span class="text-[10px] text-slate-400 font-bold tracking-tighter italic">
                            TPIN: {{ $customer->tpin ?? 'Not registered' }}
                        </span>
                    </td>
                    <td class="px-6 py-4 border-r">
                        <div class="flex items-center gap-2 mb-1">
                            <svg class="w-3 h-3 text-slate-300" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" stroke-width="2"></path></svg>
                            <span class="text-slate-600">{{ $customer->email ?? 'no-email@aura.com' }}</span>
                        </div>
                        <div class="flex items-center gap-2">
                            <svg class="w-3 h-3 text-slate-300" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z" stroke-width="2"></path></svg>
                            <span class="text-slate-400 font-mono">{{ $customer->phone ?? '---' }}</span>
                        </div>
                    </td>
                    <td class="px-6 py-4 border-r text-right font-mono font-black text-slate-900 bg-slate-50/30">
                        K{{ number_format($customer->currentBalance(), 2) }}
                    </td>
                    <td class="px-6 py-4 text-center border-r">
                        <span class="px-2 py-0.5 rounded-sm text-[8px] font-black border {{ $customer->status == 'active' ? 'bg-emerald-50 text-emerald-600 border-emerald-200' : 'bg-slate-50 text-slate-500 border-slate-200' }}">
                            {{ ucfirst($customer->status) }}
                        </span>
                    </td>
                    <td class="px-6 py-4 text-right">
                        <div class="flex gap-4 justify-end">
                            <a href="{{ route('customers.edit', $customer->id) }}" class="text-indigo-600 hover:text-indigo-900 text-[10px] font-black border-b border-indigo-100 pb-0.5">
                              Edit
                            </a>
                            <a href="{{ route('customers.show', $customer->id) }}" class="text-indigo-600 hover:text-indigo-900 text-[10px] font-black border-b border-indigo-100 pb-0.5">Profile</a>
                            <a href="{{ route('customers.statement', $customer->id) }}" class="text-slate-400 hover:text-slate-900 text-[10px] font-black border-b border-slate-100 pb-0.5">Statement</a>
                        </div>
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="6" class="p-20 text-center text-slate-300 font-black text-xs">No customers found in directory</td>
                </tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>
@endsection