@extends('layouts.admin')

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

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

@section('content')
<div class="max-w-6xl mx-auto">
    <form method="POST" action="{{ route('admin.customers.store') }}" enctype="multipart/form-data" class="space-y-6">
        @csrf
        
        <!-- Basic Details Section -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-lg font-bold text-gray-900 text-gray-900 mb-6">Basic Details</h3>
            
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                <!-- Image Upload -->
                <div class="md:col-span-1">
                    <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">Customer Image</label>
                    <div class="flex flex-col items-start gap-3">
                        <div class="size-24 rounded-lg bg-gray-100 flex items-center justify-center border-2 border-dashed border-gray-300">
                            <span class="material-symbols-outlined text-gray-400 text-4xl">person</span>
                        </div>
                        <button type="button" onclick="document.getElementById('customer_image').click()" class="flex items-center gap-2 px-4 py-2 bg-primary text-white rounded-lg text-sm font-medium hover:bg-blue-600 transition-colors">
                            <span class="material-symbols-outlined text-lg">cloud_upload</span>
                            Upload Image
                        </button>
                        <input type="file" id="customer_image" name="image" accept="image/jpeg,image/png" class="hidden" onchange="previewImage(this)">
                        <p class="text-xs text-gray-500">JPG or PNG, max 5MB</p>
                    </div>
                </div>
                
                <!-- Form Fields -->
                <div class="md:col-span-2 grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">Name <span class="text-red-500">*</span></label>
                        <input type="text" name="name" value="{{ old('name') }}" required placeholder="Enter Name" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                        @error('name')
                            <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                        @enderror
                    </div>
                    
                    <div>
                        <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">Email <span class="text-red-500">*</span></label>
                        <input type="email" name="email" value="{{ old('email') }}" required placeholder="Enter Email Address" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                        @error('email')
                            <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                        @enderror
                    </div>
                    
                    <div>
                        <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">Phone Number <span class="text-red-500">*</span></label>
                        <input type="text" name="phone" value="{{ old('phone') }}" required placeholder="Enter Phone Number" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                        @error('phone')
                            <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                        @enderror
                    </div>
                    
                    <div>
                        <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">Company Name</label>
                        <input type="text" name="company" value="{{ old('company') }}" placeholder="Enter Company Name" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">Website</label>
                        <input type="url" name="website" value="{{ old('website') }}" placeholder="Enter Website" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                    </div>
                    
                    <div class="md:col-span-2">
                        <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">Notes</label>
                        <textarea name="notes" rows="3" placeholder="Enter Notes" class="w-full px-3 py-2 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm resize-y">{{ old('notes') }}</textarea>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Address Sections -->
        <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
            <!-- Billing Address -->
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
                <h3 class="text-lg font-bold text-gray-900 text-gray-900 mb-6">Billing Address</h3>
                
                <div class="space-y-4">
                    <div>
                        <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">Name <span class="text-red-500">*</span></label>
                        <input type="text" name="billing_name" value="{{ old('billing_name') }}" required placeholder="Enter Name" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">Address Line 1 <span class="text-red-500">*</span></label>
                        <input type="text" name="billing_address_line1" value="{{ old('billing_address_line1') }}" required placeholder="Enter Address Line 1" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">Address Line 2</label>
                        <input type="text" name="billing_address_line2" value="{{ old('billing_address_line2') }}" placeholder="Enter Address Line 2" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                    </div>
                    
                    <div class="grid grid-cols-3 gap-4">
                        <div>
                            <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">Country <span class="text-red-500">*</span></label>
                            <input type="text" name="billing_country" value="{{ old('billing_country') }}" required placeholder="Enter Country" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">State <span class="text-red-500">*</span></label>
                            <input type="text" name="billing_state" value="{{ old('billing_state') }}" required placeholder="Enter State" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">Pincode <span class="text-red-500">*</span></label>
                            <input type="text" name="billing_pincode" value="{{ old('billing_pincode') }}" required placeholder="Enter Pincode" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                        </div>
                    </div>
                    
                    <div>
                        <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">City <span class="text-red-500">*</span></label>
                        <input type="text" name="billing_city" value="{{ old('billing_city') }}" required placeholder="Enter City" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                    </div>
                </div>
            </div>
            
            <!-- Shipping Address -->
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
                <div class="flex items-center justify-between mb-6">
                    <h3 class="text-lg font-bold text-gray-900 text-gray-900">Shipping Address</h3>
                    <button type="button" onclick="copyBillingToShipping()" class="flex items-center gap-1 text-sm font-medium text-primary hover:underline">
                        <span class="material-symbols-outlined text-base">content_copy</span>
                        Copy From Billing
                    </button>
                </div>
                
                <div class="space-y-4">
                    <div>
                        <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">Name <span class="text-red-500">*</span></label>
                        <input type="text" name="shipping_name" value="{{ old('shipping_name') }}" required placeholder="Enter Name" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">Address Line 1 <span class="text-red-500">*</span></label>
                        <input type="text" name="shipping_address_line1" value="{{ old('shipping_address_line1') }}" required placeholder="Enter Address Line 1" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">Address Line 2</label>
                        <input type="text" name="shipping_address_line2" value="{{ old('shipping_address_line2') }}" placeholder="Enter Address Line 2" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                    </div>
                    
                    <div class="grid grid-cols-3 gap-4">
                        <div>
                            <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">Country <span class="text-red-500">*</span></label>
                            <input type="text" name="shipping_country" value="{{ old('shipping_country') }}" required placeholder="Enter Country" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">State <span class="text-red-500">*</span></label>
                            <input type="text" name="shipping_state" value="{{ old('shipping_state') }}" required placeholder="Enter State" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">Pincode <span class="text-red-500">*</span></label>
                            <input type="text" name="shipping_pincode" value="{{ old('shipping_pincode') }}" required placeholder="Enter Pincode" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                        </div>
                    </div>
                    
                    <div>
                        <label class="block text-sm font-semibold text-gray-900 text-gray-900 mb-2">City <span class="text-red-500">*</span></label>
                        <input type="text" name="shipping_city" value="{{ old('shipping_city') }}" required placeholder="Enter City" class="w-full h-10 px-3 rounded-lg border border-gray-200 bg-white text-gray-900 text-gray-900 focus:ring-1 focus:ring-primary focus:border-primary text-sm">
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Form Actions -->
        <div class="flex justify-end gap-3">
            <a href="{{ route('admin.customers.index') }}" class="px-6 py-2 border border-gray-200 text-gray-700 rounded-lg hover:bg-gray-50800 transition-colors">Cancel</a>
            <button type="submit" class="px-6 py-2 bg-primary text-white rounded-lg hover:bg-blue-600 transition-colors font-medium">Create Customer</button>
        </div>
    </form>
</div>

<script>
function previewImage(input) {
    if (input.files && input.files[0]) {
        const reader = new FileReader();
        reader.onload = function(e) {
            const preview = input.previousElementSibling.previousElementSibling;
            preview.innerHTML = `<img src="${e.target.result}" class="w-full h-full object-cover rounded-lg">`;
        };
        reader.readAsDataURL(input.files[0]);
    }
}

function copyBillingToShipping() {
    const billingFields = ['name', 'address_line1', 'address_line2', 'country', 'state', 'pincode', 'city'];
    billingFields.forEach(field => {
        const billingValue = document.querySelector(`input[name="billing_${field}"]`).value;
        document.querySelector(`input[name="shipping_${field}"]`).value = billingValue;
    });
}
</script>
@endsection

