@extends('layouts.admin')

@section('title', $division->exists ? 'Edit Division' : 'Add Division')

@section('content')
<div class="container mx-auto max-w-3xl">
    <div class="flex items-center gap-4 mb-6">
        <a href="{{ route('admin.divisions.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">{{ $division->exists ? 'Edit' : 'Add' }} Division</h1>
    </div>

    <div class="bg-white shadow-sm rounded-xl border border-slate-100 p-8">
        <form action="{{ $division->exists ? route('admin.divisions.update', $division) : route('admin.divisions.store') }}" method="POST" enctype="multipart/form-data">
            @csrf
            @if($division->exists)
                @method('PUT')
            @endif

            <div class="space-y-6">
                <div>
                    <label class="block text-slate-700 text-sm font-bold mb-2" for="title">Title <span class="text-red-500">*</span></label>
                    <input class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-lg focus:ring-2 focus:ring-brand-primary focus:border-transparent outline-none transition" 
                           id="title" name="title" type="text" placeholder="e.g. Technology Solutions" required
                           value="{{ old('title', $division->title) }}">
                </div>

                <div>
                    <label class="block text-slate-700 text-sm font-bold mb-2">Description <span class="text-red-500">*</span></label>
                    <x-quill-editor name="description" :value="$division->description" placeholder="Brief outline of this division..." />
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 items-end">
                    <div>
                        <x-media-picker 
                            name="icon_path" 
                            label="Icon SVG (From Library)" 
                            :value="null" 
                        />
                        <div class="p-3 bg-slate-50 rounded-xl border border-slate-100 mt-3">
                            <label class="block text-[10px] font-bold text-slate-400 uppercase tracking-wider mb-2">Or Direct Upload</label>
                            <input class="w-full text-xs text-slate-500 file:mr-4 file:py-1.5 file:px-3 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" 
                                   id="icon_file" name="icon_file" type="file" accept=".svg">
                        </div>
                    </div>

                    @if($division->icon)
                    <div class="flex flex-col items-center justify-center p-6 bg-slate-50 border border-dashed border-slate-200 rounded-xl h-[120px]">
                        <p class="text-[10px] font-bold text-slate-400 uppercase tracking-wider mb-2">Current Icon</p>
                        <div class="w-12 h-12 text-brand-primary flex items-center justify-center">
                            {!! App\Helpers\SvgHelper::sanitize($division->icon) !!}
                        </div>
                    </div>
                    @endif
                </div>

                <div x-data="{ showManual: {{ $errors->has('icon') || (!$division->icon && !old('icon_file')) ? 'true' : 'false' }} }">
                    <button type="button" @click="showManual = !showManual" class="text-xs font-bold text-brand-primary hover:underline mb-2 flex items-center gap-1">
                        <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 20l4-16m4 4l4 4m0 0l-4 4m4-4H4"></path></svg>
                        <span x-text="showManual ? 'Hide manual SVG code entry' : 'Show manual SVG code entry (Advanced)'"></span>
                    </button>
                    
                    <div x-show="showManual" x-transition>
                        <label class="block text-slate-700 text-sm font-bold mb-2" for="icon">Icon SVG Path (Internal Content)</label>
                        <textarea class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-lg focus:ring-2 focus:ring-brand-primary focus:border-transparent outline-none transition font-mono text-sm" 
                                  id="icon" name="icon" rows="3" placeholder='<path d="..."></path>'>{{ old('icon', $division->icon) }}</textarea>
                        @error('icon')
                            <p class="text-red-500 text-xs mt-1">{{ $message }}</p>
                        @enderror
                    </div>
                </div>

                <div>
                    <label class="block text-slate-700 text-sm font-bold mb-2" for="services">Services <span class="text-red-500">*</span></label>
                    <input class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-lg focus:ring-2 focus:ring-brand-primary focus:border-transparent outline-none transition" 
                           id="services" name="services" type="text" placeholder="Service 1, Service 2, Service 3" required
                           value="{{ old('services', $division->services) }}">
                    <p class="text-xs text-slate-500 mt-2">Comma-separated list of services offered by this division.</p>
                </div>
            </div>

            <div class="mt-8 pt-6 border-t border-slate-100 flex items-center justify-end gap-3">
                <a href="{{ route('admin.divisions.index') }}" class="px-6 py-3 font-bold text-sm text-slate-500 hover:text-slate-800 hover:bg-slate-100 rounded-lg transition">Cancel</a>
                <button type="submit" class="bg-brand-primary hover:bg-brand-accent text-white font-bold py-3 px-8 rounded-lg shadow-lg shadow-brand-primary/30 transition flex items-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>
                    {{ $division->exists ? 'Update Division' : 'Create Division' }}
                </button>
            </div>
        </form>
    </div>
</div>
@endsection
