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

<?php $__template->section('page_actions'); ?>
<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 $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<!-- Filters -->
<div class="card" style="padding:15px;margin-bottom:20px;">
    <form method="GET" action="/pricing/board-offers" style="display:flex;gap:12px;align-items:end;flex-wrap:wrap;">
        <div class="form-group" style="margin:0;flex:1;min-width:200px;">
            <label class="form-label" style="font-size:12px;">بحث</label>
            <input type="text" name="q" value="<?= e($filters['search'] ?? '') ?>" class="form-input" placeholder="عنوان العرض أو رقم القرار...">
        </div>
        <div class="form-group" style="margin:0;width:150px;">
            <label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="is_active" class="form-select">
                <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:38px;">
            <i data-lucide="search" style="width:14px;height:14px;"></i>
        </button>
    </form>
</div>

<!-- Table -->
<div class="card" style="overflow-x:auto;">
    <table class="table">
        <thead>
            <tr>
                <th>العرض</th>
                <th>مسار الكاش</th>
                <th>مسار التقسيط</th>
                <th>الفرع</th>
                <th>الفترة</th>
                <th>الحالة</th>
                <th>إجراءات</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($rows)): ?>
            <tr><td colspan="7" style="text-align:center;color:#6B7280;padding:40px;">لا توجد عروض حالياً</td></tr>
            <?php else: ?>
            <?php foreach ($rows as $row): ?>
            <tr>
                <td>
                    <strong style="color:#1F2937;"><?= e($row['title_ar']) ?></strong>
                    <?php if ($row['board_decision_number']): ?>
                    <br><small style="color:#6B7280;">قرار رقم: <?= e($row['board_decision_number']) ?></small>
                    <?php endif; ?>
                </td>
                <td>
                    <?php if ($row['cash_discount_type']): ?>
                    <span style="background:#ECFDF5;color:#065F46;padding:2px 8px;border-radius:4px;font-size:12px;">
                        <?php if ($row['cash_discount_type'] === 'percentage'): ?>
                            <?= e($row['cash_discount_value']) ?>%
                        <?php else: ?>
                            <?= money($row['cash_discount_value']) ?>
                        <?php endif; ?>
                    </span>
                    <?php else: ?>
                    <span style="color:#9CA3AF;">—</span>
                    <?php endif; ?>
                </td>
                <td>
                    <?php if ($row['inst_months'] || $row['inst_interest_rate'] !== null || $row['inst_down_payment_pct'] !== null): ?>
                    <div style="font-size:12px;line-height:1.6;">
                        <?php if ($row['inst_down_payment_pct'] !== null): ?>
                        <span style="background:#EFF6FF;color:#1E40AF;padding:1px 6px;border-radius:3px;">مقدم <?= e($row['inst_down_payment_pct']) ?>%</span>
                        <?php endif; ?>
                        <?php if ($row['inst_months']): ?>
                        <span style="background:#FEF3C7;color:#92400E;padding:1px 6px;border-radius:3px;"><?= (int) $row['inst_months'] ?> شهر</span>
                        <?php endif; ?>
                        <?php if ($row['inst_interest_rate'] !== null): ?>
                        <span style="background:#FEE2E2;color:#991B1B;padding:1px 6px;border-radius:3px;">فائدة <?= e($row['inst_interest_rate']) ?>%</span>
                        <?php endif; ?>
                        <?php if ($row['inst_grace_months'] > 0): ?>
                        <br><span style="background:#F3E8FF;color:#6B21A8;padding:1px 6px;border-radius:3px;margin-top:3px;display:inline-block;">
                            <?= (int) $row['inst_grace_months'] ?> شهر سماح
                            <?= $row['inst_grace_type'] === 'full_free_under_n' ? '(إعفاء كامل)' : '' ?>
                        </span>
                        <?php endif; ?>
                    </div>
                    <?php else: ?>
                    <span style="color:#9CA3AF;">—</span>
                    <?php endif; ?>
                </td>
                <td>
                    <?php if ($row['branch_id']): ?>
                    <span style="font-size:12px;"><?= e($row['branch_name'] ?? 'فرع #' . $row['branch_id']) ?></span>
                    <?php else: ?>
                    <span style="color:#6B7280;font-size:12px;">كل الفروع</span>
                    <?php endif; ?>
                </td>
                <td style="font-size:12px;white-space:nowrap;">
                    <?= e($row['effective_from']) ?><br>
                    <span style="color:#6B7280;">إلى</span> <?= e($row['effective_to']) ?>
                </td>
                <td>
                    <?php
                    $today = date('Y-m-d');
                    $isExpired = $row['effective_to'] < $today;
                    $isActive = $row['is_active'] && !$isExpired;
                    ?>
                    <?php if ($isActive): ?>
                    <span style="background:#D1FAE5;color:#065F46;padding:3px 10px;border-radius:12px;font-size:11px;font-weight:600;">مفعّل</span>
                    <?php elseif ($isExpired): ?>
                    <span style="background:#FEE2E2;color:#991B1B;padding:3px 10px;border-radius:12px;font-size:11px;font-weight:600;">منتهي</span>
                    <?php else: ?>
                    <span style="background:#F3F4F6;color:#6B7280;padding:3px 10px;border-radius:12px;font-size:11px;font-weight:600;">معطّل</span>
                    <?php endif; ?>
                </td>
                <td style="white-space:nowrap;">
                    <a href="/pricing/board-offers/<?= (int) $row['id'] ?>/edit" class="btn btn-sm btn-outline" title="تعديل">
                        <i data-lucide="pencil" style="width:13px;height:13px;"></i>
                    </a>
                    <form method="POST" action="/pricing/board-offers/<?= (int) $row['id'] ?>/toggle" style="display:inline;">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-sm btn-outline" title="<?= $row['is_active'] ? 'تعطيل' : 'تفعيل' ?>">
                            <i data-lucide="<?= $row['is_active'] ? 'pause' : 'play' ?>" style="width:13px;height:13px;"></i>
                        </button>
                    </form>
                </td>
            </tr>
            <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>
</div>

<?php if (!empty($pagination) && $pagination['total_pages'] > 1): ?>
<div style="display:flex;justify-content:center;gap:6px;margin-top:20px;">
    <?php for ($p = 1; $p <= $pagination['total_pages']; $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 $__template->endSection(); ?>
