
 <script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>
 <?php include("header.php"); 
include("manu.php"); 

if (isset($_POST['b2']) && $_POST['b2'] == "Bittu") {
    
    $dl = mysqli_real_escape_string($connection,$_POST['dl']);
    $dob = mysqli_real_escape_string($connection,$_POST['dob']);
    $background = mysqli_real_escape_string($connection,$_POST['background']);
    $card_type = mysqli_real_escape_string($connection,$_POST['card_type']);
    $application_no = "HKB_".rand(000000,999999);
    if($card_type=="N"){
        $type="1";
    }else if($card_type=="C"){
        $type="2";
    }else{
         $type="1";
    }
    
    if($background=="B"){
        $color="true";
    }else if($background=="W"){
         $color="false";
    }else{
         $color="false";
    }
  
    $username = $udata['phone'];
    $walletamount=$udata['walletamount'];
            
            
    if($walletamount > $fee){
         $data_check=mysqli_query($connection,"SELECT * FROM dlprint WHERE dl_no='$dl'");
       if(mysqli_num_rows($data_check) > 0){
            $data_checked = mysqli_fetch_array($data_check);
             
            $checked_dl_no=$data_checked['dl_no'];
            $checked_dob=$data_checked['dob'];
              $checked_response=$data_checked['response'];
            $checked_front=$data_checked['front'];
              $checked_back=$data_checked['back'];
                $checked_pdf=$data_checked['pdf'];
                $d_application_no = "HKB_D".rand(000000,999999);
                 $fee = "5";
        $debit_fee =  $walletamount - $fee;
         $debit = mysqli_query($connection,"UPDATE `usrtable` SET walletamount='$debit_fee' WHERE phone='$username'"); 
         if($debit){
          $insert = mysqli_query($connection, "INSERT INTO dlprint (application_no,username,dl_no,dob,response,status,message, fee,front,back,pdf) VALUES ('$d_application_no','$username','$checked_dl_no', '$checked_dob', '$checked_response','success','D found', '$fee', '$checked_front','$checked_back','$checked_pdf');");
         
          date_default_timezone_set('Asia/Kolkata');
          $timestamp = date("Y-m-d H:i:s");
          $summary = mysqli_query($connection,"INSERT INTO `card_summary`(`name`, `number`, `status`, `fee`,`type`,`old_balance`,`new_balance`, `date`, `userid`) VALUES ('$d_application_no','$checked_dl_no','DL D Instant Print PDF','$fee','D','$wallet','$debit_fee','$timestamp','".$_SESSION['userid']."')");     
                  if($insert){
              ?>
               <script>
                        $(function(){
                            Swal.fire(
                                'Duplicate Dl : <?php echo $dl;?> is Downloaded',
                                'Application : <?php echo $d_application_no; ?> Message : File Generated',
                                'success'
                            )
                        })
                        setTimeout(() => {
                            window.location='DL_Instant_Hkb_list.php';
                        }, 1200);
                    </script>
              <?php
          }else{
              ?>
             <script>
                        $(function(){
                            Swal.fire(
                                'Error whiloe insert',
                                'Contact',
                                'error'
                            )
                        })
                        setTimeout(() => {
                            window.location='';
                        }, 1200);
                    </script>
              <?php
          }  

}
}else{     

    $fee = "20";
    $walletamount=$udata['walletamount'];
    $debit_fee =  $walletamount - $fee;
    $api_hkb ="1zGBBu1D-zgMz-6AGI-3m94-E0XlAN00yKQx";   //Api key Buy From Https://axenapi.online
      // API endpoint
$url = "https://axenapi.online/Dashboard/Verify_api/Dl/DL_hyper_link.php?api=$api_hkb&dl=$dl&dob=$dob&color=$color&type=$type";

$curl = curl_init();
curl_setopt_array($curl, array( 
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
));

 $response = curl_exec($curl);
 curl_close($curl);

$resdata = json_decode($response, true);
// Check for cURL errors
$front=$resdata['front'];
$back=$resdata['back'];
$a4=$resdata['a4'];
if (curl_errno($ch)) {
     echo "cURL Error: " . curl_error($ch);

} else if($resdata['msg']){
    // Output API response
    ?>
    <script>
            $(function(){
                Swal.fire(
                    '<?php echo $resdata['msg']; ?>',
                    'Sorry',
                    'error'
                )
            });
            window.setTimeout(function(){
                window.location.href='DL_Instant_Hkb.php';
            },20000);
            
        </script>
        <?php
   
}else if($resdata['mgs']){
    ?>
     <script>
            $(function(){
                Swal.fire(
                    'Aurth Token Not Found :<?php echo $resdata['mgs']; ?>',
                    'Admin Token Error Contact : ',
                    'warning'
                )
            });
            window.setTimeout(function(){
                window.location.href='DL_Instant_Hkb.php';
            },20000);
            
        </script>
        <?php
}else if($resdata['front'] ==''){
    
    ?>
     <script>
            $(function(){
                Swal.fire(
                    '<?php echo $response; ?>',
                    'Please Contact Admin : ',
                    'error'
                )
            });
            window.setTimeout(function(){
                window.location.href='DL_Instant_Hkb.php';
            },200000);
            
        </script>
        <?php
}else{
     $debit = mysqli_query($connection,"UPDATE `usertbale` SET walletamount='$debit_fee' WHERE phone='$username'");
     if($debit){
      $insert = mysqli_query($connection, "INSERT INTO dlprint (application_no,username,dl_no,dob,response,status, fee,front,back,pdf) VALUES ('$application_no','$username','$dl', '$dob', '$response','success', '$fee', '$front','$back','$a4');");
      
          date_default_timezone_set('Asia/Kolkata');
          $timestamp = date("Y-m-d H:i:s");
          $summary = mysqli_query($connection,"INSERT INTO `card_summary`(`name`, `number`, `status`, `fee`,`type`,`old_balance`,`new_balance`, `date`, `userid`) VALUES ('$application_no','$dl','DL HKB Print PDF','$fee','D','$wallet','$debit_fee','$timestamp','".$_SESSION['userid']."')");  
          
           if($insert){
          ?>
           <script>
                        $(function(){
                            Swal.fire(
                                'Dl : <?php echo $dl;?> is Downloaded',
                                'Application : <?php echo $application_no; ?> Message : File Generated?>',
                                'success'
                            )
                        })
                        setTimeout(() => {
                            window.location='DL_Instant_Hkb_list.php';
                        }, 1200);
                    </script>
          <?php
      }
     
     }
    //  echo "API Response: " . $response;
}

// Close cURL session
curl_close($ch);

       }     
    }else{
        ?>
        <script>
            $(function(){
                Swal.fire(
                    'Opps',
                    'Wallet Balance Insufficient ! Please Recharge ',
                    'error'
                )
            });
            window.setTimeout(function(){
                window.location.href='wallet.php';
            },);
            
        </script>
        <?php
    }

}

