<?php
include('header.php');

// Sabse pehle error reporting off rakhein taaki white screen na aaye
error_reporting(0);
ini_set('display_errors', 0);

function uploadFile($file)
{
    if (!empty($file['name'])) {
        if (!is_dir('uploads')) {
            mkdir('uploads', 0777, true);
        }
        $filename = rand(10000,99999) . time() . "_" . basename($file['name']);
        $target = "uploads/" . $filename;
        if (move_uploaded_file($file['tmp_name'], $target)) {
            return $target;
        }
    }
    return '';
}

$username = $_SESSION['phone'] ?? '';
$userid   = $_SESSION['userid'] ?? '';

/* WALLET FETCH */
$q = mysqli_query($conn,"SELECT walletamount, pfee FROM usertable WHERE phone='$username'");
$user = mysqli_fetch_assoc($q);
$wallet = (float)($user['walletamount'] ?? 0);
$fee    = (float)($user['pfee'] ?? 0);

if(isset($_POST['sub']) && $_POST['sub']=="1"){

    if($wallet < $fee){
        echo "<script>alert('Wallet Balance Insufficient'); window.location='wallet.php';</script>";
        exit;
    }

    $name  = mysqli_real_escape_string($conn, $_POST['name'] ?? '');
    $fname = mysqli_real_escape_string($conn, $_POST['fname'] ?? '');

    // Date Formats (Safe for SQL)
    $dobInput = $_POST['dob'] ?? '';
    $dob = !empty($dobInput) ? date('d-m-Y', strtotime($dobInput)) : '';
    $oldDobInput = $_POST['email'] ?? '';
    $email = !empty($oldDobInput) ? date('d-m-Y', strtotime($oldDobInput)) : '';

    $aadhaar_no = mysqli_real_escape_string($conn, $_POST['aadhaar_no'] ?? '');
    $mobile_no  = mysqli_real_escape_string($conn, $_POST['mobile_no'] ?? '');
    $father_aadhaar_no = mysqli_real_escape_string($conn, $_POST['father_aadhaar_no'] ?? '');
    $child_name_hindi  = mysqli_real_escape_string($conn, $_POST['child_name_hindi'] ?? '');
    $gender            = mysqli_real_escape_string($conn, $_POST['gender'] ?? '');
    $village           = mysqli_real_escape_string($conn, $_POST['village'] ?? '');
    $block             = mysqli_real_escape_string($conn, $_POST['block'] ?? '');
    $tehsil            = mysqli_real_escape_string($conn, $_POST['tehsil'] ?? '');
    $district          = mysqli_real_escape_string($conn, $_POST['district'] ?? '');
    $pincode           = mysqli_real_escape_string($conn, $_POST['pincode'] ?? '');
    $state             = mysqli_real_escape_string($conn, $_POST['state'] ?? '');
    $purpose           = mysqli_real_escape_string($conn, $_POST['purpose'] ?? '');

    $photo1 = $_POST['photo1'] ?? '';
    $photo2 = $_POST['photo2'] ?? '';
    $photo3 = $_POST['photo3'] ?? '';
    $photo4 = $_POST['photo4'] ?? '';
    $photo5 = $_POST['photo5'] ?? '';

    $link  = uploadFile($_FILES['poi'] ?? []);
    $link2 = uploadFile($_FILES['poa'] ?? []);
    $link3 = uploadFile($_FILES['pob'] ?? []);

    date_default_timezone_set("Asia/Kolkata");
    $dateof_issue = date('d-m-Y H:i:s');

    /* 
       🔥 FULL INSERT QUERY 
       Maine isme photo, poh, slip, reson, reson2 saare columns add kar diye hain
       Taki database koi error na de.
    */
    $sql = "INSERT INTO dob (
            parent, appliedby, name, fname, dob, aadhaar_no, mobile_no, email,
            father_aadhaar_no, child_name_hindi, gender, village, block, tehsil,
            district, pincode, state, purpose,
            finger1, finger2, finger3, finger4, finger5,
            status, photo, poh, poi, poa, pob, slip, date, reson, reson2
        ) VALUES (
            '$userid', 
            '$username',
            '$name',
            '$fname',
            '$dob',
            '$aadhaar_no',
            '$mobile_no',
            '$email',
            '$father_aadhaar_no',
            '$child_name_hindi',
            '$gender',
            '$village',
            '$block',
            '$tehsil',
            '$district',
            '$pincode',
            '$state',
            '$purpose',
            '$photo1', '$photo2', '$photo3', '$photo4', '$photo5',
            'pending', '', '', '$link', '$link2', '$link3', '', '$dateof_issue', '', ''
        )";

    $res = mysqli_query($conn, $sql);

    if($res){
        // Wallet Deduct
        mysqli_query($conn,"UPDATE usertable SET walletamount = walletamount - $fee WHERE phone='$username'");
        // History
        mysqli_query($conn,"INSERT INTO wallethistory (userid, amount, balance, purpose, status, type) VALUES ('$username','$fee','$wallet','$purpose','Success','Debit')");

        echo "<script>
        alert('Application Submitted Successfully');
        window.location='doblist.php';
        </script>";
    } else {
        // Agar fir bhi error aaye toh error message dikhayega
        echo "DB Error: " . mysqli_error($conn);
    }
}
?>


