<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>نهاية خدمة #<?= (int) $record->id ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?><a href="/hr/end-of-service" class="btn btn-secondary">رجوع</a><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<?php $sl = ['draft'=>'مسودة','calculated'=>'تم الحساب','pending_approval'=>'في انتظار الاعتماد','approved'=>'معتمد','paid'=>'تم الصرف','cancelled'=>'ملغى']; ?>
<?php $sc = ['draft'=>'#FEF3C7;color:#92400E','calculated'=>'#DBEAFE;color:#1E40AF','pending_approval'=>'#FDE8E8;color:#9B1C1C','approved'=>'#D1FAE5;color:#065F46','paid'=>'#E5E7EB;color:#374151','cancelled'=>'#E5E7EB;color:#6B7280']; ?>
<?php $tl = ['resignation'=>'استقالة','termination'=>'فصل','end_of_contract'=>'انتهاء عقد','retirement'=>'تقاعد','death'=>'وفاة','disability'=>'عجز','mutual_agreement'=>'اتفاق مشترك']; ?>

<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:<?= $sc[$record->status] ?? '#E5E7EB;color:#374151' ?>;"><?= e($sl[$record->status] ?? $record->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;"><?php if ($profile): ?><a href="/hr/employees/<?= (int) $profile->id ?>"><?= e($profile->first_name_ar . ' ' . $profile->last_name_ar) ?></a><?php else: ?>-<?php endif; ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">نوع الإنهاء</span><div><?= e($tl[$record->termination_type] ?? $record->termination_type) ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">تاريخ الإنهاء</span><div><?= e($record->effective_date ?? '-') ?></div></div>
        <?php if ($record->years_of_service !== null): ?><div><span style="color:#6B7280;font-size:13px;">سنوات الخدمة</span><div style="font-weight:600;"><?= number_format((float) $record->years_of_service, 1) ?> سنة</div></div><?php endif; ?>
        <?php if ($approver): ?><div><span style="color:#6B7280;font-size:13px;">معتمد بواسطة</span><div><?= e($approver['name']) ?></div></div><?php endif; ?>
    </div>
    <?php if ($record->notes): ?>
    <div style="padding:0 16px 16px;">
        <span style="color:#6B7280;font-size:13px;">ملاحظات</span>
        <div style="margin-top:4px;padding:12px;background:#F9FAFB;border-radius:6px;"><?= nl2br(e($record->notes)) ?></div>
    </div>
    <?php endif; ?>
</div>

<?php if ($record->status !== 'draft'): ?>
<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;">
        <table style="width:100%;border-collapse:collapse;">
            <tbody>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:10px 0;color:#6B7280;">آخر راتب أساسي</td>
                    <td style="padding:10px 0;text-align:left;font-weight:600;"><?= number_format((float) ($record->last_basic_salary ?? 0), 2) ?> ج.م</td>
                </tr>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:10px 0;color:#6B7280;">مكافأة نهاية الخدمة</td>
                    <td style="padding:10px 0;text-align:left;font-weight:600;"><?= number_format((float) ($record->total_severance ?? 0), 2) ?> ج.م</td>
                </tr>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:10px 0;color:#6B7280;">تعويض فترة الإنذار</td>
                    <td style="padding:10px 0;text-align:left;"><?= number_format((float) ($record->notice_period_compensation ?? 0), 2) ?> ج.م</td>
                </tr>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:10px 0;color:#6B7280;">رصيد الإجازات</td>
                    <td style="padding:10px 0;text-align:left;"><?= number_format((float) ($record->leave_compensation ?? 0), 2) ?> ج.م</td>
                </tr>
                <tr style="border-bottom:1px solid #F3F4F6;background:#F0FDF4;">
                    <td style="padding:10px 0;font-weight:700;">إجمالي المستحقات</td>
                    <td style="padding:10px 0;text-align:left;font-weight:700;color:#059669;"><?= number_format((float) ($record->total_entitlement ?? 0), 2) ?> ج.م</td>
                </tr>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:10px 0;color:#DC2626;">(-) خصومات (سلف وغيرها)</td>
                    <td style="padding:10px 0;text-align:left;color:#DC2626;"><?= number_format((float) ($record->total_deductions ?? 0), 2) ?> ج.م</td>
                </tr>
                <tr style="background:#EFF6FF;">
                    <td style="padding:12px 0;font-weight:700;font-size:16px;">صافي التسوية</td>
                    <td style="padding:12px 0;text-align:left;font-weight:700;font-size:18px;color:#1D4ED8;"><?= number_format((float) ($record->net_settlement ?? 0), 2) ?> ج.م</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<div style="display:flex;gap:10px;flex-wrap:wrap;">
    <?php if (in_array($record->status, ['draft', 'calculated'], true)): ?>
    <form method="POST" action="/hr/end-of-service/<?= (int) $record->id ?>/calculate"><?= csrf_field() ?><button type="submit" class="btn btn-primary"><?= $record->status === 'draft' ? 'حساب المستحقات' : 'إعادة الحساب' ?></button></form>
    <?php endif; ?>
    <?php if ($record->status === 'calculated'): ?>
    <form method="POST" action="/hr/end-of-service/<?= (int) $record->id ?>/approve"><?= csrf_field() ?><button type="submit" class="btn btn-primary">اعتماد</button></form>
    <?php endif; ?>
    <?php if ($record->status === 'approved'): ?>
    <form method="POST" action="/hr/end-of-service/<?= (int) $record->id ?>/pay" style="display:flex;gap:8px;align-items:end;">
        <?= csrf_field() ?>
        <div><label style="display:block;margin-bottom:4px;font-size:13px;">تاريخ الصرف</label><input type="date" name="payment_date" value="<?= date('Y-m-d') ?>" class="form-control"></div>
        <button type="submit" class="btn btn-primary">صرف المستحقات</button>
    </form>
    <?php endif; ?>
</div>
<?php $__template->endSection(); ?>
