<?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'); ?>
<?php if (can('hr.employee.manage')): ?>
<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>
<?php endif; ?>
<?php if (can('hr.employee.view_salary')): ?>
<a href="/hr/employees/<?= (int) $profile->id ?>/salary" class="btn btn-secondary">تفاصيل الراتب</a>
<?php endif; ?>
<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->nationality ?? '-') ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">الموقف من التجنيد</span><div><?= e($militaryStatuses[$profile->military_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 && $department->parent_id) ? '<a href="/hr/departments/' . (int) $department->parent_id . '">' . e($idara->name_ar ?? '-') . '</a>' : ($department && ($department->department_type ?? '') === 'idara' ? '<a href="/hr/departments/' . (int) $department->id . '">' . e($department->name_ar) . '</a>' : '-') ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">القسم</span><div><?= ($department && ($department->department_type ?? '') === 'qism') ? '<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><?= e($workforceTypes[$profile->workforce_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><?= e($profile->direct_manager ?? '-') ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">كود البصمة</span><div><?= e($profile->fingerprint_code ?? '-') ?></div></div>
        <?php if (can('hr.employee.view_salary')): ?>
        <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 style="font-weight:700;font-size:15px;color:#047857;"><?= number_format((float) ($comprehensiveSalary ?? 0), 2) ?> ج.م</div></div>
        <div><span style="color:#6B7280;font-size:13px;">الأجر التأميني</span><div><?= number_format((float) ($profile->insurance_salary ?? 0), 2) ?> ج.م</div></div>
        <?php endif; ?>
        <div><span style="color:#6B7280;font-size:13px;">الرقم التأميني</span><div><?= e($profile->insurance_number ?? '-') ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">تاريخ بداية التأمين</span><div><?= e($profile->insurance_start_date ?? '-') ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">رقم البطاقة الضريبية</span><div><?= e($profile->tax_card_number ?? '-') ?></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="graduation-cap" 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><?= e($profile->education_level ?? '-') ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">التخصص</span><div><?= e($profile->education_specialization ?? '-') ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">الجامعة / المؤسسة التعليمية</span><div><?= e($profile->education_institution ?? '-') ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">سنة التخرج</span><div><?= e($profile->education_year ?? '-') ?></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;">
    <?php if (can('hr.contract.manage')): ?>
    <a href="/hr/contracts/create?employee_id=<?= (int) $profile->id ?>" class="btn btn-secondary">إنشاء عقد</a>
    <?php endif; ?>
    <?php if (can('hr.document.view')): ?>
    <a href="/hr/documents/employee/<?= (int) $profile->id ?>" class="btn btn-secondary">المستندات</a>
    <?php endif; ?>
    <?php if (can('hr.attendance.view')): ?>
    <a href="/hr/attendance/monthly/<?= (int) $profile->id ?>" class="btn btn-secondary">سجل الحضور</a>
    <?php endif; ?>
    <?php if (can('hr.leave.view')): ?>
    <a href="/hr/leaves/balances/<?= (int) $profile->id ?>" class="btn btn-secondary">أرصدة الإجازات</a>
    <?php endif; ?>
</div>
<script>if(typeof lucide!=='undefined')lucide.createIcons();</script>
<?php $__template->endSection(); ?>
