@php use Illuminate\Support\Facades\URL;use Illuminate\Support\Str;use Seka\Portal\Modul\Terminy\Entities\Udalost\DruhUdalosti;use Seka\Portal\Modul\Terminy\Entities\Udalost\StavRegistracie;use Seka\Portal\Modul\Terminy\Entities\Udalost\StavUdalosti; @endphp
<form wire:submit.prevent="submit">

    @include('flash::message')

    <div class="form-row">
        <div class="col">
            <div class="form-group">
                <label for="nazov">Názov <span class="text-red">*</span></label>
                <input type="text"
                       autocomplete="off"
                       id="nazov"
                       class="form-control @error('nazov') is-invalid @enderror"
                       name="nazov"
                       placeholder="názov udalosti"
                       @disabled($this->trashed)
                       wire:model.blur.live="nazov"
                >
                <small class="form-text text-muted">
                    Názov udalosti, napr. "Školenie administratívnych pracovníkov..."
                </small>
                @error('nazov')
                <div class="invalid-feedback">{{ $message }}</div>
                @enderror
            </div>
        </div>
    </div>

    <div class="form-row">
        <div class="col">
            <div class="form-group">
                <label for="datum_konania_od">Dátum a čas začiatku <span class="text-red">*</span></label>
                <div wire:ignore>
                    <input type="text"
                           class="form-control"
                           data-flatpickr
                           data-alt-input="true"
                           data-alt-format="d.m.Y o H:i"
                           data-date-format="Y-m-d\TH:i"
                           data-week-numbers="true"
                           data-time_24hr="true"
                           data-month-selector-type="static"
                           data-enable-time="true"
                           id="datum_konania_od"
                           name="datum_konania_od"
                           placeholder="dátum a čas začiatku udalosti"
                           @disabled($this->trashed)
                           wire:model.defer="datum_od"
                    >
                </div>
                <small class="form-text text-muted">Zadajte prosím dátum a čas začiatku udalosti.</small>
                @error('datum_od')
                <div class="invalid-feedback">{{ $message }}</div>
                @enderror
            </div>
        </div>

        <div class="col">
            <div class="form-group">
                <label for="datum_konania_do">Dátum a čas ukončenia <span class="text-red">*</span></label>
                <div wire:ignore>
                    <input type="text"
                           class="form-control"
                           data-flatpickr
                           data-enable-time="true"
                           data-alt-input="true"
                           data-alt-format="d.m.Y o H:i"
                           data-date-format="Y-m-d\TH:i"
                           data-week-numbers="true"
                           data-time_24hr="true"
                           data-month-selector-type="static"
                           id="datum_konania_do"
                           name="datum_konania_do"
                           placeholder="dátum a čas ukončenia udalosti"
                           @disabled($this->trashed)
                           wire:model.defer="datum_do"
                    >
                </div>
                <small class="form-text text-muted">Zadajte prosím dátum a čas ukončenia udalosti.</small>
                @error('datum_do')
                <div class="invalid-feedback">{{ $message }}</div>
                @enderror
            </div>
        </div>
    </div>
    <div class="form-row">
        <div class="col-md-9 col-sm-12">
            <div class="form-group">
                <label for="miesto_konania">Miesto konania <span class="text-red">*</span></label>
                <select class="custom-select form-control @error('miesto_konania_id') is-invalid @enderror"
                        id="miesto_konania"
                        name="miesto_konania"
                        autocomplete="off"
                        @disabled($this->trashed)
                        wire:model.blur.live="miesto_konania_id"
                >
                    <option value="">zvoľte miesto konania udalosti</option>
                    @forelse($miestaKonania as $miestoKonania)
                        <option wire:key="wk-miesto-konania-{{ $miestoKonania->id }}"
                                value="{{ $miestoKonania->id }}"
                        >{{ $miestoKonania->adresa }}</option>
                    @empty
                        <option value="">neexistuje žiadne miesto konania udalosti</option>
                    @endforelse
                </select>
                <small class="form-text text-muted">Zadajte miesto konania udalosti.</small>
                @error('miesto_konania_id')
                <div class="invalid-feedback">{{ $message }}</div>
                @enderror
            </div>
        </div>

        <div class="col-md-3 col-sm-12">
            <div class="form-group">
                <label for="kapacita">Kapacita <span class="text-red">*</span></label>
                <input type="number"
                       min="1"
                       class="form-control @error('kapacita') is-invalid @enderror"
                       id="kapacita"
                       name="kapacita"
                       placeholder="maximálna kapacita udalosti"
                       @disabled($this->trashed)
                       wire:model.blur.live="kapacita"
                >
                <small class="form-text text-muted">Zadajte maximálnu kapacitu udalosti.</small>
                @error('kapacita')
                <div class="invalid-feedback">{{ $message }}</div>
                @enderror
            </div>
        </div>
    </div>

    <div class="form-row">
        <div class="col">
            <div class="form-group">
                <label for="stav">Stav udalosti <span class="text-red">*</span></label>
                <select class="custom-select form-control @error('stav') is-invalid @enderror"
                        name="stav"
                        id="stav"
                        autocomplete="off"
                        @disabled($this->trashed)
                        wire:model.change.live="stav"
                >
                    <option value="">zvoľte možnosť</option>
                    @foreach(StavUdalosti::cases() as $case)
                        <option wire:key="wk-stav-udalosti-{{ $case->name }}"
                                value="{{ $case->name }}"
                        >{{ Str::ucfirst($case->label()) }}</option>
                    @endforeach
                </select>
                @error('stav')
                <div class="invalid-feedback">{{ $message }}</div>
                @enderror
            </div>
        </div>

        <div class="col">
            <div class="form-group">
                <label for="druh">Druh udalosti <span class="text-red">*</span></label>
                <select class="custom-select form-control @error('druh') is-invalid @enderror"
                        name="druh"
                        id="druh"
                        autocomplete="off"
                        @disabled($this->trashed)
                        wire:model.change.live="druh"
                >
                    <option value="">zvoľte možnosť</option>
                    @foreach(DruhUdalosti::cases() as $case)
                        <option wire:key="wk-druh-udalosti-{{ $case->name }}"
                                value="{{ $case->name }}"
                        >{{ Str::ucfirst($case->label()) }}</option>
                    @endforeach
                </select>
                @error('druh')
                <div class="invalid-feedback">{{ $message }}</div>
                @enderror
            </div>
        </div>

        <div class="col">
            <div class="form-group">
                <label for="registracia">Registrácia <span class="text-red">*</span></label>
                <select class="custom-select form-control @error('registracia') is-invalid @enderror"
                        name="registracia"
                        id="registracia"
                        autocomplete="off"
                        @disabled($this->trashed)
                        wire:model.change.live="registracia"
                >
                    <option value="">zvoľte možnosť</option>
                    @foreach(StavRegistracie::cases() as $case)
                        <option wire:key="wk-stav-registracie-{{ $case->name }}"
                                value="{{ $case->name }}"
                        >{{ Str::ucfirst($case->label()) }}</option>
                    @endforeach
                </select>
                @error('registracia')
                <div class="invalid-feedback">{{ $message }}</div>
                @enderror
            </div>
        </div>
    </div>

    <div class="form-row">
        <div class="col">
            <div class="form-group">
                <label for="">Upozorniť pracoviská na udalosť</label>
                <div class="custom-control custom-switch align-middle">
                    <input type="checkbox" class="custom-control-input" id="user-notification-switch"
                           name="user-notification-switch" wire:model="povolene_upozornenie"
                           wire:click="$dispatch('toggleStavUpozornenia')">
                    <label class="custom-control-label" for="user-notification-switch">zapnite v prípade, ak si želáte
                        informovať pracoviská o vytvorení, resp. zmene udalosti formou notifikácie</label>
                </div>
                @error('povolene_upozornenie')
                <div class="invalid-feedback">{{ $message }}</div>
                @enderror
            </div>
        </div>
    </div>

    <div class="form-group" wire:ignore>
        <label for="popis">Poznámka</label>
        <textarea class="form-control popis"
                  id="popis"
                  name="popis"
                  placeholder="poznámka k udalosti"
                  rows="16"
                  wire:model.defer="popis"
        ></textarea>
        <small class="form-text text-muted">Zadajte poznámku k udalosti.</small>
        @error('popis')
        <div class="invalid-feedback">{{ $message }}</div>
        @enderror
    </div>

    <hr>

    <div class="form-row">
        <div class="col d-flex justify-content-between">
            <div class="col-auto m-0 p-0">
                @can('terminy')
                    @if($this->trashed)
                        <button type="button" wire:click="restore()" class="btn btn-secondary">
                            <span wire:loading.remove wire:target="restore" class="icon-cw"></span>
                            <span wire:loading wire:target="restore" class="spinner-grow spinner-grow-sm"
                                  role="status"></span>
                            Obnoviť
                        </button>
                    @endif
                @endcan
                @if(!$this->trashed && $this->exists)
                    <button type="button" class="btn btn-danger" wire:click="trash()">
                        <span wire:loading.remove wire:target="trash" class="icon-cancel-circled"></span>
                        <span wire:loading wire:target="trash" class="spinner-grow spinner-grow-sm"
                              role="status"></span>
                        Zneplatniť
                    </button>
                @endif
            </div>

            <div class="col-auto m-0 p-0">
                <button type="button" class="btn btn-link" wire:click="cancel()">Zavrieť</button>
                @if(!$this->trashed)
                    <button type="button" wire:click="save()" class="btn btn-primary">
                        <span wire:loading.remove wire:target="save" class="icon-floppy"></span>
                        <span wire:loading wire:target="save" class="spinner-grow spinner-grow-sm" role="status"></span>
                        @if($this->exists)
                            Upraviť
                        @else
                            Vytvoriť
                        @endif
                    </button>
                @endif
            </div>
        </div>
    </div>

</form>

@push('styles')
    <style>
        div.flatpickr-time {
            height: auto !important;
        }
    </style>
@endpush

@push('scripts')
    <script src="https://npmcdn.com/flatpickr/dist/l10n/sk.js"></script>
    <script type="application/javascript">
        flatpickr.localize(flatpickr.l10ns.sk);
        flatpickr.l10ns.default.firstDayOfWeek = 1;
    </script>
@endpush

@push('scripts')
    <script src="{{mix('/js/tinymce/tinymce.min.js', '/vendor/seka/terminy')}}"></script>
    <script>
        const editorElement = 'textarea.popis'
        const editor_config = {
            path_absolute: '{{ URL::secure('/') }}',
            selector: editorElement,
            language_url: "{{asset('/vendor/seka/terminy/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)
                    }
                })
            },
            setup: function (editor) {
                editor.on('init change', function () {
                    editor.save()
                })

                editor.on('change', function () {
                    @this.
                    set('popis', editor.getContent(), true)
                })
            }
        }

        @if($this->trashed))
        editor_config.readonly = 1
        @endif

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

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