<?php
/** @var \App\Models\Modification $modif */
/** @var \App\Models\Status $status */
?>

{{-- Pushed in header. --}}
@push('styles')
    <meta name="fs-section" content="{{ $section }}">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <meta name="submission-has-errors" content="{{ $errors->any() ? '1' : '0' }}">
@endpush

@push('scripts')
    @vite('resources/js/submissions.js')
@endpush

{{-- Server side errors summary --}}
@if($errors->any())
    @foreach( $errors->all() as $error )
        <x-form-error-text message="{{ $error }}" />
    @endforeach
@endif

<div class="block">
    <form action="{{ $isEdit ? route('submit.update', [ $section, $entry->id ] ) : route('submit.store', $section ) }}"
          method="POST" x-data="Submission()" x-init="init()" @submit.prevent="submitForm($event)">
        @include('submissions.fs-upload')

        <!-- ABOUT THE ENTRY -->
        <x-form-group-title label="{{ $words['about_the'] }}" icon="puzzle" />

        @if( section_must_not_be( ['translations', 'homebrew'], $section ) )
            <div class="form-group">
                <x-form-field-title name="{{ $words['entry_title'] }}" required="true" />
                <input class="form-input" type="text" name="entry_title" value="{{ old('entry_title', $entry->title ?? '' ) }}" required>
                @error('entry_title')
                    <x-form-error-text message="{{ $message }}" />
                @enderror
            </div>
        @elseif( section_must_be( 'translations', $section ) )
            <div class="form-group">
                <x-form-field-title name="{{ $words['entry_title'] }}" helper="{{ $words['entry_title_helper'] }}" />
                <input class="form-input" type="text" name="entry_title" value="{{ old('entry_title', $entry->title, '' ) }}">
                @error('entry_title')
                    <x-form-error-text message="{{ $message }}" />
                @enderror
            </div>
        @endif

        @if( section_must_be( ['romhacks', 'lua-scripts'], $section ) )
            <div class="form-group">
                <x-form-field-title name="{{ $words['type_of_hack'] }}" required="true" />
                <div class="form-type-of-checkboxes form-group level" id="modifications-group" x-ref="modificationsGroup">
                    @foreach( $modifications as $modif )
                        <label><input class="form-checkbox" type="checkbox" name="modifications[]" value="{{ $modif->id }}" {{ in_array($modif->id, $oldModifications) ? 'checked' : '' }}>{{ $modif->name }}</label>
                    @endforeach
                </div>
                <div class="form-error-text" x-show="errorKey === 'noModifications'" x-text="errorMessage"></div>
            </div>
        @elseif( section_must_be( [ 'utilities', 'documents' ], $section ) )
           <div class="form-group">
               <x-form-field-title name="Categories" required="true" />
               <x-category-selector :section="$section" :selected="$oldCategories" />
           </div>
        @endif

        @if( section_must_be( 'utilities', $section ) )
            <div class="form-group">
                <x-form-field-title name="{{ $words['system'] }}" required="true" />
                <div class="form-type-of-checkboxes form-group level" id="systems-group" x-ref="systemsGroup">
                    @foreach( $systems as $system )
                        <label><input class="form-checkbox" type="checkbox" name="systems[]" value="{{ $system->id }}" {{ in_array($system->id, $oldSystems) ? 'checked' : '' }}>{{ $system->name }}</label>
                    @endforeach
                </div>
                <div class="form-error-text" x-show="errorKey === 'noSystems'" x-text="errorMessage"></div>
            </div>
        @endif

        @if( section_must_be( ['romhacks', 'translations', 'homebrew', 'utilities', 'documents', 'lua-scripts'], $section ) )
            <div class="form-group grid-c3">
                <div>
                    <x-form-field-title name="{{ $words['version'] }}" required="true" />
                    <input class="form-input" type="text" name="version" value="{{ old( 'version', $entry->version ?? '' ) }}" required>
                </div>
                <div>
                    <x-form-field-title name="{{ $words['release_date'] }}" helper="{{ $words['release_date_helper'] }}" required="true" />
                    {{-- TODO: Add max to the date --}}
                    <input type="date" class="form-input" name="release-date" value="{{ old('release-date') ?? $entry->release_date?->format('Y-m-d') ?? '' }}" required>
                </div>
                <div>
                    @if( section_must_be( ['utilities', 'documents'], $section ) )
                        <x-form-field-title name="{{ $words['level'] }}" required="true" />
                        <div class="form-status-radio form-group level">
                            @foreach( $levels as $level )
                                <label><input class="form-radio" type="radio" name="level" value="{{ $level->id }}" {{ old('level', $entry->level_id ) == $level->id ? 'checked' : '' }} required>{{ $level->name }}</label>
                            @endforeach
                        </div>
                    @else
                        <x-form-field-title name="{{ $words['status'] }}" required="true" />
                        <div class="form-status-radio form-group level">
                            @foreach( $statuses as $status )
                                <label><input class="form-radio" type="radio" name="status" value="{{ $status->id }}" {{ old('status', $entry->status_id ) == $status->id ? 'checked' : '' }} required>{{ $status->name }}</label>
                            @endforeach
                        </div>
                    @endif
                </div>
            </div>
        @endif

        @if( section_must_be( [ 'translations', 'utilities', 'documents'] , $section ) )
            <x-form-field-title name="Languages" required="true" />
            <x-languages-selector :selected="$oldLanguages" />
             <div class="form-error-text" x-show="errorKey === 'noLanguages'" x-text="errorMessage"></div>
            @error('languages')
            <x-form-error-text message="{{ $message }}" />
            @enderror
            @error('languages.*')
            <x-form-error-text message="{{ $message }}" />
            @enderror
        @endif

        <div class="form-group" x-ref="descriptionField">
            <x-form-field-title name="{{ $words['description'] }}" required="true" />
            <x-markdown-textarea name="description" value="{{ old('description', $entry->description ?? '') }}" />
            <div class="form-error-text" x-show="errorKey === 'noDescription'" x-text="errorMessage"></div>
            @error('description')
                <x-form-error-text message="{{ $message }}" />
            @enderror
        </div>

        <x-form-group-title label="{{ $words['about_game'] }}" icon="gamepad-2" />
        <div x-ref="gameSelector">
            <livewire:game-selector
                :game-id="old('game_id', $entry->game_id ?? null)"
                :new-game-title="old('new-game-title')"
                :new-game-platform="old('new-game-platform')"
                :new-game-genre="old('new-game-genre')"
                :platform-only-id="old('platform_only_id', $entry->platform_id ?? null)"
                :section="$section"
            />
        </div>
        <div class="form-error-text" x-show="errorKey === 'noGame'" x-text="errorMessage"></div>
        @error('game_id')
            <x-form-error-text message="{{ $message }}" />
        @enderror
        @error('new-game-title')
            <x-form-error-text message="{{ $message }}" />
        @enderror
        @error('new-game-platform')
            <x-form-error-text message="{{ $message }}" />
        @enderror
        @error('new-game-genre')
            <x-form-error-text message="{{ $message }}" />
        @enderror
        @if( section_must_be( [ 'romhacks', 'translations' ], $section ))
             <livewire:hashes-upload :old-hashes="old('hashes', $entry->hashes->toArray(), [])" />
        @endif

        @if( section_must_not_be( [ 'translations', 'utilities', 'documents' ], $section ) )
            <x-form-field-title name="Languages" required="true" />
            <x-languages-selector :selected="$oldLanguages" />
         <div class="form-error-text" x-show="errorKey === 'noLanguages'" x-text="errorMessage"></div>
            @error('languages')
                <x-form-error-text message="{{ $message }}" />
            @enderror
            @error('languages.*')
                <x-form-error-text message="{{ $message }}" />
            @enderror
        @endif

        <x-form-group-title label="{{ $words['attachments'] }}" icon="paperclip" />
        <x-main-image-field :old-path="old('main-image', $entry->main_image ?? '') ?? ''" />
        <div class="form-error-text" x-show="errorKey === 'noMainImage'" x-text="errorMessage"></div>
        <x-gallery-field :old-paths="old('gallery', $entry->gallery->pluck('image')->toArray() ?? [] )"/>
         <div class="form-error-text" x-show="errorKey === 'noGalleryImages'" x-text="errorMessage"></div>
        @error('gallery')
            <x-form-error-text message="{{ $message }}" />
        @enderror
        @error('gallery.*')
            <x-form-error-text message="{{ $message }}" />
        @enderror

        <x-form-group-title label="{{ $words['authors'] }}" icon="users" />
        <livewire:authors-selector :old-authors="old('authors', $entry->authors->map(fn($a) => ['id' => $a->id, 'name' => $a->name ])->toArray() ?? [])" :old-new-authors="old('new-authors', [])" />
        <div class="form-error-text" x-show="errorKey === 'noAuthors'" x-text="errorMessage"></div>
        @error('authors')
            <x-form-error-text message="{{ $message }}" />
        @enderror
        @error('new-authors')
            <x-form-error-text message="{{ $message }}" />
        @enderror
        <x-staff-credits-field :old-staff-credits="old('staff_credits', $entry->staff_credits ?? null)" />

        @if( section_must_not_be( 'documents', $section ) )
            <x-form-group-title label="{{ $words['related_links'] }}" icon="link" />
            <div class="form-group grid-c2">
                <div>
                    <x-form-field-title name="{{ $words['release_site'] }}" helper="{{ $words['release_site_helper'] }}" required="" />
                    <input class="form-input" type="url" name="release_site" value="{{ old( 'release_site', $entry->relevant_link ?? '' ) }}">
                </div>
                <div>
                    <x-form-field-title name="{{ $words['youtube_video'] }}" required="" />
                    <input class="form-input" type="url" name="youtube_video" value="{{ old( 'youtube_video', $entry->youtube_link ?? '' ) }}">
                </div>
            </div>
        @else
            <div class="form-group"></div>
        @endif

        @if($isEdit)
            <x-form-group-title label="Entry Management" icon="wrench" />
            @can('moderate',$entry)
                <div class="form-group grid-c2">
                    <div>
                        <x-form-field-title name="Staff comment" />
                        <textarea class="form-textarea" name="staff_comment" rows="3">{{ old('staff_comment', $entry->staff_comment ?? '' ) }}</textarea>
                    </div>
                    <div>
                        <x-form-field-title name="Owner" required="true" />
                        <livewire:xf-user-selector :initial-user-id="old('owner_user_id', $entry->user_id)" />
                    </div>
                </div>
                <div class="form-group grid-c2">
                    <div>
                        <x-form-field-title name="XenForo Comments Thread ID" />
                        <input type="text" name="comments_thread_id" class="form-input" value="{{ old('comments_thread_id', $entry->comments_thread_id) }}">
                    </div>
                </div>
                <div class="form-group">
                    <x-form-field-title name="Metadata" required="true" />
                    <div class="form-type-of-checkboxes form-group level" id="entry-metadata">
                        <label><input class="form-checkbox" type="checkbox" name="featured" value="1" {{ old('featured', $entry->featured ) ? 'checked' : '' }}>Featured entry</label>
                        <label><input class="form-checkbox" type="checkbox" name="refresh_created_at" value="1">Refresh created at</label>
                        @if( section_must_be(['romhacks', 'homebrew'], $section ) )
                            <label><input class="form-checkbox" type="checkbox" name="nsfw-entry" value="1" {{ old('nsfw-entry', $entry->nsfw ) ? 'checked' : '' }}>NSFW</label>
                        @endif
                        @if( section_must_be( ['romhacks', 'translations'], $section ) )
                            <label><input class="form-checkbox" type="checkbox" name="bypass_hashes" value="1">Bypass hashes requirements</label>
                        @endif
                    </div>
                </div>
            @endcan
            @cannot('moderate', $entry)

            @endcannot
        @endif

        @csrf

        <div class="submit" x-ref="submitButton">
            <x-submit-entry-status :section="$section" :is-edit="$isEdit" :current-state="$entry->state ?? null" :entry="$entry" />
            <button id="submit-button" type="submit" class="btn primary" style="padding:1%;">Submit</button>
        </div>
         <div class="form-error-text" x-show="errorKey === 'isSubmitting'" x-text="errorMessage"></div>
    </form>
</div>
