@extends('layouts.admin')

@section('title', $partner->exists ? 'Edit Partner' : 'Add Partner')

@section('content')
<div class="container mx-auto max-w-2xl">
    <div class="flex items-center gap-4 mb-6">
        <a href="{{ route('admin.partners.index') }}" class="w-10 h-10 rounded-full bg-white border border-slate-200 flex items-center justify-center text-slate-500 hover:text-brand-primary hover:border-brand-primary transition shadow-sm">
            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path></svg>
        </a>
        <h1 class="text-2xl font-bold text-slate-900">{{ $partner->exists ? 'Edit Partner' : 'Add Partner' }}</h1>
    </div>

    <form action="{{ $partner->exists ? route('admin.partners.update', $partner) : route('admin.partners.store') }}" method="POST" enctype="multipart/form-data">
        @csrf
        @if($partner->exists)
            @method('PUT')
        @endif

        <div class="bg-white shadow-sm rounded-2xl border border-slate-100 overflow-hidden">
            <div class="p-8 space-y-6">
                <!-- Name -->
                <div>
                    <label class="block text-slate-700 text-sm font-bold mb-2">Partner Name <span class="text-red-500">*</span></label>
                    <input class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-brand-primary focus:border-transparent outline-none transition @error('name') border-red-500 @enderror" 
                           name="name" type="text" placeholder="e.g. Microsoft" required
                           value="{{ old('name', $partner->name) }}">
                    @error('name')
                        <p class="text-red-500 text-xs mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <!-- Website URL -->
                <div>
                    <label class="block text-slate-700 text-sm font-bold mb-2">Website URL (Optional)</label>
                    <input class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-brand-primary focus:border-transparent outline-none transition @error('website_url') border-red-500 @enderror" 
                           name="website_url" type="url" placeholder="https://example.com"
                           value="{{ old('website_url', $partner->website_url) }}">
                    @error('website_url')
                        <p class="text-red-500 text-xs mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <!-- Partner Logo -->
                <x-media-picker 
                    name="logo_path" 
                    label="Partner Logo" 
                    :value="$partner->logo_path" 
                    :preview="$partner->logo_path ? asset($partner->logo_path) : null" 
                />
                <div class="p-4 bg-slate-50 rounded-2xl border border-slate-100 mt-4">
                    <p class="text-[11px] text-slate-500 leading-relaxed italic">
                        <strong>Direct Upload:</strong> You can also upload a logo directly below.
                    </p>
                    <input class="mt-2 w-full text-xs text-slate-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-xs file:font-bold file:bg-brand-primary/10 file:text-brand-primary hover:file:bg-brand-primary/20 transition cursor-pointer" 
                           name="logo" type="file" accept="image/*">
                </div>
                @error('logo')
                    <p class="text-red-500 text-xs mt-1">{{ $message }}</p>
                @enderror

                <!-- Status Switch -->
                <div class="pt-4 mt-4 border-t border-slate-50">
                    <label class="flex items-center gap-3 cursor-pointer group">
                        <div class="relative">
                            <input type="checkbox" name="is_active" class="sr-only peer" value="1" {{ old('is_active', $partner->exists ? $partner->is_active : true) ? 'checked' : '' }}>
                            <div class="w-11 h-6 bg-slate-200 peer-focus:outline-none peer-focus:ring-2 peer-focus:ring-brand-primary/20 rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-slate-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-brand-primary transition-colors"></div>
                        </div>
                        <span class="text-sm font-semibold text-slate-700 group-hover:text-slate-900 transition-colors">Active (Shown on website)</span>
                    </label>
                </div>
            </div>

            <!-- Footer Buttons -->
            <div class="bg-slate-50/50 p-8 border-t border-slate-50 flex items-center gap-4">
                <button type="submit" class="flex-1 bg-brand-primary hover:bg-brand-accent text-white font-bold py-3 px-4 rounded-xl shadow-md shadow-brand-primary/30 transition flex items-center justify-center gap-2">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg>
                    {{ $partner->exists ? 'Update Partner' : 'Save Partner' }}
                </button>
                <a href="{{ route('admin.partners.index') }}" class="px-6 py-3 text-sm font-bold text-slate-500 hover:text-slate-800 transition">
                    Cancel
                </a>
            </div>
        </div>
    </form>
</div>
@endsection
