<div class="row">
    <div class="col-xs-12">
        <div class="box box-danger">
            <?php echo $this->Form->create(null, ['templates' => ['submitContainer' => '{{content}}',], 'id' => 'pWiadomosc', 'name' => 'pWiadomosc', 'class' => 'form-horizontal', 'type' => 'file']); ?>
            <div class="box-body">
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Tytuł</label>
                    <div class="col-xs-12 col-md-10">
                        <?php echo $this->Form->input('data.pWiadomosc.b:Tytul', array('class' => 'form-control', 'div' => false, 'label' => false, 'value' => $message['bWiadomosc']['bTytul'])); ?>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Treść</label>
                    <div class="col-xs-12 col-md-10">
                        <?php echo $this->Form->input('data.pWiadomosc.b:Tresc', array('class' => 'tinymce', 'div' => false, 'label' => false, 'value' => $message['bWiadomosc']['bTrescHTML'])); ?>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Priorytet</label>
                    <div class="col-xs-12 col-md-10">
                        <?php echo $this->Form->input('data.pWiadomosc.b:Priorytet', array('class' => 'form-control', 'type' => 'select', 'options' => $priorytety, 'div' => false, 'label' => false, 'value' => $message['bWiadomosc']['bPriorytet'])); ?>
                    </div>
                </div>
                <?php echo $this->Form->input('data.pWiadomosc.b:Gid', array('div' => false, 'label' => false, 'type' => 'hidden', 'value' => $message['bWiadomosc']['bGid'])); ?>
                <div class="form-group custom-error-holder">
                    <label class="col-xs-12 col-md-2 control-label">Odbiorcy</label>
                    <div class="col-xs-12 col-md-10" id="receiver-search-holder">
                        <div class="input-group">
                            <?php
                            /*
                            <div class="input-group-addon ac-search-trigger" id="receiver-search-trigger"><i
                                        class="fa fa-search"></i></div>
                             */
                            ?>
                            <div class="input-group-addon receivers-list-trigger" id="receivers-list-trigger" data-zmetody="wiadomosc">
                                <i class="fa fa-list"></i>
                            </div>
                            <input type="text" class="form-control" id="receiver-search" name="receiver-search"
                                   data-loader="#receiver-search-loader" placeholder="Wyszukaj odbiorców"/>
                        </div>
                    </div>
                    <?php /*
                <div class="loader-holder" id="receiver-search-loader">
                    <div class="loader"></div>
                </div>
                */ ?>
                </div>
                <div class="form-group">
                    <div class="col-xs-12 col-md-10 col-md-offset-2 msg-receivers-container">

                    </div>
                </div>
                <?php /*
            <div class="form-group">
                <label class="col-xs-12 col-md-2 control-label">Wysyłka e-mail</label>
                <div class="col-xs-12 col-md-10">
                    <?php echo $this->Form->checkbox('b:CzyWysylkaEmail', array('div' => false, 'label' => false)); ?>
                </div>
            </div>
            <div class="form-group">
                <label class="col-xs-12 col-md-2 control-label">Publikacja</label>
                <div class="col-xs-12 col-md-10">
                    <?php echo $this->Form->checkbox('pCzyOpublikowac', array('div' => false, 'label' => false, 'checked' => 'checked', 'name' => 'data[pCzyOpublikowac]')); ?>
                </div>
            </div>
            */ ?>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">Załączniki</label>
                    <div class="col-xs-12 col-md-10" data-file-multiple style="display: none;">
                        <label class="btn btn-primary" for="file-selector">
                            <input name="data[file][]" id="file-selector"
                                   onchange="fileInputMultiple(this, '#remove-attachments');"
                                   type="file" style="display:none;">
                            <span>Wybierz pliki</span>
                        </label>
                        <a href="#" class="btn btn-danger" id="remove-attachments"
                           onclick="clearFileInputSingle('#file-selector', this)">
                            <i class="fa fa-trash"></i>
                        </a>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-md-2 control-label">&nbsp;</label>
                    <div class="col-xs-12 col-md-10">
                        <a  style="margin-right: 20px;" data-menu-action="wiadomosci" href="#" class="btn btn-default pull-left">Anuluj</a>
                        <?php echo $this->Form->submit('Zapisz', array('name' => 'data[action]', 'div' => false, 'class' => 'btn btn-success')); ?>
                        <?php echo $this->Form->submit('Wyślij', array('name' => 'data[action]', 'div' => false, 'class' => 'btn btn-success')); ?>
                    </div>
                </div>
            </div>
            <?php echo $this->Form->end(); ?>
        </div>
    </div>
</div>
<div class="modal fade" id="receivers-list-modal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Wybierz odbiorców</h4>
            </div>
            <div class="modal-body" id="receivers-list-body">
                <div class="row">
                    <div class="loader-holder" id="receivers-list-loader">
                        <div class="loader"></div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Anuluj</button>
                <button type="button" class="btn btn-success" id="receivers-list-submit" data-module="wiadomosci">Wybierz</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<?php echo $this->Html->script('messages'); ?>

