@extends('layouts.admin')

@section('content')
<div class="max-w-6xl mx-auto py-8 px-4">

    <div class="flex items-center justify-between mb-6">
        <h1 class="font-['Sora'] text-2xl font-semibold text-[#5E35B1]">Customers</h1>
        @if(Auth::guard('admin')->user()->hasPermission('customers.create'))
        <a href="{{ route('customers.create') }}"
           class="bg-[#C2185B] text-white px-4 py-2 rounded-lg font-medium hover:bg-[#a3134b] transition">
            + Add Customer
        </a>
        @endif
    </div>
@include('partials.master-tabs')
    @if(session('status'))
    <div class="mb-4 bg-[#00897B]/10 border border-[#00897B] text-[#00897B] px-4 py-3 rounded-lg">
        {{ session('status') }}
    </div>
    @endif

    @if($errors->any())
    <div class="mb-4 bg-red-50 border border-red-400 text-red-700 px-4 py-3 rounded-lg">
        {{ $errors->first() }}
    </div>
    @endif

    <form method="GET" class="mb-6">
        <input type="text" name="q" value="{{ $q }}" placeholder="Search by name or phone..."
               class="w-full max-w-md border border-[#5E35B1]/20 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-[#5E35B1]/40">
    </form>

    <div class="bg-white rounded-xl shadow-sm border border-[#5E35B1]/10 overflow-hidden">
        <table class="w-full text-left">
            <thead class="bg-[#FFF9F0] border-b-2 border-dashed border-[#F57C00]/40">
                <tr class="font-['Sora'] text-sm text-[#5E35B1]">
                    <th class="px-4 py-3">Name</th>
                    <th class="px-4 py-3">Phone</th>
                    <th class="px-4 py-3">Address</th>
                    <th class="px-4 py-3 text-center">Orders</th>
                    <th class="px-4 py-3 text-right">Total Spent</th>
                    <th class="px-4 py-3"></th>
                </tr>
            </thead>
            <tbody>
                @forelse($customers as $customer)
                <tr class="border-b border-dashed border-[#5E35B1]/10 hover:bg-[#FFF9F0]/60">
                    <td class="px-4 py-3 font-medium text-gray-800">{{ $customer->name }}</td>
                    <td class="px-4 py-3 text-gray-600">{{ $customer->phone ?? '—' }}</td>
                    <td class="px-4 py-3 text-gray-600 truncate max-w-xs">{{ $customer->address ?? '—' }}</td>
                    <td class="px-4 py-3 text-center text-gray-600">{{ $customer->orders_count }}</td>
                    <td class="px-4 py-3 text-right text-gray-800">
                        ₹{{ number_format($customer->orders_sum_total_amount ?? 0, 0) }}
                    </td>
                    <td class="px-4 py-3 text-right whitespace-nowrap">
                        @if(Auth::guard('admin')->user()->hasPermission('customers.edit'))
                        <a href="{{ route('customers.edit', $customer) }}" class="text-[#00897B] hover:underline text-sm font-medium">Edit</a>
                        @endif
                        @if(Auth::guard('admin')->user()->hasPermission('customers.delete'))
                        <form action="{{ route('customers.destroy', $customer) }}" method="POST" class="inline"
                              onsubmit="return confirm('Delete {{ $customer->name }}?');">
                            @csrf @method('DELETE')
                            <button class="text-red-500 hover:underline text-sm font-medium ml-3">Delete</button>
                        </form>
                        @endif
                    </td>
                </tr>
                @empty
                <tr><td colspan="6" class="px-4 py-8 text-center text-gray-400">No customers found.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="mt-4">{{ $customers->links() }}</div>
</div>
@endsection

