<div class="row">
    <div class="col-xs-12">
        <div class="box box-danger">
            <?php if($uprawnienia['ModulRejestruZastepstw']['bCzyDodawanie'] == 'true') : ?>
                <div class="row">
					<div class="col-xs-6">
						<div class="pull-left">
							<div class="btn btn-danger btn-flathidden-messages-toggle" style="cursor: default;">
								<span style="margin-right: 10px;">Bieżące</span>
								<label class="switch switch-current-year">
									<input type="checkbox" name="CurrentDeputy" id="CurrentDeputy" />
								</label>
							</div>
						</div>
					</div>
                    <div class="col-xs-6">
                        <div class="pull-right">
                            <a data-menu-action="dodaj_zastepstwo" href="/dodaj_zastepstwo" class="btn btn-success btn-flat">
                                <span style="margin-right: 10px;"><i class="fa fa-plus"></i> Dodaj</span>
                                <label class="switch"></label>
                            </a>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
            <div class="box-body table-responsive table-fixed-height" style="max-height: 85vh!important;">
                <table id="ZastepstwaTable" class="table hover-rows custom-table" data-start-height="65vh">
                    <thead>
                    <tr>
                        <th>Od</th>
                        <th>Do</th>
                        <th>Zastępowany</th>
                        <th>Zastępujący</th>
                        <th class="no-sortable text-right"></th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php if (isset($replacements)): ?>
                        <?php foreach ($replacements as $replacement): ?>
                            <tr>
                                <td><span class="span-date date-od"><?php echo $replacement['bDataOd']; ?></span></td>
                                <td><span class="span-date date-do"><?php echo $replacement['bDataDo']; ?></span></td>
                                <td>
                                    <strong>
                                        <?php echo $replacement['bZastepowany']['cNazwaWyswietlana']; ?>
                                    </strong>
                                    <br/>
                                    <?php echo $replacement['bZastepowany']['cEmail']; ?>
                                </td>
                                <td>
                                    <strong>
                                        <?php echo $replacement['bZastepujacy']['cNazwaWyswietlana']; ?>
                                    </strong>
                                    <br/>
                                    <?php echo $replacement['bZastepujacy']['cEmail']; ?>
                                </td>
                                <td class="text-right table-actions">
                                    <?php if($uprawnienia['ModulRejestruZastepstw']['bCzyEdycja'] == 'true') : ?>
                                        <a data-menu-action="zastepstwo/<?php echo $replacement['bGid']; ?>" href="zastepstwo/<?php echo $replacement['bGid']; ?>"
                                           class="btn btn-primary">
                                            <i class="fa fa-pencil"></i>
                                        </a>
                                    <?php endif; ?>

                                    <?php if($uprawnienia['ModulRejestruZastepstw']['bCzyUsuwanie'] == 'true') : ?>
                                        <a class="btn btn-danger" data-remove-entry data-gid="<?php echo $replacement['bGid']; ?>" data-url="usun_zastepstwo" data-title="Na pewno usunąć?"><i class="fa fa-trash"></i></a>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script>
   $(function () {
	   $(document).on('click', '#CurrentDeputy', function () {
		   var tr = $('#ZastepstwaTable > tbody > tr'),
				   dod = null,
				   ddo = null,
				   now = moment().format('YYYY-MM-DD'),
				   check = $(this).is(':checked');

		   for (var i = 0; i < tr.length; i++) {
			   if (check) {
				   dod = $(tr[i]).find('.date-od').text().trim();
				   ddo = $(tr[i]).find('.date-do').text().trim();
				   if (now >= dod && now <= ddo ) {
					   $(tr[i]).show();
				   } else {
					   $(tr[i]).hide();
				   }
			   } else {
				   $(tr[i]).show();
			   }
		   }
	   });
   });

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