<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>عروض مجلس الإدارة<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/pricing" class="btn btn-outline" style="display:inline-flex;align-items:center;gap:6px;">
    <i data-lucide="arrow-right" style="width:15px;height:15px;"></i> التسعير
</a>
<?php if (can('pricing.board_offers.create')): ?>
<a href="/pricing/board-offers/create" class="btn btn-primary" style="display:inline-flex;align-items:center;gap:6px;">
    <i data-lucide="plus" style="width:15px;height:15px;"></i> عرض جديد
</a>
<?php endif; ?>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<?php
$today = date('Y-m-d');
$activeCount = 0;
$expiredCount = 0;
$disabledCount = 0;
foreach ($rows as $r) {
    $exp = $r['effective_to'] < $today;
    if ($r['is_active'] && !$exp) $activeCount++;
    elseif ($exp) $expiredCount++;
    else $disabledCount++;
}
?>

<!-- Stats -->
<div style="display:grid;grid-template-columns:repeat(3,1fr);gap:12px;margin-bottom:20px;">
    <div style="background:#fff;border:1px solid #E5E7EB;border-radius:10px;padding:14px 18px;display:flex;align-items:center;gap:12px;">
        <div style="width:36px;height:36px;border-radius:8px;background:#D1FAE5;display:flex;align-items:center;justify-content:center;">
            <i data-lucide="check-circle" style="width:18px;height:18px;color:#059669;"></i>
        </div>
        <div>
            <div style="font-size:22px;font-weight:700;color:#059669;"><?= $activeCount ?></div>
            <div style="font-size:12px;color:#6B7280;">عروض مفعّلة</div>
        </div>
    </div>
    <div style="background:#fff;border:1px solid #E5E7EB;border-radius:10px;padding:14px 18px;display:flex;align-items:center;gap:12px;">
        <div style="width:36px;height:36px;border-radius:8px;background:#FEE2E2;display:flex;align-items:center;justify-content:center;">
            <i data-lucide="clock" style="width:18px;height:18px;color:#DC2626;"></i>
        </div>
        <div>
            <div style="font-size:22px;font-weight:700;color:#DC2626;"><?= $expiredCount ?></div>
            <div style="font-size:12px;color:#6B7280;">منتهية</div>
        </div>
    </div>
    <div style="background:#fff;border:1px solid #E5E7EB;border-radius:10px;padding:14px 18px;display:flex;align-items:center;gap:12px;">
        <div style="width:36px;height:36px;border-radius:8px;background:#F3F4F6;display:flex;align-items:center;justify-content:center;">
            <i data-lucide="pause-circle" style="width:18px;height:18px;color:#6B7280;"></i>
        </div>
        <div>
            <div style="font-size:22px;font-weight:700;color:#6B7280;"><?= $disabledCount ?></div>
            <div style="font-size:12px;color:#6B7280;">معطّلة</div>
        </div>
    </div>
</div>

<!-- Filters -->
<div class="card" style="padding:12px 15px;margin-bottom:16px;">
    <form method="GET" action="/pricing/board-offers" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div class="form-group" style="margin:0;flex:1;min-width:200px;">
            <input type="text" name="q" value="<?= e($filters['search'] ?? '') ?>" class="form-input" placeholder="بحث بعنوان العرض أو رقم القرار..." style="height:36px;">
        </div>
        <div class="form-group" style="margin:0;width:130px;">
            <select name="is_active" class="form-select" style="height:36px;">
                <option value="">كل الحالات</option>
                <option value="1" <?= ($filters['is_active'] ?? '') === '1' ? 'selected' : '' ?>>مفعّل</option>
                <option value="0" <?= ($filters['is_active'] ?? '') === '0' ? 'selected' : '' ?>>معطّل</option>
            </select>
        </div>
        <button type="submit" class="btn btn-outline" style="height:36px;padding:0 14px;">
            <i data-lucide="search" style="width:14px;height:14px;"></i>
        </button>
    </form>
</div>

<!-- Offers List -->
<?php if (empty($rows)): ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;"><i data-lucide="award" style="width:48px;height:48px;color:#D1D5DB;"></i></div>
    <h3 style="color:#374151;margin:0 0 8px;">لا توجد عروض مجلس إدارة</h3>
    <p style="color:#6B7280;font-size:14px;margin:0 0 20px;">عروض مجلس الإدارة تتحكم في شروط الدفع — خصومات الكاش، نسب المقدم، عدد الأشهر، وفترات السماح.</p>
    <?php if (can('pricing.board_offers.create')): ?>
    <a href="/pricing/board-offers/create" class="btn btn-primary">
        <i data-lucide="plus" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> إنشاء أول عرض
    </a>
    <?php endif; ?>