<style>
    @media print {

        html,
        body {
            display: none;
        }
    }
</style>

   
    <style>
    @media print {

        html,
        body {
            display: none;
        }
    }
    </style>
</head>

<body>

             
                
            
    <!-- ============================================================== -->
    <!-- 						Content Start	 						-->
    <!-- ============================================================== -->
    <div class="container-fluid">
                <div class="row page-header">
    
        <div class="col-lg-6 align-self-center ">
           
        </div>
        
    </div>
    <!--<section class="main-content">-->
                             <marquee width="auto"   direction="left" height="auto" transition="0.5s" >
     
</marquee>
      <style>
body{
    background:#f4f6fb;
}

.card{
    border:none;
    border-radius:16px;
    box-shadow:0 10px 30px rgba(0,0,0,0.08);
    overflow:hidden;
}

.card-header{
    background:linear-gradient(135deg,#ff9933,#ff5722);
    color:#fff;
    font-weight:600;
    text-align:center;
    padding:18px;
    font-size:18px;
}

.card-body{
    padding:25px;
    background:#fff;
}

label{
    font-size:13px;
    font-weight:600;
    margin-bottom:6px;
    color:#333;
}

.form-control{
    border-radius:10px;
    padding:10px 12px;
    border:1px solid #e3e6f0;
    background:#fafbff;
    transition:0.2s;
}

.form-control:focus{
    border-color:#ff9933;
    box-shadow:0 0 0 0.2rem rgba(255,153,51,0.15);
    background:#fff;
}

.row > div{
    margin-bottom:15px;
}
</style>

<div class="container-fluid mt-3">

<div class="card">

    <div class="card-header">
        Add Customer - DOB Correction Form
    </div>

    <div class="card-body">

        <form id="regform" method="post" enctype="multipart/form-data"
              class="form-horizontal" autocomplete="off"
              onsubmit="myButton.disabled = true; return true;">

        <input type="hidden" name="sub" value="1">

        <div class="row">

            <div class="col-md-4">
                <label>NAME</label>
                <input type="text" id="fullname" name="name" class="form-control" required>
            </div>
            
             <div class="col-md-4">
                <label>Gender</label>
                <input type="text" id="fname" name="fname" class="form-control" required>
            </div>

            <div class="col-md-4">
                <label>OLD DOB</label>
                <input type="date" id="email" name="email" class="form-control">
            </div>

            <div class="col-md-4">
                <label>NEW DOB</label>
                <input type="date" id="dob" name="dob" class="form-control">
            </div>

            <div class="col-md-4">
                <label>Aadhaar No</label>
                <input type="text" id="gender" name="gender" class="form-control" required>
            </div>

            <div class="col-md-4">
                <label>Mobile Number</label>
                <input type="number" id="mobileno" name="mobile_no" class="form-control" required>
            </div>

            <div class="col-md-4">
                <label>FATHER NAME</label>
                <input type="text" id="child_name_hindi" name="child_name_hindi" class="form-control">
            </div>

            <div class="col-md-4">
                <label>Village/ग्राम</label>
                <input type="text" id="village" name="village" class="form-control" required>
            </div>

            <div class="col-md-4">
                <label>Post/पोस्ट</label>
                <input type="text" id="block" name="block" class="form-control" required>
            </div>

            <div class="col-md-4">
                <label>Landmark</label>
                <input type="text" id="tehsil" name="tehsil" class="form-control" required>
            </div>

            <div class="col-md-4">
                <label>District</label>
                <input type="text" id="district" name="district" class="form-control" required>
            </div>

            <div class="col-md-4">
                <label>State</label>
                <input type="text" id="state" name="state" class="form-control" required>
            </div>


            <div class="col-md-4">
                <label>Pincode</label>
                <input type="text" id="pincode" name="pincode" class="form-control" required>
            </div>

            <div class="col-md-4">
                <label>DOCUMENTS</label>
                <input type="file" id="poa" name="poa" class="form-control">
            </div>

           <div class="col-md-4">
    <label>Purpose</label>
    <input type="text" id="purpose" name="purpose" class="form-control"
           value="DOB CORRECTION" readonly>
</div>
</div>
</div>
              <div class="container">
    <div class="form-group">
        <div class="row">
                                    <div class="col-md-1"></div>
                                    <div class="col-md-2">
                                        <div class="card">
                                            <div class="card-body">
                                                <img src="https://media.tenor.com/9CkHkarE87MAAAAM/fingerprint-scanner.gif" id="im1"
                                                    class="img-fluid rounded-end border p-1">
                                                <input class="form-control" type="hidden" id="pic1" name="photo1" style="width: 205px;" id="cim1"required>
                                                <div style="width:100px;height:auto" class="text-center mt-2">
                                                    <img src="assets/img/check.png" style="display:none; width:205px;"
                                                        id="cim1" alt="">
                                                </div>
                                                <button type="button"
                                                    class="btn btn-outline-info px-4 mt-1 capture btn-sm w-100"
                                                    data-id="1">Capture</button>
                                                <h5 id="q1" class="text-center mt-3"></h5>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-2">
                                        <div class="card">
                                            <div class="card-body">
                                                <img src="https://media.tenor.com/9CkHkarE87MAAAAM/fingerprint-scanner.gif" id="im2"
                                                    class="img-fluid rounded-end border p-1">
                                                <input class="form-control" type="hidden" id="pic2" name="photo2" required> 
                                                <div style="width:100px; height : auto" class="text-center mt-2">
                                                    <img src="assets/img/check.png" style="display:none; width:205px;"
                                                        id="cim2" alt="">
                                                </div>
                                                <button type="button"
                                                    class="btn btn-outline-info px-4 mt-1 capture btn-sm w-100"
                                                    data-id="2">Capture</button>
                                                <h5 id="q2" class="text-center mt-3"></h5>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-2">
                                        <div class="card">
                                            <div class="card-body">
                                                <img src="https://media.tenor.com/9CkHkarE87MAAAAM/fingerprint-scanner.gif" id="im3"
                                                    class="img-fluid rounded-end border p-1">
                                                <input class="form-control" type="hidden" id="pic3" name="photo3" required>
                                                <div style="width:100px; height : auto" class="text-center mt-2">
                                                    <img src="assets/img/check.png" style="display:none; width:205px;"
                                                        id="cim3" alt=""required>
                                                </div>
                                                <button type="button"
                                                    class="btn btn-outline-info px-4 mt-1 capture btn-sm w-100"
                                                    data-id="3">Capture</button>
                                                <h5 id="q3" class="text-center mt-3"></h5>
                                            </div>
                                        </div>
                                    </div>
                                      <div class="col-md-2">
                                        <div class="card">
                                            <div class="card-body">
                                                <img src="https://media.tenor.com/9CkHkarE87MAAAAM/fingerprint-scanner.gif" id="im4"
                                                    class="img-fluid rounded-end border p-1">
                                                <input class="form-control" type="hidden" id="pic4" name="photo4" required>
                                                <div style="width:100px; height : auto" class="text-center mt-2">
                                                    <img src="assets/img/check.png" style="display:none; width:205px;"
                                                        id="cim4" alt=""required>
                                                </div>
                                                <button type="button"
                                                    class="btn btn-outline-info px-4 mt-1 capture btn-sm w-100"
                                                    data-id="4">Capture</button>
                                                <h5 id="q4" class="text-center mt-3"></h5>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-2">
                                        <div class="card">
                                            <div class="card-body">
                                                <img src="https://media.tenor.com/9CkHkarE87MAAAAM/fingerprint-scanner.gif" id="im5"
                                                    class="img-fluid rounded-end border p-1">
                                                <input class="form-control" type="hidden" id="pic5" name="photo5" required>
                                                <div style="width:100px; height : auto" class="text-center mt-2">
                                                    <img src="assets/img/check.png" style="display:none; width:205px;"
                                                        id="cim5" alt=""required>
                                                </div>
                                                <button type="button"
                                                    class="btn btn-outline-info px-4 mt-1 capture btn-sm w-100"
                                                    data-id="5">Capture</button>
                                                <h5 id="q5" class="text-center mt-3"></h5>
                                            </div>
                                        </div>
                                   </div>
                            
                                    
                                        </div>
                                    </div>
                                    <div class="col-md-1"></div>
                                </div>
                            <br>

<div class="form-group text-center">
    <button type="submit" name="myButton" class="btn-submit-premium">
        <i class="fa fa-floppy-o"></i>
        Submit Application
    </button>
</div>
<style>
.btn-submit-premium{
    background: linear-gradient(135deg, #1f3c88, #2d6cdf);
    color: #fff;
    border: none;
    padding: 14px 45px;
    font-size: 16px;
    font-weight: 600;
    border-radius: 12px;
    cursor: pointer;
    box-shadow: 0 8px 20px rgba(45,108,223,0.25);
    transition: all 0.3s ease;
    display: inline-flex;
    align-items: center;
    gap: 10px;
}

.btn-submit-premium i{
    font-size: 16px;
}

.btn-submit-premium:hover{
    transform: translateY(-2px);
    box-shadow: 0 12px 25px rgba(45,108,223,0.35);
}

.btn-submit-premium:active{
    transform: scale(0.98);
}
</style>

                        </form>
                    </div>
                </div>
            </div>
        </div>
       

                                                
                
            <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
                    <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
                    <a class="btn btn-primary" href="../logout.php">Logout</a>
                </div>
            </div>
        </div>
    </div>


    </div>
    <!-- End of Page Wrapper -->

    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
        <i class="fas fa-angle-up"></i>
    </a>

    <!-- Logout Modal-->
    <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
                    <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
                    <a class="btn btn-primary" href="../logout.php">Logout</a>
                </div>
            </div>
        </div>
    </div>

    <!-- Bootstrap core JavaScript-->
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="js/sb-admin-2.min.js"></script>

    <!-- 						Content End		 						-->
    <!-- ============================================================== -->
    <!-- Common Plugins -->

    <script src="assets/lib/jquery/dist/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
    <script src="model/js/mfs100-9.0.2.6.js"></script>
    <script type="text/javascript" src="model/js/capture.js"></script>
    <script src="assets/lib/formatter/jquery.formatter.min.js"></script>
    <script>
    $(function() {
        /* BirthDate */
        $('input[name="birthdate"]').daterangepicker({
            singleDatePicker: true,
            showDropdowns: true,
            minYear: 1901,
            maxYear: parseInt(moment().format('YYYY'), 10),
            locale: {
                format: "DD/MM/YYYY",
            }
        });
    });
    $('#mobileno').formatter({
        'pattern': '+91 {{99}}-{{99}}-{{999999}}',
        'persistent': true
    });
    $('#aadhaar-no').formatter({
        'pattern': '{{9999}} {{9999}} {{9999}}',
        'persistent': true
    });
    document.addEventListener('contextmenu', event => event.preventDefault());
    document.onkeydown = function(e) {
        if (e.keyCode == 123) {
            return false;
        }
        if (e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0)) {
            return false;
        }
        if (e.ctrlKey && e.shiftKey && e.keyCode == 'J'.charCodeAt(0)) {
            return false;
        }
        if (e.ctrlKey && e.keyCode == 'U'.charCodeAt(0)) {
            return false;
        }
        if (e.ctrlKey && e.shiftKey && e.keyCode == 'C'.charCodeAt(0)) {
            return false;
        }
    }
    document.addEventListener("keyup", function(e) {
        var keyCode = e.keyCode ? e.keyCode : e.which;
        if (keyCode == 44) {
            stopPrntScr();
        }
    });

    function stopPrntScr() {

        var inpFld = document.createElement("input");
        inpFld.setAttribute("value", ".");
        inpFld.setAttribute("width", "0");
        inpFld.style.height = "0px";
        inpFld.style.width = "0px";
        inpFld.style.border = "0px";
        document.body.appendChild(inpFld);
        inpFld.select();
        document.execCommand("copy");
        inpFld.remove(inpFld);
    }

    function AccessClipboardData() {
        try {
            window.clipboardData.setData('text', "Access   Restricted");
        } catch (err) {}
    }
    setInterval("AccessClipboardData()", 300);
    </script>
    <script src="jquery-1.8.2.js"></script>
<script src="mfs100-9.0.2.6.js"></script>

    <script src="assets/lib/bootstrap/js/popper.min.js"></script>
    <script src="assets/lib/bootstrap/js/bootstrap.min.js"></script>
    <script src="assets/lib/pace/pace.min.js"></script>
    <script src="assets/lib/jasny-bootstrap/js/jasny-bootstrap.min.js"></script>
    <script src="assets/lib/slimscroll/jquery.slimscroll.min.js"></script>
    <script src="assets/lib/nano-scroll/jquery.nanoscroller.min.js"></script>
    <script src="assets/lib/metisMenu/metisMenu.min.js"></script>
    <script src="assets/js/custom.js"></script>
    <!--Chart Script-->
    <script src="assets/lib/chartjs/chart.min.js"></script>
    <script src="assets/lib/chartjs/chartjs-sass.js"></script>
    <!--Vetor Map Script-->
    <script src="assets/lib/vectormap/jquery-jvectormap-2.0.2.min.js"></script>
    <script src="assets/lib/vectormap/jquery-jvectormap-us-aea-en.js"></script>
    <!-- Chart C3 -->
    <script src="assets/lib/chart-c3/d3.min.js"></script>
    <script src="assets/lib/chart-c3/c3.min.js"></script>
    <!-- Datatables-->
    <script src="assets/lib/datatables/jquery.dataTables.min.js"></script>
    <script src="assets/lib/datatables/dataTables.responsive.min.js"></script>
    <script src="assets/lib/toast/jquery.toast.min.js"></script>
    <script src="assets/js/dashboard.js"></script>
    <!--Sweet Alerts-->
    <script src="assets/lib/sweet-alerts2/sweetalert2.min.js"></script>

</body>

</html>