<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تقييم أداء: <?= e($profile->first_name_ar . ' ' . $profile->last_name_ar) ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?><a href="/hr/performance/cycles/<?= (int) $review->cycle_id ?>" class="btn btn-secondary">رجوع للدورة</a><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<?php $rsl = ['pending'=>'بانتظار التقييم','in_progress'=>'قيد التقييم','submitted'=>'مكتمل','acknowledged'=>'تم الإقرار']; ?>
<?php $rsc = ['pending'=>'#FEF3C7;color:#92400E','in_progress'=>'#FEF3C7;color:#92400E','submitted'=>'#D1FAE5;color:#065F46','acknowledged'=>'#DBEAFE;color:#1E40AF']; ?>

<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;font-size:16px;display:flex;align-items:center;gap:8px;">بيانات التقييم <span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:<?= $rsc[$review->status] ?? '#E5E7EB;color:#374151' ?>;"><?= e($rsl[$review->status] ?? $review->status) ?></span></h3></div>
    <div style="padding:16px;display:grid;grid-template-columns:repeat(auto-fit,minmax(200px,1fr));gap:16px;">
        <div><span style="color:#6B7280;font-size:13px;">الموظف</span><div style="font-weight:600;"><a href="/hr/employees/<?= (int) $profile->id ?>"><?= e($profile->first_name_ar . ' ' . $profile->last_name_ar) ?></a></div></div>
        <?php if ($department): ?><div><span style="color:#6B7280;font-size:13px;">القسم</span><div><?= e($department['name_ar']) ?></div></div><?php endif; ?>
        <div><span style="color:#6B7280;font-size:13px;">دورة التقييم</span><div><a href="/hr/performance/cycles/<?= (int) $cycle->id ?>"><?= e($cycle->name_ar) ?></a></div></div>
        <?php if ($reviewer): ?><div><span style="color:#6B7280;font-size:13px;">المقيّم</span><div><?= e($reviewer['name']) ?></div></div><?php endif; ?>
        <?php if ($review->review_date): ?><div><span style="color:#6B7280;font-size:13px;">تاريخ التقييم</span><div><?= e($review->review_date) ?></div></div><?php endif; ?>
    </div>
</div>

<?php if ((float) ($review->overall_rating ?? 0) > 0): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;font-size:16px;">التقييم العام</h3></div>
    <div style="padding:16px;text-align:center;">
        <?php $rating = (float) $review->overall_rating; $color = $rating >= 4 ? '#059669' : ($rating >= 3 ? '#D97706' : '#DC2626'); ?>
        <div style="font-size:48px;font-weight:700;color:<?= $color ?>;"><?= number_format($rating, 1) ?></div>
        <div style="font-size:14px;color:#6B7280;">من 5</div>
        <div style="margin-top:8px;">
            <?php for ($i = 1; $i <= 5; $i++): ?>
            <span style="font-size:24px;color:<?= $i <= round($rating) ? '#FBBF24' : '#D1D5DB' ?>;">&#9733;</span>
            <?php endfor; ?>
        </div>
    </div>
</div>

<?php
$scores = json_decode($review->scores_json ?? '{}', true);
?>
<?php if (!empty($scores)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;font-size:16px;">تفاصيل التقييم</h3></div>
    <div style="padding:16px;">
        <?php foreach ($scores as $criterion => $score): ?>
        <div style="display:flex;justify-content:space-between;align-items:center;padding:8px 0;border-bottom:1px solid #F3F4F6;">
            <span><?= e($criterion) ?></span>
            <div style="display:flex;align-items:center;gap:8px;">
                <div style="width:100px;height:6px;background:#E5E7EB;border-radius:3px;overflow:hidden;"><div style="height:100%;width:<?= min(100, (float) $score / 5 * 100) ?>%;background:<?= (float) $score >= 4 ? '#059669' : ((float) $score >= 3 ? '#D97706' : '#DC2626') ?>;border-radius:3px;"></div></div>
                <span style="font-weight:600;min-width:30px;"><?= number_format((float) $score, 1) ?></span>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
</div>
<?php endif; ?>

<?php $goalsText = $review->goals_json ? json_decode($review->goals_json, true) : null; ?>
<?php if ($review->strengths || $review->improvements || $goalsText): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px;display:grid;grid-template-columns:repeat(auto-fit,minmax(250px,1fr));gap:16px;">
        <?php if ($review->strengths): ?>
        <div><span style="color:#059669;font-size:13px;font-weight:600;">نقاط القوة</span><div style="margin-top:4px;padding:12px;background:#F0FDF4;border-radius:6px;line-height:1.6;"><?= nl2br(e($review->strengths)) ?></div></div>
        <?php endif; ?>
        <?php if ($review->improvements): ?>
        <div><span style="color:#D97706;font-size:13px;font-weight:600;">مجالات التحسين</span><div style="margin-top:4px;padding:12px;background:#FFFBEB;border-radius:6px;line-height:1.6;"><?= nl2br(e($review->improvements)) ?></div></div>
        <?php endif; ?>
        <?php if ($goalsText): ?>
        <div><span style="color:#2563EB;font-size:13px;font-weight:600;">الأهداف</span><div style="margin-top:4px;padding:12px;background:#EFF6FF;border-radius:6px;line-height:1.6;"><?= nl2br(e(is_string($goalsText) ? $goalsText : json_encode($goalsText, JSON_UNESCAPED_UNICODE))) ?></div></div>
        <?php endif; ?>
    </div>
</div>
<?php endif; ?>

<?php if ($review->notes): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px;">
        <span style="color:#6B7280;font-size:13px;">ملاحظات المقيّم</span>
        <div style="margin-top:4px;padding:12px;background:#F9FAFB;border-radius:6px;line-height:1.6;"><?= nl2br(e($review->notes)) ?></div>
    </div>
</div>
<?php endif; ?>
<?php endif; ?>

<?php if ($review->status !== 'acknowledged' && can('hr.performance.manage')): ?>
<div style="display:flex;gap:10px;">
    <a href="/hr/performance/reviews/<?= (int) $review->id ?>/edit" class="btn btn-primary"><?= $review->status === 'pending' ? 'إجراء التقييم' : 'تعديل التقييم' ?></a>
</div>
<?php endif; ?>
<?php $__template->endSection(); ?>
