<!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-shell {
            width: 100%;
        }

        .report-header {
            width: 100%;
            border: 1px solid #d7deea;
            background: #f8fafc;
            padding: 18px 20px;
            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;
            border-radius: 12px;
        }

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

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

        .company-subtitle {
            margin: 4px 0 0 12px;
            font-size: 12px;
            color: #64748b;
        }

        .report-meta {
            width: 320px;
        <?php if(!empty($RTL)){?> text-align: left;
        <?php }else{?> text-align: right;
        <?php }?>
        }

        .header-title {
            margin: 0;
            font-size: 20px;
            font-weight: 500;
            color: #0f172a;
        }

        .header-subtitle {
            margin: 6px 0 0;
            font-size: 11px;
            line-height: 1.35;
            color: #64748b;
        }

        .header-meta {
            margin: 6px 0 0;
            font-size: 10px;
            font-weight: 700;
            letter-spacing: .08em;
            text-transform: uppercase;
            color: #475569;
        }

        .report-card {
            width: 100%;
            margin-bottom: 18px;
            border: 1px solid #cbd5e1;
            border-radius: 14px;
            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;
        }

        .week-title {
            background: #e2e8f0;
            color: #0f172a;
            font-size: 13px;
            font-weight: 700;
            padding: 12px 14px;
        }

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

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

        .th3 {
            font-size: 12px;
            font-weight: 700;
            padding: 10px 12px;
            border: 1px solid #cbd5e1;
            background: #f1f5f9;
            color: #334155;
        <?php if(!empty($RTL)){?> text-align: right;
        <?php }?>
        }

        .td {
            border: 1px solid #e2e8f0;
            padding: 10px 12px;
            vertical-align: top;
        <?php if(!empty($RTL)){?> text-align: right;
        <?php }?>
        }

        .row-alt .td {
            background: #f8fafc;
        }

        .n {
            font-size: 12px;
        <?php if(!empty($RTL)){?> text-align: right;
        <?php }?>
        }

        .time-text {
            font-weight: 600;
            color: #0f172a;
        }

        .status-badge {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 999px;
            font-size: 11px;
            font-weight: 700;
        }

        .status-holiday {
            background: #e0f2fe;
            color: #0369a1;
        }

        .status-leave {
            background: #fef3c7;
            color: #b45309;
        }

        .status-absent {
            background: #fee2e2;
            color: #b91c1c;
        }

        .status-hourly {
            display: inline-block;
            margin-left: 6px;
            padding: 3px 7px;
            border-radius: 999px;
            background: #fce7f3;
            color: #be185d;
            font-size: 10px;
            font-weight: 700;
        }

        .empty-state {
            border: 1px solid #e2e8f0;
            padding: 14px;
            text-align: center;
            color: #64748b;
            background: #f8fafc;
        }
    </style>
</head>
<body>
<?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-shell">
<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>
                <p class="company-subtitle"><?= lang('works_hours_deatils') ?></p>
            </td>
            <td class="report-meta">
                <p class="header-title"><?= lang('works_hours_deatils') ?></p>
                <p class="header-subtitle"><?= lang('department') . ' : ' . (!empty($dept_name->deptname) ? $dept_name->deptname : lang('all') . ' ' . lang('departments')) . ', ' . $month ?></p>
                <p class="header-meta"><?= lang('generated_on') ?> <?= display_date(date('Y-m-d')) ?></p>
            </td>
        </tr>
    </table>
