@extends('layouts.admin')

@section('title', 'Edit Supplier')

@section('page-title', 'Edit Supplier')

@section('breadcrumbs')
    <a href="{{ route('admin.suppliers.index') }}" class="hover:text-primary">Suppliers</a>
    <span class="material-symbols-outlined text-xs">chevron_right</span>
    <span class="text-primary font-medium">Edit Supplier</span>
@endsection

@section('content')
<div class="max-w-[1400px] mx-auto">
    <form method="POST" action="{{ route('admin.suppliers.update', $supplier) }}" class="bg-white rounded-xl shadow-sm border border-gray-200 p-6" enctype="multipart/form-data" id="edit-supplier-form">
        @csrf
        @method('PUT')
        
        <div class="mb-6">
            <h2 class="text-2xl font-bold text-gray-900">Edit Supplier</h2>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
            <!-- Image Upload Section -->
            <div class="md:col-span-1">
                <label class="block text-sm font-medium text-gray-700 mb-2">Image</label>
                <div class="space-y-3">
                    <div class="w-32 h-32 rounded-lg bg-gray-100 border-2 border-dashed border-gray-300 flex items-center justify-center overflow-hidden" id="image-preview-container">
                        @if($supplier->image_url)
                            <img id="image-preview" src="{{ $supplier->image_url }}" alt="Preview" class="w-full h-full object-cover">
                            <span class="material-symbols-outlined text-4xl text-gray-400 hidden" id="image-placeholder">image</span>
                        @else
                            <span class="material-symbols-outlined text-4xl text-gray-400" id="image-placeholder">image</span>
                            <img id="image-preview" src="" alt="Preview" class="hidden w-full h-full object-cover">
                        @endif
                    </div>
                    <button type="button" onclick="document.getElementById('supplier-image').click()" class="w-full flex items-center justify-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="supplier-image" name="image" accept="image/jpeg,image/png,image/jpg" class="hidden" onchange="previewImage(this)">
                    <p class="text-xs text-gray-500 text-center">JPG or PNG format, not exceeding 5MB.</p>
                </div>
            </div>

            <!-- Form Fields -->
            <div class="md:col-span-2 space-y-4">
                <!-- Name -->
                <div>
                    <label for="supplier-name" class="block text-sm font-medium text-gray-700 mb-2">Name <span class="text-red-500">*</span></label>
                    <input type="text" id="supplier-name" name="name" value="{{ old('name', $supplier->name) }}" required class="w-full h-11 px-4 rounded-lg border border-gray-300 bg-white text-gray-900 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-shadow" placeholder="Enter Name">
                    @error('name')
                        <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                    @enderror
                </div>

                <!-- Email -->
                <div>
                    <label for="supplier-email" class="block text-sm font-medium text-gray-700 mb-2">Email</label>
                    <input type="email" id="supplier-email" name="email" value="{{ old('email', $supplier->email) }}" class="w-full h-11 px-4 rounded-lg border border-gray-300 bg-white text-gray-900 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-shadow" placeholder="Enter Email Address">
                    @error('email')
                        <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                    @enderror
                </div>

                <!-- Phone Number -->
                <div>
                    <label for="supplier-phone" class="block text-sm font-medium text-gray-700 mb-2">Phone Number <span class="text-red-500">*</span></label>
                    <input type="text" id="supplier-phone" name="phone" value="{{ old('phone', $supplier->phone) }}" required class="w-full h-11 px-4 rounded-lg border border-gray-300 bg-white text-gray-900 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-shadow" placeholder="Enter Phone Number">
                    @error('phone')
                        <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                    @enderror
                </div>

                <!-- Balance -->
                <div>
                    <label for="supplier-balance" class="block text-sm font-medium text-gray-700 mb-2">Balance</label>
                    <input type="number" id="supplier-balance" name="balance" value="{{ old('balance', $supplier->balance) }}" step="0.01" min="0" class="w-full h-11 px-4 rounded-lg border border-gray-300 bg-white text-gray-900 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-shadow">
                    @error('balance')
                        <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                    @enderror
                </div>

                <!-- Mode -->
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Mode</label>
                    <div class="flex gap-4">
                        <label class="flex items-center gap-2 cursor-pointer">
                            <input type="radio" name="mode" value="credit" {{ old('mode', $supplier->mode) == 'credit' ? 'checked' : '' }} class="text-primary focus:ring-primary">
                            <span class="text-sm text-gray-700">Credit</span>
                        </label>
                        <label class="flex items-center gap-2 cursor-pointer">
                            <input type="radio" name="mode" value="debit" {{ old('mode', $supplier->mode) == 'debit' ? 'checked' : '' }} class="text-primary focus:ring-primary">
                            <span class="text-sm text-gray-700">Debit</span>
                        </label>
                    </div>
                    @error('mode')
                        <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                    @enderror
                </div>

                <!-- Address -->
                <div>
                    <label for="supplier-address" class="block text-sm font-medium text-gray-700 mb-2">Address</label>
                    <textarea id="supplier-address" name="address" rows="2" class="w-full px-4 py-3 rounded-lg border border-gray-300 bg-white text-gray-900 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-shadow resize-none" placeholder="Enter Address">{{ old('address', $supplier->address) }}</textarea>
                    @error('address')
                        <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                    @enderror
                </div>

                <!-- City, State, Zip -->
                <div class="grid grid-cols-3 gap-4">
                    <div>
                        <label for="supplier-city" class="block text-sm font-medium text-gray-700 mb-2">City</label>
                        <input type="text" id="supplier-city" name="city" value="{{ old('city', $supplier->city) }}" class="w-full h-11 px-4 rounded-lg border border-gray-300 bg-white text-gray-900 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-shadow" placeholder="City">
                        @error('city')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>
                    <div>
                        <label for="supplier-state" class="block text-sm font-medium text-gray-700 mb-2">State</label>
                        <input type="text" id="supplier-state" name="state" value="{{ old('state', $supplier->state) }}" class="w-full h-11 px-4 rounded-lg border border-gray-300 bg-white text-gray-900 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-shadow" placeholder="State">
                        @error('state')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>
                    <div>
                        <label for="supplier-zip" class="block text-sm font-medium text-gray-700 mb-2">Zip</label>
                        <input type="text" id="supplier-zip" name="zip" value="{{ old('zip', $supplier->zip) }}" class="w-full h-11 px-4 rounded-lg border border-gray-300 bg-white text-gray-900 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-shadow" placeholder="Zip">
                        @error('zip')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>
                </div>

                <!-- Country, Tax ID -->
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label for="supplier-country" class="block text-sm font-medium text-gray-700 mb-2">Country</label>
                        <input type="text" id="supplier-country" name="country" value="{{ old('country', $supplier->country) }}" class="w-full h-11 px-4 rounded-lg border border-gray-300 bg-white text-gray-900 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-shadow" placeholder="Country">
                        @error('country')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>
                    <div>
                        <label for="supplier-tax-id" class="block text-sm font-medium text-gray-700 mb-2">Tax ID</label>
                        <input type="text" id="supplier-tax-id" name="tax_id" value="{{ old('tax_id', $supplier->tax_id) }}" class="w-full h-11 px-4 rounded-lg border border-gray-300 bg-white text-gray-900 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-shadow" placeholder="Tax ID">
                        @error('tax_id')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>
                </div>

                <!-- Notes -->
                <div>
                    <label for="supplier-notes" class="block text-sm font-medium text-gray-700 mb-2">Notes</label>
                    <textarea id="supplier-notes" name="notes" rows="3" class="w-full px-4 py-3 rounded-lg border border-gray-300 bg-white text-gray-900 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-shadow resize-none" placeholder="Enter Notes">{{ old('notes', $supplier->notes) }}</textarea>
                    @error('notes')
                        <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                    @enderror
                </div>
            </div>
        </div>

        <!-- Action Buttons -->
        <div class="flex items-center justify-end gap-3 pt-6 mt-6 border-t border-gray-200">
            <a href="{{ route('admin.suppliers.index') }}" class="px-5 py-2.5 rounded-lg border border-gray-300 text-gray-700 font-medium hover:bg-gray-50 transition-colors">
                Cancel
            </a>
            <button type="submit" class="px-5 py-2.5 rounded-lg bg-primary text-white font-medium hover:bg-blue-600 transition-colors flex items-center gap-2">
                <span class="material-symbols-outlined text-lg">save</span>
                Update Supplier
            </button>
        </div>
    </form>
</div>

@push('scripts')
<script>
function previewImage(input) {
    if (input.files && input.files[0]) {
        const reader = new FileReader();
        reader.onload = function(e) {
            document.getElementById('image-preview').src = e.target.result;
            document.getElementById('image-preview').classList.remove('hidden');
            document.getElementById('image-placeholder').classList.add('hidden');
        };
        reader.readAsDataURL(input.files[0]);
    }
}
</script>
@endpush
@endsection
