@extends('dashboard.app')

@php
    $isEdit = ($mode ?? 'create') === 'edit';
    $formAction = $isEdit
        ? route('dashboard.admin.catalog.update', data_get($selected, 'emoji_id'))
        : route('dashboard.admin.catalog.store');
    $textareas = [
        'aliases' => data_get($selected, 'aliases', []),
        'shortcodes' => data_get($selected, 'shortcodes', []),
        'alt_shortcodes' => data_get($selected, 'alt_shortcodes', []),
        'keywords_en' => data_get($selected, 'keywords_en', []),
        'keywords_id' => data_get($selected, 'keywords_id', []),
    ];
@endphp

@section('page_title', $isEdit ? 'Edit Emoji' : 'Create Emoji')
@section('page_subtitle', 'Edit/add emoji data in database. Publish frozen JSON from Catalog page after batch updates.')

@section('dashboard_content')
    @if (session('status'))
        <div class="mb-6 rounded-2xl border border-emerald-200 bg-emerald-50 px-4 py-3 text-sm text-emerald-700 dark:border-emerald-500/30 dark:bg-emerald-500/15 dark:text-emerald-200">
            {{ session('status') }}
        </div>
    @endif

    @if (session('error'))
        <div class="mb-6 rounded-2xl border border-red-300/40 bg-red-500/10 px-4 py-3 text-sm text-red-700 dark:text-red-200">
            {{ session('error') }}
        </div>
    @endif

    @if ($errors->any())
        <div class="mb-6 rounded-2xl border border-amber-300/40 bg-amber-400/10 px-4 py-3 text-sm text-amber-700 dark:text-amber-200">
            {{ $errors->first() }}
        </div>
    @endif

    <div class="rounded-2xl glass-card p-5">
        <form method="POST" action="{{ $formAction }}" class="grid gap-4">
            @csrf
            @if ($isEdit)
                @method('PUT')
            @endif

            <div class="grid gap-4 md:grid-cols-3">
                <label class="text-sm text-slate-700 dark:text-gray-300">
                    Emoji
                    <input name="emoji" value="{{ old('emoji', data_get($selected, 'emoji', '')) }}" class="mt-2 w-full rounded-xl border border-slate-200 dark:border-slate-700 px-3 py-2 text-slate-900 dark:text-slate-100 theme-surface bg-white dark:bg-slate-900 placeholder:text-slate-400 dark:placeholder:text-slate-500">
                </label>
                <label class="text-sm text-slate-700 dark:text-gray-300 md:col-span-2">
                    Slug
                    <input name="slug" value="{{ old('slug', data_get($selected, 'slug', '')) }}" required class="mt-2 w-full rounded-xl border border-slate-200 dark:border-slate-700 px-3 py-2 text-slate-900 dark:text-slate-100 theme-surface bg-white dark:bg-slate-900 placeholder:text-slate-400 dark:placeholder:text-slate-500">
                </label>
            </div>

            <div class="grid gap-4 md:grid-cols-2">
                <label class="text-sm text-slate-700 dark:text-gray-300">
                    Name
                    <input name="name" value="{{ old('name', data_get($selected, 'name', '')) }}" required class="mt-2 w-full rounded-xl border border-slate-200 dark:border-slate-700 px-3 py-2 text-slate-900 dark:text-slate-100 theme-surface bg-white dark:bg-slate-900 placeholder:text-slate-400 dark:placeholder:text-slate-500">
                </label>
                <label class="text-sm text-slate-700 dark:text-gray-300">
                    Category
                    <input name="category" value="{{ old('category', data_get($selected, 'category', '')) }}" class="mt-2 w-full rounded-xl border border-slate-200 dark:border-slate-700 px-3 py-2 text-slate-900 dark:text-slate-100 theme-surface bg-white dark:bg-slate-900 placeholder:text-slate-400 dark:placeholder:text-slate-500">
                </label>
                <label class="text-sm text-slate-700 dark:text-gray-300">
                    Subcategory
                    <input name="subcategory" value="{{ old('subcategory', data_get($selected, 'subcategory', '')) }}" class="mt-2 w-full rounded-xl border border-slate-200 dark:border-slate-700 px-3 py-2 text-slate-900 dark:text-slate-100 theme-surface bg-white dark:bg-slate-900 placeholder:text-slate-400 dark:placeholder:text-slate-500">
                </label>
                <label class="text-sm text-slate-700 dark:text-gray-300">
                    Unified
                    <input name="unified" value="{{ old('unified', data_get($selected, 'unified', '')) }}" class="mt-2 w-full rounded-xl border border-slate-200 dark:border-slate-700 px-3 py-2 text-slate-900 dark:text-slate-100 theme-surface bg-white dark:bg-slate-900 placeholder:text-slate-400 dark:placeholder:text-slate-500">
                </label>
                <label class="text-sm text-slate-700 dark:text-gray-300">
                    Version
                    <input name="version" value="{{ old('version', data_get($selected, 'version', '')) }}" class="mt-2 w-full rounded-xl border border-slate-200 dark:border-slate-700 px-3 py-2 text-slate-900 dark:text-slate-100 theme-surface bg-white dark:bg-slate-900 placeholder:text-slate-400 dark:placeholder:text-slate-500">
                </label>
            </div>

            <div class="grid gap-4 md:grid-cols-2">
                @foreach ($textareas as $field => $values)
                    <label class="text-sm text-slate-700 dark:text-gray-300">
                        {{ strtoupper(str_replace('_', ' ', $field)) }} (comma or line-separated)
                        <textarea name="{{ $field }}" rows="3" class="mt-2 w-full rounded-xl border border-slate-200 dark:border-slate-700 px-3 py-2 text-slate-900 dark:text-slate-100 theme-surface bg-white dark:bg-slate-900 placeholder:text-slate-400 dark:placeholder:text-slate-500">{{ old($field, is_array($values) ? implode(PHP_EOL, $values) : '') }}</textarea>
                    </label>
                @endforeach
            </div>

            <label class="text-sm text-slate-700 dark:text-gray-300">
                Description
                <textarea name="description" rows="3" class="mt-2 w-full rounded-xl border border-slate-200 dark:border-slate-700 px-3 py-2 text-slate-900 dark:text-slate-100 theme-surface bg-white dark:bg-slate-900 placeholder:text-slate-400 dark:placeholder:text-slate-500">{{ old('description', data_get($selected, 'description', '')) }}</textarea>
            </label>

            <div class="grid gap-4 md:grid-cols-2">
                <label class="text-sm text-slate-700 dark:text-gray-300">
                    Permalink
                    <input name="permalink" value="{{ old('permalink', data_get($selected, 'permalink', '')) }}" class="mt-2 w-full rounded-xl border border-slate-200 dark:border-slate-700 px-3 py-2 text-slate-900 dark:text-slate-100 theme-surface bg-white dark:bg-slate-900 placeholder:text-slate-400 dark:placeholder:text-slate-500">
                </label>
                <label class="text-sm text-slate-700 dark:text-gray-300">
                    Title
                    <input name="title" value="{{ old('title', data_get($selected, 'title', '')) }}" class="mt-2 w-full rounded-xl border border-slate-200 dark:border-slate-700 px-3 py-2 text-slate-900 dark:text-slate-100 theme-surface bg-white dark:bg-slate-900 placeholder:text-slate-400 dark:placeholder:text-slate-500">
                </label>
                <label class="text-sm text-slate-700 dark:text-gray-300">
                    Meta title
                    <input name="meta_title" value="{{ old('meta_title', data_get($selected, 'meta_title', '')) }}" class="mt-2 w-full rounded-xl border border-slate-200 dark:border-slate-700 px-3 py-2 text-slate-900 dark:text-slate-100 theme-surface bg-white dark:bg-slate-900 placeholder:text-slate-400 dark:placeholder:text-slate-500">
                </label>
                <label class="text-sm text-slate-700 dark:text-gray-300">
                    Meta description
                    <input name="meta_description" value="{{ old('meta_description', data_get($selected, 'meta_description', '')) }}" class="mt-2 w-full rounded-xl border border-slate-200 dark:border-slate-700 px-3 py-2 text-slate-900 dark:text-slate-100 theme-surface bg-white dark:bg-slate-900 placeholder:text-slate-400 dark:placeholder:text-slate-500">
                </label>
            </div>

            <div class="flex flex-wrap items-center justify-between gap-3">
                <label class="inline-flex items-center gap-2 text-sm text-slate-700 dark:text-gray-300">
                    <input type="hidden" name="supports_skin_tone" value="0">
                    <input type="checkbox" name="supports_skin_tone" value="1" @checked(old('supports_skin_tone', data_get($selected, 'supports_skin_tone', false)))>
                    <span>Supports skin tone</span>
                </label>

                <div class="flex items-center gap-2">
                    <a href="{{ route('dashboard.admin.catalog') }}" class="rounded-xl border border-slate-200 dark:border-slate-700 px-4 py-2 text-sm text-slate-700 dark:text-slate-100 hover:bg-slate-50 dark:hover:bg-white/5">Back to catalog</a>
                    <button class="rounded-xl bg-slate-900 dark:bg-white/10 border border-slate-900 dark:border-slate-700 px-4 py-2 text-sm font-semibold text-white force-white hover:opacity-90">
                        {{ $isEdit ? 'Update emoji' : 'Create emoji' }}
                    </button>
                </div>
            </div>
        </form>
    </div>
@endsection
