<div>
    <div class="pull-left">
        <?php
        $monthNumber = date('m', strtotime($initDate));
        $month = '';
        switch($monthNumber)
        {
            case '01': $month = 'Styczeń'; break;
            case '02': $month = 'Luty'; break;
            case '03': $month = 'Marzec'; break;
            case '04': $month = 'Kwiecień'; break;
            case '05': $month = 'Maj'; break;
            case '06': $month = 'Czerwiec'; break;
            case '07': $month = 'Lipiec'; break;
            case '08': $month = 'Sierpień'; break;
            case '09': $month = 'Wrzesień'; break;
            case '10': $month = 'Październik'; break;
            case '11': $month = 'Listopad'; break;
            case '12': $month = 'Grudzień'; break;
        }

        if (empty($nrEwid)) {
            foreach ($events as $k => $event) {
                if ($ifLogged[$k] == 'true') {
                    $nrEwid = intval(explode('(', $k)[1]);
                }
            }
        }
        ?>
        <h2 class="new-calendar-header" data-init-date="<?php echo $initDate; ?>" data-nr-ewid="<?php echo $nrEwid; ?>"><?php echo date('Y', strtotime($initDate));  echo ' '.$month; ?></h2>
    </div>
    <div class="text-right pull-right" style="margin-bottom: 10px;">
        <input type="text" name="datepicker" data-events-url="calendar_feed_ajax" class="chooseMonthDatepicker"
               placeholder="Wybierz miesiąc" id="ChooseCalendar" autocomplete="off"/>
        <a class="btn btn-default btn-flat" id="ShowCurrentCalendar">Aktualny miesiąc</a>
        <a class="btn btn-default btn-flat" id="ShowPrevCalendar"><i class="fa fa-chevron-left"></i></a>
        <a class="btn btn-default btn-flat" id="ShowNextCalendar"><i class="fa fa-chevron-right"></i></a>
    </div>
    <div class="clearfix">

    </div>
</div>
<table class="table table-responsive table-bordered table-striped table-hover new-calendar">
    <thead>
    <th style="vertical-align: middle;"></th>
    <?php foreach ($days as $k => $day) : ?>
        <?php
        $czySwieto = false;
        foreach ($day['events'] as $e) {
            if($e['bCzySwieto'] === 'true') $czySwieto = true;
        }
        ?>
        <th class="text-center" style="<?php if($day['title'] == 'So') echo 'color: blue;' ?><?php if($day['title'] == 'Ni') echo 'color: red;'; ?><?php if($czySwieto) echo 'color: red'; ?>">
            <div><?php echo $day['day']; ?></div><br />
            <div><?php echo $day['title']; ?></div>
        </th>
    <?php endforeach; ?>
    </thead>
    <?php foreach ($events as $k => $event) : ?>
        <tr>
            <td style="padding-left: 5px!important;" <?php if($ifLogged[$k] == 'true') echo 'class="loggedUserCalendar"'; ?>><?php echo $k; ?></td>
            <?php foreach ($days as $kday => $day) : ?>
                <td class="text-center td-<?php echo md5($k) . '-' . md5($day['day']); ?>"
                    style="<?php if ($day['title'] == 'So') echo 'background: rgba(0, 0, 255, 0.11);' ?><?php if ($day['title'] == 'Ni') echo 'background: rgba(255, 0, 0, 0.11);'; ?>">
                    <?php $isEntered = false; ?>
                    <?php foreach ($day['events'] as $e) : ?>
                        <?php if (!$isEntered) : ?>
                            <?php if ($e['pracownik'] == $k) : ?>
                                <?php if ($e['bRodzaj'] == 'Nieobecnosc') : ?>
                                    <?php if (!empty($e['bNieobecnoscKod'])) : ?>
                                        <span data-toggle="tooltip"
                                              title="<?php echo $e['bNieobecnoscOpis']; ?>"><?php echo $e['bNieobecnoscKod']; ?></span>
                                        <?php $isEntered = true; ?>
                                        <style>
                                            .td-<?php echo md5($k).'-'.md5($day['day']); ?> {
                                                background: <?php echo $e['bKolorTlaHTML']; ?> !important;
                                                color: <?php echo $e['bKolorPierwszegoPlanuHTML']; ?> !important;
                                            }
                                        </style>
                                    <?php endif; ?>
                                <?php elseif ($e['bRodzaj'] == 'PracaPlanowana') : ?>
                                    <?php if ($hourFromTo) : ?>
                                        <span data-toggle="tooltip" title="Planowy czas pracy" style="color: #ffffaa">
										<b><?php echo $e['bGodzinString']; ?></b>
									</span>
                                        <br>
                                        <span data-toggle="tooltip" title="od">
										<?php echo $e['bGodzinaRozpoczeciaString']; ?>
									</span>
                                        <br/>
                                        <span data-toggle="tooltip" title="do">
										<?php echo $e['bGodzinaZakonczeniaString']; ?>
									</span>
                                    <?php else: ?>
                                        <span data-toggle="tooltip" title="Planowy czas pracy">
										<?php echo $e['bGodzinString']; ?>
									</span>
                                    <?php endif; ?>
                                    <?php $isEntered = true; ?>
                                    <style>
                                        .td-<?php echo md5($k).'-'.md5($day['day']); ?> {
                                            background: <?php echo $e['bKolorTlaHTML']; ?> !important;
                                            color: <?php echo $e['bKolorPierwszegoPlanuHTML']; ?> !important;
                                        }
                                    </style>
                                <?php endif; ?>
                            <?php endif; ?>
                            <?php if ($e['bCzySwieto'] === "true") : ?>
                                <style>
                                    .td-<?php echo md5($k).'-'.md5($day['day']); ?> {
                                        background: rgba(255, 0, 0, 0.11) !important;
                                    }
                                </style>
                            <?php endif; ?>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </td>
            <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
