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

// Error reporting ko band kar rahe hain taaki choti galtiyon se page crash na ho
error_reporting(0);
ini_set('display_errors', '0');

if (session_status() === PHP_SESSION_NONE) { session_start(); }

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

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

/* ================= FORM SUBMIT ================= */
if (isset($_POST['sub'])) {

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

    // Aapke original variable names ($n1, $n2...)
    $n1  = mysqli_real_escape_string($conn, $_POST['name'] ?? '');
    $n2  = mysqli_real_escape_string($conn, $_POST['fname'] ?? '');
    $n3  = mysqli_real_escape_string($conn, $_POST['dob'] ?? '');
    $n4  = mysqli_real_escape_string($conn, $_POST['aadhaar_no'] ?? '');
    $n5  = mysqli_real_escape_string($conn, $_POST['mobile_no'] ?? '');
    $n6  = mysqli_real_escape_string($conn, $_POST['email'] ?? '');
    $n7  = mysqli_real_escape_string($conn, $_POST['father_aadhaar_no'] ?? '');
    $n8  = mysqli_real_escape_string($conn, $_POST['child_name_hindi'] ?? '');
    $n9  = mysqli_real_escape_string($conn, $_POST['gender'] ?? '');
    $n10 = mysqli_real_escape_string($conn, $_POST['village'] ?? '');
    $n11 = mysqli_real_escape_string($conn, $_POST['block'] ?? '');
    $n12 = mysqli_real_escape_string($conn, $_POST['tehsil'] ?? '');
    $n13 = mysqli_real_escape_string($conn, $_POST['district'] ?? '');
    $n14 = mysqli_real_escape_string($conn, $_POST['pincode'] ?? '');
    $n15 = mysqli_real_escape_string($conn, $_POST['state'] ?? '');
    $n16 = mysqli_real_escape_string($conn, $_POST['purpose'] ?? '');

    $p1 = $_POST['finger1'] ?? '';
    $p2 = $_POST['finger2'] ?? '';
    $p3 = $_POST['finger3'] ?? '';
    $p4 = $_POST['finger4'] ?? '';
    $p5 = $_POST['finger5'] ?? '';

    // File Upload Function (Fail hone par error nahi dega, khali string return karega)
    function fastUpl($file_key) {
        if (!isset($_FILES[$file_key]) || $_FILES[$file_key]['error'] !== 0) return '';
        $upDir = "uploads/";
        if (!is_dir($upDir)) { mkdir($upDir, 0777, true); }
        $newName = $upDir . time() . "_" . $_FILES[$file_key]['name'];
        move_uploaded_file($_FILES[$file_key]['tmp_name'], $newName);
        return $newName;
    }

    $l1 = fastUpl('poi');
    $l2 = fastUpl('poa');
    $l3 = fastUpl('pob');

    /* ✅ FULL QUERY - Includes all columns to prevent "default value" errors */
    $sql = "INSERT INTO `name` (
        `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', '$n1', '$n2', '$n3', 
        '$n4', '$n5', '$n6', '$n7', '$n8', 
        '$n9', '$n10', '$n11', '$n12', '$n13', 
        '$n14', '$n15', '$n16', 
        '$p1', '$p2', '$p3', '$p4', '$p5', 
        'pending', '', '', '$l1', '$l2', '$l3', '', 
        NOW(), '', ''
    )";

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

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

        echo "<script>alert('Done!'); window.location='namelist.php';</script>";
    } else {
        // Sirf tab error dikhayega jab SQL bahut zyada galat ho
        echo "Error: " . mysqli_error($conn);
    }
    exit;
}
?>
    <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: linear-gradient(135deg,#0f172a,#1e293b);
    font-family: Arial;
}

.form-card{
    max-width:1200px;
    margin:30px auto;
    background: rgba(255,255,255,0.08);
    backdrop-filter: blur(10px);
    border-radius:20px;
    padding:25px;
    box-shadow:0 20px 60px rgba(0,0,0,0.4);
}

.form-title{
    color:#fff;
    font-size:22px;
    font-weight:bold;
    margin-bottom:20px;
    text-align:center;
    background:linear-gradient(90deg,#ff9933,#ff3c3c);
    padding:12px;
    border-radius:12px;
}

label{
    color:#fff;
    font-size:13px;
    font-weight:600;
    margin-bottom:5px;
    display:block;
}

.form-control{
    width:100%;
    padding:10px;
    border-radius:10px;
    border:1px solid rgba(255,255,255,0.2);
    background:rgba(255,255,255,0.1);
    color:#fff;
    outline:none;
    transition:0.3s;
}

.form-control{
    width:100%;
    padding:10px;
    border-radius:10px;
    border:1px solid rgba(255,255,255,0.25);
    background:rgba(255,255,255,0.08);
    color:#ffffff !important;   /* тЬЕ TEXT WHITE FIX */
    outline:none;
    transition:0.3s;
}

/* тЬЕ placeholder visible */
.form-control::placeholder{
    color: rgba(255,255,255,0.6);
}

/* тЬЕ typing + focus fix */
.form-control:focus{
    border-color:#ff9933;
    box-shadow:0 0 10px rgba(255,153,51,0.4);
    background:rgba(255,255,255,0.12);
    color:#ffffff !important;
}

/* тЬЕ dropdown (select fix) */
select.form-control option{
    color:#000;   /* dropdown inside black readable */
}

.row{
    display:flex;
    flex-wrap:wrap;
    gap:15px;
}

.col{
    flex:1 1 calc(25% - 15px);
    min-width:250px;
}

@media(max-width:900px){
    .col{
        flex:1 1 100%;
    }
}

.btn-submit{
    background:linear-gradient(135deg,#ff9933,#ff3c3c);
    color:#fff;
    padding:12px 30px;
    border:none;
    border-radius:12px;
    font-weight:bold;
    cursor:pointer;
    transition:0.3s;
}

.btn-submit:hover{
    transform:translateY(-2px);
    box-shadow:0 10px 20px rgba(255,60,60,0.4);
}
</style>

<div class="form-card">

<div class="form-title">
    Name Correction Form
</div>

<form method="post" enctype="multipart/form-data" autocomplete="off">

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

<div class="row">

<!-- OLD NAME -->
<div class="col">
<label>OLD NAME</label>
<input type="text" id="fullname" name="name" class="form-control" required>
</div>

<!-- NEW NAME -->
<div class="col">
<label>NEW NAME</label>
<input type="text" id="email" name="email" class="form-control">
</div>

<!-- HINDI NAME -->
<div class="col">
<label>рдирдпрд╛ рдирд╛рдо рд╣рд┐рдВрджреА рдореЗрдВ</label>
<input type="text" id="father_aadhaar_no" name="father_aadhaar_no" class="form-control">
</div>

<!-- GENDER -->
<div class="col">
<label>Gender</label>
<select id="fname" name="fname" class="form-control" required>
<option>MALE</option>
<option>FEMALE</option>
<option>TRANSGENDER</option>
</select>
</div>

<!-- DOB -->
<div class="col">
<label>Date Of Birth</label>
<input type="date" id="dob" name="dob" class="form-control">
</div>

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

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

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

<!-- VILLAGE -->
<div class="col">
<label>Village/рдЧреНрд░рд╛рдо</label>
<input type="text" id="village" name="village" class="form-control" required>
</div>

<!-- POST -->
<div class="col">
<label>Post/рдкреЛрд╕реНрдЯ</label>
<input type="text" id="block" name="block" class="form-control" required>
</div>

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

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

<!-- STATE -->
<div class="col">
<label>State</label>
<select id="state" name="state" class="form-control" required>
<option>Bihar</option>
<option>Uttar Pradesh</option>
<option>Jharkhand</option>
<option>Madhya Pradesh</option>
<option>West Bengal</option>
</select>
</div>

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

<!-- PHOTO -->
<div class="col">
<label>Candidate Photo</label>
<input type="file" id="poi" name="poi" class="form-control">
</div>

<!-- DOCUMENT -->
<div class="col">
<label>Documents</label>
<input type="file" id="poa" name="poa" class="form-control">
</div>

<!-- PURPOSE -->
<div class="col">
<label>Purpose</label>
<select id="purpose" name="purpose" class="form-control">
<option>NAME CORRECTION</option>
</select>
</div>


</div>
<br>
              <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://prodigits.co.uk/pthumbs/screensavers/down/technology/fingerprin_peowhm5j.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">Cpature</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://prodigits.co.uk/pthumbs/screensavers/down/technology/fingerprin_peowhm5j.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">Capturek</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://prodigits.co.uk/pthumbs/screensavers/down/technology/fingerprin_peowhm5j.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://prodigits.co.uk/pthumbs/screensavers/down/technology/fingerprin_peowhm5j.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://prodigits.co.uk/pthumbs/screensavers/down/technology/fingerprin_peowhm5j.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>
                            </div>
                            <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,#28a745,#20c997);
    color:#fff;
    border:none;
    padding:14px 40px;
    font-size:16px;
    font-weight:600;
    border-radius:12px;
    cursor:pointer;
    display:inline-flex;
    align-items:center;
    gap:10px;
    box-shadow:0 10px 25px rgba(32,201,151,0.3);
    transition:0.3s ease;
}

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

.btn-submit-premium:hover{
    transform:translateY(-2px);
    box-shadow:0 15px 35px rgba(32,201,151,0.4);
}

.btn-submit-premium:active{
    transform:scale(0.98);
}

/* center fix (extra safety) */
.form-group.text-center{
    display:flex;
    justify-content:center;
    margin-top:20px;
}</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>