<!DOCTYPE html>
<html>
<head>
    <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');
    }

    $rootPath = defined('ROOTPATH') ? ROOTPATH : FCPATH;
    $img = '';
    $logo_candidates = array(
        config_item('invoice_logo'),
        config_item('company_logo'),
        'uploads/default_logo.png',
    );
    foreach ($logo_candidates as $logo_path) {
        if (empty($logo_path)) {
            continue;
        }
        $logo_path = trim((string) $logo_path);
        $is_absolute_windows = preg_match('/^[A-Za-z]:[\\\\\/]/', $logo_path) === 1;
        $is_absolute_unix = isset($logo_path[0]) && ($logo_path[0] === '/' || $logo_path[0] === '\\');
        if ($is_absolute_windows || $is_absolute_unix) {
            $local_logo = $logo_path;
        } else {
            $local_logo = rtrim($rootPath, '/\\') . DIRECTORY_SEPARATOR . ltrim($logo_path, '/\\');
        }
        if (is_file($local_logo) && is_readable($local_logo)) {
            $img = $local_logo;
            break;
        }
    }

    $currency = $this->db->where('code', config_item('default_currency'))->get('tbl_currencies')->row();
    $design_info = $this->db->where('designations_id', $training_info->designations_id)->get('tbl_designations')->row();
    $dept_info = !empty($design_info) ? $this->db->where('departments_id', $design_info->departments_id)->get('tbl_departments')->row() : null;

    $status_key = 'terminated';
    $status_text = lang('terminated');
    if ((string) $training_info->status === '0') {
        $status_key = 'pending';
        $status_text = lang('pending');
    } elseif ((string) $training_info->status === '1') {
        $status_key = 'started';
        $status_text = lang('started');
    } elseif ((string) $training_info->status === '2') {
        $status_key = 'completed';
        $status_text = lang('completed');
    }

    $performance_key = 'excellent';
    $performance_text = lang('excellent');
    if ((string) $training_info->performance === '0') {
        $performance_key = 'pending';
        $performance_text = lang('not_concluded');
    } elseif ((string) $training_info->performance === '1') {
        $performance_key = 'started';
        $performance_text = lang('satisfactory');
    } elseif ((string) $training_info->performance === '2') {
        $performance_key = 'average';
        $performance_text = lang('average');
    } elseif ((string) $training_info->performance === '3') {
        $performance_key = 'terminated';
        $performance_text = lang('poor');
    }

    $avatar = '';
    if (!empty($training_info->avatar)) {
        $avatarPath = $rootPath . '/' . ltrim($training_info->avatar, '/');
        if (file_exists($avatarPath)) {
            $avatar = $avatarPath;
        } else {
            $avatar = base_url() . ltrim($training_info->avatar, '/');
        }
    }

    $detail_rows = array(
        array(lang('emp_id'), !empty($training_info->employment_id) ? $training_info->employment_id : '-'),
        array(lang('departments'), !empty($dept_info->deptname) ? $dept_info->deptname : '-'),
        array(lang('designation'), !empty($design_info->designations) ? $design_info->designations : '-'),
        array(lang('joining_date'), !empty($training_info->joining_date) ? display_date($training_info->joining_date) : '-'),
        array(lang('course_training'), !empty($training_info->training_name) ? $training_info->training_name : '-'),
        array(lang('vendor'), !empty($training_info->vendor_name) ? $training_info->vendor_name : '-'),
        array(lang('start_date'), !empty($training_info->start_date) ? display_date($training_info->start_date) : '-'),
        array(lang('finish_date'), !empty($training_info->finish_date) ? display_date($training_info->finish_date) : '-'),
    );
    ?>
    <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: 14px 18px;
            background: #f8fafc;
            margin-bottom: 18px;
        }

        .brand-logo {
            width: 46px;
            height: 46px;
            background: #ffffff;
            border: 1px solid #d7deea;
            padding: 4px;
        }

        .brand-title {
            font-size: 15px;
            font-weight: 400;
            color: #0f172a;
            margin: 0 0 2px;
            line-height: 1.2;
        }

        .brand-text {
            font-size: 12px;
            color: #64748b;
            margin: 0;
            line-height: 1.2;
        }

        .report-meta {
            width: 280px;
            vertical-align: top;
        <?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 2px;
            line-height: 1.1;
        }

        .header-subtitle {
            font-size: 10px;
            color: #64748b;
            margin: 0 0 12px;
            line-height: 1.1;
        }

        .range-chip {
            display: block;
            margin-top: 6px;
            background: transparent;
            color: #475569;
            font-size: 9px;
            font-weight: 700;
            letter-spacing: .08em;
            text-transform: uppercase;
            line-height: 1.2;
        }

        .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;
        }

        .table-shell {
            border: 1px solid #d7deea;
            overflow: hidden;
            background: #ffffff;
            margin-bottom: 12px;
        }

        .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;
        }

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

        .key {
            width: 38%;
            font-size: 10px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: .06em;
            color: #64748b;
        }

        .val {
            font-size: 12px;
            color: #0f172a;
            line-height: 1.45;
        }

        .profile-card {
            border: 1px solid #d7deea;
            background: #ffffff;
            margin-bottom: 18px;
        }

        .avatar-cell {
            width: 140px;
            padding: 16px;
            vertical-align: top;
        }

        .avatar-box {
            width: 112px;
            height: 124px;
            border: 1px solid #d7deea;
            background: #f8fafc;
            padding: 8px;
            text-align: center;
            overflow: hidden;
        }

        .avatar-box img {
            width: auto;
            height: auto;
            max-width: 96px;
            max-height: 106px;
        }

        .profile-meta {
            padding: 16px 18px 16px 0;
            vertical-align: top;
        }

        .profile-name {
            font-size: 22px;
            font-weight: 700;
            color: #0f172a;
            margin: 0 0 8px;
        }

        .pill {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 999px;
            font-size: 9px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: .08em;
        }

        .pill-completed,
        .pill-excellent {
            background: #dcfce7;
            color: #166534;
        }

        .pill-started,
        .pill-average {
            background: #dbeafe;
            color: #1d4ed8;
        }

        .pill-pending {
            background: #fef3c7;
            color: #92400e;
        }

        .pill-terminated {
            background: #fee2e2;
            color: #991b1b;
        }

        .note-box {
            border: 1px solid #d7deea;
            background: #ffffff;
            padding: 14px 16px;
        }

        .note-title {
            font-size: 10px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: .08em;
            color: #475569;
            margin-bottom: 8px;
        }

        .note-body {
            border: 1px solid #e2e8f0;
            background: #f8fafc;
            padding: 12px 14px;
            font-size: 12px;
            color: #334155;
            line-height: 1.5;
            min-height: 34px;
        }

        .footer {
            border-top: 1px solid #e2e8f0;
            padding-top: 8px;
            font-size: 10px;
            color: #64748b;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="page-wrap">
        <div class="brand-shell">
            <table>
                <tr>
                    <td style="width: 65px; border: 0; vertical-align: top;">
                        <?php if (!empty($img)) { ?>
                            <img class="brand-logo" src="<?= $img ?>" alt="<?= html_escape(config_item('company_name')) ?>">
                        <?php } ?>
                    </td>
                    <td style="border: 0; padding-left: 2px; vertical-align: top;">
                        <p class="brand-title"><?= html_escape(config_item('company_name')) ?></p>
                        <p class="brand-text"><?= html_escape(config_item('company_address')) ?></p>
                    </td>
                    <td class="report-meta" style="border:0; padding-top: 7px;">
                        <p class="header-title"><?= lang('training_details') ?></p>
                        <p class="header-subtitle"><?= lang('employee_training_profile') ?> <?= lang('for') ?> <?= html_escape($training_info->fullname) ?></p>
                        <div style="border:0;" class="range-chip"><?= lang('generated_on') ?> <?= display_date(date('Y-m-d')) ?></div>
                    </td>
                </tr>
            </table>
        </div>

        <table class="metrics">
            <tr>
                <td class="metric-card">
                    <div class="metric-label"><?= lang('training_cost') ?></div>
                    <p class="metric-value"><?= display_money($training_info->training_cost, $currency->symbol) ?></p>
                    <div class="metric-note"><?= lang('vendor') ?>: <?= !empty($training_info->vendor_name) ? html_escape($training_info->vendor_name) : '-' ?></div>
                </td>
                <td class="metric-gap"></td>
                <td class="metric-card">
                    <div class="metric-label"><?= lang('status') ?></div>
                    <p class="metric-value" style="font-size:15px;">
                        <span class="pill pill-<?= html_escape($status_key) ?>"><?= html_escape($status_text) ?></span>
                    </p>
                    <div class="metric-note"><?= lang('course_training') ?>: <?= !empty($training_info->training_name) ? html_escape($training_info->training_name) : '-' ?></div>
                </td>
                <td class="metric-gap"></td>
                <td class="metric-card">
                    <div class="metric-label"><?= lang('performance') ?></div>
                    <p class="metric-value" style="font-size:15px;">
                        <span class="pill pill-<?= html_escape($performance_key) ?>"><?= html_escape($performance_text) ?></span>
                    </p>
                    <div class="metric-note"><?= lang('start_date') ?>: <?= !empty($training_info->start_date) ? display_date($training_info->start_date) : '-' ?></div>
                </td>
                <td class="metric-gap"></td>
                <td class="metric-card">
                    <div class="metric-label"><?= lang('finish_date') ?></div>
                    <p class="metric-value" style="font-size:15px;"><?= !empty($training_info->finish_date) ? display_date($training_info->finish_date) : '-' ?></p>
                    <div class="metric-note"><?= lang('departments') ?>: <?= !empty($dept_info->deptname) ? html_escape($dept_info->deptname) : '-' ?></div>
                </td>
            </tr>
        </table>

        <div class="profile-card">
            <table>
                <tr>
                    <td class="avatar-cell">
                        <div class="avatar-box">
                            <?php if (!empty($avatar)) { ?>
                                <img src="<?= $avatar ?>" alt="<?= html_escape($training_info->fullname) ?>" width="96" height="106">
                            <?php } ?>
                        </div>
                    </td>
                    <td class="profile-meta">
                        <p class="profile-name"><?= html_escape($training_info->fullname) ?></p>
                        <table>
                            <tbody>
                                <?php foreach (array_slice($detail_rows, 0, 4) as $index => $row) : ?>
                                    <tr class="row<?= ($index % 2 !== 0) ? ' row-alt' : '' ?>">
                                        <td class="key"><?= html_escape($row[0]) ?></td>
                                        <td class="val"><?= html_escape($row[1]) ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </td>
                </tr>
            </table>
        </div>

        <div class="table-shell">
            <table>
                <thead>
                    <tr class="table-head">
                        <td colspan="2"><?= lang('training_details') ?></td>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach (array_slice($detail_rows, 4) as $index => $row) : ?>
                        <tr class="row<?= ($index % 2 !== 0) ? ' row-alt' : '' ?>">
                            <td class="key"><?= html_escape($row[0]) ?></td>
                            <td class="val"><?= html_escape($row[1]) ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>

        <?php if (!empty(strip_html_tags($training_info->remarks, true))) { ?>
            <div class="note-box">
                <div class="note-title"><?= lang('remarks') ?></div>
                <div class="note-body"><?= nl2br(strip_html_tags($training_info->remarks, true)); ?></div>
            </div>
        <?php } ?>

        <div class="footer">
            <?= html_escape(config_item('company_name')) ?> | <?= lang('generated_on') ?> <?= display_date(date('Y-m-d')) ?>
        </div>
    </div>
</body>
</html>