</table>

<script>
    $(function () {
        $('body').tooltip({
            selector: '[data-toggle="tooltip"]'
        });
    });
</script>

<script>
    $(function () {
        $(document).find('#ChooseCalendar').datepicker({
            format: "YYYY-MM",
            locale: 'pl',
            changeMonth: true,
            changeYear: true,
            showButtonPanel: true,
            onClose: function (dateText, inst) {
                var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
                var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
                $(this).val($.datepicker.formatDate('MM yy', new Date(year, month, 1)));
                month = parseInt(month);

                var data = {};

                if (month < 10) {
                    data = {
                        'direction': 'next',
                        'start': year + "-0" + month + "-01",
                        'nrEwid': $('[data-nr-ewid]').data('nr-ewid')
                    };
                } else {
                    data = {
                        'direction': 'next',
                        'start': year + "-" + month + "-01",
                        'nrEwid': $('[data-nr-ewid]').data('nr-ewid')
                    };
                }

                changeCalendar(data);
            }
        }).on('change', function (ev) {
            console.log('change');
        }).on('show', function () {
            console.log('show');
        });

        $("#ChooseCalendar").focus(function () {
            $(".ui-datepicker-calendar").hide();
            $("#ui-datepicker-div").position({
                my: "center top",
                at: "center bottom",
                of: $(this)
            });
        });

        if (1 == <?=$print?>) {
            $('#print_report_employee').show();
        }

        $('#ShowCurrentCalendar').click(function () {
            var data = {
                'direction': 'current',
                'start': $('[data-init-date]').data('init-date'),
                'nrEwid': $('[data-nr-ewid]').data('nr-ewid')
            };
            changeCalendar(data);
        });

        $('#ShowNextCalendar').click(function () {
            var data = {
                'direction': 'next',
                'start': $('[data-init-date]').data('init-date'),
                'nrEwid': $('[data-nr-ewid]').data('nr-ewid')
            };
            changeCalendar(data);
        });

        $('#ShowPrevCalendar').click(function () {
            var data = {
                'direction': 'prev',
                'start': $('[data-init-date]').data('init-date'),
                'nrEwid': $('[data-nr-ewid]').data('nr-ewid')
            };
            changeCalendar(data);
        });

        function changeCalendar(data) {
            var time = new Date().getTime();
            $(document).find('.custom-loader').css('display', 'block');
            $.ajax({
                url: local_url + 'calendar_feed_ajax_employee?t=' + time,
                method: 'POST',
                data: data,
                dataType: 'html'
            }).done(function (response) {
                $('#ModalContentEmployee').html(response);
                let $myDiv = $('#ModalContentWithNewCallendar');
                if ($myDiv.length) {
                    $myDiv.html(response);
                }
                $(document).find('.custom-loader').css('display', 'none');
            });
        }
    });
</script>

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