<?php
/**
 * Admin Promo Codes
 */
require_once 'includes/header.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    if ($action === 'add') {
        $data = [
            'code' => strtoupper(trim($_POST['code'])),
            'type' => $_POST['type'],
            'value' => (float)$_POST['value'],
            'min_deposit' => (float)$_POST['min_deposit'],
            'max_uses' => (int)$_POST['max_uses'],
            'expires_at' => $_POST['expires_at'] ?: null,
            'is_active' => 1
        ];
        dbInsert('promo_codes', $data);
        echo '<div class="alert alert-success"><i class="fas fa-check"></i> Promo code created!</div>';
    } elseif ($action === 'delete') {
        dbDelete('promo_codes', 'id = ?', [$_POST['code_id']]);
        echo '<div class="alert alert-success"><i class="fas fa-check"></i> Promo code deleted!</div>';
    } elseif ($action === 'toggle') {
        $code = dbFetch("SELECT is_active FROM promo_codes WHERE id = ?", [$_POST['code_id']]);
        dbUpdate('promo_codes', ['is_active' => !$code['is_active']], 'id = ?', [$_POST['code_id']]);
    }
}

$codes = dbFetchAll("SELECT * FROM promo_codes ORDER BY created_at DESC");
?>

<div class="page-header">
    <h1 class="page-title">Promo Codes</h1>
    <button class="btn btn-primary" onclick="document.getElementById('addModal').classList.add('active')">
        <i class="fas fa-plus"></i> Create Code
    </button>
</div>

<div class="card">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table">
                <thead>
                    <tr>
                        <th>Code</th>
                        <th>Type</th>
                        <th>Value</th>
                        <th>Min Deposit</th>
                        <th>Uses</th>
                        <th>Expires</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($codes)): ?>
                    <tr><td colspan="8" class="text-center py-4">No promo codes found</td></tr>
                    <?php else: ?>
                    <?php foreach ($codes as $code): ?>
                    <tr>
                        <td class="font-mono font-semibold"><?= $code['code'] ?></td>
                        <td><?= ucfirst($code['type']) ?></td>
                        <td><?= $code['type'] === 'percent' ? $code['value'].'%' : CURRENCY_SYMBOL.$code['value'] ?></td>
                        <td><?= CURRENCY_SYMBOL ?><?= number_format($code['min_deposit'] ?? 0) ?></td>
                        <td><?= $code['used_count'] ?? 0 ?>/<?= $code['max_uses'] ?: '∞' ?></td>
                        <td><?= $code['expires_at'] ? date('d M Y', strtotime($code['expires_at'])) : 'Never' ?></td>
                        <td><span class="badge badge-<?= $code['is_active'] ? 'success' : 'danger' ?>"><?= $code['is_active'] ? 'Active' : 'Inactive' ?></span></td>
                        <td>
                            <form method="POST" style="display:inline">
                                <input type="hidden" name="action" value="toggle">
                                <input type="hidden" name="code_id" value="<?= $code['id'] ?>">
                                <button class="btn btn-sm btn-secondary"><i class="fas fa-power-off"></i></button>
                            </form>
                            <form method="POST" style="display:inline" onsubmit="return confirm('Delete?')">
                                <input type="hidden" name="action" value="delete">
                                <input type="hidden" name="code_id" value="<?= $code['id'] ?>">
                                <button class="btn btn-sm btn-danger"><i class="fas fa-trash"></i></button>
                            </form>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="modal" id="addModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4>Create Promo Code</h4>
                <button class="modal-close" onclick="document.getElementById('addModal').classList.remove('active')">&times;</button>
            </div>
            <form method="POST">
                <input type="hidden" name="action" value="add">
                <div class="modal-body">
                    <div class="form-group">
                        <label class="form-label">Code</label>
                        <input type="text" name="code" class="form-control" required placeholder="WELCOME50">
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="form-label">Type</label>
                                <select name="type" class="form-control">
                                    <option value="percent">Percentage</option>
                                    <option value="fixed">Fixed Amount</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="form-label">Value</label>
                                <input type="number" name="value" class="form-control" value="10">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="form-label">Min Deposit</label>
                                <input type="number" name="min_deposit" class="form-control" value="500">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="form-label">Max Uses (0 = unlimited)</label>
                                <input type="number" name="max_uses" class="form-control" value="100">
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="form-label">Expires At</label>
                        <input type="date" name="expires_at" class="form-control">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Create Code</button>
                </div>
            </form>
        </div>
    </div>
</div>

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