</div>
<div style="width: 100%;">
    <div class="report-card">
        <div class="report-card-inner">
            <table style="width: 100%;">
                <tr>
                    <td style="padding: 0;">
                        <h1 class="report-title"><?= lang('works_hours_deatils') . ' ' ?><?php echo $month; ?></h1>
                        <p class="report-subtitle"><?= lang('department') . ' : ' . (!empty($dept_name->deptname) ? $dept_name->deptname : lang('all') . ' ' . lang('departments')) ?></p>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <?php if (!empty($attendace_info)) { ?>
        <table class="table_tr1">
            <?php foreach ($attendace_info as $week => $v_attndc_info) { ?>
                <tr>
                    <th colspan="8" class="week-title"><?= lang('week') ?> : <?php echo $week; ?></th>
                </tr>
                <tr>
                    <th class="th3"><?= lang('name') ?></th>
                    <?php
                    if (!empty($v_attndc_info)) {
                        foreach ($v_attndc_info as $date => $attendace) {
                            $total_hour = 0;
                            $total_minutes = 0;
                            ?>
                            <th class="th3"><?= display_date($date) ?></th>
                        <?php }
                    }; ?>
                </tr>
                <?php
                foreach ($employee as $rowIndex => $v_employee) {
                    ?>
                    <tr class="<?= ($rowIndex % 2 !== 0) ? 'row-alt' : '' ?>">
                        <td class="td n"><?php echo $v_employee->fullname ?></td>
                        <?php
                        if (!empty($v_attndc_info)):foreach ($v_attndc_info as $date => $attendace):

                            $total_hh = 0;
                            $total_mm = 0;
                            $hourly_leave = null;
                            foreach ($attendace as $key => $v_attendace) {
                                if ($key == $v_employee->user_id) {
                                    ?>
                                    <?php
                                    if (!empty($v_attendace)) {
                                        foreach ($v_attendace as $v_attandc) {
                                            if ($v_attandc->attendance_status == 1) {

                                                // calculate the start timestamp
                                                $startdatetime = safe_strtotime($v_attandc->date_in . " " . $v_attandc->clockin_time);
                                                // calculate the end timestamp
                                                $enddatetime = safe_strtotime($v_attandc->date_out . " " . $v_attandc->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;

                                                if (!empty($v_attandc->leave_application_id)) { // check leave type is hours
                                                    $is_hours = get_row('tbl_leave_application', array('leave_application_id' => $v_attandc->leave_application_id));
                                                    if (!empty($is_hours) && $is_hours->leave_type == 'hours') {
                                                        $hourly_leave = "<small class='status-hourly' title='" . lang('hourly') . ' ' . lang('leave') . ": " . $is_hours->hours . ":00".' '.lang('hour')."'>" . lang('hourly') . ' ' . lang('leave') . "</small>";

                                                    }
                                                }
                                                // output the result
                                                //echo round($hoursDiff) . " : " . round($minutesDiffRemainder) . " m";
                                            } elseif ($v_attandc->attendance_status == 'H') {
                                                $holiday = 1;
                                            } elseif ($v_attandc->attendance_status == '3') {
                                                $leave = 1;
                                            } elseif ($v_attandc->attendance_status == '0') {
                                                $absent = 1;
                                            }
                                        }
                                    }
                                }
                            }
                            ?>
                            <td class="td">

                                <?php
                                if ($total_mm > 59) {
                                    $total_hh += intval($total_mm / 60);
                                    $total_mm = intval($total_mm % 60);
                                }
                                $total_hour += $total_hh;
                                $total_minutes += $total_mm;

                                if ($total_hh != 0 || $total_mm != 0) {
                                    echo '<span class="time-text">' . $total_hh . " : " . $total_mm . " m" . '</span>' .' '. $hourly_leave;
                                } elseif (!empty($holiday)) {
                                    echo '<span class="status-badge status-holiday">' . lang('holiday') . '</span>';
                                } elseif (!empty($leave)) {
                                    echo '<span class="status-badge status-leave">' . lang('on_leave') . '</span>';
                                } elseif (!empty ($absent)) {
                                    echo '<span class="status-badge status-absent">' . lang('absent') . '</span>';
                                } else {
                                    echo '<span class="time-text">' . $total_hh . " : " . $total_mm . " m" . '</span>';
                                }
                                ?>
                            </td>
                            <?php
                            $holiday = NULL;
                            $leave = NULL;
                            $absent = NULL;
                        endforeach;
                        endif;
                        ?>
                    </tr>
                <?php }; ?>
            <?php } ?>
        </table>
    <?php } else { ?>
        <div class="empty-state"><?= lang('nothing_to_display') ?></div>
    <?php } ?>
</div>
</div>
</body>
</html>
