<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title><?= lang('performance_appraisal_details') ?></title>
    <?php
    $direction = $this->session->userdata('direction');
    $RTL = (!empty($direction) && $direction == 'rtl') ? 'on' : config_item('RTL');
    $text_align = !empty($RTL) ? 'right' : 'left';
    $opposite_align = !empty($RTL) ? 'left' : 'right';

    $logo = FCPATH . config_item('company_logo');
    if (!file_exists($logo)) {
        $logo = FCPATH . 'uploads/default_logo.png';
    }

    $avatar = '';
    if (!empty($get_appraisal_info->avatar)) {
        $avatar_path = FCPATH . $get_appraisal_info->avatar;
        $avatar = file_exists($avatar_path) ? $avatar_path : base_url() . $get_appraisal_info->avatar;
    }

    if (!function_exists('appraisal_rating_label')) {
        function appraisal_rating_label($value)
        {
            if ((int)$value === 1) {
                return lang('beginner');
            } elseif ((int)$value === 2) {
                return lang('intermediate');
            } elseif ((int)$value === 3) {
                return lang('advanced');
            } elseif ((int)$value === 4) {
                return lang('expert_leader');
            }
            return lang('not_set');
        }
    }

    if (!function_exists('appraisal_rating_class')) {
        function appraisal_rating_class($value)
        {
            $value = (int)$value;
            if ($value === 1) {
                return 'level-one';
            } elseif ($value === 2) {
                return 'level-two';
            } elseif ($value === 3) {
                return 'level-three';
            } elseif ($value === 4) {
                return 'level-four';
            }
            return 'level-empty';
        }
    }

    if (!function_exists('appraisal_value')) {
        function appraisal_value($source, $field)
        {
            return (!empty($source) && isset($source->$field)) ? $source->$field : null;
        }
    }

    $technical_competencies = array(
        'customer_experiece_management' => lang('customer_experience_management'),
        'marketing' => lang('marketing'),
        'management' => lang('management'),
        'administration' => lang('administration'),
        'presentation_skill' => lang('presentation_skill'),
        'quality_of_work' => lang('quality_of_work'),
        'efficiency' => lang('efficiency'),
    );

    $behavioural_competencies = array(
        'integrity' => lang('integrity'),
        'professionalism' => lang('professionalism'),
        'team_work' => lang('team_work'),
        'critical_thinking' => lang('critical_thinking'),
        'conflict_management' => lang('conflict_management'),
        'attendance' => lang('attendance'),
        'ability_to_meed_deadline' => lang('ability_to_meet_deadline'),
    );
    ?>
    <style type="text/css">
        body {
            margin: 0;
            color: #1f2937;
            font-family: DejaVu Sans, Arial, Helvetica, sans-serif;
            font-size: 12px;
            line-height: 1.45;
        }

        .page {
            padding: 20px 22px 24px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        .report-header {
            border-bottom: 2px solid #0f172a;
            padding-bottom: 14px;
            margin-bottom: 16px;
        }

        .logo-cell {
            width: 62px;
            vertical-align: top;
        }

        .logo {
            width: 52px;
            height: 52px;
        }

        .company-name {
            color: #0f172a;
            font-size: 22px;
            font-weight: bold;
            margin: 0 0 3px;
            text-align: <?= $text_align ?>;
        }

        .report-kicker {
            color: #64748b;
            font-size: 10px;
            letter-spacing: 0.8px;
            margin: 0 0 4px;
            text-align: <?= $text_align ?>;
            text-transform: uppercase;
        }

        .report-title {
            color: #111827;
            font-size: 16px;
            font-weight: bold;
            margin: 0;
            text-align: <?= $text_align ?>;
        }

        .profile-card {
            background: #f8fafc;
            border: 1px solid #dbe4ee;
            margin-bottom: 16px;
            padding: 12px;
        }

        .avatar-cell {
            width: 112px;
            vertical-align: top;
        }

        .avatar {
            width: 96px;
            height: 104px;
            border: 1px solid #cbd5e1;
        }

        .avatar-empty {
            width: 96px;
            height: 104px;
            border: 1px solid #cbd5e1;
            background: #e2e8f0;
            color: #64748b;
            font-size: 10px;
            text-align: center;
            vertical-align: middle;
        }

        .employee-name {
            color: #0f172a;
            font-size: 19px;
            font-weight: bold;
            margin: 0 0 10px;
            text-align: <?= $text_align ?>;
        }

        .meta-table td {
            border-bottom: 1px solid #e2e8f0;
            padding: 5px 0;
            text-align: <?= $text_align ?>;
        }

        .meta-label {
            color: #64748b;
            font-size: 10px;
            text-transform: uppercase;
            width: 34%;
        }

        .meta-value {
            color: #111827;
            font-weight: bold;
        }

        .section {
            margin-top: 14px;
        }

        .section-title {
            background: #0f172a;
            border: 1px solid #0f172a;
            color: #ffffff;
            font-size: 12px;
            font-weight: bold;
            padding: 8px 10px;
            text-align: <?= $text_align ?>;
        }

        .rating-table th {
            background: #e2e8f0;
            border: 1px solid #cbd5e1;
            color: #334155;
            font-size: 10px;
            padding: 7px 8px;
            text-align: <?= $text_align ?>;
            text-transform: uppercase;
        }

        .rating-table td {
            border: 1px solid #dbe4ee;
            padding: 8px;
            text-align: <?= $text_align ?>;
            vertical-align: middle;
        }

        .rating-table tr:nth-child(even) td {
            background: #f8fafc;
        }

        .competency-col {
            width: 50%;
        }

        .rating-col {
            width: 25%;
        }

        .badge {
            border-radius: 3px;
            display: inline-block;
            font-size: 10px;
            font-weight: bold;
            padding: 4px 7px;
        }

        .level-one {
            background: #fef3c7;
            color: #92400e;
        }

        .level-two {
            background: #dbeafe;
            color: #1d4ed8;
        }

        .level-three {
            background: #dcfce7;
            color: #166534;
        }

        .level-four {
            background: #ede9fe;
            color: #5b21b6;
        }

        .level-empty {
            background: #fee2e2;
            color: #991b1b;
        }

        .remarks {
            border: 1px solid #dbe4ee;
            color: #111827;
            min-height: 52px;
            padding: 10px;
            text-align: <?= $text_align ?>;
        }

        .footer-note {
            color: #94a3b8;
            font-size: 10px;
            margin-top: 14px;
            text-align: <?= $opposite_align ?>;
        }
    </style>
</head>
<body>
<div class="page">
    <div class="report-header">
        <table>
            <tr>
                <td class="logo-cell">
                    <img class="logo" src="<?= $logo ?>" alt="<?= html_escape(config_item('company_name')) ?>">
                </td>
                <td>
                    <p class="report-kicker"><?= lang('performance') ?></p>
                    <p class="company-name"><?= html_escape(config_item('company_name')) ?></p>
                    <p class="report-title"><?= lang('performance_appraisal_of') ?> <?= html_escape($get_appraisal_info->fullname) ?></p>
                </td>
            </tr>
        </table>
    </div>

    <div class="profile-card">
        <table>
            <tr>
                <td class="avatar-cell">
                    <?php if (!empty($avatar)): ?>
                        <img class="avatar" src="<?= $avatar ?>" alt="<?= html_escape($get_appraisal_info->fullname) ?>">
                    <?php else: ?>
                        <table class="avatar-empty"><tr><td><?= lang('employee') ?></td></tr></table>
                    <?php endif; ?>
                </td>
                <td>
                    <p class="employee-name"><?= html_escape($get_appraisal_info->fullname) ?></p>
                    <table class="meta-table">
                        <tr>
                            <td class="meta-label"><?= lang('emp_id') ?></td>
                            <td class="meta-value"><?= html_escape($get_appraisal_info->employment_id) ?></td>
                        </tr>
                        <tr>
                            <td class="meta-label"><?= lang('departments') ?></td>
                            <td class="meta-value"><?= html_escape($get_appraisal_info->deptname) ?></td>
                        </tr>
                        <tr>
                            <td class="meta-label"><?= lang('designation') ?></td>
                            <td class="meta-value"><?= html_escape($get_appraisal_info->designations) ?></td>
                        </tr>
                        <tr>
                            <td class="meta-label"><?= lang('appraisal_month') ?></td>
                            <td class="meta-value"><?= date('M Y', safe_strtotime($get_appraisal_info->appraisal_month)) ?></td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>

    <div class="section">
        <div class="section-title"><?= lang('technical_competency') ?></div>
        <table class="rating-table">
            <thead>
            <tr>
                <th class="competency-col">Competency</th>
                <th class="rating-col"><?= lang('expected') ?></th>
                <th class="rating-col"><?= lang('assigned') ?></th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($technical_competencies as $field => $label): ?>
                <?php
                $expected = appraisal_value($performance_indicator_details, $field);
                $assigned = appraisal_value($get_appraisal_info, $field);
                ?>
                <tr>
                    <td><?= html_escape($label) ?></td>
                    <td><span class="badge <?= appraisal_rating_class($expected) ?>"><?= appraisal_rating_label($expected) ?></span></td>
                    <td><span class="badge <?= appraisal_rating_class($assigned) ?>"><?= appraisal_rating_label($assigned) ?></span></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <div class="section">
        <div class="section-title"><?= lang('behavioural_competency') ?></div>
        <table class="rating-table">
            <thead>
            <tr>
                <th class="competency-col">Competency</th>
                <th class="rating-col"><?= lang('expected') ?></th>
                <th class="rating-col"><?= lang('assigned') ?></th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($behavioural_competencies as $field => $label): ?>
                <?php
                $expected = appraisal_value($performance_indicator_details, $field);
                $assigned = appraisal_value($get_appraisal_info, $field);
                ?>
                <tr>
                    <td><?= html_escape($label) ?></td>
                    <td><span class="badge <?= appraisal_rating_class($expected) ?>"><?= appraisal_rating_label($expected) ?></span></td>
                    <td><span class="badge <?= appraisal_rating_class($assigned) ?>"><?= appraisal_rating_label($assigned) ?></span></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <div class="section">
        <div class="section-title"><?= lang('remarks') ?></div>
        <div class="remarks">
            <?= !empty($get_appraisal_info->general_remarks) ? nl2br(html_escape($get_appraisal_info->general_remarks)) : lang('not_set') ?>
        </div>
    </div>

    <div class="footer-note">
        <?= html_escape(config_item('company_name')) ?> &middot; <?= lang('performance') ?>
    </div>
</div>
</body>
</html>
