<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?><?= !empty($memberView) ? 'سجل مدفوعات: ' . e($member['full_name_ar'] ?? '') : (!empty($singleView) ? 'تفاصيل الدفعة #' . (int) ($payment['id'] ?? 0) : 'كل المدفوعات') ?><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<?php if (!empty($singleView) && !empty($payment)): ?>
<!-- Single Payment View -->
<div class="card" style="padding:20px;margin-bottom:20px;">
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:20px;">
        <h3 style="margin:0;color:#0D7377;">تفاصيل الدفعة #<?= (int) $payment['id'] ?></h3>
        <?php if ($payment['is_voided']): ?>
            <span style="background:#FEF2F2;color:#DC2626;padding:6px 16px;border-radius:20px;font-weight:700;">ملغاة</span>
        <?php else: ?>
            <span style="background:#F0FDF4;color:#059669;padding:6px 16px;border-radius:20px;font-weight:700;">سارية</span>
        <?php endif; ?>
    </div>
    <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;">
        <table style="width:100%;font-size:14px;">
            <tr><td style="padding:6px 0;color:#6B7280;width:40%;">العضو</td><td style="padding:6px 0;"><a href="/members/<?= (int) $payment['member_id'] ?>" style="color:#0D7377;font-weight:600;"><?= e($payment['member_name']) ?></a></td></tr>
            <tr><td style="padding:6px 0;color:#6B7280;">رقم العضوية</td><td style="padding:6px 0;"><?= e($payment['membership_number'] ?? '—') ?></td></tr>
            <tr><td style="padding:6px 0;color:#6B7280;">نوع الدفعة</td><td style="padding:6px 0;font-weight:600;"><?= e(\App\Modules\Payments\Models\Payment::getPaymentTypeLabel($payment['payment_type'])) ?></td></tr>
            <tr><td style="padding:6px 0;color:#6B7280;">المبلغ</td><td style="padding:6px 0;font-size:20px;font-weight:700;color:#0D7377;"><?= money($payment['amount']) ?></td></tr>
            <tr><td style="padding:6px 0;color:#6B7280;">طريقة الدفع</td><td style="padding:6px 0;"><?= e(\App\Modules\Payments\Models\Payment::getPaymentMethodLabel($payment['payment_method'])) ?></td></tr>
        </table>
        <table style="width:100%;font-size:14px;">
            <tr><td style="padding:6px 0;color:#6B7280;width:40%;">رقم الإيصال</td><td style="padding:6px 0;font-weight:600;direction:ltr;text-align:right;"><?= e($payment['receipt_number'] ?? '—') ?></td></tr>
            <tr><td style="padding:6px 0;color:#6B7280;">تاريخ الدفع</td><td style="padding:6px 0;"><?= e($payment['payment_date']) ?></td></tr>
            <tr><td style="padding:6px 0;color:#6B7280;">استلمها</td><td style="padding:6px 0;"><?= e($payment['received_by_name'] ?? '—') ?></td></tr>
            <?php if ($payment['is_voided']): ?>
            <tr><td style="padding:6px 0;color:#6B7280;">ألغاها</td><td style="padding:6px 0;color:#DC2626;"><?= e($payment['voided_by_name'] ?? '—') ?> — <?= e($payment['voided_at'] ?? '') ?></td></tr>
            <tr><td style="padding:6px 0;color:#6B7280;">سبب الإلغاء</td><td style="padding:6px 0;"><?= e($payment['void_reason'] ?? '—') ?></td></tr>
            <?php endif; ?>
            <?php if ($payment['notes']): ?>
            <tr><td style="padding:6px 0;color:#6B7280;">ملاحظات</td><td style="padding:6px 0;"><?= e($payment['notes']) ?></td></tr>
            <?php endif; ?>
        </table>
    </div>

    <?php if (!$payment['is_voided'] && ($canVoid['allowed'] ?? false) && can('payment.void_receipt')): ?>
    <div style="margin-top:20px;padding:15px;background:#FEF2F2;border:1px solid #FECACA;border-radius:8px;">
        <form method="POST" action="/payments/<?= (int) $payment['id'] ?>/void" onsubmit="return confirm('هل أنت متأكد من إلغاء هذه الدفعة والإيصال المرتبط بها؟');">
            <?= csrf_field() ?>
            <label class="form-label" style="color:#DC2626;font-weight:700;">إلغاء الدفعة</label>
            <div style="display:flex;gap:10px;margin-top:8px;">
                <input type="text" name="void_reason" class="form-input" placeholder="سبب الإلغاء (مطلوب)" required style="flex:1;">
                <button type="submit" class="btn" style="background:#DC2626;color:#fff;border:none;padding:8px 20px;border-radius:6px;cursor:pointer;">إلغاء الدفعة</button>
            </div>
        </form>
    </div>
    <?php endif; ?>

    <div style="margin-top:15px;display:flex;gap:10px;">
        <?php if ($payment['receipt_id'] && can('receipt.print')): ?><a href="/receipts/<?= (int) $payment['receipt_id'] ?>/print" class="btn btn-outline" target="_blank">طباعة الإيصال</a><?php endif; ?>
        <a href="/payments" class="btn btn-outline">← العودة</a>
    </div>
