<?php
require_once __DIR__ . '/../includes/config.php';

$page_title = 'Reduceri';

require_once __DIR__ . '/../includes/header.php';

// Get discounts
$result = $db->query("SELECT * FROM discounts ORDER BY valid_from DESC");
$discounts = [];
if ($result && $result->num_rows > 0) {
    while ($discount = $result->fetch_assoc()) {
        $discounts[] = $discount;
    }
}
?>

<div style="display:flex;justify-content:flex-end;margin-bottom:16px">
    <a href="<?php echo APP_URL; ?>/admin/discount-form.php" class="btn btn-primary">
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" width="15" height="15"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
        Reducere nouă
    </a>
</div>

<?php if (empty($discounts)): ?>
    <div class="empty-state" style="grid-column:1/-1">
        <h3>Nicio reducere</h3>
        <p>Creează prima reducere</p>
    </div>
<?php else: ?>
    <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(280px,1fr));gap:14px">
        <?php foreach ($discounts as $d): ?>
            <div style="background:var(--bg2);border:1px solid var(--border);border-radius:var(--radius2);padding:16px 18px;display:flex;align-items:center;gap:14px;<?php echo !$d['active'] ? 'opacity:0.55' : ''; ?>">
                <div style="width:52px;height:52px;border-radius:12px;display:flex;align-items:center;justify-content:center;font-family:'Syne',sans-serif;font-size:13px;font-weight:700;flex-shrink:0;background:<?php echo $d['active'] ? 'var(--accent-bg)' : 'var(--bg4)'; ?>;color:<?php echo $d['active'] ? 'var(--accent)' : 'var(--text3)'; ?>">
                    <?php echo $d['type'] === 'percent' ? $d['value'] . '%' : $d['value'] . 'L'; ?>
                </div>
                <div style="flex:1">
                    <div style="font-family:'Syne',sans-serif;font-size:14px;font-weight:600;margin-bottom:3px"><?php echo htmlspecialchars($d['name']); ?></div>
                    <div style="font-size:12px;color:var(--text3)"><?php echo $d['type'] === 'percent' ? 'Procentual' : 'Sumă fixă'; ?> · <?php echo formatDate($d['valid_from']); ?> – <?php echo formatDate($d['valid_to']); ?></div>
                    <div style="margin-top:6px"><?php echo $d['active'] ? '<span class="badge badge-green">Activă</span>' : '<span class="badge badge-gray">Inactivă</span>'; ?></div>
                </div>
                <div style="display:flex;flex-direction:column;gap:6px">
                    <a href="<?php echo APP_URL; ?>/admin/discount-form.php?id=<?php echo $d['id']; ?>" class="btn btn-ghost btn-sm">✎</a>
                    <form method="POST" action="<?php echo APP_URL; ?>/api/delete-discount.php" style="display:inline" onsubmit="return confirm('Ștergi reducerea?')">
                        <input type="hidden" name="id" value="<?php echo $d['id']; ?>">
                        <button type="submit" class="btn btn-danger btn-sm">✕</button>
                    </form>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

<?php
require_once __DIR__ . '/../includes/footer.php';
?>
