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

    // Validate required fields
    $requiredFields = [
        'name', 'parents', 'address', 'state', 'block_name', 'distict_name', 'division',
        'dob', 'birth_place', 'height', 'weight', 'addhar', 'gender_name', 'mobileno'
    ];
    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 application WHERE mobileno = '" . $_POST['mobileno'] . "'");
        if ($iCheckPassword != "") {
            $errors[] = "Mobile Number is already registered. Please use a different number.";
        }
    }

    // Process file upload
    $newfile_age_proof = '';
    if (empty($errors) && isset($_FILES["age_proof"]["name"]) && !empty($_FILES["age_proof"]["name"])) {
        $filename = basename($_FILES['age_proof']['name']);
        $ext1 = strtolower(substr($filename, strrpos($filename, '.') + 1));
        if (in_array($ext1, ['jpg', 'jpeg', 'png', 'gif', 'svg', 'webp', 'pdf'])) {
            $newfile_age_proof = date("d-m-Y-h-i-s") . rand(1000, 9999) . "." . $ext1;
            move_uploaded_file($_FILES['age_proof']['tmp_name'], "uploads/" . $newfile_age_proof);
        } else {
            $errors[] = "Invalid file format for Age Proof. Allowed formats: jpg, jpeg, png, gif, svg, webp, pdf.";
        }
    } else {
        if (empty($errors)) {
            $errors[] = "Age Proof file is required.";
        }
    }

    if (empty($errors)) {
        // Insert data into the database
        $aryData = [
            'token' => token('application'),
            'name_hi' => $_POST['name'],
            'parents' => $_POST['parents'],
            'address' => $_POST['address'],
            'state' => $_POST['state'],
            'block_name' => $_POST['block_name'],
            'distict_name' => $_POST['distict_name'],
            'division' => $_POST['division'],
            'dob' => $_POST['dob'],
            'birth_place' => $_POST['birth_place'],
            'height' => $_POST['height'],
            'weight' => $_POST['weight'],
            'age_proof' => $newfile_age_proof,
            'addhar' => $_POST['addhar'],
            'gender_name' => $_POST['gender_name'],
            'prior_sports' => $_POST['prior_sports'] ?? '',
            'school_name' => $_POST['school_name'] ?? '',
            'school_board' => $_POST['school_board'] ?? '',
            'school_address' => $_POST['school_address'] ?? '',
            'parents_occupation' => $_POST['parents_occupation'] ?? '',
            'mother_height' => $_POST['mother_height'] ?? '',
            'father_height' => $_POST['father_height'] ?? '',
            'exp_shooting' => $_POST['exp_shooting'] ?? 'नही',
            'exp_swimming' => $_POST['exp_swimming'] ?? 'नही',
            'exp_horse_riding' => $_POST['exp_horse_riding'] ?? 'नही',
            'trainer_name' => $_POST['trainer_name'] ?? '',
            'trainer_mobile' => $_POST['trainer_mobile'] ?? '',
            'email' => $_POST['email'] ?? '',
            'mobileno' => $_POST['mobileno']
        ];

        $db->insertAry("application", $aryData);
        $_SESSION['success'] = "Submitted Successfully";
        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="game1.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">
                        <div style="background-color:#fff;border-radius:10px">
                            <label>आवेदक का नाम </label><br>
                            <input type="text" name="name" id="name" 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 Parent 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>
                            <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="state" id="state" class="selects" placeholder="Enter State" required>
                        </div>
                    </div>
                    <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);" required>
                            <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['id']; ?>"><?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">
                            <option value="0">Select Block</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="division" id="division" class="selects" placeholder="Enter Division" 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="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>
                            <input type="text" name="birth_place" id="birth_place" class="selects" placeholder="Enter Birth Place" 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="height" id="height" class="selects" placeholder="Enter Height" 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="weight" id="weight" class="selects" placeholder="Enter Weight" 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="addhar" id="addhar" class="selects" placeholder="Enter Aadhar Number" 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="file" name="age_proof" id="age_proof" class="selects" accept=".jpg,.jpeg,.png,.pdf" 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="gender_name" id="gender_name" class="selects" required>
                                <option value="">Select Gender</option>
                                <option value="Male">Male</option>
                                <option value="Female">Female</option>
                            </select>
                        </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="prior_sports" id="prior_sports" class="selects" placeholder="Prior Sports Details">
                        </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="school_name" id="school_name" class="selects" placeholder="Enter School Name">
                        </div>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-6">
                        <div style="background-color:#fff;border-radius:10px">
                            <label>स्कूल का बोर्ड (M.P./C.B.S.E./I.C.S.E./Other)</label><br>
                            <select name="school_board" id="school_board" class="selects">
                                <option value="">Select Board</option>
                                <option value="M.P. Board">M.P. Board</option>
                                <option value="C.B.S.E. Board">C.B.S.E. Board</option>
                                <option value="I.C.S.E. Board">I.C.S.E. Board</option>
                                <option value="Other">Other</option>
                            </select>
                        </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="school_address" id="school_address" class="selects" placeholder="Enter School Address"></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="parents_occupation" id="parents_occupation" class="selects" placeholder="Enter Occupation">
                        </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="mother_height" id="mother_height" class="selects" placeholder="Enter Mother's Height">
                        </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="father_height" id="father_height" class="selects" placeholder="Enter Father's Height">
                        </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" pattern="[0-9]{10}" 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 Address">
                        </div>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-6">
                         <div style="background-color:#fff;border-radius:10px">
                            <label>निशानेबाजी (Shooting) में अनुभव</label><br>
                            <div style="padding: 15px;">
                                <label><input type="radio" name="exp_shooting" value="हाँ"> हाँ</label>
                                <label style="margin-left: 20px;"><input type="radio" name="exp_shooting" value="नही" checked> नही</label>
                            </div>
                        </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>तैराकी (Swimming) में अनुभव</label><br>
                            <div style="padding: 15px;">
                                <label><input type="radio" name="exp_swimming" value="हाँ"> हाँ</label>
                                <label style="margin-left: 20px;"><input type="radio" name="exp_swimming" value="नही" checked> नही</label>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-6">
                         <div style="background-color:#fff;border-radius:10px">
                            <label>घुड़सवारी (Horse Riding) में अनुभव</label><br>
                            <div style="padding: 15px;">
                                <label><input type="radio" name="exp_horse_riding" value="हाँ"> हाँ</label>
                                <label style="margin-left: 20px;"><input type="radio" name="exp_horse_riding" value="नही" checked> नही</label>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-6"></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="trainer_name" id="trainer_name" class="selects" placeholder="Enter Trainer/Coach Name">
                        </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="trainer_mobile" id="trainer_mobile" class="selects" placeholder="Enter Trainer/Coach Mobile">
                        </div>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-6"></div>
                </div>

                <div class="row selects1">
                    <div class="col-lg-12 col-md-12 col-sm-12" style="display: flex; justify-content:center; margin-top: 20px;">
                        <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; border: none;">Submit</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <!--- Form Ends--->
<script>
function fetchBlocks(distictId) {
    if (distictId == 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_id=' + distictId);
}
</script>
</body>

</html>
