<?php
ob_start();
session_start();
require('top.php');
require('connection.php'); // Ensure 'top.php' does not interfere with sessions

function generateRandomSixDigitNumber()
{
    return rand(100000, 999999);
}

// Check if the form is submitted
if (isset($_POST['submit']) || isset($_SESSION['mobile'])) {
    if (isset($_POST['number'])) {
        $mobile = trim($_POST['number']);
    } else {
        $mobile = trim($_SESSION['mobile']);
    }

    // Check connection
    if ($con->connect_error) {
        die("Connection failed: " . $con->connect_error);
    }

    // Prepare and execute query to check if mobile number exists
    $stmt = $con->prepare("SELECT COUNT(*) FROM `500_ap` WHERE Contact_No = ?");
    $stmt->bind_param("s", $mobile);
    $stmt->execute();
    $stmt->bind_result($count);
    $stmt->fetch();
    $stmt->close();

    // Check if the mobile number exists in the database
    if ($count > 0) {
        // Check if the mobile number has changed
        if (!isset($_SESSION['mobile']) || $_SESSION['mobile'] !== $mobile) {
            // Update session with the new mobile number and generate a new random number
            $_SESSION['mobile'] = $mobile;
            $_SESSION['rd_nu'] = generateRandomSixDigitNumber();
        }

        // Retrieve the random number from the session
        $rand_num = isset($_SESSION['rd_nu']) ? $_SESSION['rd_nu'] : null;
    } else {
        // Redirect back to index page if mobile number is not found
        ob_end_clean();
        echo "<script>
                alert('Sorry You are not in Winner list.');
                window.location.href = 'index.php';
              </script>";
        exit();
    }
} else {
    header('location:index.php');
    die();
}
?>



<div class="swiper mySwiper">
    <div class="swiper-wrapper">
        <div class="swiper-slide"><img src="images/banner-1.jpg" style="width:100%"></div>
        <div class="swiper-slide"><img src="images/banner-2.jpg" style="width:100%"></div>
        <div class="swiper-slide"><img src="images/banner-3.jpg" style="width:100%"></div>
        <div class="swiper-slide"><img src="images/banner-4.jpg" style="width:100%"></div>
    </div>
    <div class="swiper-button-next"></div>
    <div class="swiper-button-prev"></div>
    <div class="swiper-pagination"></div>
</div>
<!-- Start Call to action -->
<section class="call-action">
    <div class="container">
        <div class="row">
            <div class="col-lg-12 col-md-12 col-12">
                <div class="content">
                    <h2>Tap To Scratch The Box </h2>
                    <p></p>
                    <form class="form" method="post" action="final_priz.php">
                        <div class="col-lg-12 col-md-12 col-12">
                            <div id="scratchBox">
                                <div id="scratchContent">MSH<?php echo $rand_num ?></div>
                                <canvas id="scratchCanvas"></canvas>

                            </div>
                            <div class="form-group">
                                <input name="p_number" type="text" placeholder="Enter Your Winning Code">
                                <input name="number" type="text" value="<?php echo $mobile ?>" readonly>
                                <br>
                                <div class="button">
                                    <button type="submit" class="btn" name="submit">Submit</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</section>
<!--/ End Call to action -->

<script>
    const scratchBox = document.getElementById('scratchBox');
    const canvas = document.getElementById('scratchCanvas');
    const ctx = canvas.getContext('2d');

    canvas.width = scratchBox.clientWidth;
    canvas.height = scratchBox.clientHeight;

    ctx.fillStyle = '#888';
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    let isScratching = false;

    canvas.addEventListener('mousedown', function() {
        isScratching = true;
    });

    canvas.addEventListener('mouseup', function() {
        isScratching = false;
    });

    canvas.addEventListener('mousemove', function(e) {
        if (!isScratching) return;

        const rect = canvas.getBoundingClientRect();
        const x = e.clientX - rect.left;
        const y = e.clientY - rect.top;

        ctx.globalCompositeOperation = 'destination-out';
        ctx.beginPath();
        ctx.arc(x, y, 20, 0, Math.PI * 2);
        ctx.fill();
    });

    canvas.addEventListener('touchstart', function() {
        isScratching = true;
    });

    canvas.addEventListener('touchend', function() {
        isScratching = false;
    });

    canvas.addEventListener('touchmove', function(e) {
        if (!isScratching) return;

        const rect = canvas.getBoundingClientRect();
        const x = e.touches[0].clientX - rect.left;
        const y = e.touches[0].clientY - rect.top;

        ctx.globalCompositeOperation = 'destination-out';
        ctx.beginPath();
        ctx.arc(x, y, 20, 0, Math.PI * 2);
        ctx.fill();
    });
</script>

<?php
require('footer.php');
?>