?>
<div class="content-wrap">
    <div class="main">
        <div class="col-md-12">
            <div class="main-content">
                <!--<section class="section">-->
                    <div class="section-header">
                        <div class="container-fluid">
                            <!--<div class="row">-->
                            <!--    <h1>Information <?php echo $walletamount; ?></h1>-->
                            <!--</div>-->
                        </div>
                    </div>
                    <!--<div class="page-wrapper" style="display: block;">-->
                    <!--    <div class="page-breadcrumb">-->
                    <!--        <div class="row mb-2">-->
                    <!--            <div class="col-sm-6">-->
                    <!--                <h4 class="page-title text-truncate text-dark font-weight-medium mb-1">Instant Dl Print</h4>-->
                    <!--            </div>-->
                    <!--            <div class="col-sm-6 text-right">-->
                    <!--                <a href="dlprint_list" class="btn btn-primary btn-sm">Pan List</a>-->
                    <!--            </div>-->
                    <!--        </div>-->
                    <!--    </div>-->
                        <div class="col-md-12">
                            <div class="card card-default">
                                <div class="card-header">
                                    <div class="card-title">
                                        <h3><strong>Enter beneficiary Details</strong></h3>
                                        <h4>Disclaimer :- CHARGE - ₹ 20, FAST SERVICE</h4>
                                        <h4>Disclaimer :- Instant Service</h4>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="container-fluid">
                            <div class="card col-12">
                                <hr>
                                <div class="row">
                                    <div class="col-lg-4 col-md-6 col-sm-6">
                                        <div class="card">
                                            <div class="card-body">
                                                <form name="" action="" method="post" id="dlprint">
                                                    <div class="card-body">
                                                        <div class="form-group">
                                                            <label class="card-title" for="dl">Dl Number</label>
                                                            <input type="text" class="form-control" name="dl" id="dl" placeholder="Enter Dl Number">
                                                        </div>
                                                        <div class="form-group">
                                                            <label class="card-title" for="dob">D.O.B</label>
                                                            <input type="text" class="form-control" name="dob" id="dob" placeholder="Enter Dob">
                                                        </div>
                                                        <div class="form-group">
                                                            <label class="card-title" for="background">Card Background<span class="required-mark text-red" style="color:red;">*</span></label>
                                                            <input type="hidden" name="b2" value="Bittu">
                                                            <select name="background" id="background" required="" class="form-control">
                                                                <option value="B">Blue</option>
                                                                <option value="W">White</option>
                                                            </select>
                                                        </div>
                                                        <div class="form-group">
                                                            <label class="card-title" for="card_type">Card Type<span class="required-mark text-red" style="color:red;">*</span></label>
                                                            <select name="card_type" id="card_type" required="" class="form-control">
                                                                <option value="C">Chip Pvc</option>
                                                                <option value="N">Normal Pvc</option>
                                                            </select>
                                                        </div>
                                                        <div class="form-actions">
                                                            <div class="text-left">
                                                                <button type="submit" class="btn btn-info">Submit</button>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </section>
            </div>
        </div>
    </div>
</div>

