<div class="row">
    <div class="col-xs-12">
        <div class="box box-danger">
            <div class="box-body table-responsive table-fixed-height">
                <table id="PrzebiegiPracy" class="table hover-rows custom-table FullTable" data-start-sort="2" data-start-sort-direction="desc" data-start-height="50vh">
                    <thead>
                    <tr class="text-uppercase">
                        <th class="hidden"></th>
                        <th>Zakład</th>
                        <th>Od</th>
                        <th>Do</th>
                        <th class="text-center lmd-1">Suma<br/>L/M/D</th>
                        <th class="text-center lmd-2">Urlop<br/>L/M/D</th>
                        <th class="text-center lmd-3">Wysługa<br/>L/M/D</th>
                        <th class="text-center lmd-4">Jubileusz<br/>L/M/D</th>
                        <th class="text-center lmd-5">Emerytura<br/>L/M/D</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php $sumL = 0;
                    $sumM = 0;
                    $sumD = 0; ?>
                    <?php $urlL = 0;
                    $urlM = 0;
                    $urlD = 0; ?>
                    <?php $wysL = 0;
                    $wysM = 0;
                    $wysD = 0; ?>
                    <?php $jubL = 0;
                    $jubM = 0;
                    $jubD = 0; ?>
                    <?php $emeL = 0;
                    $emeM = 0;
                    $emeD = 0; ?>
                    <?php foreach ($workflows['bPrzebiegiPracy']['bPrzebiegPracy'] as $k => $flow): ?>
                        <?php
                        $sumL += (int)$flow['bLat'];
                        $sumM += (int)$flow['bMiesiecy'];
                        $sumD += (int)$flow['bDni'];
                        $urlL += (int)$flow['bLatU'];
                        $urlM += (int)$flow['bMiesiecyU'];
                        $urlD += (int)$flow['bDniU'];
                        $wysL += (int)$flow['bLatW'];
                        $wysM += (int)$flow['bMiesiecyW'];
                        $wysD += (int)$flow['bDniW'];
                        $jubL += (int)$flow['bLatJ'];
                        $jubM += (int)$flow['bMiesiecyJ'];
                        $jubD += (int)$flow['bDniJ'];
                        $emeL += (int)$flow['bLatE'];
                        $emeM += (int)$flow['bMiesiecyE'];
                        $emeD += (int)$flow['bDniE'];
                        ?>
                        <tr
                            <?php if (!empty($flow['bInformacjeDodatkowe'])): ?>
                                class="more-info-append" data-append-to="#more-info-container"
                                data-more-info="<?php echo $flow['bInformacjeDodatkowe']; ?>"
                            <?php endif; ?>
                        >
                            <td class="hidden"><?php echo $k; ?></td>
                            <td class="info-addon"><?php echo $flow['bNazwaZakladu']; ?></td>
                            <td><span class="span-date"><?php echo $flow['bDataOd']; ?></span></td>
                            <td><span class="span-date"><?php echo $flow['bDataDo']; ?></span></td>
                            <td class="text-center lmd-1"><?php echo $flow['bLat'] . '/' . $flow['bMiesiecy'] . '/' . $flow['bDni']; ?></td>
                            <td class="text-center lmd-2"><?php echo $flow['bLatU'] . '/' . $flow['bMiesiecyU'] . '/' . $flow['bDniU']; ?></td>
                            <td class="text-center lmd-3"><?php echo $flow['bLatW'] . '/' . $flow['bMiesiecyW'] . '/' . $flow['bDniW']; ?></td>
                            <td class="text-center lmd-4"><?php echo $flow['bLatJ'] . '/' . $flow['bMiesiecyJ'] . '/' . $flow['bDniJ']; ?></td>
                            <td class="text-center lmd-5"><?php echo $flow['bLatE'] . '/' . $flow['bMiesiecyE'] . '/' . $flow['bDniE']; ?></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;
                    }

                    if($urlD >= 30) {
                        $urlM += floor($urlD / 30);
                        $urlD = $urlD % 30;
                    }
                    if($urlM >= 12) {
                        $urlL += floor($urlM / 12);
                        $urlM = $urlM % 12;
                    }

                    if($wysD >= 30) {
                        $wysM += floor($wysD / 30);
                        $wysD = $wysD % 30;
                    }
                    if($wysM >= 12) {
                        $wysL += floor($wysM / 12);
                        $wysM = $wysM % 12;
                    }

                    if($jubD >= 30) {
                        $jubM += floor($jubD / 30);
                        $jubD = $jubD % 30;
                    }
                    if($jubM >= 12) {
                        $jubL += floor($jubM / 12);
                        $jubM = $jubM % 12;
                    }

                    if($emeD >= 30) {
                        $emeM += floor($emeD / 30);
                        $emeD = $emeD % 30;
                    }
                    if($emeM >= 12) {
                        $emeL += floor($emeM / 12);
                        $emeM = $emeM % 12;
                    }
                    ?>

                    </tbody>
                    <tfoot>
                    <tr>
                        <?php $count = count($workflows['bPrzebiegiPracy']['bPrzebiegPracy'] ?? []); ?>
                        <th class="hidden"><?php echo ($count + 1); ?></th>
                        <th>PODSUMOWANIE:</th>
                        <th></th>
                        <th></th>
                        <th class="text-center" data-lmd="1"><?php echo $sumL . '/' . $sumM . '/' . $sumD; ?></th>
                        <th class="text-center" data-lmd="2"><?php echo $urlL . '/' . $urlM . '/' . $urlD; ?></th>
                        <th class="text-center" data-lmd="3"><?php echo $wysL . '/' . $wysM . '/' . $wysD; ?></th>
                        <th class="text-center" data-lmd="4"><?php echo $jubL . '/' . $jubM . '/' . $jubD; ?></th>
                        <th class="text-center" data-lmd="5"><?php echo $emeL . '/' . $emeM . '/' . $emeD; ?></th>
                    </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
</div>
<div class="row more-info-container">
    <div class="col-xs-12">
        <div class="box">
            <div class="box-header">
                <h2 class="box-title">Informacje dodatkowe</h2>
            </div>
            <div class="box-body" id="more-info-container">

            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $(document).on('change keyup blur', '[aria-controls="PrzebiegiPracy"]', 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);

            var sumD = 0;
            var sumM = 0;
            var sumL = 0;
            var counter = 0;
            $('table').find('.lmd-2').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="2"]').html(sumL+"/"+sumM+"/"+sumD);

            var sumD = 0;
            var sumM = 0;
            var sumL = 0;
            var counter = 0;
            $('table').find('.lmd-3').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="3"]').html(sumL+"/"+sumM+"/"+sumD);

            var sumD = 0;
            var sumM = 0;
            var sumL = 0;
            var counter = 0;
            $('table').find('.lmd-4').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="4"]').html(sumL+"/"+sumM+"/"+sumD);

            var sumD = 0;
            var sumM = 0;
            var sumL = 0;
            var counter = 0;
            $('table').find('.lmd-5').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="5"]').html(sumL+"/"+sumM+"/"+sumD);
        });
    });
</script>

<script>
    $(function () {
        $('.page-name:first-of-type').html('<?php echo $pageName; ?>');

        initiateDataTable();
    })
</script>

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