<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>إجراء تأديبي #<?= (int) $action->id ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?><a href="/hr/disciplinary" class="btn btn-secondary">رجوع</a><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<?php $sl = ['investigation'=>'تحت التحقيق','pending_decision'=>'في انتظار القرار','decided'=>'تم تحديد العقوبة','appealed'=>'متظلم','appeal_reviewed'=>'تم مراجعة التظلم','enforced'=>'منفذ','cancelled'=>'ملغى']; ?>
<?php $sc = ['investigation'=>'#DBEAFE;color:#1E40AF','pending_decision'=>'#FEF3C7;color:#92400E','decided'=>'#FDE8E8;color:#9B1C1C','appealed'=>'#E0E7FF;color:#3730A3','appeal_reviewed'=>'#E0E7FF;color:#3730A3','enforced'=>'#DEF7EC;color:#03543F','cancelled'=>'#E5E7EB;color:#374151']; ?>
<?php $pl = ['warning'=>'إنذار','wage_deduction'=>'خصم من الراتب','raise_postponement'=>'تأجيل علاوة','suspension'=>'إيقاف','demotion'=>'تنزيل درجة','termination'=>'فصل']; ?>

<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[$action->status] ?? '#E5E7EB;color:#374151' ?>;"><?= e($sl[$action->status] ?? $action->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($action->incident_date ?? '-') ?></div></div>
        <?php if ($action->action_number): ?><div><span style="color:#6B7280;font-size:13px;">رقم الإجراء</span><div><?= e($action->action_number) ?></div></div><?php endif; ?>
    </div>
    <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;line-height:1.6;"><?= nl2br(e($action->incident_description ?? '')) ?></div>
    </div>
    <?php if ($action->investigation_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;line-height:1.6;"><?= nl2br(e($action->investigation_notes)) ?></div>
    </div>
    <?php endif; ?>
    <?php if ($action->investigation_result): ?>
    <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;line-height:1.6;"><?= nl2br(e($action->investigation_result)) ?></div>
    </div>
    <?php endif; ?>
</div>

<?php if ($action->penalty_type): ?>
<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;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:700;color:#DC2626;"><?= e($pl[$action->penalty_type] ?? $action->penalty_type) ?></div></div>
        <?php $pd = (int) ($action->wage_deduction_days ?? $action->suspension_days ?? 0); if ($pd > 0): ?><div><span style="color:#6B7280;font-size:13px;">عدد الأيام</span><div style="font-weight:600;"><?= $pd ?> يوم</div></div><?php endif; ?>
        <div><span style="color:#6B7280;font-size:13px;">تاريخ التنفيذ</span><div><?= e($action->effective_date ?? '-') ?></div></div>
        <?php if ($decider): ?><div><span style="color:#6B7280;font-size:13px;">قرار بواسطة</span><div><?= e($decider['name']) ?></div></div><?php endif; ?>
    </div>
    <?php if ($action->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($action->notes)) ?></div>
    </div>
    <?php endif; ?>
</div>
<?php endif; ?>

<?php if ($action->status === 'appealed' || $action->appeal_text): ?>
<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 if ($action->appeal_date): ?><div style="margin-bottom:8px;"><span style="color:#6B7280;font-size:13px;">تاريخ التظلم:</span> <?= e($action->appeal_date) ?></div><?php endif; ?>
        <div style="padding:12px;background:#FEF3C7;border-radius:6px;"><?= nl2br(e($action->appeal_text ?? '')) ?></div>
    </div>
</div>
<?php endif; ?>

<div style="display:flex;gap:10px;flex-wrap:wrap;">
    <?php if (in_array($action->status, ['investigation', 'pending_decision'], true) && can('hr.disciplinary.manage')): ?>
    <a href="/hr/disciplinary/<?= (int) $action->id ?>/edit" class="btn btn-secondary">تعديل</a>
    <?php endif; ?>

    <?php if (in_array($action->status, ['investigation', 'pending_decision'], true) && can('hr.disciplinary.manage')): ?>
    <div class="card" style="flex:1;min-width:300px;padding:16px;">
        <h4 style="margin:0 0 12px;font-size:14px;">اتخاذ قرار تأديبي</h4>
        <form method="POST" action="/hr/disciplinary/<?= (int) $action->id ?>/decide" style="display:flex;flex-direction:column;gap:10px;">
            <?= csrf_field() ?>
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:10px;">
                <div><label style="display:block;margin-bottom:4px;font-size:13px;">نوع الجزاء</label>
                    <select name="penalty_type" class="form-control" required>
                        <?php foreach ($pl as $k => $v): ?><option value="<?= $k ?>"><?= $v ?></option><?php endforeach; ?>
                    </select>
                </div>
                <div><label style="display:block;margin-bottom:4px;font-size:13px;">عدد الأيام</label><input type="number" name="penalty_days" value="0" min="0" max="6" class="form-control"></div>
            </div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">تاريخ التنفيذ</label><input type="date" name="effective_date" value="<?= date('Y-m-d') ?>" class="form-control"></div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">ملاحظات</label><textarea name="notes" class="form-control" rows="2"></textarea></div>
            <div style="display:flex;gap:6px;"><button type="submit" class="btn btn-danger">تنفيذ الجزاء</button></div>
            <div style="font-size:12px;color:#6B7280;">* وفقاً للمادة 58: أقصى خصم 5 أيام/شهر، أقصى إيقاف 6 أيام/شهر</div>
        </form>
    </div>
    <?php endif; ?>

    <?php if ($canAppeal && in_array($action->status, ['decided', 'enforced'], true)): ?>
    <div class="card" style="flex:1;min-width:300px;padding:16px;">
        <h4 style="margin:0 0 12px;font-size:14px;">تقديم تظلم <span style="color:#6B7280;font-size:12px;">(خلال 7 أيام من القرار)</span></h4>
        <form method="POST" action="/hr/disciplinary/<?= (int) $action->id ?>/appeal" style="display:flex;flex-direction:column;gap:10px;">
            <?= csrf_field() ?>
            <div><textarea name="appeal_text" class="form-control" rows="3" placeholder="سبب التظلم..." required></textarea></div>
            <button type="submit" class="btn btn-primary">تقديم التظلم</button>
        </form>
    </div>
    <?php endif; ?>
</div>
<?php $__template->endSection(); ?>