</div>
<?php else: ?>
<div style="display:flex;flex-direction:column;gap:12px;">
<?php foreach ($rows as $row):
    $isExpired = $row['effective_to'] < $today;
    $isFuture = $row['effective_from'] > $today;
    $isActive = $row['is_active'] && !$isExpired && !$isFuture;
    $hasCash = !empty($row['cash_discount_type']) && !empty($row['cash_discount_value']);
    $hasInst = ($row['inst_months'] || $row['inst_interest_rate'] !== null || $row['inst_down_payment_pct'] !== null);
    $usageCount = (int) ($row['usage_count'] ?? 0);

    if ($isActive) {
        $borderColor = '#059669'; $statusBg = '#D1FAE5'; $statusColor = '#065F46'; $statusLabel = 'ساري';
    } elseif ($isFuture) {
        $borderColor = '#2563EB'; $statusBg = '#DBEAFE'; $statusColor = '#1E40AF'; $statusLabel = 'قادم';
    } elseif ($isExpired) {
        $borderColor = '#DC2626'; $statusBg = '#FEE2E2'; $statusColor = '#991B1B'; $statusLabel = 'منتهي';
    } else {
        $borderColor = '#9CA3AF'; $statusBg = '#F3F4F6'; $statusColor = '#6B7280'; $statusLabel = 'معطّل';
    }

    $daysLeft = $isActive ? (int) ((strtotime($row['effective_to']) - strtotime($today)) / 86400) : 0;
