<?php
use App\Modules\Settings\Services\BrandingService;

$rd = BrandingService::receiptDesign();
$clubNameAr = BrandingService::clubNameAr();
$clubNameEn = BrandingService::clubNameEn();
$logoUrl = BrandingService::logo();

$showLogo      = $rd['show_logo'];
$showWatermark = $rd['show_watermark'];
$watermarkText = $rd['watermark_text'];
$watermarkOp   = $rd['watermark_opacity'];
$headerColor   = $rd['header_color'];
$showFooter    = $rd['show_footer_print_info'];
?>
<?php $__template->layout('Layout.print'); ?>
<?php $__template->section('title'); ?>إيصال رقم <?= e($receipt['receipt_number']) ?><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<div style="max-width:700px;margin:0 auto;font-family:'Cairo',sans-serif;direction:rtl;position:relative;overflow:hidden;">

    <?php if ($showWatermark && $watermarkText): ?>
    <div style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%) rotate(-35deg);font-size:52px;font-weight:900;color:<?= e($headerColor) ?>;opacity:<?= e($watermarkOp) ?>;white-space:nowrap;pointer-events:none;z-index:0;">
        <?= e($watermarkText) ?>
    </div>
    <?php endif; ?>

    <div style="position:relative;z-index:1;">
        <?php if ($receipt['is_voided']): ?>
        <div style="text-align:center;color:#DC2626;font-size:24px;font-weight:700;border:3px solid #DC2626;padding:10px;margin-bottom:20px;">ملغى</div>
        <?php endif; ?>

        <div style="text-align:center;margin-bottom:30px;padding-bottom:16px;border-bottom:2px solid <?= e($headerColor) ?>;">
            <?php if ($showLogo && $logoUrl): ?>
                <div style="margin-bottom:10px;"><img src="<?= e($logoUrl) ?>" alt="Logo" style="max-height:45px;"></div>
            <?php endif; ?>
            <h2 style="margin:0;color:<?= e($headerColor) ?>;"><?= e($clubNameAr) ?></h2>
            <p style="margin:5px 0;color:#6B7280;"><?= e($clubNameEn) ?></p>
            <h3 style="margin:15px 0 5px;color:#1A1A2E;">إيصال تحصيل</h3>
        </div>

        <table style="width:100%;border-collapse:collapse;margin-bottom:20px;font-size:14px;">
            <tr>
                <td style="padding:8px;border:1px solid #E5E7EB;background:#F9FAFB;font-weight:600;width:30%;">رقم الإيصال</td>
                <td style="padding:8px;border:1px solid #E5E7EB;direction:ltr;text-align:right;font-weight:700;font-size:16px;"><?= e($receipt['receipt_number']) ?></td>
            </tr>
            <tr>
                <td style="padding:8px;border:1px solid #E5E7EB;background:#F9FAFB;font-weight:600;">التاريخ</td>
                <td style="padding:8px;border:1px solid #E5E7EB;"><?= e(substr($receipt['issued_at'], 0, 10)) ?></td>
            </tr>
            <tr>
                <td style="padding:8px;border:1px solid #E5E7EB;background:#F9FAFB;font-weight:600;">اسم العضو</td>
                <td style="padding:8px;border:1px solid #E5E7EB;font-weight:600;"><?= e($receipt['member_name']) ?></td>
            </tr>
            <tr>
                <td style="padding:8px;border:1px solid #E5E7EB;background:#F9FAFB;font-weight:600;">رقم العضوية</td>
                <td style="padding:8px;border:1px solid #E5E7EB;"><?= e($receipt['membership_number'] ?? '—') ?></td>
            </tr>
            <?php if ($receipt['form_number'] ?? ''): ?>
            <tr>
                <td style="padding:8px;border:1px solid #E5E7EB;background:#F9FAFB;font-weight:600;">رقم الاستمارة</td>
                <td style="padding:8px;border:1px solid #E5E7EB;"><?= e($receipt['form_number']) ?></td>
            </tr>
            <?php endif; ?>
            <tr>
                <td style="padding:8px;border:1px solid #E5E7EB;background:#F9FAFB;font-weight:600;">البيان</td>
                <td style="padding:8px;border:1px solid #E5E7EB;"><?= e($receipt['description_ar'] ?? '—') ?></td>
            </tr>
            <?php if ($receipt['payment_type'] ?? ''): ?>
            <tr>
                <td style="padding:8px;border:1px solid #E5E7EB;background:#F9FAFB;font-weight:600;">نوع الدفعة</td>
                <td style="padding:8px;border:1px solid #E5E7EB;"><?= e(\App\Modules\Payments\Models\Payment::getPaymentTypeLabel($receipt['payment_type'])) ?></td>
            </tr>
            <?php endif; ?>
            <?php if ($receipt['payment_method'] ?? ''): ?>
            <tr>
                <td style="padding:8px;border:1px solid #E5E7EB;background:#F9FAFB;font-weight:600;">طريقة الدفع</td>
                <td style="padding:8px;border:1px solid #E5E7EB;"><?= e(\App\Modules\Payments\Models\Payment::getPaymentMethodLabel($receipt['payment_method'])) ?></td>
            </tr>
            <?php endif; ?>
        </table>

        <?php
        $breakdown = $receipt['fee_breakdown'] ?? null;
        if (is_array($breakdown) && !empty($breakdown)):
            $detailItems = [];
            $totalLine = '';
            foreach ($breakdown as $bLine) {
                if (str_contains($bLine, '═══')) continue;
                if (str_starts_with($bLine, '💵')) { $totalLine = $bLine; continue; }
                $detailItems[] = $bLine;
            }
        ?>
        <table style="width:100%;border-collapse:collapse;margin-bottom:15px;font-size:13px;">
            <thead>
                <tr style="background:#F9FAFB;">
                    <th style="padding:8px 10px;border:1px solid #E5E7EB;text-align:right;font-weight:700;color:<?= e($headerColor) ?>;" colspan="2">تفصيل المبلغ</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($detailItems as $idx => $dItem): ?>
                <tr>
                    <td style="padding:6px 10px;border:1px solid #E5E7EB;width:30px;text-align:center;color:#6B7280;font-size:12px;"><?= $idx + 1 ?></td>
                    <td style="padding:6px 10px;border:1px solid #E5E7EB;"><?= e($dItem) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr style="background:#F0FDF4;">
                    <td colspan="2" style="padding:10px;border:2px solid <?= e($headerColor) ?>;font-weight:700;font-size:16px;text-align:center;color:<?= e($headerColor) ?>;">
                        <?= e($totalLine ?: ('💵 الإجمالي: ' . money($receipt['amount']))) ?>
                    </td>
                </tr>
            </tfoot>
        </table>
        <?php else: ?>
        <table style="width:100%;border-collapse:collapse;margin-bottom:15px;font-size:14px;">
            <tr>
                <td style="padding:10px;border:2px solid <?= e($headerColor) ?>;font-weight:700;font-size:22px;text-align:center;color:<?= e($headerColor) ?>;direction:ltr;">
                    <?= money($receipt['amount']) ?>
                </td>
            </tr>
        </table>
        <?php endif; ?>

        <table style="width:100%;border-collapse:collapse;margin-bottom:20px;font-size:14px;">
            <tr>
                <td style="padding:8px;border:1px solid #E5E7EB;background:#F9FAFB;font-weight:600;width:30%;">المبلغ (رقماً)</td>
                <td style="padding:8px;border:1px solid #E5E7EB;font-size:22px;font-weight:700;color:<?= e($headerColor) ?>;direction:ltr;text-align:right;"><?= money($receipt['amount']) ?></td>
            </tr>
            <tr>
                <td style="padding:8px;border:1px solid #E5E7EB;background:#F9FAFB;font-weight:600;">المبلغ (كتابةً)</td>
                <td style="padding:8px;border:1px solid #E5E7EB;font-size:13px;"><?= e($receipt['amount_in_words_ar'] ?? '') ?></td>
            </tr>
        </table>

        <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:30px;margin-top:50px;text-align:center;font-size:13px;">
            <div style="border-top:1px solid #000;padding-top:10px;">توقيع المستلم</div>
            <div style="border-top:1px solid #000;padding-top:10px;">أمين الخزينة<br><small><?= e($receipt['issued_by_name'] ?? '') ?></small></div>
            <div style="border-top:1px solid #000;padding-top:10px;">المدير المالي</div>
        </div>

        <?php if ($showFooter): ?>
        <div style="margin-top:30px;text-align:center;font-size:11px;color:#9CA3AF;">
            طبع بتاريخ: <?= date('Y-m-d H:i:s') ?> — عدد مرات الطباعة: <?= (int) ($receipt['print_count'] ?? 0) + 1 ?>
        </div>
        <?php endif; ?>
    </div>
</div>
<?php $__template->endSection(); ?>
