<!DOCTYPE html>
<html>

<head>
    <title><?php
            if (!empty($title)) {
                echo $title;
            } else {
                config_item('company_name');
            }
            ?></title>
    <?php
    $direction = $this->session->userdata('direction');
    if (!empty($direction) && $direction == 'rtl') {
        $RTL = 'on';
    } else {
        $RTL = config_item('RTL');
    }
    ?>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style type="text/css">
        body {
            margin: 0;
            color: #0f172a;
            font-family: DejaVu Sans, sans-serif;
            font-size: 12px;
            line-height: 1.45;
        }

        .report-header {
            width: 100%;
            border-bottom: 2px solid #0f172a;
            padding-bottom: 12px;
            margin-bottom: 18px;
        }

        .company-table {
            width: 100%;
            vertical-align: middle;
        }

        .company-logo-cell {
            width: 64px;
            border: 0;
            vertical-align: middle;
        }

        .company-logo {
            width: 52px;
            height: 52px;
        }

        .company-name-cell {
            border: 0;
            vertical-align: middle;
        }

        .company-name {
            margin: 0 0 0 12px;
            font-size: 18px;
            font-weight: 700;
            color: #0f172a;
        }

        .report-card {
            width: 100%;
            margin-bottom: 18px;
            border: 1px solid #cbd5e1;
            background: #f8fafc;
        }

        .report-card-inner {
            padding: 16px 18px;
        }

        .report-title {
            margin: 0;
            font-size: 20px;
            font-weight: 700;
            text-align: center;
            color: #0f172a;
        }

        .report-subtitle {
            margin: 8px 0 0;
            font-size: 12px;
            font-weight: 700;
            text-align: center;
            color: #475569;
        }

        .table_tr1 {
            width: 100%;
            <?php if (!empty($RTL)) { ?>text-align: right;
            <?php } ?>
        }

        .table_tr1 .th {
            border-bottom: 1px solid #cbd5e1;
            background-color: #e2e8f0;
            font-size: 12px;
            <?php if (!empty($RTL)) { ?>text-align: right;
            <?php } ?>
        }

        .table_tr2 th,
        .table_tr3 th,
        .table_tr1 .th,
        .table_tr3 td {
            padding: 3px 0px 3px 5px;
            <?php if (!empty($RTL)) { ?>text-align: right;
            <?php } ?>
        }

        .table_tr3 th {
            border-bottom: 1px solid #cbd5e1;
            <?php if (!empty($RTL)) { ?>text-align: right;
            <?php } ?>
        }

        .table_tr3 td {
            border-bottom: 1px solid #e2e8f0;
            font-size: 12px;
            <?php if (!empty($RTL)) { ?>text-align: right;
            <?php } ?>
        }

        .table_tr3 .td {
            font-size: 13px;
            background: #e2e8f0;
            <?php if (!empty($RTL)) { ?>text-align: right;
            <?php } ?>
        }

        .th3 {
            font-size: 13px;
            <?php if (!empty($RTL)) { ?>text-align: right;
            <?php } else { ?>text-align: left;
            <?php } ?>
        }
    </style>
</head>

