<?php $__template->layout('Layout.main'); ?>

<?php $__template->section('title'); ?>تحصيل طلب دفع<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/cashier" class="btn btn-outline">&#8592; العودة للطابور</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<?php
$typeLabel = \App\Modules\Cashier\Services\PaymentRequestService::getPaymentTypeLabel($pr['payment_type']);
$statusColor = match($pr['status']) {
    'pending'    => '#F59E0B',
    'processing' => '#3B82F6',
    default      => '#6B7280',
};
$statusLabel = match($pr['status']) {
    'pending'    => 'معلق',
    'processing' => 'قيد التنفيذ',
    default      => $pr['status'],
};
?>

<div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;max-width:900px;">
    <!-- Request Details -->
    <div class="card" style="padding:25px;">
        <h3 style="margin:0 0 20px;color:#1A1A2E;border-bottom:2px solid #E5E7EB;padding-bottom:10px;">بيانات الطلب</h3>
        <table style="width:100%;font-size:14px;">
            <tr>
                <td style="padding:8px 0;color:#6B7280;width:40%;">رقم الطلب</td>
                <td style="padding:8px 0;font-weight:700;direction:ltr;text-align:right;"><?= e($pr['request_number']) ?></td>
            </tr>
            <tr>
                <td style="padding:8px 0;color:#6B7280;">نوع الدفعة</td>
                <td style="padding:8px 0;font-weight:600;"><?= $typeLabel ?></td>
            </tr>
            <tr>
                <td style="padding:8px 0;color:#6B7280;">المبلغ</td>
                <td style="padding:8px 0;font-weight:700;font-size:24px;color:#059669;direction:ltr;text-align:right;"><?= money($pr['amount']) ?></td>
            </tr>
            <tr>
                <td style="padding:8px 0;color:#6B7280;">العملة</td>
                <td style="padding:8px 0;"><?= e($pr['currency']) ?></td>
            </tr>
            <tr>
                <td style="padding:8px 0;color:#6B7280;">الحالة</td>
                <td style="padding:8px 0;"><span style="color:<?= $statusColor ?>;font-weight:700;"><?= $statusLabel ?></span></td>
            </tr>
            <?php if ($pr['description_ar']): ?>
            <tr>
                <td style="padding:8px 0;color:#6B7280;">الوصف</td>
                <td style="padding:8px 0;"><?= e($pr['description_ar']) ?></td>
            </tr>
            <?php endif; ?>
            <?php
            $notesData = $pr['notes'] ? json_decode($pr['notes'], true) : null;
            $feeBreakdown = $notesData['fee_breakdown'] ?? null;
            ?>
            <?php if (is_array($feeBreakdown) && !empty($feeBreakdown)): ?>
            <tr>
                <td colspan="2" style="padding:12px 0;">
                    <div style="background:#FFFBEB;border:2px solid #F59E0B;border-radius:10px;padding:16px 20px;">
                        <div style="font-weight:700;color:#92400E;margin-bottom:10px;font-size:14px;">📊 تفصيل المبلغ (لماذا هذا الرقم):</div>
                        <?php foreach ($feeBreakdown as $line): ?>
                            <?php if (str_contains($line, '═══')): ?>
                                <hr style="border:0;border-top:2px solid #FCD34D;margin:8px 0;">
                            <?php elseif (str_starts_with($line, '💵')): ?>
                                <div style="font-weight:700;color:#059669;font-size:16px;margin-top:6px;background:#ECFDF5;padding:6px 10px;border-radius:6px;"><?= e($line) ?></div>
                            <?php else: ?>
                                <div style="font-size:14px;color:#374151;padding:3px 0;"><?= e($line) ?></div>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </div>
                </td>
            </tr>
            <?php endif; ?>
            <?php if ($pr['notes'] && !is_array($notesData)): ?>
            <tr>
                <td style="padding:8px 0;color:#6B7280;">ملاحظات</td>
                <td style="padding:8px 0;"><?= e($pr['notes']) ?></td>
            </tr>
            <?php endif; ?>
            <tr>
                <td style="padding:8px 0;color:#6B7280;">تاريخ الطلب</td>
                <td style="padding:8px 0;"><?= e($pr['created_at']) ?></td>
            </tr>
            <tr>
                <td style="padding:8px 0;color:#6B7280;">بواسطة</td>
                <td style="padding:8px 0;"><?= e($pr['requested_by_name'] ?? '—') ?></td>
            </tr>
        </table>
    </div>

    <!-- Member Info + Payment Form -->
    <div>
        <!-- Member Card -->
        <div class="card" style="padding:20px;margin-bottom:20px;background:linear-gradient(135deg, #F0FDF4, #ECFDF5);border:1px solid #A7F3D0;">
            <h4 style="margin:0 0 15px;color:#065F46;">بيانات العضو</h4>
            <table style="width:100%;font-size:14px;">
                <tr>
                    <td style="padding:6px 0;color:#6B7280;">الاسم</td>
                    <td style="padding:6px 0;font-weight:700;">
                        <a href="/members/<?= (int)$pr['member_id'] ?>" style="color:#0D7377;"><?= e($pr['member_name'] ?? '—') ?></a>
                    </td>
                </tr>
                <tr>
                    <td style="padding:6px 0;color:#6B7280;">رقم الاستمارة</td>
                    <td style="padding:6px 0;font-weight:700;color:#D97706;"><?= e($pr['form_number'] ?? '—') ?></td>
                </tr>
                <?php if ($pr['membership_number']): ?>
                <tr>
                    <td style="padding:6px 0;color:#6B7280;">رقم العضوية</td>
                    <td style="padding:6px 0;font-weight:700;color:#0D7377;"><?= e($pr['membership_number']) ?></td>
                </tr>
                <?php endif; ?>
                <?php if ($pr['phone_mobile']): ?>
                <tr>
                    <td style="padding:6px 0;color:#6B7280;">الهاتف</td>
                    <td style="padding:6px 0;direction:ltr;text-align:right;"><?= e($pr['phone_mobile']) ?></td>
                </tr>
                <?php endif; ?>
            </table>
        </div>

        <!-- Payment Action -->
        <div class="card" style="padding:25px;border:2px solid #0D7377;">
            <h3 style="margin:0 0 20px;color:#0D7377;">تحصيل الدفعة</h3>
            <form method="POST" action="/cashier/<?= (int)$pr['id'] ?>/complete" id="paymentForm">
                <?= csrf_field() ?>
                <div class="form-group" style="margin-bottom:20px;">
                    <label class="form-label" style="font-weight:600;">طريقة الدفع</label>
                    <select name="payment_method" class="form-select" style="font-size:16px;padding:12px;">
                        <option value="cash">نقدي</option>
                        <option value="visa">فيزا</option>
                        <option value="bank_transfer">تحويل بنكي</option>
                        <option value="check">شيك</option>
                    </select>
                </div>

                <div style="background:#F9FAFB;border-radius:8px;padding:15px;margin-bottom:20px;text-align:center;">
                    <div style="font-size:13px;color:#6B7280;margin-bottom:5px;">المبلغ المطلوب تحصيله</div>
                    <div style="font-size:32px;font-weight:700;color:#059669;direction:ltr;"><?= money($pr['amount']) ?></div>
                </div>

                <button type="submit" class="btn btn-primary" style="width:100%;padding:15px;font-size:18px;background:#059669;border-color:#059669;"
                        onclick="return confirm('تأكيد تحصيل <?= money($pr['amount']) ?> بطريقة ' + document.querySelector('[name=payment_method]').selectedOptions[0].text + '؟')">
                    تأكيد التحصيل
                </button>
            </form>

            <hr style="margin:20px 0;border:0;border-top:1px solid #E5E7EB;">

            <form method="POST" action="/cashier/<?= (int)$pr['id'] ?>/cancel" style="text-align:center;">
                <?= csrf_field() ?>
                <input type="hidden" name="reason" id="cancelReason" value="">
                <button type="submit" class="btn btn-outline" style="color:#DC2626;border-color:#DC2626;font-size:13px;"
                        onclick="var r = prompt('سبب الإلغاء:'); if(!r) return false; document.getElementById('cancelReason').value = r; return confirm('تأكيد إلغاء الطلب؟');">
                    إلغاء الطلب
                </button>
            </form>
        </div>
    </div>
</div>
<?php $__template->endSection(); ?>
