<!DOCTYPE html>
<html>
<head>
    <title><?php
        if (!empty($title)) {
            echo $title;
        } else {
            echo 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-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;
        }

        .year-block {
            margin-bottom: 18px;
            border: 1px solid #cbd5e1;
            background: #ffffff;
        }

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

        .week-block {
            padding: 14px;
            border-top: 1px solid #e2e8f0;
        }

        .week-title {
            margin: 0 0 10px;
            font-size: 13px;
            font-weight: 700;
            color: #334155;
        }

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

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

        .table-log th {
            background: #f1f5f9;
            color: #334155;
            font-size: 12px;
            font-weight: 700;
        }

        .date-row td {
            background: #eef2f7;
            color: #0f172a;
            font-size: 13px;
            font-weight: 700;
        }

        .summary-row td {
            background: #dbe4f0;
            font-weight: 700;
            color: #0f172a;
        }

        .muted-text {
            color: #64748b;
        }

        .status-badge {
            display: inline-block;
            padding: 5px 10px;
            font-size: 11px;
            font-weight: 700;
        }

        .status-current {
            background: #ede9fe;
            color: #6d28d9;
        }

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

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

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

        .status-empty {
            color: #64748b;
            font-style: italic;
        }

        .duration-text {
            font-weight: 700;
            color: #0f172a;
        }

        .right {
        <?php if (!empty($RTL)) { ?> text-align: left;
        <?php } else { ?> text-align: right;
        <?php } ?>
        }
    </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>
                </td>
            </tr>
        </table>
    </div>

    <div class="report-card">
        <div class="report-card-inner">
            <h1 class="report-title"><?= $user_info->fullname . ' ' . lang('time_logs') ?></h1>
        </div>
    </div>

    <?php if (!empty($mytime_info)) { ?>
        <?php foreach ($mytime_info as $year => $v_time_info) { ?>
            <div class="year-block">
                <h2 class="year-title"><?= $year ?></h2>
                <?php if (!empty($v_time_info)) { ?>
                    <?php foreach ($v_time_info as $week => $v_attendace) {
                        $total_hh = 0;
                        $total_mm = 0;
                    ?>
                        <div class="week-block">
                            <p class="week-title"><?= lang('week') ?> : <?= $week ?></p>
                            <table class="table-log">
                                <tr>
                                    <th><?= lang('clock_in_time') ?></th>
                                    <th><?= lang('clock_out_time') ?></th>
                                    <th><?= lang('ip_address') ?></th>
                                    <th><?= lang('hours') ?></th>
                                </tr>
                                <?php if (!empty($v_attendace)) { ?>
                                    <?php foreach ($v_attendace as $key => $v_mytime) { ?>
                                        <tr class="date-row">
                                            <td colspan="4"><?= $key ?></td>
                                        </tr>
                                        <?php
                                        $attendance_row = null;
                                        if (!empty($v_mytime)) {
                                            foreach ($v_mytime as $attendance_item) {
                                                if (is_object($attendance_item)) {
                                                    $attendance_row = $attendance_item;
                                                    break;
                                                }
                                            }
                                        }
                                        ?>
                                        <?php if (!empty($attendance_row) && (string) $attendance_row->attendance_status === '1') { ?>
                                            <?php $clock_rows = get_result('tbl_clock', array('attendance_id' => $attendance_row->attendance_id)); ?>
                                            <?php if (!empty($clock_rows)) { ?>
                                                <?php foreach ($clock_rows as $clock_row) { ?>
                                                    <tr>
                                                        <td><?= !empty($clock_row->clockin_time) ? display_time($clock_row->clockin_time) : '-' ?></td>
                                                        <td>
                                                            <?php
                                                            if (empty($clock_row->clockout_time)) {
                                                                echo '<span class="status-badge status-current">' . lang('currently_clock_in') . '</span>';
                                                            } else {
                                                                $comments = '';
                                                                if (!empty($clock_row->comments)) {
                                                                    $comments = ' <small class="muted-text">(' . $clock_row->comments . ')</small>';
                                                                }
                                                                echo display_time($clock_row->clockout_time) . $comments;
                                                            }
                                                            ?>
                                                        </td>
                                                        <td><?= !empty($clock_row->ip_address) ? $clock_row->ip_address : '-' ?></td>
                                                        <td>
                                                            <?php
                                                            if (!empty($clock_row->clockout_time) && !empty($clock_row->clockin_time)) {
                                                                $startdatetime = safe_strtotime($attendance_row->date_in . ' ' . $clock_row->clockin_time);
                                                                $enddatetime = safe_strtotime($attendance_row->date_out . ' ' . $clock_row->clockout_time);
                                                                $difference = max(0, $enddatetime - $startdatetime);
                                                                $minutes = (int) floor($difference / 60);
                                                                $hours = floor($minutes / 60);
                                                                $mins = $minutes % 60;
                                                                $total_mm += $mins;
                                                                $total_hh += $hours;
                                                                echo '<span class="duration-text">' . $hours . ' : ' . str_pad($mins, 2, '0', STR_PAD_LEFT) . ' m</span>';
                                                            } else {
                                                                echo '-';
                                                            }
                                                            ?>
                                                        </td>
                                                    </tr>
                                                <?php } ?>
                                            <?php } else { ?>
                                                <tr>
                                                    <td>-</td>
                                                    <td>-</td>
                                                    <td>-</td>
                                                    <td>-</td>
                                                </tr>
                                            <?php } ?>
                                        <?php } elseif (!empty($attendance_row) && (string) $attendance_row->attendance_status === 'H') { ?>
                                            <tr>
                                                <td colspan="4" class="right"><span class="status-badge status-holiday"><?= lang('holiday') ?></span></td>
                                            </tr>
                                        <?php } elseif (!empty($attendance_row) && (string) $attendance_row->attendance_status === '3') { ?>
                                            <tr>
                                                <td colspan="4" class="right"><span class="status-badge status-leave"><?= lang('on_leave') ?></span></td>
                                            </tr>
                                        <?php } elseif (!empty($attendance_row) && (string) $attendance_row->attendance_status === '0') { ?>
                                            <tr>
                                                <td colspan="4" class="right"><span class="status-badge status-absent"><?= lang('absent') ?></span></td>
                                            </tr>
                                        <?php } else { ?>
                                            <tr>
                                                <td colspan="4" class="right"><span class="status-empty"><?= lang('no_data_available') ?></span></td>
                                            </tr>
                                        <?php } ?>
                                    <?php } ?>
                                    <tr class="summary-row">
                                        <td colspan="3" class="right"><strong><?= lang('total_working_hour') ?> :</strong></td>
                                        <td>
                                            <?php
                                            if ($total_mm > 59) {
                                                $total_hh += intval($total_mm / 60);
                                                $total_mm = intval($total_mm % 60);
                                            }
                                            echo '<span class="duration-text">' . $total_hh . ' : ' . $total_mm . ' m</span>';
                                            ?>
                                        </td>
                                    </tr>
                                <?php } else { ?>
                                    <tr>
                                        <td colspan="4" class="right"><span class="status-empty"><?= lang('no_data_available') ?></span></td>
                                    </tr>
                                <?php } ?>
                            </table>
                        </div>
                    <?php } ?>
                <?php } ?>
            </div>
        <?php } ?>
    <?php } ?>
</div>
</body>
</html>