?>
<div class="card" style="padding:0;border-right:4px solid <?= $borderColor ?>;overflow:hidden;<?= !$isActive && !$isFuture ? 'opacity:0.75;' : '' ?>">
    <div style="padding:16px 20px;display:flex;align-items:flex-start;gap:16px;">
        <!-- Main Info -->
        <div style="flex:1;min-width:0;">
            <div style="display:flex;align-items:center;gap:10px;margin-bottom:6px;">
                <h4 style="margin:0;font-size:15px;color:#1F2937;"><?= e($row['title_ar']) ?></h4>
                <span style="background:<?= $statusBg ?>;color:<?= $statusColor ?>;padding:2px 10px;border-radius:10px;font-size:11px;font-weight:600;white-space:nowrap;"><?= $statusLabel ?></span>
                <?php if ($isActive && $daysLeft <= 14): ?>
                <span style="background:#FEF3C7;color:#92400E;padding:2px 8px;border-radius:10px;font-size:11px;font-weight:600;">ينتهي خلال <?= $daysLeft ?> يوم</span>
                <?php endif; ?>
            </div>
            <div style="display:flex;align-items:center;gap:12px;font-size:12px;color:#6B7280;margin-bottom:10px;">
                <?php if ($row['board_decision_number']): ?>
                <span><i data-lucide="file-text" style="width:12px;height:12px;vertical-align:middle;margin-left:3px;"></i>قرار <?= e($row['board_decision_number']) ?><?= $row['board_decision_date'] ? ' — ' . e($row['board_decision_date']) : '' ?></span>
                <?php endif; ?>
                <span><i data-lucide="calendar" style="width:12px;height:12px;vertical-align:middle;margin-left:3px;"></i><?= e($row['effective_from']) ?> إلى <?= e($row['effective_to']) ?></span>
                <span><i data-lucide="map-pin" style="width:12px;height:12px;vertical-align:middle;margin-left:3px;"></i><?= $row['branch_name'] ? e($row['branch_name']) : 'كل الفروع' ?></span>
                <?php if ($usageCount > 0): ?>
                <span style="color:#0D7377;font-weight:600;"><i data-lucide="users" style="width:12px;height:12px;vertical-align:middle;margin-left:3px;"></i><?= $usageCount ?> استخدام</span>
                <?php endif; ?>
            </div>

            <!-- Paths -->
            <div style="display:flex;gap:16px;flex-wrap:wrap;">
                <?php if ($hasCash): ?>
                <div style="background:#F0FDF4;border:1px solid #BBF7D0;border-radius:8px;padding:8px 12px;min-width:160px;">
                    <div style="font-size:11px;color:#166534;font-weight:600;margin-bottom:3px;"><i data-lucide="banknote" style="width:12px;height:12px;vertical-align:middle;margin-left:3px;"></i>مسار الكاش</div>
                    <div style="font-size:16px;font-weight:700;color:#059669;">
                        <?php if ($row['cash_discount_type'] === 'percentage'): ?>
                            خصم <?= e($row['cash_discount_value']) ?>%
                        <?php else: ?>
                            خصم <?= money($row['cash_discount_value']) ?>
                        <?php endif; ?>
                    </div>
                </div>
                <?php endif; ?>

                <?php if ($hasInst): ?>
                <div style="background:#EFF6FF;border:1px solid #BFDBFE;border-radius:8px;padding:8px 12px;min-width:200px;">
                    <div style="font-size:11px;color:#1E40AF;font-weight:600;margin-bottom:3px;"><i data-lucide="layers" style="width:12px;height:12px;vertical-align:middle;margin-left:3px;"></i>مسار التقسيط</div>
                    <div style="display:flex;gap:8px;flex-wrap:wrap;font-size:12px;">
                        <?php if ($row['inst_down_payment_pct'] !== null): ?>
                        <span style="background:#fff;padding:2px 6px;border-radius:4px;border:1px solid #BFDBFE;">مقدم <?= e($row['inst_down_payment_pct']) ?>%</span>
                        <?php endif; ?>
                        <?php if ($row['inst_months']): ?>
                        <span style="background:#fff;padding:2px 6px;border-radius:4px;border:1px solid #BFDBFE;"><?= (int) $row['inst_months'] ?> شهر</span>
                        <?php endif; ?>
                        <?php if ($row['inst_interest_rate'] !== null): ?>
                        <span style="background:#fff;padding:2px 6px;border-radius:4px;border:1px solid #BFDBFE;">فائدة <?= e($row['inst_interest_rate']) ?>%</span>
                        <?php endif; ?>
                        <?php if ($row['inst_grace_months'] > 0): ?>
                        <span style="background:#F3E8FF;padding:2px 6px;border-radius:4px;border:1px solid #E9D5FF;color:#6B21A8;">
                            <?= (int) $row['inst_grace_months'] ?> شهر سماح<?= $row['inst_grace_type'] === 'full_free_under_n' ? ' (إعفاء كامل)' : '' ?>
                        </span>
                        <?php endif; ?>
                    </div>
                </div>
                <?php endif; ?>

                <?php if (!$hasCash && !$hasInst): ?>
                <div style="color:#9CA3AF;font-size:13px;padding:8px 0;">لم يتم تحديد مسار دفع</div>
                <?php endif; ?>
            </div>
        </div>

        <!-- Actions -->
        <div style="display:flex;flex-direction:column;gap:6px;align-items:flex-end;">
            <?php if (can('pricing.board_offers.edit')): ?>
            <a href="/pricing/board-offers/<?= (int) $row['id'] ?>/edit" class="btn btn-sm btn-outline" style="font-size:12px;padding:5px 12px;white-space:nowrap;">
                <i data-lucide="pencil" style="width:12px;height:12px;vertical-align:middle;margin-left:3px;"></i> تعديل
            </a>
            <form method="POST" action="/pricing/board-offers/<?= (int) $row['id'] ?>/toggle" style="margin:0;">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-sm btn-outline" style="font-size:12px;padding:5px 12px;white-space:nowrap;<?= $row['is_active'] ? 'color:#DC2626;border-color:#FCA5A5;' : 'color:#059669;border-color:#86EFAC;' ?>">
                    <i data-lucide="<?= $row['is_active'] ? 'pause' : 'play' ?>" style="width:12px;height:12px;vertical-align:middle;margin-left:3px;"></i>
                    <?= $row['is_active'] ? 'تعطيل' : 'تفعيل' ?>
                </button>
            </form>
            <?php endif; ?>
        </div>
    </div>
</div>
<?php endforeach; ?>
</div>

<?php if (!empty($pagination) && $pagination['last_page'] > 1): ?>
<div style="display:flex;justify-content:center;gap:6px;margin-top:20px;">
    <?php for ($p = 1; $p <= $pagination['last_page']; $p++): ?>
    <a href="?page=<?= $p ?>&q=<?= urlencode($filters['search'] ?? '') ?>&is_active=<?= urlencode($filters['is_active'] ?? '') ?>"
       class="btn btn-sm <?= $p === $pagination['current_page'] ? 'btn-primary' : 'btn-outline' ?>">
        <?= $p ?>
    </a>
    <?php endfor; ?>
</div>
<?php endif; ?>
<?php endif; ?>

<?php $__template->endSection(); ?>