</div>

<?php else: ?>
<!-- List View -->
<?php if (empty($memberView)): ?>
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/payments" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div><label class="form-label" style="font-size:12px;">بحث</label><input type="text" name="q" value="<?= e($filters['search'] ?? '') ?>" placeholder="اسم، رقم عضوية، إيصال..." class="form-input" style="min-width:200px;"></div>
        <div><label class="form-label" style="font-size:12px;">النوع</label>
            <select name="payment_type" class="form-select"><option value="">الكل</option>
                <option value="membership_fee" <?= ($filters['payment_type'] ?? '') === 'membership_fee' ? 'selected' : '' ?>>رسوم عضوية</option>
                <option value="form_fee" <?= ($filters['payment_type'] ?? '') === 'form_fee' ? 'selected' : '' ?>>رسوم استمارة</option>
                <option value="addition_fee" <?= ($filters['payment_type'] ?? '') === 'addition_fee' ? 'selected' : '' ?>>رسوم إضافة</option>
                <option value="annual_subscription" <?= ($filters['payment_type'] ?? '') === 'annual_subscription' ? 'selected' : '' ?>>اشتراك سنوي</option>
                <option value="down_payment" <?= ($filters['payment_type'] ?? '') === 'down_payment' ? 'selected' : '' ?>>مقدم</option>
            </select>
        </div>
        <div><label class="form-label" style="font-size:12px;">من</label><input type="date" name="date_from" value="<?= e($filters['date_from'] ?? '') ?>" class="form-input"></div>
        <div><label class="form-label" style="font-size:12px;">إلى</label><input type="date" name="date_to" value="<?= e($filters['date_to'] ?? '') ?>" class="form-input"></div>
        <button type="submit" class="btn btn-outline">بحث</button>
        <a href="/payments" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>
<?php endif; ?>

<?php if (!empty($memberView) && !empty($summary)): ?>
<?php $__template->include('Payments.Views._partials.payment-summary', ['summary' => $summary]); ?>
<?php endif; ?>

<?php
// Split payments into own vs inherited (from previous owner) if this is a transferred membership
$ownPayments = $rows;
$inheritedPayments = [];
if (!empty($memberView) && !empty($hasTransfer) && !empty($ownershipDate)) {
    $cutoff = substr((string) $ownershipDate, 0, 10);
    $ownPayments = [];
    $inheritedPayments = [];
    foreach ($rows as $r) {
        $pDate = substr($r['payment_date'] ?? '', 0, 10);
        if ($pDate >= $cutoff) {
            $ownPayments[] = $r;
        } else {
            $inheritedPayments[] = $r;
        }
    }
}
?>

