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

$design = BrandingService::carnetDesign();
$clubNameAr = BrandingService::clubNameAr();
$clubNameEn = BrandingService::clubNameEn();
$clubSubtitle = BrandingService::subtitle();
$logoUrl = BrandingService::logo();

$bgColor    = $design['front_bg_color'];
$textColor  = $design['front_text_color'];
$showSub    = $design['front_show_subtitle'];
$showEn     = $design['front_show_english_name'];
$showType   = $design['front_show_member_type'];
$showQr     = $design['front_show_qr'];
$qrPos      = $design['front_qr_position'];
$logoPos    = $design['front_logo_position'];
$showStrip  = $design['back_show_branch_strip'];
$stripColor = $design['back_strip_color'];
$showInstr  = $design['back_show_instructions'];
$instrText  = $design['back_instructions_text'];
$backFields = $design['back_fields'] ?? [];

// QR position CSS
$qrStyle = 'position:absolute;';
if (str_contains($qrPos, 'bottom')) $qrStyle .= 'bottom:15px;'; else $qrStyle .= 'top:15px;';
if (str_contains($qrPos, 'left'))   $qrStyle .= 'left:15px;';   else $qrStyle .= 'right:15px;';

// Logo position CSS
$logoAlign = 'text-align:center;';
if ($logoPos === 'top-right') $logoAlign = 'text-align:right;';
elseif ($logoPos === 'top-left') $logoAlign = 'text-align:left;';

