<table id="WyksztalceniaTable" class="table hover-rows custom-table HalfTable" data-start-sort="1" data-start-sort-direction="desc" data-start-height="25vh">
    <thead>
    <tr class="text-uppercase">
        <th class="hidden"></th>
        <th>Uczelnia</th>
        <th>Od</th>
        <th>Do</th>
        <th class="text-center lmd-1">Suma<br/>L/M/D</th>
        <th>Kod wykształcenia</th>
    </tr>
    </thead>
    <tbody>
    <?php // if (!empty($trainings['bSzkolenia']['bSzkolenie'])): ?>
    <?php if (!empty($educations) && isset($educations['bWyksztalcenia'])): ?>

    <?php $sumL = 0;
    $sumM = 0;
    $sumD = 0;
    ?>
    <?php foreach ($educations['bWyksztalcenia']['bWyksztalcenie'] as $k => $education): ?>
        <?php
        $sumL += (int)$education['bLat'];
        $sumM += (int)$education['bMiesiecy'];
        $sumD += (int)$education['bDni'];
        ?>
        <tr
            <?php if (!empty($education['bInformacjeDodatkowe'])): ?>
                class="more-info-append" data-append-to="#more-info-container"
                data-more-info="<?php echo $education['bInformacjeDodatkowe']; ?>"
            <?php endif; ?>
        >
            <td class="hidden"><?php echo $k; ?></td>
            <td class="info-addon"><?php echo $education['bNazwaUczelni']; ?></td>
            <td><span class="span-date"><?php echo $education['bDataOd']; ?></span></td>
            <td><span class="span-date"><?php echo $education['bDataDo']; ?></span></td>
            <td class="text-center lmd-1"><?php echo $education['bLat'] . '/' . $education['bMiesiecy'] . '/' . $education['bDni']; ?></td>
            <td><?php echo $education['bWyksztalcenieOpisKodu']; ?></td>
        </tr>
    <?php endforeach; ?>

    <?php
    if($sumD >= 30) {
        $sumM += floor($sumD / 30);
        $sumD = $sumD % 30;
    }
    if($sumM >= 12) {
        $sumL += floor($sumM / 12);
        $sumM = $sumM % 12;
    }
    ?>

    </tbody>
    <tfoot>
    <tr>
        <th>PODSUMOWANIE:</th>
        <th></th>
        <th></th>
        <th class="text-center" data-lmd="1"><?php echo $sumL . '/' . $sumM . '/' . $sumD; ?></th>
        <th></th>
    </tr>
    </tfoot>
    <?php endif; ?>
</table>

<script>
    $(function () {
        $(document).on('change keyup blur', '[aria-controls="WyksztalceniaTable"]', function () {
            var sumD = 0;
            var sumM = 0;
            var sumL = 0;
            var counter = 0;
            $('table').find('.lmd-1').each(function () {
                counter++;
                if(counter > 2) {
                    var s = $(this).html().split('/');
                    sumD += parseInt(s[2]);
                    sumM += parseInt(s[1]);
                    sumL += parseInt(s[0]);
                }
            });
            if(sumD >= 30) {
                sumM += Math.floor(sumD / 30);
                sumD = sumD % 30;
            }
            if(sumM >= 12) {
                sumL += Math.floor(sumM / 12);
                sumM = sumM % 12;
            }

            $('.dataTables_scrollFoot [data-lmd="1"]').html(sumL+"/"+sumM+"/"+sumD);
        });
    });
</script>

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