@php use Illuminate\Support\Facades\URL;use Seka\Vzdelavanie\Enums\ModelStatusEnum;use Seka\Vzdelavanie\Models\Testovanie\DruhSpravnejOdpovedeEnum; @endphp
<form id="editor" class="row" method="post" action="{{ $actionRoute }}">
    @if(isset($edit) && $edit === true)
        @method('PUT')
    @endif
    @csrf()
    <div class="col-12">
        <div class="form-group">
            <label>
                Znenie otázky
                <span class="text-red">*</span>
            </label>
            <textarea name="znenie" class="form-control my-editor"
                      rows="10">{{ $otazka->znenie ?? old('znenie') }}</textarea>
        </div>
    </div>

    <div class="col-6">
        <div class="form-group">
            <label>
                Druh odpovede
                <span class="text-red">*</span>
            </label>
            <select class="custom-select mb-2" name="druh_spravnej_odpovede" autocomplete="off">
                @php
                    $selected = DruhSpravnejOdpovedeEnum::JEDNA;

                    if (isset($otazka) && $otazka->druh_spravnej_odpovede) {
                        $selected = $otazka->druh_spravnej_odpovede;
                    }
                @endphp
                @foreach(DruhSpravnejOdpovedeEnum::cases() as $case)
                    <option value="{{ $case->value }}"
                            @if($selected->equal($case)) selected @endif>{{ $case->label() }}</option>
                @endforeach
            </select>
        </div>
    </div>
    <div class="col-6">
        <div class="form-group">
            <label>
                Stav
                <span class="text-red">*</span>
            </label>
            <div class="form-group">
                @php
                    $checked = ModelStatusEnum::PUBLIKOVANE;
                    if (isset($otazka) && $otazka->stav) {
                        $checked = $otazka->stav;
                    }
                @endphp
                @foreach(ModelStatusEnum::cases() as $case)
                    <div class="form-check form-check-inline">
                        <input type="radio" class="form-check-input" name="stav" id="public-{{ $loop->index }}"
                               value="{{ $case->value }}"
                               @if($checked->equal($case)) checked @endif>
                        <label class="form-check-label" for="public-{{ $loop->index }}">{{ $case->label() }}</label>
                    </div>
                @endforeach
                @if($errors->has('stav'))
                    @foreach($errors->get('stav') as $message)
                        <div class="invalid-feedback">{{ $message }}</div>
                    @endforeach
                @endif
            </div>
        </div>
    </div>
    <div class="col-12">
        <div class="form-group">
            <button class="btn btn-primary" type="submit">
                <i class="fa fa-save"></i>
                Uložiť
            </button>
        </div>
    </div>
</form>

@push('scripts')
    <script src="{{mix('/js/tinymce/tinymce.min.js', '/vendor/seka/vzdelavanie')}}"></script>
    <script>
        const editor_config = {
            path_absolute: '{{ URL::secure('/') }}',
            selector: "textarea.my-editor",
            language_url: "{{asset('/vendor/seka/vzdelavanie/js/tinymce/langs/sk.js')}}",
            language: 'sk',
            plugins: [
                "advlist autolink lists link image charmap print preview hr anchor pagebreak",
                "searchreplace wordcount visualblocks visualchars code fullscreen",
                "insertdatetime media nonbreaking save table directionality",
                "emoticons template paste textpattern"
            ],
            toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image media code | fullscreen",
            relative_urls: false,
            convert_urls: false,
            file_picker_callback: function (callback, value, meta) {
                const x = window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth
                const y = window.innerHeight || document.documentElement.clientHeight || document.getElementsByTagName('body')[0].clientHeight
                const cmsURL = editor_config.path_absolute + '/file-manager?editor=tinymce5&type=Files'

                tinyMCE.activeEditor.windowManager.openUrl({
                    url: cmsURL,
                    title: 'Správca súborov',
                    width: x * 0.8,
                    height: y * 0.8,
                    resizable: 'yes',
                    close_previous: 'no',
                    onMessage: (api, message) => {
                        callback(message.content)
                    }
                });
            }
        };

        tinymce.init(editor_config)
    </script>
@endpush

@push('styles')
    <style>
        div.mce-fullscreen {
            position: fixed;
            top: 80px !important;
            left: 0;
        }
    </style>
@endpush