// Back field data map
$fieldLabels = [
    'full_name_ar' => 'الاسم',
    'full_name_en' => 'Name',
    'membership_number' => 'رقم العضوية',
    'branch_name' => 'الفرع',
    'issued_at' => 'تاريخ الإصدار',
    'national_id' => 'الرقم القومي',
    'phone' => 'الهاتف',
];
$fieldValues = [
    'full_name_ar' => $carnet['full_name_ar'] ?? '—',
    'full_name_en' => $carnet['full_name_en'] ?? '—',
    'membership_number' => $carnet['membership_number'] ?? '—',
    'branch_name' => $carnet['branch_name'] ?? '—',
    'issued_at' => isset($carnet['issued_at']) ? substr($carnet['issued_at'], 0, 10) : '—',
    'national_id' => $carnet['national_id'] ?? '—',
    'phone' => $carnet['phone'] ?? '—',
];
?>
<?php $__template->layout('Layout.print'); ?>
<?php $__template->section('title'); ?>كارنيه العضوية — <?= e($carnet['membership_number'] ?? $carnet['carnet_number']) ?><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<style>
    @media print { body { margin: 0; } .print-header { display: none; } }
    .carnet-container { width: 340px; margin: 20px auto; font-family: 'Cairo', Arial, sans-serif; }
    .carnet-front {
        width: 340px; height: 215px; background: <?= e($bgColor) ?>; border-radius: 12px;
        color: <?= e($textColor) ?>; padding: 20px; position: relative; overflow: hidden; margin-bottom: 15px;
        box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    }
    .carnet-back {
        width: 340px; height: 215px; background: #fff; border-radius: 12px;
        border: 2px solid <?= e($stripColor) ?>; padding: 20px; position: relative;
        box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    }
    .carnet-front .club-name { <?= $logoAlign ?> font-size: 16px; font-weight: 700; margin-bottom: 4px; }
    .carnet-front .club-subtitle { <?= $logoAlign ?> font-size: 11px; opacity: 0.8; margin-bottom: 15px; }
    .carnet-front .front-logo { <?= $logoAlign ?> margin-bottom: 8px; }
    .carnet-front .front-logo img { max-height: 30px; }
    .carnet-front .member-name { font-size: 14px; font-weight: 700; margin-bottom: 5px; }
    .carnet-front .member-name-en { font-size: 11px; opacity: 0.8; margin-bottom: 8px; }
    .carnet-front .member-number { font-size: 20px; font-weight: 700; letter-spacing: 2px; direction: ltr; text-align: right; }
    .carnet-front .member-type { font-size: 11px; margin-top: 5px; opacity: 0.8; }
    .carnet-front .qr-area { <?= $qrStyle ?> width: 70px; height: 70px; background: #fff; border-radius: 6px; padding: 5px; }
    .carnet-front .qr-area svg { width: 60px; height: 60px; }
    .carnet-front .carnet-num { position: absolute; bottom: 15px; right: 15px; font-size: 10px; opacity: 0.7; }
    .carnet-back .back-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; border-bottom: 2px solid <?= e($stripColor) ?>; padding-bottom: 10px; }
    .carnet-back .back-header .logo-text { font-size: 12px; font-weight: 700; color: <?= e($stripColor) ?>; }
    .carnet-back .back-header .back-logo img { max-height: 25px; }
    .carnet-back .branch-strip { position: absolute; left: 0; top: 0; bottom: 0; width: 30px; background: <?= e($stripColor) ?>; border-radius: 12px 0 0 12px; writing-mode: vertical-rl; text-orientation: mixed; display: flex; align-items: center; justify-content: center; color: #fff; font-size: 11px; font-weight: 600; }
    .carnet-back .instructions { font-size: 10px; color: #6B7280; text-align: center; position: absolute; bottom: 10px; left: 40px; right: 10px; }
    .carnet-back .member-info { margin-right: 35px; font-size: 12px; color: #1A1A2E; }
    .carnet-back .member-info div { margin-bottom: 4px; }
    .carnet-back .member-info strong { color: <?= e($stripColor) ?>; }
</style>

<div class="carnet-container">
    <!-- Front -->
    <div class="carnet-front">
        <?php if ($logoUrl): ?>
            <div class="front-logo"><img src="<?= e($logoUrl) ?>" alt="Logo"></div>
        <?php endif; ?>
        <div class="club-name"><?= e($clubNameEn) ?> — <?= e($clubNameAr) ?></div>
        <?php if ($showSub): ?>
            <div class="club-subtitle"><?= e($clubSubtitle) ?></div>
        <?php endif; ?>
        <div class="member-name"><?= e($carnet['full_name_ar']) ?></div>
        <?php if ($showEn && !empty($carnet['full_name_en'])): ?>
            <div class="member-name-en"><?= e($carnet['full_name_en']) ?></div>
        <?php endif; ?>
        <div class="member-number"><?= e($carnet['membership_number'] ?? '—') ?></div>
        <?php if ($showType): ?>
            <div class="member-type"><?= $carnet['carnet_type'] === 'seasonal' ? 'عضوية موسمية' : 'عضو عامل' ?></div>
        <?php endif; ?>
        <?php if ($showQr): ?>
            <div class="qr-area"><?= $qrSvg ?></div>
        <?php endif; ?>
        <div class="carnet-num"><?= e($carnet['carnet_number']) ?></div>
    </div>

    <!-- Back -->
    <div class="carnet-back">
        <?php if ($showStrip): ?>
            <div class="branch-strip"><?= e($carnet['branch_name'] ?? 'فرع شيراتون') ?></div>
        <?php endif; ?>
        <div class="back-header">
            <?php if ($logoUrl): ?>
                <div class="back-logo"><img src="<?= e($logoUrl) ?>" alt="Logo"></div>
            <?php else: ?>
                <div class="logo-text"><?= e($clubNameEn) ?><br><?= e($clubNameAr) ?></div>
            <?php endif; ?>
        </div>
        <div class="member-info">
            <?php foreach ($backFields as $field): ?>
                <?php if (isset($fieldLabels[$field])): ?>
                    <div><strong><?= e($fieldLabels[$field]) ?>:</strong> <?= e($fieldValues[$field] ?? '—') ?></div>
                <?php endif; ?>
            <?php endforeach; ?>
        </div>
        <?php if ($showInstr): ?>
            <div class="instructions"><?= e($instrText) ?></div>
        <?php endif; ?>
    </div>
</div>
<?php $__template->endSection(); ?>
