<?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/employees/<?= (int) $profile->id ?>/edit" class="btn btn-primary" style="display:inline-flex;align-items:center;gap:6px;">
    <i data-lucide="edit" style="width:16px;height:16px;"></i> تعديل
</a>
<a href="/hr/employees/<?= (int) $profile->id ?>/salary" class="btn btn-secondary">تفاصيل الراتب</a>
<a href="/hr/employees" class="btn btn-secondary">رجوع</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<?php
$statusColors = ['active' => '#DEF7EC;color:#03543F', 'on_leave' => '#FEF3C7;color:#92400E', 'suspended' => '#FDE8E8;color:#9B1C1C', 'terminated' => '#E5E7EB;color:#374151', 'resigned' => '#E5E7EB;color:#374151'];
$sc = $statusColors[$profile->employment_status] ?? '#E5E7EB;color:#374151';
?>

<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;">
            <i data-lucide="user" style="width:18px;height:18px;color:#6B7280;"></i> البيانات الشخصية
            <span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:<?= $sc ?>;"><?= e($statuses[$profile->employment_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;"><?= e($profile->employee_number) ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">الاسم بالعربي</span><div style="font-weight:600;"><?= e($profile->first_name_ar . ' ' . $profile->last_name_ar) ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">الرقم القومي</span><div><?= e($profile->national_id) ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">تاريخ الميلاد</span><div><?= e($profile->date_of_birth ?? '-') ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">الجنس</span><div><?= e($genders[$profile->gender] ?? '-') ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">الحالة الاجتماعية</span><div><?= e($maritalStatuses[$profile->marital_status] ?? '-') ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">الهاتف</span><div><?= e($profile->phone ?? '-') ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">البريد</span><div><?= e($profile->email ?? '-') ?></div></div>
    </div>
</div>

<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;">
            <i data-lucide="briefcase" style="width:18px;height:18px;color:#6B7280;"></i> البيانات الوظيفية
        </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><?= $department ? '<a href="/hr/departments/' . (int) $department->id . '">' . e($department->name_ar) . '</a>' : '-' ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">المسمى الوظيفي</span><div><?= $jobTitle ? e($jobTitle->name_ar) : '-' ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">تاريخ التعيين</span><div><?= e($profile->hire_date) ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">سنوات الخدمة</span><div style="font-weight:600;"><?= number_format($yearsOfService, 1) ?> سنة</div></div>
        <div><span style="color:#6B7280;font-size:13px;">نوع التوظيف</span><div><?= e($types[$profile->employment_type] ?? '-') ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">هيكل الرواتب</span><div><?= $structure ? e($structure->name_ar) : '-' ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">الراتب الأساسي</span><div style="font-weight:600;"><?= number_format((float) $profile->basic_salary, 2) ?> ج.م</div></div>
        <div><span style="color:#6B7280;font-size:13px;">رقم التأمينات</span><div><?= e($profile->insurance_number ?? '-') ?></div></div>
    </div>
</div>

<?php if ($activeContract): ?>
<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;">
            <i data-lucide="file-text" style="width:18px;height:18px;color:#6B7280;"></i> العقد الحالي
        </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><a href="/hr/contracts/<?= (int) $activeContract['id'] ?>"><?= e($activeContract['contract_number']) ?></a></div></div>
        <div><span style="color:#6B7280;font-size:13px;">نوع العقد</span><div><?= $activeContract['contract_type'] === 'definite' ? 'محدد المدة' : 'غير محدد المدة' ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">تاريخ البداية</span><div><?= e($activeContract['start_date']) ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">تاريخ النهاية</span><div><?= e($activeContract['end_date'] ?? 'غير محدد') ?></div></div>
    </div>
</div>
<?php endif; ?>

<?php if (!empty($recentLeaves)): ?>
<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 class="table-responsive">
        <table class="data-table">
            <thead><tr><th>النوع</th><th>من</th><th>إلى</th><th>الأيام</th><th>الحالة</th></tr></thead>
            <tbody>
                <?php foreach ($recentLeaves as $lv): ?>
                <tr>
                    <td><?= e($lv['leave_type_name']) ?></td>
                    <td><?= e($lv['start_date']) ?></td>
                    <td><?= e($lv['end_date']) ?></td>
                    <td><?= number_format((float) $lv['total_days'], 1) ?></td>
                    <td>
                        <?php
                        $lvColors = ['pending' => '#FEF3C7;color:#92400E', 'approved' => '#DEF7EC;color:#03543F', 'rejected' => '#FDE8E8;color:#9B1C1C', 'cancelled' => '#E5E7EB;color:#374151'];
                        $lvLabels = ['pending' => 'معلق', 'approved' => 'موافق', 'rejected' => 'مرفوض', 'cancelled' => 'ملغي'];
                        ?>
                        <span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:<?= $lvColors[$lv['status']] ?? '#E5E7EB;color:#374151' ?>;"><?= e($lvLabels[$lv['status']] ?? $lv['status']) ?></span>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<div style="display:flex;gap:10px;flex-wrap:wrap;">
    <a href="/hr/contracts/create?employee_id=<?= (int) $profile->id ?>" class="btn btn-secondary">إنشاء عقد</a>
    <a href="/hr/documents/employee/<?= (int) $profile->id ?>" class="btn btn-secondary">المستندات</a>
    <a href="/hr/attendance/monthly/<?= (int) $profile->id ?>" class="btn btn-secondary">سجل الحضور</a>
    <a href="/hr/leaves/balances/<?= (int) $profile->id ?>" class="btn btn-secondary">أرصدة الإجازات</a>
</div>
<script>if(typeof lucide!=='undefined')lucide.createIcons();</script>
<?php $__template->endSection(); ?>