<script>
    $(document).ready(function () {

        setTimeout(function () {
            tinymce.remove();
            initEditor();
            initMultipleFiles();
        });

        setTimeout(function () {
            <?php if(isset($message['bWiadomosc']['bOdbiorcy']['bAdres'])) : ?>
                <?php foreach ($message['bWiadomosc']['bOdbiorcy']['bAdres'] as $adres) : ?>
                    addReceiverOnEditEnter("<?php echo $adres['bNazwa']; ?>", "<?php echo $adres['bEmail']; ?>", "<?php echo $adres['bGid']; ?>", "<?php echo $adres['bNazwaWyswietlana']; ?>");
                <?php endforeach; ?>
            <?php endif; ?>
        });

        jQuery.validator.addMethod("selectpeople", function(value, element) {
            var s_text = $('#receiver-search').val();

            if($('.receiver-email-input').length > 0) {
                return true;
            } else {
                return false;
            }
        }, "Proszę wybrać poprawną wartość w polu 'Odbiorcy'");

        $('#pWiadomosc').validate({
            rules: {
                'data[pWiadomosc][b:Tytul]': {
                    required: true
                },
                'receiver-search': {
                    selectpeople: true
                }
            },
            submitHandler: function(form) {
                $('#table-loading2[data-main-loader]').css('visibility', 'visible');
                $('#table-loading2[data-main-loader]').css('opacity', '1');
                $('#table-loading2[data-main-loader]').css('display', 'block');

                $('[data-file-multiple] input[type="file"]').each(function () {
                    if($(this).val().length == 0 || $(this).val().length == null || $(this).val().length == undefined) {
                        $(this).parents('[data-file-multiple]').remove();
                    }
                });
                $('[data-file-multiple]:hidden').remove();

                var formData = $(form).serializeArray();
                var formAction = $(form).attr('action');

                $('#pWiadomosc').ajaxSubmit({
                    success: function (response) {
                        $('#table-loading2[data-main-loader]').css('visibility', 'hidden');
                        $('#table-loading2[data-main-loader]').css('opacity', '0');
                        $('#table-loading2[data-main-loader]').css('display', 'none');

                        response = JSON.parse(response);
                        if(response['login'] != undefined) {
                            window.location = local_url + 'login';
                        }
                        if(response['error'] != undefined) {
                            showError(response['error']);
                        } else {
                            hideMessages();
                            showSuccess(response['message']);
                            // tinymce.remove();
                            loadAjaxView('/wiadomosci');
                        }
                    },
                    complete: function (xhr) {
                    }
                });
                /*$.ajax({
                    type: 'POST',
                    url: formAction,
                    data: formData,
                }).done(function (response) {
                    response = JSON.parse(response);
                    console.log(response);
                    if(response['error'] != undefined) {
                        showError(response['error']);
                    } else {
                        hideMessages();
                        showSuccess(response['message']);
                        loadAjaxView('/wiadomosci');
                    }
                });*/
//                form.submit();
            }
        });

        setInterval(function () {
            $.ajax({
                url: local_url + "sesja_plus",
            });
        }, 60000);
    });
</script>

<script>
    $(document).ready(function () {
        var licznikZalacznikow = 0;
        function przygotujZalacznik(bGid, bGidRodzica, bNazwa, bPlik, bStronaKodowa, bTresc) {
            var html = '<div class="col-xs-12 col-md-10 col-md-offset-2 mb10" data-file-multiple="" style="">'+
                '<label class="btn btn-primary">'+
                    '<input type="hidden" name="bZalacznik['+licznikZalacznikow+'][bGid]" value="'+bGid+'" />'+
                    '<input type="hidden" name="bZalacznik['+licznikZalacznikow+'][bGidRodzica]" value="'+bGidRodzica+'" />'+
                    '<input type="hidden" name="bZalacznik['+licznikZalacznikow+'][bNazwa]" value="'+bNazwa+'" />'+
                    '<input type="hidden" name="bZalacznik['+licznikZalacznikow+'][bPlik]" value="'+bPlik+'" />'+
                    '<input type="hidden" name="bZalacznik['+licznikZalacznikow+'][bStronaKodowa]" value="'+bStronaKodowa+'" />'+
                    '<input type="hidden" name="bZalacznik['+licznikZalacznikow+'][bTresc]" value="'+bTresc+'" />'+
                    '<span>'+bPlik+'</span>'+
                '</label>'+
                '<a href="#" class="btn btn-danger ml3" style="" data-usun-zalacznik id="remove-attachments">'+
                    '<i class="fa fa-trash"></i>'+
                '</a>'+
            '</div>';
            $('[data-file-multiple]:last-of-type').before(html);
            licznikZalacznikow++;
        }

        setTimeout(function () {
            <?php if(isset($zalaczniki['bGid']) && !empty($zalaczniki['bGid'])) : ?>
                <?php $z = $zalaczniki; ?>
                przygotujZalacznik("<?php echo $z['bGid']; ?>", "<?php echo $z['bGidRodzica']; ?>", "<?php echo $z['bNazwa']; ?>", "<?php echo $z['bPlik']; ?>", "<?php echo $z['bStronaKodowa']; ?>", "<?php echo $z['bTresc']; ?>");
            <?php else : ?>
                <?php foreach ($zalaczniki as $z) : ?>
                    przygotujZalacznik("<?php echo $z['bGid']; ?>", "<?php echo $z['bGidRodzica']; ?>", "<?php echo $z['bNazwa']; ?>", "<?php echo $z['bPlik']; ?>", "<?php echo $z['bStronaKodowa']; ?>", "<?php echo $z['bTresc']; ?>");
                <?php endforeach; ?>
            <?php endif; ?>
        });
    });
</script>

<script>
    $(function(){
        const elemsData = document.querySelectorAll('[data-menu-action]');
        elemsData.forEach(elem => elem.onclick = testFun);
    });

    function testFun(e) {
        e.preventDefault();

        $(this).parents('.sidebar').find('li.active:not(.treeview)').removeClass('active');
        $(this).parents('li').addClass('active');

        var url = $(this).data('menu-action');
        localStorage.setItem('menu_action', url);

        loadAjaxView(url);
    }
</script>

<?php echo $this->element('alert_close'); ?>
