<!DOCTYPE html>
<html>
<head>
    <title><?= lang('overtime_report') ?></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <?php
    $direction = $this->session->userdata('direction');
    if (!empty($direction) && $direction == 'rtl') {
        $RTL = 'on';
    } else {
        $RTL = config_item('RTL');
    }

    $hh = 0;
    $mm = 0;
    $entry_count = 0;
    $approved_count = 0;
    if (!empty($overtime_info)) {
        foreach ($overtime_info as $entry) {
            $hh += (int) date('H', safe_strtotime($entry->overtime_hours));
            $mm += (int) date('i', safe_strtotime($entry->overtime_hours));
            $entry_count++;
            if (!empty($entry->status) && $entry->status === 'approved') {
                $approved_count++;
            }
        }
    }
    if ($mm > 59) {
        $hh += intval($mm / 60);
        $mm = intval($mm % 60);
    }
    $total_overtime = sprintf('%02d : %02d m', $hh, $mm);
    ?>
    <style>
        body {
            margin: 0;
            color: #172033;
            background: #ffffff;
            font: 12px/1.55 DejaVu Sans, Arial, Helvetica, sans-serif;
            min-width: 100%;
            min-height: 100%;
        <?php if (!empty($RTL)) { ?> text-align: right;
        <?php } ?>
        }
        table {
            width: 100%;
            border-collapse: collapse;
            border-spacing: 0;
        <?php if (!empty($RTL)) { ?> text-align: right;
        <?php } ?>
        }
        .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-meta {
            width: 280px;
        <?php if (!empty($RTL)) { ?> text-align: left;
        <?php } else { ?> text-align: right;
        <?php } ?>
        }
        .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; }
        .range-chip { display: inline-block; padding-top: 4px; color: #475569; font-size: 9px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
        .metrics { margin: 0 0 18px; }
        .metric-card { width: 32%; 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: 21px; font-weight: 700; color: #0f172a; margin: 0; }
        .metric-note { font-size: 11px; color: #94a3b8; margin-top: 4px; }
        .table-shell { border: 1px solid #d7deea; overflow: hidden; background: #ffffff; }
        .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; }
        .row td { padding: 12px 14px; border-bottom: 1px solid #e2e8f0; color: #1e293b; vertical-align: top; }
        .row-alt td { background: #f8fafc; }
        .number {
        <?php if (!empty($RTL)) { ?> text-align: left;
        <?php } else { ?> text-align: right;
        <?php } ?>
        }
        .strong { font-weight: 700; color: #0f172a; }
        .muted { color: #64748b; }
        .status-approved { color: #047857; font-weight: 700; }
        .status-pending { color: #b45309; font-weight: 700; }
        .status-rejected { color: #be123c; font-weight: 700; }
        .total-row td { padding: 14px; background: #eef2ff; border-top: 1px solid #c7d2fe; font-weight: 700; color: #1e1b4b; }
    </style>
</head>
<body>
<?php
$rootPath = defined('ROOTPATH') ? ROOTPATH : FCPATH;
$img = $rootPath . '/' . config_item('company_logo');
$logoExists = file_exists($img);
if (empty($logoExists)) {
    $img = base_url() . config_item('company_logo');
}
if (!file_exists($img)) {
    $img = $rootPath . '/' . 'uploads/default_logo.png';
}
?>
<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('overtime_report') ?></p>
                </td>
                <td class="report-meta" style="border: 0;">
                    <p class="header-title"><?= lang('overtime_report') ?></p>
                    <p class="header-subtitle"><?= $monthyaer ?></p>
                    <span class="range-chip"><?= lang('generated_on') ?> <?= display_date(date('Y-m-d')) ?></span>
                </td>
            </tr>
        </table>
    </div>

    <table class="metrics">
        <tr>
            <td class="metric-card">
                <div class="metric-label"><?= lang('month') ?></div>
                <p class="metric-value"><?= $monthyaer ?></p>
                <div class="metric-note"><?= lang('overtime_report') ?></div>
            </td>
            <td class="metric-gap"></td>
            <td class="metric-card">
                <div class="metric-label"><?= lang('total_overtime_hour') ?></div>
                <p class="metric-value"><?= $total_overtime ?></p>
                <div class="metric-note"><?= $approved_count ?> <?= lang('approved') ?></div>
            </td>
            <td class="metric-gap"></td>
            <td class="metric-card">
                <div class="metric-label"><?= lang('entries') ?></div>
                <p class="metric-value"><?= $entry_count ?></p>
                <div class="metric-note"><?= lang('records') ?></div>
            </td>
        </tr>
    </table>

    <div class="table-shell">
        <table>
            <thead>
            <tr class="table-head">
                <td style="width: 50px;"><?= lang('sl') ?></td>
                <td><?= lang('name') ?></td>
                <td><?= lang('overtime_date') ?></td>
                <td><?= lang('overtime_hour') ?></td>
                <td><?= lang('status') ?></td>
            </tr>
            </thead>
            <tbody>
            <?php if (!empty($overtime_info)) : ?>
                <?php $row_index = 0; ?>
                <?php foreach ($overtime_info as $index => $entry) : ?>
                    <?php
                    $status = !empty($entry->status) ? $entry->status : 'pending';
                    $status_class = 'status-pending';
                    if ($status === 'approved') {
                        $status_class = 'status-approved';
                    } elseif ($status === 'rejected') {
                        $status_class = 'status-rejected';
                    }
                    ?>
                    <tr class="row<?= ($row_index % 2 !== 0) ? ' row-alt' : '' ?>">
                        <td><?= $index + 1 ?></td>
                        <td><?= !empty($entry->fullname) ? $entry->fullname : lang('unknown') ?></td>
                        <td><?= !empty($entry->overtime_date) ? display_date($entry->overtime_date) : '-' ?></td>
                        <td class="strong"><?= !empty($entry->overtime_hours) ? $entry->overtime_hours : '-' ?></td>
                        <td class="<?= $status_class ?>"><?= lang($status) ?></td>
                    </tr>
                    <?php $row_index++; ?>
                <?php endforeach; ?>
                <tr class="total-row">
                    <td colspan="3"><?= lang('total_overtime_hour') ?></td>
                    <td><?= $total_overtime ?></td>
                    <td><?= $entry_count ?> <?= lang('records') ?></td>
                </tr>
            <?php else : ?>
                <tr class="row">
                    <td colspan="5" class="muted"><?= lang('nothing_to_display') ?></td>
                </tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>