<div class="card" style="margin-bottom:20px;">
    <?php if (!empty($memberView) && !empty($hasTransfer) && !empty($inheritedPayments)): ?>
    <div style="padding:12px 20px;background:#F0FDF4;border-bottom:2px solid #059669;">
        <strong style="color:#059669;">مدفوعات العضوية الحالية</strong>
        <span style="color:#6B7280;font-size:12px;margin-right:10px;">(من <?= e(substr((string) $ownershipDate, 0, 10)) ?>)</span>
    </div>
    <?php endif; ?>
    <div class="table-responsive">
        <table class="data-table">
            <thead><tr>
                <th>التاريخ</th>
                <?php if (empty($memberView)): ?><th>العضو</th><?php endif; ?>
                <th>النوع</th><th>المبلغ</th><th>الطريقة</th><th>الإيصال</th><th>الحالة</th><th>الإجراءات</th>
            </tr></thead>
            <tbody>
                <?php foreach ($ownPayments as $r): ?>
                <tr style="<?= ($r['is_voided'] ?? false) ? 'opacity:0.5;text-decoration:line-through;' : '' ?>">
                    <td style="font-size:12px;white-space:nowrap;"><?= e($r['payment_date'] ?? '') ?></td>
                    <?php if (empty($memberView)): ?><td><a href="/members/<?= (int) $r['member_id'] ?>" style="color:#0D7377;"><?= e($r['member_name'] ?? $r['full_name_ar'] ?? '—') ?></a></td><?php endif; ?>
                    <td style="font-size:13px;"><?= e(\App\Modules\Payments\Models\Payment::getPaymentTypeLabel($r['payment_type'])) ?></td>
                    <td style="font-weight:700;direction:ltr;text-align:right;"><?= money($r['amount']) ?></td>
                    <td style="font-size:13px;"><?= e(\App\Modules\Payments\Models\Payment::getPaymentMethodLabel($r['payment_method'])) ?></td>
                    <td style="font-size:12px;direction:ltr;text-align:right;"><?= e($r['receipt_number'] ?? '—') ?></td>
                    <td><?php if ($r['is_voided'] ?? false): ?><span style="color:#DC2626;font-weight:600;">ملغاة</span><?php else: ?><span style="color:#059669;font-weight:600;">سارية</span><?php endif; ?></td>
                    <td><a href="/payments/<?= (int) $r['id'] ?>" class="btn btn-sm btn-outline">عرض</a></td>
                </tr>
                <?php endforeach; ?>
                <?php if (empty($ownPayments)): ?><tr><td colspan="<?= empty($memberView) ? 8 : 7 ?>" style="text-align:center;padding:40px;color:#6B7280;">لا توجد مدفوعات</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php if (!empty($memberView) && !empty($inheritedPayments)): ?>
<div class="card">
    <div style="padding:12px 20px;background:#FFF7ED;border-bottom:2px solid #D97706;">
        <strong style="color:#D97706;">مدفوعات العضوية السابقة (موروثة)</strong>
        <span style="color:#6B7280;font-size:12px;margin-right:10px;">(قبل انتقال الملكية)</span>
    </div>
    <div class="table-responsive">
        <table class="data-table">
            <thead><tr>
                <th>التاريخ</th>
                <th>النوع</th><th>المبلغ</th><th>الطريقة</th><th>الإيصال</th><th>الحالة</th><th>الإجراءات</th>
            </tr></thead>
            <tbody>
                <?php foreach ($inheritedPayments as $r): ?>
                <tr style="opacity:0.7;<?= ($r['is_voided'] ?? false) ? 'text-decoration:line-through;' : '' ?>">
                    <td style="font-size:12px;white-space:nowrap;"><?= e($r['payment_date'] ?? '') ?></td>
                    <td style="font-size:13px;"><?= e(\App\Modules\Payments\Models\Payment::getPaymentTypeLabel($r['payment_type'])) ?></td>
                    <td style="font-weight:700;direction:ltr;text-align:right;"><?= money($r['amount']) ?></td>
                    <td style="font-size:13px;"><?= e(\App\Modules\Payments\Models\Payment::getPaymentMethodLabel($r['payment_method'])) ?></td>
                    <td style="font-size:12px;direction:ltr;text-align:right;"><?= e($r['receipt_number'] ?? '—') ?></td>
                    <td><?php if ($r['is_voided'] ?? false): ?><span style="color:#DC2626;font-weight:600;">ملغاة</span><?php else: ?><span style="color:#059669;font-weight:600;">سارية</span><?php endif; ?></td>
                    <td><a href="/payments/<?= (int) $r['id'] ?>" class="btn btn-sm btn-outline">عرض</a></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>
<?php endif; ?>
<?php $__template->endSection(); ?>