<html>
<?php
include('config.php');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $errors = [];


 // Validate required fields
    $requiredFields = ['distict_name',  'game_name', 'gender_name', 'category_name'];
    foreach ($requiredFields as $field) {
        if (empty($_POST[$field])) {
            $errors[] = ucfirst(str_replace('_', ' ', $field)) . " is required.";
        }
    }


    // Check if the mobile number already exists
    if (empty($errors)) {
        $iCheckPassword = $db->getVal("SELECT id FROM registration WHERE mobileno = '" . $_POST['mobileno'] . "'");
        if ($iCheckPassword != "") {
            $errors[] = "Mobile Number is already registered. Please use a different number.";
        }
    }

    // Process file upload if no errors so far
    if (empty($errors) && isset($_FILES["thumbnail"]["name"]) && !empty($_FILES["thumbnail"]["name"])) {
        $filename = basename($_FILES['thumbnail']['name']);
        $ext1 = strtolower(substr($filename, strrpos($filename, '.') + 1));
        if (in_array($ext1, ['jpg', 'jpeg', 'png', 'gif', 'svg', 'webp'])) {
            $newfile = date("d-m-Y-h-i-s") . rand(1000, 9999) . "." . $ext1;
            move_uploaded_file($_FILES['thumbnail']['tmp_name'], "uploads/" . $newfile);
        }
    }
     if (empty($errors) && isset($_FILES["thumbnail1"]["name"]) && !empty($_FILES["thumbnail1"]["name"])) {
        $filename = basename($_FILES['thumbnail1']['name']);
        $ext1 = strtolower(substr($filename, strrpos($filename, '.') + 1));
        if (in_array($ext1, ['jpg', 'jpeg', 'png', 'gif', 'svg', 'webp'])) {
            $newfile1 = date("d-m-Y-h-i-s") . rand(1000, 9999) . "." . $ext1;
            move_uploaded_file($_FILES['thumbnail1']['tmp_name'], "uploads/" . $newfile1);
        }
    }

    if (empty($errors)) {
        // Insert data into the database
        $aryData = [
            'token' => token('registration'),
            'distict_name' => $_POST['distict_name'],
            'block_name' => $_POST['block_name'],
            'game_name' => $_POST['game_name'],
            'name_hi' => $_POST['name_hi'],
            'name_en' => $_POST['name_en'],
            'parents' => $_POST['parents'],
            'gender_name' => $_POST['gender_name'],
            'dob' => $_POST['dob'],
            'address' => $_POST['address'],
            'addhar' => $_POST['addhar'],
            'blood_group' => $_POST['blood_group'],
            'email' => $_POST['email'],
            'mobileno' => $_POST['mobileno'],
            'whatsno' => $_POST['whatsno'],
            'category_name' => $_POST['category_name'],
            'thumbnail' => $newfile ?? '',
            'thumbnail1' => $newfile1 ?? '',

        ];

        $db->insertAry("registration", $aryData);
        $_SESSION['success'] = "Submitted Successfully";
        //echo $flgIn = $db->getLastQuery();exit;
        echo "<script>alert('Form submitted successfully!');</script>";
        redirect($FileName);
        unset($_POST);
    } else {
        echo "<script>alert('" . implode("\\n", $errors) . "');</script>";
    }
}
?>


<head>
    <?php include('inc_meta.php') ?>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

    <style>
        .selects {
            padding: 15px;
            width: 90%;
            background-color: #e9ecef;
            border: 1px solid;
            border-radius: 10px;
        }
         .selects1 {
            margin-top:25px
        }
    </style>
</head>

<body>
    <!--- Bannder Image Start--->
    <div>
        <img src="game.png" style="width: 100%;">
    </div>
    <!--- Bannder Image Ends--->
    <!--- Heading start--->
    <section>
        <div class="container-fluid">
            <div class="container">
                <h3 style="font-size:28px;padding-left:25px;text-align:center;">आवेदन-पत्र</h3>
            </div>
        </div>
    </section>
    <!--- Heading Ends--->
    <!--- Form Start--->
    <div class="container-fluid">
        <div class="conatiner" style="border: 1px solid; ;margin:0px 20px;padding-bottom:45px;margin-bottom:20px;padding:15px">
            <form action="" method="post" enctype="multipart/form-data">
                <div class="row selects1">
                    <div class="col-lg-4 col-md-4 col-sm-6">
                        <label>ज़िला</label><br>
                        <select name="distict_name" id="distict_name" class="selects" onchange="fetchBlocks(this.value)">
    <option value="0">Select District</option>
    <?php $aryList = $db->getRows("SELECT * FROM district ORDER BY id ASC");
    foreach ($aryList as $iList) { ?>
        <option value="<?php echo $iList['distict_name']; ?>"><?php echo $iList['distict_name']; ?></option>
    <?php } ?>
</select>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-6">
                        <label>विकासखंड</label><br>
                                <select name="block_name" id="block_name" class="selects" onchange="fetchGames(this.value)">
    <option value="0">Select Block</option>
