<?php
include('../includes/session.php');
include('../includes/config.php');
include('../template/ahkweb/eid_to_aadhaar_no.php');
if(isset($_POST['eid']) && !empty($_POST['name']) && !empty($_POST['date']) && !empty($_POST['time']) && !empty($_POST['ret_wp_no']) ){
    $name = mysqli_real_escape_string($ahk_conn,$_POST['name']);
    $eid = mysqli_real_escape_string($ahk_conn,$_POST['eid']);
   
    $ret_wp_no = mysqli_real_escape_string($ahk_conn,$_POST['ret_wp_no']);
    $fee = mysqli_real_escape_string($ahk_conn,$_POST['fee']);
    $application_no = "AHK". rand(00000000,99999999);
    $appliedby = $udata['phone'];
    $price = mysqli_fetch_assoc(mysqli_query($ahk_conn,"SELECT * FROM pricing WHERE service_name='aadhaar_no' "));
    if($udata['balance']>=$price['price']){
        $fee = $price['price'];
        $nbal = $udata['balance']-$price['price'];
        $debit = mysqli_query($ahk_conn,"UPDATE users SET balance=balance-$fee WHERE phone='$appliedby'");
        $updatehistory = mysqli_query($ahk_conn,"INSERT INTO `wallethistory`(`userid`, `amount`, `balance`, `purpose`, `status`, `type`) VALUES ('$appliedby','$fee','$nbal','Aadhaar Number Find','1','Debit')");
        if($debit && $updatehistory ){
        $submit = mysqli_query($ahk_conn,"INSERT INTO `aadhaar_no` (`appliedby`, `name`, `eid`, `status`, `ret_wp_no`,`fee`,`application_no`) VALUES('$appliedby','$name','$eid','pending','$ret_wp_no','$fee','$application_no')");
        if($submit == true){
           ?>
                <script>
                    $(function(){
                        Swal.fire(
                            'Application Submitted Success',
                            '',
                            'success'
                        )
                    })
                    setTimeout(() => {
                        window.location='';
                    }, 1200);
                </script>
                    <?php
         
        }else{
            ?>
            <script>
              $(function(){
                 Swal.fire(
                     'Opps',
                     'Try Again',
                     'error'
                 )
             })
             setTimeout(() => {
                    window.location='';
                }, 1200);
            </script>
             <?php
        }
            }else{
                ?>
                <script>
                  $(function(){
                     Swal.fire(
                         'Opps',
                         'Try Again',
                         'error'
                     )
                 })
                 setTimeout(() => {
                        window.location='';
                    }, 1200);
                </script>
                 <?php
            }
        
    }else{
        ?>
        <script>
          $(function(){
             Swal.fire(
                 'Wallet Balance is Low!',
                 'Please Recharge Now!',
                 'error'
             )
         })
         setTimeout(() => {
                window.location='wallet.php';
            }, 1200);
        </script>
         <?php
       
    }
    


}
?>