<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>العقد: <?= e($contract->contract_number) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if ($contract->status === 'draft' && can('hr.contract.manage')): ?>
<a href="/hr/contracts/<?= (int) $contract->id ?>/edit" class="btn btn-primary">تعديل</a>
<?php endif; ?>
<a href="/hr/contracts" class="btn btn-secondary">رجوع</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<?php $statusLabels = ['draft' => 'مسودة', 'active' => 'نشط', 'expired' => 'منتهي', 'terminated' => 'منهي', 'renewed' => 'مجدد']; ?>
<?php $statusColors = ['draft' => '#E5E7EB;color:#374151', 'active' => '#DEF7EC;color:#03543F', 'expired' => '#FEF3C7;color:#92400E', 'terminated' => '#FDE8E8;color:#9B1C1C', 'renewed' => '#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;">
            <i data-lucide="file-text" style="width:18px;height:18px;color:#6B7280;"></i> بيانات العقد
            <span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:<?= $statusColors[$contract->status] ?? '#E5E7EB;color:#374151' ?>;"><?= e($statusLabels[$contract->status] ?? $contract->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($contract->contract_number) ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">الموظف</span><div><a href="/hr/employees/<?= (int) $profile->id ?>"><?= e($profile->first_name_ar . ' ' . $profile->last_name_ar) ?></a></div></div>
        <div><span style="color:#6B7280;font-size:13px;">نوع العقد</span><div><?= $contract->contract_type === 'definite' ? 'محدد المدة' : 'غير محدد المدة' ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">تاريخ البداية</span><div><?= e($contract->start_date) ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">تاريخ النهاية</span><div><?= e($contract->end_date ?? 'غير محدد') ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">فترة الاختبار</span><div><?= (int) $contract->probation_months ?> أشهر</div></div>
        <div><span style="color:#6B7280;font-size:13px;">ساعات العمل</span><div><?= e($contract->working_hours_per_day) ?> ساعة/يوم</div></div>
        <div><span style="color:#6B7280;font-size:13px;">فترة الإنذار</span><div><?= (int) $contract->notice_period_months ?> أشهر</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) $contract->basic_salary, 2) ?> ج.م</div></div>
        <div><span style="color:#6B7280;font-size:13px;">إجمالي الحزمة</span><div style="font-weight:600;"><?= number_format((float) $contract->total_package, 2) ?> ج.م</div></div>
        <?php endif; ?>
    </div>
</div>

<?php if ($previousContract): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px;">
        <span style="color:#6B7280;font-size:13px;">العقد السابق:</span>
        <a href="/hr/contracts/<?= (int) $previousContract->id ?>"><?= e($previousContract->contract_number) ?></a>
    </div>
</div>
<?php endif; ?>

<?php if (!empty($renewals)): ?>
<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></tr></thead>
            <tbody>
                <?php foreach ($renewals as $r): ?>
                <tr>
                    <td><a href="/hr/contracts/<?= (int) $r['id'] ?>"><?= e($r['contract_number']) ?></a></td>
                    <td><?= e($r['start_date']) ?></td>
                    <td><?= e($r['end_date'] ?? '-') ?></td>
                    <td><span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:<?= $statusColors[$r['status']] ?? '#E5E7EB;color:#374151' ?>;"><?= e($statusLabels[$r['status']] ?? $r['status']) ?></span></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<?php if ($contract->status === 'active' && can('hr.contract.manage')): ?>
<div style="display:flex;gap:10px;flex-wrap:wrap;margin-top:20px;">
    <div class="card" style="flex:1;padding:16px;">
        <h4 style="margin:0 0 12px;">تجديد العقد</h4>
        <form method="POST" action="/hr/contracts/<?= (int) $contract->id ?>/renew" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
            <?= csrf_field() ?>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">تاريخ البداية الجديد</label><input type="date" name="new_start_date" class="form-control" required></div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">تاريخ النهاية الجديد</label><input type="date" name="new_end_date" class="form-control"></div>
            <button type="submit" class="btn btn-primary">تجديد</button>
        </form>
    </div>
    <div class="card" style="flex:1;padding:16px;">
        <h4 style="margin:0 0 12px;">إنهاء العقد</h4>
        <form method="POST" action="/hr/contracts/<?= (int) $contract->id ?>/terminate" onsubmit="return confirm('هل أنت متأكد من إنهاء هذا العقد؟');">
            <?= csrf_field() ?>
            <div style="margin-bottom:8px;"><label style="display:block;margin-bottom:4px;font-size:13px;">تاريخ الإنهاء</label><input type="date" name="termination_date" value="<?= date('Y-m-d') ?>" class="form-control"></div>
            <div style="margin-bottom:8px;"><label style="display:block;margin-bottom:4px;font-size:13px;">السبب <span style="color:#DC2626;">*</span></label><textarea name="termination_reason" class="form-control" rows="2" required></textarea></div>
            <button type="submit" class="btn btn-danger">إنهاء العقد</button>
        </form>
    </div>
</div>
<?php endif; ?>
<script>if(typeof lucide!=='undefined')lucide.createIcons();</script>
<?php $__template->endSection(); ?>