</select>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-6">
                        <label>खेल का नाम</label><br>
                        <select name="game_name" id="game_name" class="selects" >
                            <option value="0">Select Game</option>
                        </select>
                    </div>
                </div>
                <div class="row selects1">
                    <div class="col-lg-4 col-md-4 col-sm-6">
                        <div style=";background-color:#fff;border-radius:10px">
                                <label>नाम (हिन्दी)</label><br>
                                <input type="text" name="name_hi" id="name_hi" class="selects" placeholder="Enter Name" required>
                            </div>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-6">
                        <div style=";background-color:#fff;border-radius:10px">
                                <label>नाम (अंग्रेजी)</label><br>
                                <input type="text" name="name_en" id="name_en" class="selects" placeholder="Enter Name" required>
                            </div>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-6">
                       <div style=";background-color:#fff;border-radius:10px">
                                <label>पिता/माता/पति का नाम</label><br>
                                <input type="text" name="parents" id="parents" class="selects" placeholder="Enter Name" required>
                            </div>
                    </div>
                </div>
                <div class="row selects1">
                    <div class="col-lg-4 col-md-4 col-sm-6">
                        <label>लिंग</label><br>
                                <select name="gender_name" id="gender_name" class="selects">
                                    <option value="0">Select Gender</option>
                                    <?php $aryList = $db->getRows("select * from gender  order by id asc");
                                    foreach ($aryList as $iList) { ?>
                                        <option value="<?php echo $iList['gender_name']; ?>" name="gender_name"><?php echo $iList['gender_name']; ?></option>
                                    <?php } ?>
                                </select>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-6">
                        <div style=";background-color:#fff;border-radius:10px">
                                <label>जन्म दिनांक</label><br>
                                <input type="date" name="dob" id="dob" class="selects" placeholder="Enter Date of Birth" required>
                            </div>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-6">
                       <div style=";background-color:#fff;border-radius:10px">
                                <label>पूरा पता</label><br>
                                <textarea name="address" id="address" class="selects" placeholder="Enter Address" required></textarea>
                            </div>
                    </div>
                </div>
                <div class="row selects1">
                    <div class="col-lg-4 col-md-4 col-sm-6">
                        <div style="background-color:#fff;;border-radius:10px">
                                <label>आधार नं.</label><br>
                                <input type="text" name="addhar" id="addhar" class="selects" placeholder="Enter Aadhar Number" required>
                            </div>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-6">
                         <div style="background-color:#fff;;border-radius:10px">
                                <label>ब्लड ग्रुप</label><br>
                                <input type="text" name="blood_group" id="blood_group" class="selects" placeholder="Enter Blood Group" required>
                            </div>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-6">
                       <div style="background-color:#fff;;border-radius:10px">
                                <label>ई-मेल</label><br>
                                <input type="email" name="email" id="email" class="selects" placeholder="Enter Email" required>
                            </div>
                    </div>
                </div>
                <div class="row selects1">
                    <div class="col-lg-4 col-md-4 col-sm-6">
                        <div style="background-color:#fff;;border-radius:10px">
                                <label>मोबाइल नं.</label><br>
                                <input type="text" name="mobileno" id="mobileno" class="selects" placeholder="Enter Mobile Number" required>
                            </div>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-6">
                    <div style="background-color:#fff;;border-radius:10px">
                                <label>व्हाट्सएप नं.</label><br>
                                <input type="text" name="whatsno" id="whatsno" class="selects" placeholder="Enter What's App Number" required>
                            </div>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-6">
                         <div style="background-color:#fff;;border-radius:10px">
                               <label>वर्ग</label><br>
                                <select name="category_name" id="category_name" class="selects" >
                                    <option value="0">Select Category</option>
                                    <?php $aryList = $db->getRows("select * from category  order by id asc");
                                    foreach ($aryList as $iList) { ?>
                                        <option value="<?php echo $iList['category_name']; ?>" name="category_name"><?php echo $iList['category_name']; ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                    </div>

                </div>
                <div class="row selects1">
                    <div class="col-lg-4 col-md-4 col-sm-6">
                       <div style="background-color:#fff;;border-radius:10px">
                                <label>फोटो अपलोड करें</label><br>
                                <input class="selects" type="file" name="thumbnail" accept="image/*" id="thumbnail"required >
                            </div>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-6">
                       <div style="background-color:#fff;;border-radius:10px">
                                <label>खिलाड़ी/सदस्य के हस्ताक्षर</label><br>
                                <input class="selects" type="file" name="thumbnail1" accept="image/*" id="thumbnail1" required>
                            </div>
                    </div>
                </div>
            </div>
                <div class="col-lg-12 col-md-12 col-sm-12" style="display: flex; justify-content:center">
                    <button type="submit" class="thm-btn become-registration-page__btn" name="addnewrecord" style="background-color:#e33100;color:antiquewhite;width: 100px; padding: 10px; border-radius: 15px;">Submit</button>
                </div>
            </form>
        </div>
    </div>
    <!--- Form Ends--->
<script>
function fetchBlocks(distictName) {
    if (distictName === "0") {
        document.getElementById('block_name').innerHTML = '<option value="0">Select Block</option>';
        return;
    }

    const xhr = new XMLHttpRequest();
    xhr.open('POST', 'get_blocks.php', true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            document.getElementById('block_name').innerHTML = xhr.responseText;
        }
    };
    xhr.send('distict_name=' + encodeURIComponent(distictName));
}
function fetchGames(blockName) {
    if (blockName === "0") {
        document.getElementById('game_name').innerHTML = '<option value="0">Select Game</option>';
        return;
    }

    const xhr = new XMLHttpRequest();
    xhr.open('POST', 'get_games.php', true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            document.getElementById('game_name').innerHTML = xhr.responseText;
        }
    };
    xhr.send('block_name=' + encodeURIComponent(blockName));
}
</script>
</body>

</html>





