<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تنازل #<?= (int) $waiver['id'] ?><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div class="card" style="padding:20px;margin-bottom:20px;">
    <table style="width:100%;max-width:600px;font-size:14px;">
        <tr><td style="padding:6px 0;color:#6B7280;width:35%;">المتنازل</td><td style="padding:6px 0;"><a href="/members/<?= (int) $waiver['source_member_id'] ?>" style="color:#0D7377;font-weight:600;"><?= e($waiver['member_name'] ?? '') ?></a></td></tr>
        <tr><td style="padding:6px 0;color:#6B7280;">رقم العضوية</td><td style="padding:6px 0;font-weight:700;"><?= e($waiver['membership_number'] ?? '—') ?></td></tr>
        <tr><td style="padding:6px 0;color:#6B7280;">قيمة العضوية وقت التنازل</td><td style="padding:6px 0;"><?= money($waiver['membership_value_at_waiver'] ?? '0') ?></td></tr>
        <tr><td style="padding:6px 0;color:#6B7280;">نسبة التنازل</td><td style="padding:6px 0;"><?= e($waiver['waiver_fee_percentage'] ?? '30') ?>%</td></tr>
        <tr><td style="padding:6px 0;color:#6B7280;">رسوم التنازل</td><td style="padding:6px 0;font-weight:700;font-size:22px;color:#DC2626;"><?= money($waiver['waiver_fee_amount'] ?? '0') ?></td></tr>
        <tr><td style="padding:6px 0;color:#6B7280;">الحالة</td><td style="padding:6px 0;font-weight:700;color:<?= match($waiver['status']) { 'completed' => '#059669', 'approved' => '#0284C7', default => '#D97706' } ?>;">● <?= match($waiver['status']) { 'requested' => 'مقدم', 'approved' => 'معتمد — في انتظار الدفع', 'completed' => 'مكتمل', default => $waiver['status'] } ?></td></tr>
        <?php if ($waiver['target_member_id']): ?><tr><td style="padding:6px 0;color:#6B7280;">المتنازل إليه</td><td style="padding:6px 0;"><a href="/members/<?= (int) $waiver['target_member_id'] ?>" style="color:#0D7377;font-weight:600;">عضو #<?= (int) $waiver['target_member_id'] ?></a></td></tr><?php endif; ?>
    </table>
</div>

<?php if (in_array($waiver['status'], ['requested', 'approved']) && bccomp($waiver['waiver_fee_amount'] ?? '0', '0', 2) > 0): ?>
<div class="card" style="padding:20px;margin-bottom:20px;background:#FFF7ED;border:2px solid #F59E0B;">
    <h4 style="margin:0 0 15px;color:#D97706;">💰 دفع رسوم التنازل (30% من قيمة العضوية)</h4>
    <form method="POST" action="/waiver/<?= (int) $waiver['id'] ?>/pay">
        <?= csrf_field() ?>
        <input type="hidden" name="amount" value="<?= e($waiver['waiver_fee_amount']) ?>">
        <div style="display:flex;gap:10px;align-items:end;">
            <div class="form-group"><label class="form-label">المبلغ</label><input type="text" value="<?= money($waiver['waiver_fee_amount']) ?>" class="form-input" style="background:#F3F4F6;font-weight:700;font-size:18px;" readonly></div>
            <div class="form-group"><label class="form-label">طريقة الدفع</label><select name="payment_method" class="form-select"><option value="cash">نقدي</option><option value="visa">فيزا</option><option value="bank_transfer">تحويل</option></select></div>
            <button type="submit" class="btn btn-primary" style="padding:10px 25px;" onclick="return confirm('تأكيد دفع <?= money($waiver['waiver_fee_amount']) ?>؟')">💰 ادفع</button>
        </div>
    </form>
</div>
<?php endif; ?>

<?php if ($waiver['status'] === 'approved'): ?>
<form method="POST" action="/waiver/<?= (int) $waiver['id'] ?>/complete">
    <?= csrf_field() ?>
    <button type="submit" class="btn btn-primary" onclick="return confirm('⚠ إتمام التنازل — سيتم نقل العضوية. متأكد؟')">✅ إتمام التنازل</button>
</form>
<?php endif; ?>

<?php if ($waiver['status'] === 'requested'): ?>
<div style="margin-top:15px;display:flex;gap:10px;">
    <form method="POST" action="/waiver/<?= (int) $waiver['id'] ?>/approve"><?= csrf_field() ?><button type="submit" class="btn btn-primary" onclick="return confirm('اعتماد التنازل؟')">✅ اعتماد مجلس الأمناء</button></form>
    <form method="POST" action="/waiver/<?= (int) $waiver['id'] ?>/reject"><?= csrf_field() ?><button type="submit" class="btn btn-outline" style="color:#DC2626;" onclick="return confirm('رفض التنازل؟')">❌ رفض</button></form>
</div>
<?php endif; ?>
<?php $__template->endSection(); ?>