<body style="min-width: 100%; min-height: 100%; alignment-adjust: central;">
    <?php
    $rootPath = defined('ROOTPATH') ? ROOTPATH : FCPATH;
    $img = $rootPath . '/' . config_item('company_logo');
    $a = file_exists($img);
    if (empty($a)) {
        $img = base_url() . config_item('company_logo');
    }
    if (!file_exists($img)) {
        $img = $rootPath . '/' . 'uploads/default_logo.png';
    }
    ?>
    <div class="report-header">
        <table class="company-table">
            <tr>
                <td class="company-logo-cell">
                    <img class="company-logo" src="<?= $img ?>" alt="" />
                </td>

                <td class="company-name-cell">
                    <p class="company-name"><?= config_item('company_name') ?></p>
                </td>
            </tr>
        </table>
    </div>
    <div style="width: 100%;">
        <div class="report-card">
            <div class="report-card-inner">
            <table style="width: 100%;">
                <tr>
                    <td>
                        <p class="report-title"><strong><?= lang('works_hours_deatils') . ' ' ?><?php echo $month; ?></strong></p>
                        <p class="report-subtitle"><strong><?= lang('department') . ' : ' . $dept_name->deptname ?></strong></p>
                    </td>
                </tr>
            </table>
        </div>
        </div>
        <?php if (!empty($attendace_info)) { ?>
            <table class="table_tr1">
                <?php foreach ($attendace_info as $name => $v_attendace_info) { ?>
                    <tr>
                        <th class="th"><?php echo $employee[$name]->fullname; ?></th>
                        <?php if (!empty($v_attendace_info)) {
                            foreach ($v_attendace_info as $week => $v_attendace) {
                                $total_hour = 0;
                                $total_minutes = 0;
                        ?>
                    <tr>
                        <th style="width: 100%;padding: 10px;"><?= lang('week') ?>
                            : <?php echo $week; ?>
                            <table class="table_tr3" style="width: 100%;">
                                <tr>
                                    <th class="th3"><?= lang('clock_in_time') ?></th>
                                    <th class="th3"><?= lang('clock_out_time') ?></th>
                                    <th class="th3"><?= lang('ip_address') ?></th>
                                    <th class="th3"><?= lang('location') ?></th>
                                    <th class="th3"><?= lang('hours') ?></th>
                                </tr>
                                <?php
                                $total_hh = 0;
                                $total_mm = 0;
                                if (!empty($v_attendace)) {
                                    $hourly_leave = null;
                                    foreach ($v_attendace as $date => $v_mytime) {
                                        foreach ($v_mytime as $hmytime) {
                                            if ($hmytime->attendance_status == 1) {
                                                if (!empty($hmytime->leave_application_id)) { // check leave type is hours
                                                    $is_hours = get_row('tbl_leave_application', array('leave_application_id' => $hmytime->leave_application_id));
                                                    if (!empty($is_hours) && $is_hours->leave_type == 'hours') {
                                                        $hourly_leave = lang('hourly') . ' ' . lang('leave') . ": " . $is_hours->hours . ":00" . ' ' . lang('hour');
                                                    }
                                                }
                                            }
                                        }
                                ?>
                                        <tr>
                                            <td class="td" colspan="4" style="font-weight: bold"><?php echo $date; ?>
                                                <span style="float: right;text-align: right"><?= $hourly_leave ?></span>
                                            </td>
                                        </tr>
                                        <?php
                                        foreach ($v_mytime as $mytime) {
                                            if ($mytime->attendance_status == 1) {
                                        ?>
                                                <tr>
                                                    <td><?php echo display_time($mytime->clockin_time); ?></td>
                                                    <td><?php
                                                        if (empty($mytime->clockout_time)) {
                                                            echo '<span class="text-danger">' . lang('currently_clock_in') . '<span>';
                                                        } else {
                                                            if (!empty($mytime->comments)) {
                                                                $comments = ' <small> (' . $mytime->comments . ')</small>';
                                                            } else {
                                                                $comments = '';
                                                            }
                                                            echo display_time($mytime->clockout_time) . $comments;
                                                        }
                                                        ?>
                                                    </td>
                                                    <td><?= $mytime->ip_address ?></td>
                                                    <td><?= $mytime->location ?></td>
                                                    <td><?php
                                                        if (!empty($mytime->clockout_time)) {
                                                            // calculate the start timestamp
                                                            $startdatetime = safe_strtotime($mytime->date_in . " " . $mytime->clockin_time);
                                                            // calculate the end timestamp
                                                            $enddatetime = safe_strtotime($mytime->date_out . " " . $mytime->clockout_time);
                                                            // calulate the difference in seconds
                                                            $difference = $enddatetime - $startdatetime;

                                                            $years = abs(floor($difference / 31536000));
                                                            $days = abs(floor(($difference - ($years * 31536000)) / 86400));
                                                            $hours = abs(floor(($difference - ($years * 31536000) - ($days * 86400)) / 3600));
                                                            $mins = abs(floor(($difference - ($years * 31536000) - ($days * 86400) - ($hours * 3600)) / 60)); #floor($difference / 60);
                                                            $total_mm += $mins;
                                                            $total_hh += $hours;
                                                            echo $hours . " : " . $mins . " m";
                                                            // output the result
                                                        }
                                                        ?></td>
                                                <?php } elseif ($mytime->attendance_status == 'H') { ?>
                                                <tr>
                                                    <td colspan="4" style="text-align: center">
                                                        <span style="padding:5px 109px; font-size: 12px;" class="label label-info std_p"><?= lang('holiday') ?></span>
                                                    </td>
                                                </tr>
                                            <?php } elseif ($mytime->attendance_status == '3') { ?>
                                                <tr>
                                                    <td colspan="4" style="text-align: center">
                                                        <span style="padding:5px 109px; font-size: 12px;" class="label label-warning std_p"><?= lang('on_leave') ?></span>
                                                    </td>
                                                </tr>
                                            <?php } elseif ($mytime->attendance_status == '0') { ?>
                                                <tr style="">
                                                    <td colspan="4" style="text-align: center">
                                                        <span style="padding:5px 109px; font-size: 12px;" class="label label-danger std_p"><?= lang('absent') ?></span>
                                                    </td>
                                                </tr>
                                            <?php } else { ?>
                                                <tr>
                                                    <td colspan="4" style="text-align: center">
                                                        <span style=" font-size: 12px;" class=" std_p"><?= lang('no_data_available') ?> </span>
                                                    </td>
                                                </tr>
                                            <?php } ?>
                                        <?php }; ?>
                                    <?php
                                        $hourly_leave = null;
                                    } ?>
                                    <tr style="background: #cbd1dc">
                                        <td style="text-align: right">
                                            <strong style="margin-right: 10px; "><?= lang('total_working_hour') ?>
                                                : </strong>
                                        </td>
                                        <td colspan="3">
                                            <?php
                                            if ($total_mm > 59) {
                                                $total_hh += intval($total_mm / 60);
                                                $total_mm = intval($total_mm % 60);
                                            }
                                            echo $total_hh . " : " . $total_mm . " m";
                                            ?>
                                        </td>
                                    </tr>
                                <?php
                                }
                                ?>
                            </table>
                        </th>

                    </tr>

            <?php }
                        }
            ?>
            </tr>
        <?php } ?>
            </table>
        <?php } ?>
    </div>
</body>

</html>
