@extends('layouts.app')

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

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

    <div class="mb-6">
        <a href="{{ route('customers.index') }}" class="text-sm text-gray-500 hover:text-gray-700">← Customers</a>
        <h1 class="mt-2 text-2xl font-bold text-gray-900">Add Customer</h1>
    </div>

    @if($errors->any())
        <div class="mb-4 rounded-lg border border-red-200 bg-red-50 p-4 text-sm text-red-800">
            <ul class="list-inside list-disc space-y-1">
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <form action="{{ route('customers.store') }}" method="POST"
          class="space-y-6 rounded-lg border border-gray-200 bg-white p-6 shadow-sm">
        @csrf

        <div class="grid grid-cols-1 gap-4 sm:grid-cols-2">

            {{-- Name --}}
            <div>
                <label class="mb-1 block text-sm font-medium text-gray-700">
                    Full Name <span class="text-red-500">*</span>
                </label>
                <input type="text" name="name" value="{{ old('name') }}" required
                       class="block w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-green-500 focus:outline-none focus:ring-1 focus:ring-green-500">
            </div>

            {{-- Nickname --}}
            <div>
                <label class="mb-1 block text-sm font-medium text-gray-700">Nickname</label>
                <input type="text" name="nickname" value="{{ old('nickname') }}"
                       class="block w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-green-500 focus:outline-none focus:ring-1 focus:ring-green-500">
            </div>

            {{-- Business Name --}}
            <div class="sm:col-span-2">
                <label class="mb-1 block text-sm font-medium text-gray-700">Business / Company Name</label>
                <input type="text" name="business_name" value="{{ old('business_name') }}"
                       class="block w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-green-500 focus:outline-none focus:ring-1 focus:ring-green-500">
            </div>

            {{-- Phone + WhatsApp --}}
            <div class="sm:col-span-2"
                 x-data="{
                     phone: '{{ old('phone') }}',
                     sameAsPhone: {{ old('phone_is_whatsapp') ? 'true' : 'false' }}
                 }">
                <div class="grid grid-cols-1 gap-4 sm:grid-cols-2">
                    <div>
                        <label class="mb-1 block text-sm font-medium text-gray-700">Phone Number</label>
                        <input type="text" name="phone" x-model="phone" value="{{ old('phone') }}"
                               class="block w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-green-500 focus:outline-none focus:ring-1 focus:ring-green-500">
                        <label class="mt-2 flex cursor-pointer items-center gap-2 text-xs text-gray-500">
                            <input type="checkbox" name="phone_is_whatsapp" value="1" x-model="sameAsPhone"
                                   {{ old('phone_is_whatsapp') ? 'checked' : '' }}
                                   class="rounded border-gray-300 text-green-600">
                            Same number is on WhatsApp
                        </label>
                    </div>
                    <div x-show="!sameAsPhone" @if(old('phone_is_whatsapp')) style="display:none" @endif>
                        <label class="mb-1 block text-sm font-medium text-gray-700">WhatsApp Number</label>
                        <input type="text" name="whatsapp"
                               value="{{ old('whatsapp') }}"
                               :disabled="sameAsPhone"
                               class="block w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-green-500 focus:outline-none focus:ring-1 focus:ring-green-500">
                    </div>
                </div>
                {{-- Hidden: submits phone value when sameAsPhone=true --}}
                <input type="hidden" name="whatsapp" :value="phone" :disabled="!sameAsPhone" disabled>
            </div>

            {{-- Email --}}
            <div>
                <label class="mb-1 block text-sm font-medium text-gray-700">Email</label>
                <input type="email" name="email" value="{{ old('email') }}"
                       class="block w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-green-500 focus:outline-none focus:ring-1 focus:ring-green-500">
            </div>

            {{-- Address --}}
            <div class="sm:col-span-2">
                <label class="mb-1 block text-sm font-medium text-gray-700">Address</label>
                <textarea name="address" rows="2"
                          class="block w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-green-500 focus:outline-none focus:ring-1 focus:ring-green-500">{{ old('address') }}</textarea>
            </div>

            {{-- Landmark --}}
            <div class="sm:col-span-2">
                <label class="mb-1 block text-sm font-medium text-gray-700">Landmark</label>
                <input type="text" name="landmark" value="{{ old('landmark') }}"
                       class="block w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-green-500 focus:outline-none focus:ring-1 focus:ring-green-500">
            </div>

            {{-- Notes --}}
            <div class="sm:col-span-2">
                <label class="mb-1 block text-sm font-medium text-gray-700">Notes</label>
                <textarea name="notes" rows="3"
                          placeholder="Any notes about this customer…"
                          class="block w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-green-500 focus:outline-none focus:ring-1 focus:ring-green-500">{{ old('notes') }}</textarea>
            </div>

        </div>

        <div class="flex items-center justify-end gap-3 border-t border-gray-100 pt-4">
            <a href="{{ route('customers.index') }}"
               class="rounded-lg border border-gray-300 px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50">
                Cancel
            </a>
            <button type="submit"
                    class="rounded-lg bg-green-600 px-4 py-2 text-sm font-medium text-white hover:bg-green-700">
                Save Customer
            </button>
        </div>

    </form>

</div>
@endsection
