<?php echo $this->Form->create(null, ['templates' => ['submitContainer' => '{{content}}',], 'name' => 'tem:pImportuj', 'class' => 'form-horizontal', 'id' => 'Importuj', 'url' => '/importy']); ?>
    <div class="box-body">
        <h2 style="margin-left: 0.5em;"><?=$tytul?></h2>
		<?php echo $this->Element('table_loading', array('id' => 'table-loading')); ?>
        <div class="col-xs-12">
            <div class="row mb20">
                <div class="col-xs-6">
                    <label class="col-xs-12 col-md-2 control-label text-left">Wybierz rodzaj</label>
                    <div class="col-xs-12 col-md-6">
                        <?php echo $this->Form->input('dat:Nazwa', array('required' => 'required', 'class' => 'form-control', 'div' => false, 'label' => false, 'type' => 'select', 'options' => $rodzaje, 'value' => $rodzaj)); ?>
                    </div>
                </div>
            </div>

            <?php foreach ($mozliweImporty['bMozliweImporty']['bMozliwyImport'] as $import) : ?>
                <?php if ($import['bRodzaj'] == $rodzaj) : ?>
                    <div data-name="<?php echo $import['bRodzaj']; ?>">
                        <?php if ($import['bCzyOkresOd'] == 'true') : ?>
                            <div class="row mb20">
                                <div class="col-xs-6">
                                    <label class="col-xs-12 col-md-2 control-label text-left">Data od</label>
                                    <div class="col-xs-12 col-md-3">
                                        <?php echo $this->Form->input('dat:OkresOd', array('required' => 'required', 'class' => 'form-control datepicker-from', 'div' => false, 'label' => false)); ?>
                                    </div>
                                </div>
                            </div>
                        <?php endif; ?>
                        <?php if ($import['bCzyOkresDo'] == 'true') : ?>
                            <div class="row mb20">
                                <div class="col-xs-6">
                                    <label class="col-xs-12 col-md-2 control-label text-left">Data do</label>
                                    <div class="col-xs-12 col-md-3">
                                        <?php echo $this->Form->input('dat:OkresDo', array('required' => 'required', 'class' => 'form-control datepicker-to', 'div' => false, 'label' => false)); ?>
                                    </div>
                                </div>
                            </div>
                        <?php endif; ?>

                        <?php for ($i=0; $i<=4; $i++) : ?>
                            <?php if ($import['bCzyPlik'.$i] == 'true') : ?>
                                <div class="row" style="margin-bottom: 10px;">
                                    <div class="col-xs-12">
                                        <label><?php echo $import['bTytulPliku'.$i]; ?></label>
                                        <?php
                                        $fileOptions = array();
                                        $fileOptions['type'] = 'file';
                                        $fileOptions['label'] = false;
                                        $fileOptions['div'] = false;
                                        /*if (!empty($import['bRozszerzeniePliku'.$i])) {
                                            $fileOptions['accept'] = '.'.$import['bRozszerzeniePliku'.$i];
                                        }*/
                                        ?>
                                        <?php echo $this->Form->input('dat:Plik'.$i, $fileOptions); ?>
                                    </div>
                                </div>
                            <?php endif; ?>
                        <?php endfor; ?>

                        <?php for ($i=0; $i<=4; $i++) : ?>
                            <?php if ($import['bCzyZaznaczenie'.$i] == 'true') : ?>
                                <div class="row">
                                    <div class="col-xs-12">
                                        <div>
                                            <label>
                                                <?php echo $this->Form->checkbox('dat:Zaznaczenie'.$i, array('label' => false, 'div' => false)); ?>
                                                <?php echo $import['bTytulZaznaczenia'.$i]; ?>
                                            </label>
                                        </div>
                                    </div>
                                </div>
                            <?php endif; ?>
                        <?php endfor; ?>

                        <?php for ($i=0; $i<=4; $i++) : ?>
                            <?php if ($import['bCzyText'.$i] == 'true') : ?>
                                <div class="row">
                                    <div class="col-xs-12">
                                        <label><?php echo $import['bTytulText'.$i]; ?></label>
                                        <div>
                                            <?php echo $this->Form->input('dat:Text'.$i, array('class' => 'form-control', 'label' => false)); ?>
                                        </div>
                                    </div>
                                </div>
                            <?php endif; ?>
                        <?php endfor; ?>
                    </div>
                <?php endif; ?>
            <?php endforeach; ?>


            <div class="row mt20">
                <div class="col-xs-6">
                    <label class="col-xs-12 col-md-2 control-label text-left"></label>
                    <div class="col-xs-12 col-md-10">
                        <?php echo $this->Form->submit('Importuj', array('class' => 'btn btn-success btn-flat pull-left')); ?>
                    </div>
                </div>
            </div>

        </div>
    </div>
<?php echo $this->Form->end(); ?>

<script>
    $(function () {
        datepickerFromToInit();

        $('#Importuj').validate({
            rules: {
                'data[tem:pImportuj][dat:OkresOd]': {
                    datefrom: true
                },
                'data[tem:pImportuj][dat:Nazwa]': {
                    selecttype: true
                }
            },
            submitHandler: function (form) {
                var n = $('[name="data[tem:pImportuj][dat:Nazwa]"]').val();
                $('#Importuj').ajaxSubmit({
                    beforeSend: function () {
                        showLoader($('#table-loading'));
					},
                    success: function (response) {

                        hideLoader($('#table-loading'));

                        response = JSON.parse(response);
                        console.log(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');

                        if(response['error'] != undefined) {
                            showError(response['error']);
                            showTop();
                        } else {
                            hideMessages();
                            showSuccess(response['message']);
                            loadAjaxView('/importy');
                        }
                    },
                    complete: function (xhr) {
                        console.log(xhr);
                    }
                });
            }
        });
    });
</script>
