<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>طلب تحويل #<?= (int) $transfer['id'] ?><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<?php
$typeLabels = ['child_separation' => 'فصل أبناء', 'child_mandatory_25' => 'تحويل وجوبي (25 سنة)', 'sports_conversion' => 'تحويل رياضي لعامل', 'cross_branch' => 'تحويل بين فروع', 'full_transfer' => 'تحويل عضوية كامل'];
$recipientData = null;
if ($transfer['transfer_type'] === 'full_transfer' && !empty($transfer['notes'])) {
    $notesDecoded = json_decode($transfer['notes'], true);
    $recipientData = $notesDecoded['recipient_data'] ?? null;
}
?>
<div class="card" style="padding:20px;margin-bottom:20px;">
    <table style="width:100%;max-width:700px;font-size:14px;">
        <tr><td style="padding:8px 0;color:#6B7280;width:35%;">العضو المصدر</td><td style="padding:8px 0;"><a href="/members/<?= (int) $transfer['source_member_id'] ?>" style="color:#0D7377;font-weight:600;"><?= e($transfer['source_name'] ?? '') ?></a> (<?= e($transfer['source_number'] ?? '—') ?>)</td></tr>
        <?php if ($transfer['target_member_id']): ?>
        <tr><td style="padding:8px 0;color:#6B7280;">المستلم</td><td style="padding:8px 0;"><a href="/members/<?= (int) $transfer['target_member_id'] ?>" style="color:#0D7377;font-weight:700;font-size:16px;"><?= e($transfer['target_name'] ?? '') ?></a></td></tr>
        <?php elseif ($recipientData): ?>
        <tr><td style="padding:8px 0;color:#6B7280;">المستلم (بيانات)</td><td style="padding:8px 0;font-weight:600;"><?= e($recipientData['full_name_ar'] ?? '—') ?> — <?= e($recipientData['national_id'] ?? '') ?></td></tr>
        <?php endif; ?>
        <?php if (!empty($transfer['child_name'])): ?>
        <tr><td style="padding:8px 0;color:#6B7280;">الابن/الابنة</td><td style="padding:8px 0;font-weight:600;"><?= e($transfer['child_name']) ?></td></tr>
        <?php endif; ?>
        <?php if (!empty($transfer['spouse_name'])): ?>
        <tr><td style="padding:8px 0;color:#6B7280;">الزوج/ة</td><td style="padding:8px 0;font-weight:600;"><?= e($transfer['spouse_name']) ?></td></tr>
        <?php endif; ?>
        <tr><td style="padding:8px 0;color:#6B7280;">النوع</td><td style="padding:8px 0;font-weight:600;"><?= e($typeLabels[$transfer['transfer_type']] ?? $transfer['transfer_type']) ?></td></tr>
        <tr><td style="padding:8px 0;color:#6B7280;">الحالة</td><td style="padding:8px 0;font-weight:700;color:<?= match($transfer['status']) { 'completed' => '#059669', 'fee_paid' => '#2563EB', 'approved' => '#0284C7', 'rejected' => '#DC2626', default => '#D97706' } ?>;">● <?= match($transfer['status']) { 'completed' => 'مكتمل', 'fee_paid' => 'تم الدفع', 'approved' => 'معتمد', 'rejected' => 'مرفوض', default => 'مقدّم' } ?></td></tr>
        <?php if ($transfer['new_membership_number']): ?><tr><td style="padding:8px 0;color:#6B7280;">رقم العضوية (نفس الرقم)</td><td style="padding:8px 0;font-weight:700;color:#0D7377;font-size:20px;"><?= e($transfer['new_membership_number']) ?></td></tr><?php endif; ?>
        <?php if (!empty($transfer['form_number'])): ?><tr><td style="padding:8px 0;color:#6B7280;">رقم الاستمارة</td><td style="padding:8px 0;font-weight:700;color:#1A1A2E;"><?= e($transfer['form_number']) ?></td></tr><?php endif; ?>
        <?php if ($transfer['source_companions_count'] !== null): ?>
        <tr><td style="padding:8px 0;color:#6B7280;">ملحقين العضوية الأصلية</td><td style="padding:8px 0;"><?= (int) $transfer['source_companions_count'] ?></td></tr>
        <?php endif; ?>
        <?php if ($transfer['target_companions_count'] !== null): ?>
        <tr><td style="padding:8px 0;color:#6B7280;">ملحقين المستلم</td><td style="padding:8px 0;"><?= (int) $transfer['target_companions_count'] ?></td></tr>
        <?php endif; ?>
    </table>
</div>

