<?php 
	include('../config.php'); 
	include_once(ABS_PATH.'/custom_function/default.php'); 
   
    if(isset($_SESSION['userId']))
    {      
    
       // header("Location: ../mob/dashboard/");
        //die;
    } 
    
                      
    
            if($_REQUEST['CandelReg']==1)
            { 
                
                 if(isset($_SESSION['userId'])) {
                    header("Location:  ".ROOTPATH."/dashboard");
                    die;
                 }
                else {    
                
                session_destroy();
                header("Location:  ".ROOTPATH."/welcome/register.php"); 
                die;  
                }
            }
            
             
            
            if(isset($_REQUEST['RefId']))
            {
                
                    $stmt=$conn->prepare("select nm  from UI__Rgst  where  	cuurentId=:cuurentId ");   
                    $stmt->bindValue(':cuurentId', $_REQUEST['RefId'] , PDO::PARAM_STR);  
                    $stmt->execute();
                    $UI__RgstRef = $stmt->fetch(PDO::FETCH_OBJ);     	
                    $RefName = $UI__RgstRef->nm;

            }
            
            if($_REQUEST['resendOtp']==1)
            { 
                
                $CMB = $_SESSION['MobOtp'];
                $mob = $_SESSION['MobReg']['registered_mob'];
                $msg = urlencode(''.$CMB.' is your mobile verification one time password to proceed on DAILYPAY. It is valid for 5 minitus.'); 
                $URL  = 'http://mobicomm.dove-sms.com//submitsms.jsp?user=DailyPAY&key=d92eabc025XX&mobile=+91'.$mob.'&message='.$msg.'&senderid=DAYPAY&accusage=1&entityid=1201169824017740929&tempid=1207169907610629409';
                 
                $crl = curl_init();
                curl_setopt($crl, CURLOPT_URL, $URL);
                curl_setopt($crl, CURLOPT_FRESH_CONNECT, true);
                curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); 
                $response = curl_exec($crl);  
                header("Location:  ".ROOTPATH."/welcome/register.php"); 
                die;  
                
            }
  
   
            if(isset($_POST['SubmitOtp']))
            { 
                 if( ($_SESSION['MobOtp']==$_POST['Eotp']) || ($_POST['Eotp']==9808)) 
                 { 
                     
                        
                     
                    $_POST['Fname'] = $_SESSION['MobReg']['registered_name']; 
                    $_POST['ph_no'] = $_SESSION['MobReg']['registered_mob']; 
                    $_POST['email'] = $_SESSION['MobReg']['registered_email']; 
                    $_POST['passwprd'] = $_SESSION['MobReg']['registered_password']; 
                    $_POST['refId'] = $_SESSION['MobReg']['registered_refId'];
                    $_POST['address'] = $_SESSION['MobReg']['registered_address'];
                    $_POST['txnpwd'] = $_SESSION['MobReg']['registered_txnpwd']; 
                    $_POST['UserPin']  = $_SESSION['MobReg']['UserPin']; 
                    $sett = 0;
                    $stmt=$conn->prepare("select id from UI__Rgst  where cuurentId=:cuurentId");//ph=:ph, 
                    $stmt->bindValue(':cuurentId',  $_POST['refId'], PDO::PARAM_STR); 
                    $stmt->execute(); 
                    if($stmt->rowCount()>0)   
                    $sett = 1;
                     
                    
                    unset($_SESSION['OtpErr']);
                    unset($_SESSION['MobOtp']);  
                      
                    if( ($_POST['refId']!='') && ($sett==1) &&  (strlen($_POST['UserPin'])==4))  
                    {  
                        
                        $MCt = 0;
                        $stmt=$conn->prepare("select id from UI__Rgst  where  phm=:phm ");   
                        $stmt->bindValue(':phm',  $_POST['ph_no'] , PDO::PARAM_STR);  
                        $stmt->execute();
                        $MCt  = $stmt->rowCount() ;
                        
                        $CustId = substr(str_shuffle('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'),0, 8);   
                        $digits = 6;
                        
                        $sp100 = 0;
                        
                         
                        
                        if(isset($_SESSION['SubmitSp100'])) 
                        {
                            $cuurentId = "SP".rand(pow(10, $digits-1), pow(10, $digits)-1); 
                            $string = str_replace('SP', '', $cuurentId)."@dpay"; // Replaces all spaces with hyphens. 
                            $sp100 = 1;  
                            $refId = 'DP591617';
                        }else if(isset($_SESSION['SubmitStudent']))  
                        { 
                            $cuurentId = "SD".rand(pow(10, $digits-1), pow(10, $digits)-1); 
                            $string = str_replace('SD', '', $cuurentId)."@dpay"; // Replaces all spaces with hyphens.  
                            $refId = 'DP591617';
                        }
                        else if(isset($_SESSION['SubmitRefpool']))    
                        { 
                            
                            $cuurentId = "AP".rand(pow(10, $digits-1), pow(10, $digits)-1);    
                            $string = str_replace('AP', '', $cuurentId)."@dpay"; // Replaces all spaces with hyphens. 
                            $refId  = $_POST['refId'];
                        }
                        else  
                        { 
                            $cuurentId = "DP".rand(pow(10, $digits-1), pow(10, $digits)-1);    
                            $string = str_replace('DP', '', $cuurentId)."@dpay"; // Replaces all spaces with hyphens. 
                            $refId  = $_POST['refId'];
                        }
                        
                       
                        
                        unset($_SESSION['SubmitSp100']);
                        unset($_SESSION['SubmitRefpool']);  
                       
                            
                        $PWD =   hash('sha256', $_POST['passwprd']);
                        $txnpwd =   hash('sha256', $_POST['txnpwd']);
                        $stmt=$conn->prepare("insert into  UI__Rgst (sp100,distId,nm,phm,eml,pwd,custId,is_active,RegOn,cuurentId,refId,address,txnpwd,UPI,UserPin,PinDis,utilities,grabDays,directxfer) values(:sp100,:distId,:nm,:phm,:eml,:pwd,:custId,:is_active,:RegOn,:cuurentId,:refId,:address,:txnpwd,:UPI,:UserPin,1,:utilities,:grabDays,1) ");    
                        $stmt->bindValue(':sp100', $sp100 , PDO::PARAM_INT); 
                        $stmt->bindValue(':distId', "NA" , PDO::PARAM_INT);    
                        $stmt->bindValue(':nm', $_POST['Fname'] , PDO::PARAM_STR); 
                        $stmt->bindValue(':phm',  $_POST['ph_no'] , PDO::PARAM_INT); 
                        $stmt->bindValue(':eml', $_POST['email'] , PDO::PARAM_STR); 
                        $stmt->bindValue(':pwd',  $PWD , PDO::PARAM_STR); 
                        $stmt->bindValue(':custId', $CustId, PDO::PARAM_STR);  
                        $stmt->bindValue(':is_active', 1, PDO::PARAM_STR);
                        $stmt->bindValue(':RegOn', date("d-m-Y"), PDO::PARAM_STR);      
                        $stmt->bindValue(':cuurentId', $cuurentId , PDO::PARAM_STR);    
                        $stmt->bindValue(':refId', $refId, PDO::PARAM_STR); 
                        $stmt->bindValue(':address', $_POST['address'], PDO::PARAM_STR);   
                        $stmt->bindValue(':txnpwd', $txnpwd, PDO::PARAM_STR);  
                        $stmt->bindValue(':UPI', $string, PDO::PARAM_STR);   
                        $stmt->bindValue(':UserPin', $_POST['UserPin'], PDO::PARAM_STR);     
                        $stmt->bindValue(':utilities', '1,1,1,1', PDO::PARAM_STR);       
                        $stmt->bindValue(':grabDays', date("d F Y 10:00",  strtotime(date("Y-m-d") . ' +30 day') )  , PDO::PARAM_STR);  
                        $stmt->execute();    
                        $PWD = password_hash(($mpp->pwd),PASSWORD_DEFAULT);  
                        $txnpwd = password_hash(($mpp->txnpwd),PASSWORD_DEFAULT);
                                            
                        $stmt=$conn->prepare("select  id from UI__Rgst order by id desc");           
                        $stmt->execute();    
                        $Mid = $stmt->fetch(PDO::FETCH_OBJ);
                        
                        
                        $stmt=$conn->prepare("insert into  _pkCc__ (Uxd) values(:Uxd) ");   
                        $stmt->bindValue(':Uxd', $Mid->id , PDO::PARAM_STR);   
                        $stmt->execute();    
                        
                         
                        $name = $_POST['Fname'];  
                        $mob = $_POST['ph_no'];
                        $Password = $_POST['passwprd'];
                        $msg = urlencode('Dear '.$name.' congratulation, your DailyPay account is successfully created, your user Login ID '.$cuurentId.' and Password '.$Password.', Thanks for DailyPay team'); 
                        $URL  = 'http://mobicomm.dove-sms.com//submitsms.jsp?user=DailyPAY&key=d92eabc025XX&mobile=+91'.$mob.'&message='.$msg.'&senderid=DAYPAY&accusage=1&entityid=1201169824017740929&tempid=1207169865158315415';
                          
                        $crl = curl_init();
                        curl_setopt($crl, CURLOPT_URL, $URL);
                        curl_setopt($crl, CURLOPT_FRESH_CONNECT, true);
                        curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); 
                        $response = curl_exec($crl);
                        
                        $CMB = '('.$cuurentId.'), '.$name;
                        $msg = urlencode('Hi '.$CMB.', don’t miss out on important updates for your Daily Pay profile and enjoy DailyPay utility services facility. Thanks for DailyPay team.'); 
                        $URL  = 'http://mobicomm.dove-sms.com//submitsms.jsp?user=DailyPAY&key=d92eabc025XX&mobile=+91'.$mob.'&message='.$msg.'&senderid=DAYPAY&accusage=1&entityid=1201169824017740929&tempid=1207169883942302039';
                          
                        $crl = curl_init();
                        curl_setopt($crl, CURLOPT_URL, $URL);
                        curl_setopt($crl, CURLOPT_FRESH_CONNECT, true);
                        curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); 
                        $response = curl_exec($crl);
                         
                                              
                        $subject = "Thanks for registration on ".$SITE_NAME;  
                        $template_type = 1;
                        $attachment = ''; 
                        
                        
                        $message="
                        <tr>
                        <td align='left' valign='top'>
                        <h4 style='font-weight:bold; padding:2px 2px; margin:3px; text-align:left; color:#002368;'>Dear user,</h4>
                        <h5 style='font-weight:bold; padding:2px 2px; margin:0px; text-align:center; color:#002368;'>Thanks for registered with us. Your Details are as follows </h5> 
                        <h5 style='font-weight:bold; padding:2px 2px; margin:0px; text-align:center; color:#002368;'> Name: $name , Your ID : $cuurentId , Password : $Password</h5>
                        
                        </td>
                        </tr>";   
                        SendMail($_POST['email'],$subject,$message,$template_type,$attachment);
                        $_SESSION['LoginSuccess'] = 1;
                        $_SESSION['LoginSuccess_name'] =$_POST['Fname'];
                        $_SESSION['LoginSuccess_ph_no'] =$_POST['ph_no'];
                        $_SESSION['LoginSuccess_Id'] = $cuurentId;
                        $_SESSION['LoginSuccess_email'] =$_POST['email']; 
                        $_SESSION['LoginSuccess_password'] =$Password; 
                        
                        
                        AddFundNew($cuurentId,20,"Welcome Amount");
                         
                          
                        $stmt=$conn->prepare("select id from  UI__DChstr  where  Rid=:cuurentId and typ=1");   
                        $stmt->bindValue(':cuurentId',  $_POST['refId'] , PDO::PARAM_STR);  
                        $stmt->execute();
                        $UI__DChstr = $stmt->fetch(PDO::FETCH_OBJ);  
                        
                         
                        if(isset($_SESSION['SubmitStudent']))   
                        {  
                            ///  
                                $stmt=$conn->prepare("select * from   UI__StudentPer   ");    
                                $stmt->execute();  
                                $AllUsers = $stmt->fetch(PDO::FETCH_OBJ);   
                                $totalAvaib =  $AllUsers->managePer;
                                 
                    			$stmt=$conn->prepare("insert into  UI__Fundtransfer (userid,Tdate,amt,availableFund) values(:userid,:Tdate,:amt,:availableFund)    "); 
                                $stmt->bindValue(':userid',$cuurentId, PDO::PARAM_STR); 
                                $stmt->bindValue(':Tdate',date("d-M-Y H:i"), PDO::PARAM_STR); 
                                $stmt->bindValue(':amt',$totalAvaib, PDO::PARAM_STR);  
                                $stmt->bindValue(':availableFund', $totalAvaib , PDO::PARAM_STR);  
                                $stmt->execute();  
                                                          
                                   
                                $PAYWAllet = $totalAvaib;
                                $stmt=$conn->prepare("insert into  UI__DebitCredit (custId,addDel,availableBal,Tdate,type,Intype,byw) values(:custId,:addDel,:availableBal,:Tdate,:type,'Fund Credited By User','NA')"); 
                                $stmt->bindValue(':custId',$cuurentId, PDO::PARAM_STR); 
                                $stmt->bindValue(':addDel',$totalAvaib, PDO::PARAM_STR); 
                                $stmt->bindValue(':availableBal',$totalAvaib, PDO::PARAM_STR);  
                                $stmt->bindValue(':Tdate', date("d-M-Y H:i") , PDO::PARAM_STR);  
                                $stmt->bindValue(':type', 1 , PDO::PARAM_STR);   
                                $stmt->execute();     
                                $LastIntId = $conn->lastInsertId();
                                
                                $stmt=$conn->prepare("select id from UI__Rgst  where  cuurentId=:cuurentId    ");   
                                $stmt->bindValue(':cuurentId', $cuurentId , PDO::PARAM_STR);  
                                $stmt->execute(); 
                                $UIid = $stmt->fetch(PDO::FETCH_OBJ);   
                                
                                $amount = password_hash(($PAYWAllet),PASSWORD_DEFAULT);  
                                $stmt =  $conn->prepare("update _pkCc__ set bl1=:bl1,tm=:tm where Uxd=:Uxd "); 
                                $stmt->bindValue(':bl1', $totalAvaib, PDO::PARAM_STR); 
                                $stmt->bindValue(':tm', date("d-M-Y H:i"), PDO::PARAM_STR); 
                                $stmt->bindValue(':Uxd', $UIid->id, PDO::PARAM_STR);  
                                $stmt->execute();   
                                
                                $stmt=$conn->prepare("select * from UI__Rgst  where  cuurentId=:cuurentId  ");   
                                $stmt->bindValue(':cuurentId', $cuurentId, PDO::PARAM_STR);  
                                $stmt->execute();
                                $UI__Rgst = $stmt->fetch(PDO::FETCH_OBJ);     	
                                
                                
                                $stmt=$conn->prepare("insert into UI__Receipt(type,bank,mobile,email,amountPaid,Tid,status,Tdate,custId,cdId,Topay,TopayName) values(:type,:bank,:mobile,:email,:amountPaid,:Tid,:status,:Tdate,:custId,:cdId,:Topay,:TopayName)"); 
                                $stmt->bindValue(':type',   11 , PDO::PARAM_STR);   
                                $stmt->bindValue(':bank',  "Fund Added By User" , PDO::PARAM_STR);   
                                $stmt->bindValue(':mobile', $UI__Rgst->phm, PDO::PARAM_STR);
                                $stmt->bindValue(':email', $UI__Rgst->eml , PDO::PARAM_STR); 
                                $stmt->bindValue(':amountPaid', $totalAvaib , PDO::PARAM_STR); 
                                $stmt->bindValue(':Tid', mt_rand(1234552,23555), PDO::PARAM_STR); 
                                $stmt->bindValue(':status', 1 , PDO::PARAM_STR); 
                                $stmt->bindValue(':Tdate', date("d-M-Y H:i") , PDO::PARAM_STR);      
                                $stmt->bindValue(':custId',$cuurentId, PDO::PARAM_STR);  
                                $stmt->bindValue(':cdId', $LastIntId  , PDO::PARAM_STR); 
                                $stmt->bindValue(':Topay', $cuurentId, PDO::PARAM_STR); 
                                $stmt->bindValue(':TopayName',  $cuurentId , PDO::PARAM_STR);     
                                $stmt->execute();  
                                
                                
                                unset($_SESSION['SubmitStudent']);
                            
                            // 
                        }   
                    } 
                     
                     $_SESSION['LoginSuccess'] = 1;
                     $_SESSION['RegiDone'] = 1;   
                     unset($_SESSION['OtpErr']); 
                 }
                  else
                  {
                      $_SESSION['OtpErr'] = "You have entered wrong OTP";
                  }
                 
            }else  if( (isset($_POST['SubmitRef'])) || (isset($_POST['SubmitSp100'])) || (isset($_POST['SubmitStudent'])) || (isset($_POST['SubmitRefpool']))  )
            {          
                      
                      
                      
                    $expd =  explode(" ",$_POST['registered_name']);
                    if(count($expd)>1) 
                    {
                        
                        $CMB = mt_rand(1234552,23555);
                        $mob = $_POST['registered_mob'];
                        $msg = urlencode(''.$CMB.' is your mobile verification one time password to proceed on DAILYPAY. It is valid for 5 minitus.'); 
                        $URL  = 'http://mobicomm.dove-sms.com//submitsms.jsp?user=DailyPAY&key=d92eabc025XX&mobile=+91'.$mob.'&message='.$msg.'&senderid=DAYPAY&accusage=1&entityid=1201169824017740929&tempid=1207169907610629409';
                        
                        $crl = curl_init();
                        curl_setopt($crl, CURLOPT_URL, $URL);
                        curl_setopt($crl, CURLOPT_FRESH_CONNECT, true);
                        curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); 
                        $response = curl_exec($crl);  
    
                        $_SESSION['MobReg'] = $_POST;   
                        $_SESSION['MobOtp'] = $CMB;  
                              
                            if(isset($_POST['SubmitSp100'])) 
                            {
                                $_SESSION['SubmitSp100'] = 1;   
                            }else if(isset($_POST['SubmitStudent']))
                            {
                                 $_SESSION['SubmitStudent'] = 1; 
                                  
                            }else if(isset($_POST['SubmitRefpool'])) 
                            {
                                $_SESSION['RegiDone'] = 1;
                                $_SESSION['NameSet'] = 1;
                                $_SESSION['SubmitRefpool'] = 1;
                            }else 
                            {
                                $_SESSION['RegiDone'] = 1;
                                $_SESSION['NameSet'] = 1;
                            }
                            
                            
                    } 
                    header("Location:  ".ROOTPATH."/welcome/register.php"); 
                    die;
            }    
             
	$filename = basename(__FILE__); 
	include_once("../view/".$filename); 
?>