<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <?php
    $designation = !empty($profile_info->designations_id) ? $this->db->where('designations_id', $profile_info->designations_id)->get('tbl_designations')->row() : null;
    $department = (!empty($designation) && !empty($designation->departments_id)) ? $this->db->where('departments_id', $designation->departments_id)->get('tbl_departments')->row() : null;
    ?>
    <style>
        body {
            margin: 0;
            color: #172033;
            background: #ffffff;
            font: 12px/1.55 DejaVu Sans, Arial, Helvetica, sans-serif;
            min-width: 100%;
            min-height: 100%;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            border-spacing: 0;
        }
        .page-wrap { padding: 18px 22px 24px; }
        .brand-shell {
            border: 1px solid #d7deea;
            padding: 18px 20px;
            background: #f8fafc;
            margin-bottom: 18px;
        }
        .brand-logo {
            width: 60px;
            height: 60px;
            background: #ffffff;
            border: 1px solid #d7deea;
            padding: 6px;
        }
        .brand-title { font-size: 24px; font-weight: 500; color: #0f172a; margin: 0 0 4px; }
        .brand-text { font-size: 12px; color: #64748b; margin: 0; }
        .report-shell {
            border: 1px solid #d7deea;
            padding: 18px 20px;
            margin-bottom: 18px;
            background: linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);
        }
        .report-meta { width: 280px; text-align: right; }
        .header-title { font-size: 20px; font-weight: 700; color: #0f172a; margin: 0 0 4px; line-height: 1.15; }
        .header-subtitle { font-size: 10px; color: #64748b; margin: 0; line-height: 1.35; }
        .eyebrow {
            display: inline-block;
            padding: 5px 12px;
            border: 1px solid #dbe4ff;
            border-radius: 999px;
            background: #eef2ff;
            color: #4338ca;
            font-size: 10px;
            font-weight: 700;
            letter-spacing: .16em;
            text-transform: uppercase;
            margin-bottom: 10px;
        }
        .range-chip { display: inline-block; padding: 4px 0 0; background: transparent; color: #475569; font-size: 9px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
        .metrics {
            margin: 0 0 18px;
        }
        .metric-card {
            width: 23.5%;
            border: 1px solid #d7deea;
            background: #ffffff;
            padding: 14px 16px;
            vertical-align: top;
        }
        .metric-gap {
            width: 2%;
        }
        .metric-label {
            font-size: 10px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: .12em;
            color: #64748b;
            margin-bottom: 6px;
        }
        .metric-value {
            font-size: 18px;
            font-weight: 700;
            color: #0f172a;
            margin: 0;
        }
        .metric-note {
            font-size: 11px;
            color: #94a3b8;
            margin-top: 4px;
        }
        .profile-shell, .table-shell {
            border: 1px solid #d7deea;
            overflow: hidden;
            background: #ffffff;
            margin-bottom: 18px;
        }
        .profile-shell { padding: 16px 18px; }
        .avatar-box {
            width: 112px;
            height: 124px;
            border: 1px solid #d7deea;
            background: #f8fafc;
            text-align: center;
            vertical-align: middle;
            overflow: hidden;
            padding: 8px;
        }
        .avatar-box img {
            max-width: 96px;
            max-height: 106px;
            width: auto;
            height: auto;
            display: inline-block;
        }
        .profile-name { font-size: 24px; font-weight: 700; color: #0f172a; margin: 0 0 10px; }
        .detail-key { width: 32%; font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: #64748b; padding: 6px 0; }
        .detail-val { font-size: 11px; color: #0f172a; padding: 6px 0; }
        .section-head { border: 1px solid #d7deea; padding: 14px 16px; background: #f8fafc; margin-bottom: 12px; }
        .section-title { font-size: 18px; font-weight: 700; color: #0f172a; margin: 0 0 2px; }
        .section-subtitle { font-size: 10px; color: #64748b; margin: 0; }
        .week-title {
            font-size: 13px;
            font-weight: 700;
            color: #1e293b;
            margin: 14px 0 8px;
        }
        .table-shell table { border-collapse: separate; border-spacing: 0; }
        .table-head td { background: #1e293b; color: #f8fafc; font-size: 11px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; padding: 12px 14px; border-bottom: 1px solid #1e293b; }
        .row td { padding: 12px 14px; border-bottom: 1px solid #e2e8f0; color: #1e293b; vertical-align: top; text-align: center; }
        .row-alt td { background: #f8fafc; }
        .status-badge { display: inline-block; padding: 3px 8px; border-radius: 999px; font-size: 9px; font-weight: 700; text-transform: uppercase; }
        .status-holiday { background: #dbeafe; color: #1d4ed8; }
        .status-leave { background: #fef3c7; color: #92400e; }
        .status-absent { background: #fee2e2; color: #991b1b; }
        .summary-row td { padding: 12px 14px; background: #eef2ff; border-top: 1px solid #c7d2fe; font-weight: 700; color: #1e1b4b; }
        .muted { color: #64748b; }
    </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';
}
$avatar = !empty($profile_info->avatar) ? base_url() . $profile_info->avatar : '';
?>
<div class="page-wrap">
    <div class="brand-shell">
        <table>
            <tr>
                <td style="width: 84px; border: 0;">
                    <img class="brand-logo" src="<?= $img ?>" alt="<?= config_item('company_name') ?>">
                </td>
                <td style="border: 0;">
                    <p class="brand-title"><?= config_item('company_name') ?></p>
                    <p class="brand-text"><?= lang('works_hours_deatils') ?></p>
                </td>
            </tr>
        </table>
    </div>

    
    <table class="metrics">
        <tr>
            <td class="metric-card">
                <div class="metric-label"><?= lang('employee') ?></div>
                <p class="metric-value"><?= html_escape($profile_info->fullname) ?></p>
                <div class="metric-note"><?= lang('emp_id') ?>: <?= html_escape($profile_info->employment_id) ?></div>
            </td>
            <td class="metric-gap"></td>
            <td class="metric-card">
                <div class="metric-label"><?= lang('department') ?></div>
                <p class="metric-value"><?= !empty($department->deptname) ? html_escape($department->deptname) : '-' ?></p>
                <div class="metric-note"><?= lang('designation') ?>: <?= !empty($designation->designations) ? html_escape($designation->designations) : '-' ?></div>
            </td>
            <td class="metric-gap"></td>
            <td class="metric-card">
                <div class="metric-label"><?= lang('month') ?></div>
                <p class="metric-value"><?= date('F Y', safe_strtotime($date)) ?></p>
                <div class="metric-note"><?= lang('joining_date') ?>: <?= display_date($profile_info->joining_date) ?></div>
            </td>
            <td class="metric-gap"></td>
            <td class="metric-card">
                <div class="metric-label"><?= lang('weeks') ?></div>
                <p class="metric-value"><?= !empty($attendace_info) ? count($attendace_info) : 0 ?></p>
                <div class="metric-note"><?= lang('weekly_breakdown') ?></div>
            </td>
        </tr>
    </table>

    <div class="profile-shell">
        <table>
            <tr>
                <td style="width:130px; vertical-align:top;">
                    <div class="avatar-box">
                        <?php if (!empty($avatar)) { ?>
                            <img src="<?= $avatar ?>" alt="<?= html_escape($profile_info->fullname) ?>" width="96" height="106">
                        <?php } ?>
                    </div>
                </td>
                <td style="padding-left:18px; vertical-align:top;">
                    <p class="profile-name"><?= html_escape($profile_info->fullname) ?></p>
                    <table>
                        <tr><td class="detail-key"><?= lang('emp_id') ?></td><td class="detail-val"><?= html_escape($profile_info->employment_id) ?></td></tr>
                        <tr><td class="detail-key"><?= lang('departments') ?></td><td class="detail-val"><?= !empty($department->deptname) ? html_escape($department->deptname) : '-' ?></td></tr>
                        <tr><td class="detail-key"><?= lang('designation') ?></td><td class="detail-val"><?= !empty($designation->designations) ? html_escape($designation->designations) : '-' ?></td></tr>
                        <tr><td class="detail-key"><?= lang('joining_date') ?></td><td class="detail-val"><?= display_date($profile_info->joining_date) ?></td></tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>

    <div class="section-head">
        <p class="section-title"><?= lang('works_hours_deatils') ?> <?= date('F-Y', safe_strtotime($date)) ?></p>
        <p class="section-subtitle"><?= lang('weekly_breakdown') ?></p>
    </div>

    <?php define('SECONDS_PER_HOUR', 60 * 60); ?>
    <?php foreach ($attendace_info as $week => $v_attndc_info): ?>
        <p class="week-title"><?= lang('week') ?> : <?= $week ?></p>
        <div class="table-shell">
            <table>
                <thead>
                    <tr class="table-head">
                        <?php if (!empty($v_attndc_info)): foreach ($v_attndc_info as $date => $attendace): ?>
                            <td><?= display_date($date) ?></td>
                        <?php endforeach; endif; ?>
                    </tr>
                </thead>
                <tbody>
                    <tr class="row">
                        <?php
                        $week_total_hour = 0;
                        $week_total_minutes = 0;
                        $col_index = 0;
                        if (!empty($v_attndc_info)): foreach ($v_attndc_info as $day => $v_attendace):
                            $total_hh = 0;
                            $total_mm = 0;
                            $holiday = null;
                            $leave = null;
                            $absent = null;
                            if (!empty($v_attendace)) {
                                foreach ($v_attendace as $v_attandc) {
                                    if (!is_object($v_attandc) || !isset($v_attandc->attendance_status)) {
                                        continue;
                                    }
                                    if ($v_attandc->attendance_status == 1) {
                                        $startdatetime = safe_strtotime($v_attandc->date_in . ' ' . $v_attandc->clockin_time);
                                        $enddatetime = safe_strtotime($v_attandc->date_out . ' ' . $v_attandc->clockout_time);
                                        $difference = $enddatetime - $startdatetime;
                                        $hours = abs(floor($difference / 3600));
                                        $mins = abs(floor(($difference - ($hours * 3600)) / 60));
                                        $total_mm += $mins;
                                        $total_hh += $hours;
                                    } elseif ($v_attandc->attendance_status == 'H') {
                                        $holiday = 1;
                                    } elseif ($v_attandc->attendance_status == '3') {
                                        $leave = 1;
                                    } elseif ($v_attandc->attendance_status == '0') {
                                        $absent = 1;
                                    }
                                }
                            }
                            if ($total_mm > 59) {
                                $total_hh += intval($total_mm / 60);
                                $total_mm = intval($total_mm % 60);
                            }
                            $week_total_hour += $total_hh;
                            $week_total_minutes += $total_mm;
                        ?>
                            <td class="<?= ($col_index % 2 !== 0) ? 'row-alt' : '' ?>">
                                <?php if ($total_hh != 0 || $total_mm != 0) {
                                    echo $total_hh . ' : ' . $total_mm . ' m';
                                } 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 '0 : 0 m';
                                } ?>
                            </td>
                        <?php $col_index++; endforeach; endif; ?>
                    </tr>
                    <?php if ($week_total_minutes >= 60) {
                        $week_total_hour += intval($week_total_minutes / 60);
                        $week_total_minutes = intval($week_total_minutes % 60);
                    } ?>
                    <tr class="summary-row">
                        <td colspan="<?= !empty($v_attndc_info) ? count($v_attndc_info) : 1 ?>"><?= lang('total_working_hour') ?> : <?= $week_total_hour . ' : ' . $week_total_minutes . ' m' ?></td>
                    </tr>
                </tbody>
            </table>
        </div>
    <?php endforeach; ?>
</div>
</body>
</html>
