@extends('layouts.admin')

@section('content')
@php
    $contentId = old('id', data_get($content, 'id', ''));
    $isEditing = filled($contentId);
@endphp
<div class="space-y-6" x-data="tinyMceEditor">
    <x-admin.card>
        <!-- Header -->
        <div class="px-6 py-4 border-b border-gray-200 dark:border-gray-700">
            <h1 class="text-2xl font-semibold text-gray-800">
                <i class="fas fa-file-alt mr-2"></i>{{ $title }}
            </h1>
        </div>

        <!-- Content Form -->
        <form method="post" action="{{ route('admin.content-add') }}" class="p-6">
            @csrf
            <input type="hidden" name="action" value="submit">
            @if($isEditing)
                <input type="hidden" name="id" value="{{ $contentId }}">
            @endif

            <div class="space-y-6">
                <!-- Title -->
                <div>
                    <label for="title" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                        Title <span class="text-gray-400 dark:text-gray-500">(optional)</span>
                    </label>
                    <input type="text"
                           id="title"
                           name="title"
                           value="{{ old('title', data_get($content, 'title', '')) }}"
                           class="w-full px-3 py-2 border {{ $errors->has('title') ? 'border-red-500' : 'border-gray-300 dark:border-gray-600' }} rounded-md focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:text-gray-100">
                    <p class="mt-1 text-sm text-gray-500 dark:text-gray-400">
                        Leave blank to save content without a page title.
                    </p>
                    @error('title')
                        <p class="mt-1 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                    @enderror
                </div>

                <!-- URL -->
                <div>
                    <label for="url" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                        URL
                    </label>
                    <input type="text"
                           id="url"
                           name="url"
                           value="{{ old('url', data_get($content, 'url', '')) }}"
                           placeholder="/page-slug or https://example.com"
                           class="w-full px-3 py-2 border {{ $errors->has('url') ? 'border-red-500' : 'border-gray-300 dark:border-gray-600' }} rounded-md focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:text-gray-100 dark:placeholder-gray-400">
                    <p class="mt-1 text-sm text-gray-500">Internal URL (e.g., /about) or external URL (e.g., https://example.com)</p>
                    @error('url')
                        <p class="mt-1 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                    @enderror
                </div>

                <!-- Body -->
                <div>
                    <label for="body" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                        Body Content
                    </label>
                    <textarea id="body"
                              name="body"
                              rows="15"
                              data-tinymce-api-key="{{ config('tinymce.api_key', 'no-api-key') }}"
                              class="w-full px-3 py-2 border {{ $errors->has('body') ? 'border-red-500' : 'border-gray-300 dark:border-gray-600' }} rounded-md focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:text-gray-100">{{ old('body', is_string(data_get($content, 'body')) ? trim((string) data_get($content, 'body'), '\'"') : '') }}</textarea>
                    <p class="mt-1 text-sm text-gray-500">Use the rich text editor to format your content</p>
                    @error('body')
                        <p class="mt-1 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                    @enderror
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <!-- Content Type -->
                    <div>
                        <label for="contenttype" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                            Content Type <span class="text-red-500">*</span>
                        </label>
                        <select id="contenttype"
                                name="contenttype"
                                required
                                class="w-full px-3 py-2 border {{ $errors->has('contenttype') ? 'border-red-500' : 'border-gray-300 dark:border-gray-600' }} rounded-md focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:text-gray-100">
                            @foreach($contenttypelist as $typeId => $typeName)
                                <option value="{{ $typeId }}"
                                    {{ (string) old('contenttype', data_get($content, 'contenttype', '')) === (string) $typeId ? 'selected' : '' }}>
                                    {{ $typeName }}
                                </option>
                            @endforeach
                        </select>
                        @error('contenttype')
                            <p class="mt-1 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                        @enderror
                    </div>

                    <!-- Role -->
                    <div>
                        <label for="role" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                            Visible To <span class="text-red-500">*</span>
                        </label>
                        <select id="role"
                                name="role"
                                required
                                class="w-full px-3 py-2 border {{ $errors->has('role') ? 'border-red-500' : 'border-gray-300 dark:border-gray-600' }} rounded-md focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:text-gray-100">
                            @foreach($rolelist as $roleId => $roleName)
                                <option value="{{ $roleId }}"
                                    {{ (string) old('role', data_get($content, 'role', '')) === (string) $roleId ? 'selected' : '' }}>
                                    {{ $roleName }}
                                </option>
                            @endforeach
                        </select>
                        @error('role')
                            <p class="mt-1 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                        @enderror
                    </div>

                    <!-- Status -->
                    <div>
                        <label for="status" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                            Status <span class="text-red-500">*</span>
                        </label>
                        <select id="status"
                                name="status"
                                required
                                class="w-full px-3 py-2 border {{ $errors->has('status') ? 'border-red-500' : 'border-gray-300 dark:border-gray-600' }} rounded-md focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:text-gray-100">
                            @foreach($status_ids as $index => $statusId)
                                <option value="{{ $statusId }}"
                                    {{ (string) old('status', data_get($content, 'status', '')) === (string) $statusId ? 'selected' : '' }}>
                                    {{ $status_names[$index] }}
                                </option>
                            @endforeach
                        </select>
                        @error('status')
                            <p class="mt-1 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                        @enderror
                    </div>

                </div>

                <!-- Meta Description -->
                <div>
                    <label for="metadescription" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                        Meta Description
                    </label>
                    <textarea id="metadescription"
                              name="metadescription"
                              rows="3"
                              class="w-full px-3 py-2 border {{ $errors->has('metadescription') ? 'border-red-500' : 'border-gray-300 dark:border-gray-600' }} rounded-md focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:text-gray-100">{{ old('metadescription', data_get($content, 'metadescription', '')) }}</textarea>
                    <p class="mt-1 text-sm text-gray-500">SEO meta description</p>
                    @error('metadescription')
                        <p class="mt-1 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                    @enderror
                </div>

                <!-- Meta Keywords -->
                <div>
                    <label for="metakeywords" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                        Meta Keywords
                    </label>
                    <input type="text"
                           id="metakeywords"
                           name="metakeywords"
                           value="{{ old('metakeywords', data_get($content, 'metakeywords', '')) }}"
                           class="w-full px-3 py-2 border {{ $errors->has('metakeywords') ? 'border-red-500' : 'border-gray-300 dark:border-gray-600' }} rounded-md focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:text-gray-100">
                    <p class="mt-1 text-sm text-gray-500">Comma-separated keywords for SEO</p>
                    @error('metakeywords')
                        <p class="mt-1 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                    @enderror
                </div>

                <!-- Action Buttons -->
                <div class="flex gap-3 pt-4 border-t border-gray-200">
                    <button type="submit" class="px-6 py-2 bg-blue-600 dark:bg-blue-700 text-white rounded-lg hover:bg-blue-700">
                        <i class="fas fa-save mr-2"></i>{{ $isEditing ? 'Update' : 'Create' }} Content
                    </button>
                    <a href="{{ route('admin.content-list') }}" class="px-6 py-2 bg-gray-200 dark:bg-gray-700 text-gray-700 dark:text-gray-300 rounded-lg hover:bg-gray-300">
                        <i class="fas fa-times mr-2"></i>Cancel
                    </a>
                </div>
            </div>
        </form>
    </x-admin.card>
</div>
@endsection

