<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)); ?>
                    </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)); ?>
                    </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)); ?>
                    </div>
                </div>
                <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="<?php echo $action_return; ?>" href="<?php echo '/' . $action_return; ?>" <?php /*onclick="history.go(-1)"*/ ?> 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 () {
        tinymce.remove();
        initEditor();
        initMultipleFiles();

        setTimeout(function () {
            <?php if(isset($this->request->getData()['pWiadomosc']['b:Odbiorcy'])) : ?>
                <?php foreach ($this->request->getData()['pWiadomosc']['b:Odbiorcy'] as $adres) : ?>
                    addReceiverOnEditEnter("<?php echo $adres['b:Adres']['b:Nazwa']; ?>", "<?php echo implode('', $adres['b:Adres']['b:Email']); ?>", "<?php echo $adres['b:Adres']['b:Gid']; ?>");
                <?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');

                var counter = 0;
                $('[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();
                     }
                });

                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);
                        console.log(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('/<?php echo $action_return; ?>');
                        }
                    },
                    complete: function (xhr) {
                        console.log(xhr);
                    }
                });


            }
        });

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

<script>
    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'); ?>
