<?php
session_start();
require_once '../config.php';

if (!isset($_SESSION['admin_id'])) {
    header("Location: login.php");
    exit;
}

global $pdo;
$message = '';

// Handle Edit Reward
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_reward'])) {
    $id = $_POST['id'];
    $name = $_POST['name'];
    $type = $_POST['type']; // cash, bonus, empty
    $amount = $_POST['amount'];
    $probability = $_POST['probability'];
    $color = $_POST['color'];

    $stmt = $pdo->prepare("UPDATE spin_rewards SET name=?, type=?, amount=?, probability=?, color=? WHERE id=?");
    $stmt->execute([$name, $type, $amount, $probability, $color, $id]);
    $message = "Reward Updated Successfully!";
}

// Fetch Rewards
$rewards = $pdo->query("SELECT * FROM spin_rewards ORDER BY id ASC")->fetchAll();

$pageTitle = 'Spin Wheel';
require_once 'includes/header.php';
?>
<div class="custom-card">
    <div class="custom-card-header"><i class="fas fa-dharmachakra me-2"></i> Spin Wheel Rewards</div>
    <div class="p-4">
        <?php if($message): ?>
            <div class="alert alert-success fw-bold"><i class="fas fa-check-circle"></i> <?= htmlspecialchars($message) ?></div>
        <?php endif; ?>
        <div class="alert alert-warning fw-bold">
            <i class="fas fa-info-circle"></i> Total Win Probability should equal 100%.
        </div>
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead class="table-dark">
                    <tr>
                        <th>Item Label</th>
                        <th>Type</th>
                        <th>Prize Amount (₹)</th>
                        <th>Win Probability (%)</th>
                        <th>Wheel Color</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(!empty($rewards)): foreach($rewards as $r): ?>
                    <tr class="align-middle">
                        <form method="POST">
                            <input type="hidden" name="update_reward" value="1">
                            <input type="hidden" name="id" value="<?= $r['id'] ?>">
                            
                            <td><input type="text" name="name" value="<?= htmlspecialchars($r['name']) ?>" class="form-control form-control-sm" required></td>
                            
                            <td>
                                <select name="type" class="form-select form-select-sm">
                                    <option value="cash" <?= $r['type'] == 'cash' ? 'selected' : '' ?>>Main Cash</option>
                                    <option value="bonus" <?= $r['type'] == 'bonus' ? 'selected' : '' ?>>Bonus Wallet</option>
                                    <option value="empty" <?= $r['type'] == 'empty' ? 'selected' : '' ?>>Empty (Try Again)</option>
                                </select>
                            </td>
                            
                            <td><input type="number" step="0.01" name="amount" value="<?= $r['amount'] ?>" class="form-control form-control-sm" required></td>
                            
                            <td><input type="number" step="0.01" name="probability" value="<?= $r['probability'] ?>" class="form-control form-control-sm" required></td>
                            
                            <td>
                                <div class="d-flex align-items-center gap-2">
                                    <input type="color" name="color" value="<?= $r['color'] ?>" class="form-control form-control-sm form-control-color">
                                </div>
                            </td>
                            
                            <td>
                                <button type="submit" class="btn btn-sm btn-primary fw-bold"><i class="fas fa-save"></i> Save</button>
                            </td>
                        </form>
                    </tr>
                    <?php endforeach; else: ?>
                    <tr><td colspan="6" class="text-center py-4 text-danger fw-bold">No Rewards Setup in Database! Please import SQL properly.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?php require_once 'includes/footer.php'; ?>