<!-- Fee Breakdown -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="receipt" style="width:18px;height:18px;color:#D97706;"></i>
        <h3 style="margin:0;color:#D97706;font-size:15px;">تفصيل الرسوم</h3>
    </div>
    <div style="padding:20px;">
        <table style="width:100%;max-width:500px;font-size:14px;">
            <?php if (!empty($transfer['new_membership_value']) && bccomp($transfer['new_membership_value'], '0', 2) > 0): ?>
            <tr>
                <td style="padding:8px 0;color:#6B7280;">قيمة العضوية الجديدة</td>
                <td style="padding:8px 0;font-weight:600;direction:ltr;text-align:left;"><?= money($transfer['new_membership_value']) ?></td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($transfer['years_since_acquisition'])): ?>
            <tr>
                <td style="padding:8px 0;color:#6B7280;">سنوات منذ الاكتساب</td>
                <td style="padding:8px 0;font-weight:600;"><?= (int) $transfer['years_since_acquisition'] ?> سنة</td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($transfer['fee_percentage'])): ?>
            <tr>
                <td style="padding:8px 0;color:#6B7280;">نسبة رسوم الفصل</td>
                <td style="padding:8px 0;font-weight:600;"><?= e($transfer['fee_percentage']) ?>%</td>
            </tr>
            <?php endif; ?>
            <tr style="border-top:1px solid #E5E7EB;">
                <td style="padding:10px 0;color:#6B7280;">رسوم الفصل (<?= e($transfer['fee_percentage'] ?? '0') ?>% من قيمة العضوية)</td>
                <td style="padding:10px 0;font-weight:700;direction:ltr;text-align:left;color:#0D7377;"><?= money($transfer['separation_fee'] ?? '0') ?></td>
            </tr>
            <tr>
                <td style="padding:8px 0;color:#6B7280;">رسوم الاستمارة</td>
                <td style="padding:8px 0;font-weight:600;direction:ltr;text-align:left;"><?= money($transfer['form_fee'] ?? '0') ?></td>
            </tr>
            <tr>
                <td style="padding:8px 0;color:#6B7280;">اشتراك سنوي + تنمية</td>
                <td style="padding:8px 0;font-weight:600;direction:ltr;text-align:left;"><?= money($transfer['annual_subscription_fee'] ?? '0') ?></td>
            </tr>
            <?php if (bccomp($transfer['companion_surcharge'] ?? '0', '0', 2) > 0): ?>
            <tr>
                <td style="padding:8px 0;color:#7C3AED;font-weight:600;">رسوم ملحقين إضافيين</td>
                <td style="padding:8px 0;font-weight:700;direction:ltr;text-align:left;color:#7C3AED;"><?= money($transfer['companion_surcharge']) ?></td>
            </tr>
            <?php
            $csBreakdown = !empty($transfer['companion_surcharge_breakdown']) ? json_decode($transfer['companion_surcharge_breakdown'], true) : [];
            if (is_array($csBreakdown) && !empty($csBreakdown)):
            ?>
            <tr><td colspan="2" style="padding:4px 0;">
                <div style="font-size:12px;color:#6B7280;background:#F5F3FF;border:1px solid #DDD6FE;border-radius:6px;padding:8px 12px;margin-top:4px;">
                    <?php foreach ($csBreakdown as $item): ?>
                    <div style="padding:2px 0;">• <?= e($item['label'] ?? ($item['type'] ?? '')) ?>: <?= money($item['fee'] ?? $item['total'] ?? '0') ?></div>
                    <?php endforeach; ?>
                </div>
            </td></tr>
            <?php endif; ?>
            <?php endif; ?>
            <tr style="border-top:2px solid #0D7377;">
                <td style="padding:12px 0;font-weight:700;font-size:16px;">الإجمالي المطلوب</td>
                <td style="padding:12px 0;font-weight:800;font-size:22px;color:#DC2626;direction:ltr;text-align:left;"><?= money($transfer['total_fee'] ?? '0') ?></td>
            </tr>
        </table>
    </div>
</div>

<?php if ($transfer['status'] === 'requested'): ?>
<?php if (can('transfer.approve')): ?>
<div class="card" style="padding:20px;margin-bottom:20px;background:#EFF6FF;border:2px solid #3B82F6;">
    <h4 style="margin:0 0 15px;color:#1D4ED8;">اعتماد الطلب</h4>
    <div style="display:flex;gap:10px;">
        <form method="POST" action="/transfers/<?= (int) $transfer['id'] ?>/approve">
            <?= csrf_field() ?>
            <button type="submit" class="btn btn-primary" onclick="return confirm('اعتماد طلب التحويل؟')">✅ اعتماد</button>
        </form>
        <form method="POST" action="/transfers/<?= (int) $transfer['id'] ?>/reject">
            <?= csrf_field() ?>
            <button type="submit" class="btn btn-danger" onclick="return confirm('رفض طلب التحويل؟')">❌ رفض</button>
        </form>
    </div>
</div>
<?php endif; ?>
<?php endif; ?>

<?php if ($transfer['status'] === 'approved' && bccomp($transfer['total_fee'] ?? '0', '0', 2) > 0): ?>
<?php if (can('transfer.approve')): ?>
<div class="card" style="padding:20px;margin-bottom:20px;background:#FFF7ED;border:2px solid #F59E0B;">
    <h4 style="margin:0 0 15px;color:#D97706;">💰 دفع رسوم التحويل/الفصل</h4>
    <form method="POST" action="/transfers/<?= (int) $transfer['id'] ?>/pay">
        <?= csrf_field() ?>
        <input type="hidden" name="amount" value="<?= e($transfer['total_fee']) ?>">
        <div style="display:flex;gap:10px;align-items:end;">
            <div class="form-group"><label class="form-label">المبلغ</label><input type="text" value="<?= money($transfer['total_fee']) ?>" 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($transfer['total_fee']) ?>؟')">💰 ادفع</button>
        </div>
    </form>
</div>
<?php endif; ?>
<?php endif; ?>

<?php if (in_array($transfer['status'], ['approved', 'fee_paid']) && ($transfer['transfer_type'] ?? '') !== 'child_separation'): ?>
<?php if (can('transfer.approve')): ?>
<form method="POST" action="/transfers/<?= (int) $transfer['id'] ?>/complete" style="margin-top:15px;">
    <?= csrf_field() ?>
    <button type="submit" class="btn btn-primary" onclick="return confirm('إتمام التحويل؟')">✅ إتمام التحويل</button>
</form>
<?php endif; ?>
<?php endif; ?>
<script>document.addEventListener('DOMContentLoaded', function() { if (typeof lucide !== 'undefined') lucide.createIcons(); });</script>
<?php $__template->endSection(); ?>