<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once '../config.php';

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

$message = '';
$targetDir = "../assets/images/wingo/";

// Agar folder nahi hai toh bana do
if (!file_exists($targetDir)) {
    mkdir($targetDir, 0777, true);
}

// ==========================================
// HANDLE UPLOADS
// ==========================================
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $uploadSuccess = false;

    // Numbers 0 to 9 Upload Loop
    for ($i = 0; $i <= 9; $i++) {
        $inputName = 'num_' . $i;
        if (!empty($_FILES[$inputName]['name'])) {
            // Hamesha .png format mein save karenge taaki frontend me easy rahe
            $targetFile = $targetDir . "num_" . $i . ".png";
            move_uploaded_file($_FILES[$inputName]['tmp_name'], $targetFile);
            $uploadSuccess = true;
        }
    }

    // Win Image Upload
    if (!empty($_FILES['win_img']['name'])) {
        move_uploaded_file($_FILES['win_img']['tmp_name'], $targetDir . "win_popup.png");
        $uploadSuccess = true;
    }

    // Lose Image Upload
    if (!empty($_FILES['lose_img']['name'])) {
        move_uploaded_file($_FILES['lose_img']['tmp_name'], $targetDir . "lose_popup.png");
        $uploadSuccess = true;
    }

    if ($uploadSuccess) {
        $message = "Wingo Assets Uploaded Successfully!";
    }
}

$pageTitle = "Wingo Assets Manager";
require_once 'includes/header.php';
?>

<style>
    .asset-preview { width: 60px; height: 60px; object-fit: contain; border-radius: 10px; background: #f4f5f8; border: 1px solid #ddd; padding: 5px;}
    .popup-preview { width: 100px; height: auto; object-fit: contain; border-radius: 10px; background: #2d3436; padding: 10px;}
</style>

<?php if($message): ?>
    <div class="alert alert-success fw-bold border-0 shadow-sm"><i class="fas fa-check-circle"></i> <?= $message ?></div>
<?php endif; ?>

<div class="alert alert-info fw-bold">
    <i class="fas fa-lightbulb text-warning"></i> <b>Pro Tip:</b> Upload transparent background (.PNG) images for the best Original App experience!
</div>

<form method="POST" enctype="multipart/form-data">
    
    <div class="custom-card mb-4">
        <div class="custom-card-header bg-dark text-white">
            <h6 class="m-0 fw-bold"><i class="fas fa-trophy text-warning"></i> Win & Loss Popup Images</h6>
        </div>
        <div class="card-body p-4 row g-4 align-items-center">
            
            <div class="col-md-6 d-flex align-items-center gap-3">
                <div>
                    <img src="<?= $targetDir ?>win_popup.png?v=<?= time() ?>" class="popup-preview" onerror="this.src='https://via.placeholder.com/100?text=Win'">
                </div>
                <div class="flex-grow-1">
                    <label class="form-label fw-bold text-success">Winning Popup Header</label>
                    <input type="file" name="win_img" class="form-control" accept="image/png">
                </div>
            </div>

            <div class="col-md-6 d-flex align-items-center gap-3">
                <div>
                    <img src="<?= $targetDir ?>lose_popup.png?v=<?= time() ?>" class="popup-preview" onerror="this.src='https://via.placeholder.com/100?text=Lose'">
                </div>
                <div class="flex-grow-1">
                    <label class="form-label fw-bold text-danger">Losing Popup Header</label>
                    <input type="file" name="lose_img" class="form-control" accept="image/png">
                </div>
            </div>

        </div>
    </div>

    <div class="custom-card mb-4">
        <div class="custom-card-header">
            <h6 class="m-0 fw-bold"><i class="fas fa-sort-numeric-up-alt text-primary"></i> Wingo Numbers (0-9) 3D PNGs</h6>
        </div>
        <div class="card-body p-4">
            <div class="row g-4">
                <?php for($i = 0; $i <= 9; $i++): ?>
                <div class="col-md-4 d-flex align-items-center gap-3">
                    <img src="<?= $targetDir ?>num_<?= $i ?>.png?v=<?= time() ?>" class="asset-preview" onerror="this.src='https://via.placeholder.com/60?text=<?= $i ?>'">
                    <div class="flex-grow-1">
                        <label class="form-label fw-bold">Number <?= $i ?> PNG</label>
                        <input type="file" name="num_<?= $i ?>" class="form-control form-control-sm" accept="image/png">
                    </div>
                </div>
                <?php endfor; ?>
            </div>
            
            <div class="mt-4 text-end">
                <button type="submit" class="btn btn-primary btn-lg fw-bold px-5"><i class="fas fa-cloud-upload-alt"></i> Save & Update App</button>
            </div>
        </div>
    </div>

</form>

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