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

$discount_id = $_GET['id'] ?? null;
$discount = null;
$page_title = 'Reducere nouă';

if ($discount_id) {
    $discount_id = (int)$discount_id;
    $result = $db->query("SELECT * FROM discounts WHERE id = $discount_id");
    $discount = $result ? $result->fetch_assoc() : null;
    
    if (!$discount) {
        redirectWithMessage(APP_URL . '/admin/discounts.php', 'Reducere nu a fost găsită', 'error');
    }
    $page_title = 'Editează: ' . htmlspecialchars($discount['name']);
}

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

<div class="card" style="max-width:600px">
    <div class="card-header">
        <div class="card-title"><?php echo $discount ? 'Editează reducere' : 'Reducere nouă'; ?></div>
    </div>
    <div class="card-body">
        <form method="POST" action="<?php echo APP_URL; ?>/api/save-discount.php" style="display:grid;gap:14px">
            <?php if ($discount): ?>
                <input type="hidden" name="id" value="<?php echo $discount['id']; ?>">
            <?php endif; ?>

            <div class="form-group full">
                <label class="form-label">Nume reducere *</label>
                <input class="form-input" name="name" value="<?php echo $discount ? htmlspecialchars($discount['name']) : ''; ?>" placeholder="Ex: Reducere fidel" required>
            </div>

            <div class="form-grid">
                <div class="form-group">
                    <label class="form-label">Tip *</label>
                    <select class="form-select form-input" name="type" required>
                        <option value="percent" <?php echo !$discount || $discount['type'] === 'percent' ? 'selected' : ''; ?>>Procentual (%)</option>
                        <option value="fixed" <?php echo $discount && $discount['type'] === 'fixed' ? 'selected' : ''; ?>>Sumă fixă (lei)</option>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">Valoare *</label>
                    <input class="form-input" type="number" name="value" step="0.01" value="<?php echo $discount ? $discount['value'] : ''; ?>" placeholder="0.00" required>
                </div>
            </div>

            <div class="form-grid">
                <div class="form-group">
                    <label class="form-label">Valabilă de la</label>
                    <input class="form-input" type="date" name="valid_from" value="<?php echo $discount ? htmlspecialchars($discount['valid_from']) : ''; ?>">
                </div>
                <div class="form-group">
                    <label class="form-label">Valabilă până la</label>
                    <input class="form-input" type="date" name="valid_to" value="<?php echo $discount ? htmlspecialchars($discount['valid_to']) : ''; ?>">
                </div>
            </div>

            <div class="form-group">
                <label class="form-label">Status</label>
                <select class="form-select form-input" name="active">
                    <option value="1" <?php echo !$discount || $discount['active'] ? 'selected' : ''; ?>>Activă</option>
                    <option value="0" <?php echo $discount && !$discount['active'] ? 'selected' : ''; ?>>Inactivă</option>
                </select>
            </div>

            <div style="display:flex;gap:10px;margin-top:20px">
                <a href="<?php echo APP_URL; ?>/admin/discounts.php" class="btn btn-ghost" style="flex:1;text-align:center;text-decoration:none">Anulează</a>
                <button type="submit" class="btn btn-primary" style="flex:1">✓ Salvează</button>
            </div>
        </form>
    </div>
</div>

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