@extends('layouts.admin')

@section('title', $post->exists ? 'Edit Post' : 'Add New Post')

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

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

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
            <!-- Main Content Area -->
            <div class="lg:col-span-2 space-y-6">
                <!-- Title -->
                <div class="bg-white shadow-sm rounded-xl border border-slate-100 p-6">
                    <label class="block text-slate-700 text-sm font-bold mb-2">Post 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 @error('title') border-red-500 @enderror" 
                           name="title" type="text" placeholder="e.g. 10 Security Tips for 2026" required
                           value="{{ old('title', $post->title) }}">
                    @error('title')
                        <p class="text-red-500 text-xs mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <!-- Content (WYSIWYG) -->
                <div class="bg-white shadow-sm rounded-xl border border-slate-100 p-6">
                    <label class="block text-slate-700 text-sm font-bold mb-4">Post Content <span class="text-red-500">*</span></label>
                    <x-quill-editor name="content" :value="$post->content" placeholder="Write the main article content here..." />
                </div>
            </div>

            <!-- Sidebar Info -->
            <div class="space-y-6">
                <!-- Publish Status -->
                <div class="bg-white shadow-sm rounded-xl border border-slate-100 p-6">
                    <h3 class="font-bold text-slate-900 mb-4 pb-3 border-b border-slate-100">Publishing</h3>
                    
                    <label class="flex items-center gap-3 cursor-pointer group">
                        <div class="relative">
                            <input type="checkbox" name="is_published" class="sr-only peer" value="1" {{ old('is_published', $post->is_published) ? '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">Publish this post</span>
                    </label>

                    @if($post->exists && $post->published_at)
                    <p class="text-xs text-slate-500 mt-4 pt-4 border-t border-slate-100">
                        Published on:<br>
                        <span class="font-semibold">{{ $post->published_at->format('M j, Y H:i') }}</span>
                    </p>
                    @endif
                </div>

                <!-- Featured Image -->
                <x-media-picker 
                    name="image_path" 
                    label="Featured Image" 
                    :value="$post->image_path" 
                    :preview="$post->image_path ? asset($post->image_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>Note:</strong> You can still upload a direct file below if you prefer, but the library is recommended for reuse.
                    </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="image" type="file" accept="image/*">
                </div>
                @error('image')
                    <p class="text-red-500 text-xs mt-1">{{ $message }}</p>
                @enderror

                <!-- Submit Button -->
                <div class="bg-white shadow-sm rounded-xl border border-slate-100 p-6">
                    <button type="submit" class="w-full 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>
                        {{ $post->exists ? 'Update Post' : 'Save Post' }}
                    </button>
                    <a href="{{ route('admin.posts.index') }}" class="block w-full mt-3 text-center px-4 py-2 text-sm font-bold text-slate-500 hover:text-slate-800 transition">
                        Cancel
                    </a>
                </div>
            </div>
        </div>
    </form>
</div>
@endsection
