<div class="page-header">
    <div><h1>Deals</h1><p>Manage combo deals and bundle offers.</p></div>
    <?php if (\Core\Auth::can('deal.create')): ?>
    <a href="<?= url('deals/create') ?>" class="btn btn-accent"><i class="bi bi-plus-lg me-1"></i> Create Deal</a>
    <?php endif; ?>
</div>

<?php if (empty($deals)): ?>
<div class="card">
    <div class="card-body text-center py-5 text-secondary">
        <i class="bi bi-gift fs-1 d-block mb-3 opacity-25"></i>
        <h6>No deals yet</h6>
        <a href="<?= url('deals/create') ?>" class="btn btn-accent btn-sm mt-2">Create First Deal</a>
    </div>
</div>
<?php else: ?>

<div class="row g-3">
    <?php foreach ($deals as $deal): ?>
    <?php
        $expired = strtotime($deal['expires_at']) < time();
        $started = strtotime($deal['starts_at']) <= time();
        $statusClass = $deal['is_active'] && $started && !$expired ? 'success' : ($expired ? 'danger' : 'secondary');
        $statusLabel = $expired ? 'Expired' : ($deal['is_active'] ? ($started ? 'Active' : 'Upcoming') : 'Inactive');
    ?>
    <div class="col-12 col-md-6 col-xl-4">
        <div class="card h-100">
            <!-- Deal Image -->
            <div style="height:160px;overflow:hidden;border-radius:12px 12px 0 0;background:#f4f6fc;position:relative;">
                <?php if ($deal['deal_image']): ?>
                <img src="<?= e(upload_url($deal['deal_image'])) ?>"
                     style="width:100%;height:100%;object-fit:cover;">
                <?php else: ?>
                <div class="d-flex align-items-center justify-content-center h-100">
                    <i class="bi bi-gift text-secondary" style="font-size:3rem;opacity:.3;"></i>
                </div>
                <?php endif; ?>
                <span class="badge bg-<?= $statusClass ?> position-absolute top-0 end-0 m-2">
                    <?= $statusLabel ?>
                </span>
            </div>

            <div class="card-body">
                <h6 class="fw-700 mb-1"><?= e($deal['title']) ?></h6>
                <?php if ($deal['description']): ?>
                <p class="text-secondary mb-2" style="font-size:.8rem;"><?= e(str_limit($deal['description'],80)) ?></p>
                <?php endif; ?>

                <!-- Items in deal -->
                <?php if (!empty($deal['items'])): ?>
                <div class="mb-3">
                    <?php foreach ($deal['items'] as $di): ?>
                    <div class="d-flex align-items-center gap-2 mb-1">
                        <i class="bi bi-check-circle-fill text-success" style="font-size:.75rem;"></i>
                        <span style="font-size:.82rem;"><?= e($di['item_name']) ?></span>
                        <?php if ($di['quantity'] > 1): ?>
                        <span class="badge bg-light text-dark border ms-auto">x<?= $di['quantity'] ?></span>
                        <?php endif; ?>
                    </div>
                    <?php endforeach; ?>
                </div>
                <?php endif; ?>

                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <div class="fw-800 text-accent fs-5"><?= money((float)$deal['deal_price']) ?></div>
                        <div class="text-secondary" style="font-size:.72rem;">
                            <?= format_date($deal['starts_at']) ?> → <?= format_date($deal['expires_at']) ?>
                        </div>
                    </div>
                    <div class="d-flex gap-1">
                        <?php if (\Core\Auth::can('deal.edit')): ?>
                        <a href="<?= url('deals/'.$deal['id'].'/edit') ?>"
                           class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil"></i></a>
                        <?php endif; ?>
                        <?php if (\Core\Auth::can('deal.delete')): ?>
                        <form method="POST" action="<?= url('deals/'.$deal['id'].'/delete') ?>">
                            <?= csrf_field() ?>
                            <button class="btn btn-sm btn-outline-danger"
                                    data-confirm="Delete deal '<?= e($deal['title']) ?>'?">
                                <i class="bi bi-trash"></i>
                            </button>
                        </form>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php endforeach; ?>
</div>

<?php if ($total > ITEMS_PER_PAGE): ?>
<div class="d-flex justify-content-center mt-4">
    <?= paginate($total, $page, ITEMS_PER_PAGE, url('deals')) ?>
</div>
<?php endif; ?>

<?php endif; ?>
