<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Register extends CI_Controller
{
    public function index(){
        /************************************/
        // loading common model
        $this->load->model('Common_model');
        
        /************************************/
        /************************************/
       	$column['id'] = '1';
		$table = 'tbl_settings';
	
	if(isset($_GET['sponsor']))
	{
		$_SESSION['nsponsor']=$_GET['sponsor'];
	}
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
		$i = 1;
        $result23456789 = $this->Common_model->Results('tbl_language');
        foreach ($result23456789 as $row)
        {
            define('LANG_VALUE_' . $i, $row['lang_value']);
            $i++;
        }
        
	    $columntop_cities['status'] = '1';
		$tabletop_cities = 'tbl_top_cities';
        // we are getting results from common modal
		$resulttop_cities = $this->Common_model->ResultsWhere2($columntop_cities,$tabletop_cities);
		
		
	    $column3['status'] = '1';
	    $column3['status_home'] = '1';
		$table3 = 'tbl_top_category';
        // we are getting results from common modal
		$top_category = $this->Common_model->ResultsWhere2($column3,$table3);
		
	    $column3['status'] = '1';
	    $column3['status_home'] = '1';
		$table3 = 'tbl_mid_category';
        // we are getting results from common modal
		$mid_category = $this->Common_model->ResultsWhere2($column3,$table3);
		
	    $column3['status'] = '1';
	    $column3['status_home'] = '1';
		$table3 = 'tbl_end_category';
        // we are getting results from common modal
		$end_category = $this->Common_model->ResultsWhere2($column3,$table3);
		
		
	    $column3['status'] = '1';
	    $column3['status_home'] = '1';
		$table3 = 'tbl_top_category';
        // we are getting results from common modal
		$top_category = $this->Common_model->ResultsWhere2($column3,$table3);
		
	    $column32['status'] = '1';
		$table32 = 'tbl_mid_category';
        // we are getting results from common modal
		$data['mid_category2'] = $this->Common_model->ResultsWhere2($column32,$table32);
		
	    $column33['status'] = '1';
		$table33 = 'tbl_end_category';
        // we are getting results from common modal
		$data['end_category2'] = $this->Common_model->ResultsWhere2($column33,$table33);
		
	    $column31['status'] = 1;
		$table31 = 'tbl_top_cities';
        // we are getting results from common modal
		$top_cities = $this->Common_model->ResultsWhere2($column31,$table31);
		
		
	    $column31['status'] = 1;
		$table31 = 'tbl_social';
        // we are getting results from common modal
		$social = $this->Common_model->ResultsWhere2($column31,$table31);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['top_cities'] = $top_cities;
		$data['top_category'] = $top_category;
		$data['mid_category'] = $mid_category;
		$data['end_category'] = $end_category;
		$data['social'] = $social;
        
        /************************************/
        $data['page'] = 'register';
        
        /************************************/
        $column31['status'] = 1;
        $table31 = 'tbl_social';
        // we are getting results from common modal
        $data['social'] = $this->Common_model->ResultsWhere2($column31,$table31);
        
        /************************************/
        // we are loading admin dashboard
        $this->load->view('website/register',$data);	
    }

    public function check_email(){
        
        /************************************/
        // loading common model
        $this->load->model('Common_model');
        /************************************/
        $status = 1;
        $this->load->helper('email');
        if (isset($_POST['cust_email'])){
            if (valid_email($_POST['cust_email'])){
                $cust_email = $_POST['cust_email'];
        
                $column13['cust_email'] = $cust_email;
                $number_of_rows = $this->Common_model->Count_Where($column13,'tbl_customer');
        
                if ($number_of_rows > 0)
                {
                    $status = 0;
                    $error_message = 'This Email is already Exits in Another Account.';
                }
            }
            else{
                $status = 0;
                $error_message = 'Email is not valid';
            }
        }
        
        // response for user
        $myObj = new \stdClass();
        $myObj->error = $error_message;
        $myObj->log = $status;
        if(isset($_SESSION['type'])){
            $myObj->form = $_SESSION['type'];
        }

        $myJSON = json_encode($myObj);

        echo $myJSON;
        
    }

    public function get_otp()
    {
        /************************************/
        // loading common model
        $this
            ->load
            ->model('Common_model');
            
        /************************************/
        $column['id'] = '1';
        $table = 'tbl_settings';
        
        /************************************/
        // we are getting results from common modal
        $data = $this->Common_model->Result($column,$table);
        /************************************/
        
        $i = 1;
        $result = $this->Common_model->Results('tbl_language');
        foreach ($result as $row)
        {
            define('LANG_VALUE_' . $i, $row['lang_value']);
            $i++;
        }

        $status = 0;
        $success_message = '';
        $error_message = '';
        $header = '';
        
        if (isset($_POST['mobile_number'])){
                $_SESSION['type'] = 2;
                
                $mob = $_POST['mobile_number'];
    
                $column13['cust_phone'] = $mob;
                $number_of_rows = $this->Common_model->Count_Where($column13,'tbl_customer');
    
                if ($number_of_rows > 0)
                {
                    $error_message = 'This Mobile is already Exits. Please Login..';
                }
                else
                {
                    $otp = mt_rand(100000, 999999);
                    $_SESSION['otp'] = $otp;
                    if(isset($_SESSION['otp']))
                {
                    $_SESSION['mob'] = $mob;
                    $status = 1;
                    $sender = $data['sms_sender'];
                    $username = $data['sms_user'];
                    $password2 = $data['sms_pass'];
                    $number = $mob;
                    $web='tryitindia.com';
                    $message="Your Verification OTP TRYIT India is $otp Thanks, Tryitindia.com";
                   // $message="Your Verification OTP is $otp. Thanks ".$data['base_name'];
                    // $success_message = $message;
                    $info_message = $_SESSION['otp_limit'];
                   // $url = "http://buzz.azmarq.com/http-api.php?username=" . urlencode($username) . "&password=" . urlencode($password2) . "&senderid=" . urlencode($sender) . "&route=1&number=" . urlencode($number) . "&message=" . urlencode($message);
                    $url = "https://2factor.in/API/R1/?module=TRANS_SMS&apikey=2a3157c9-ed20-11eb-8089-0200cd936042&to=".urlencode($number)."&from=TRYITT&templatename=tryit+otp&var1=".urlencode($otp)."&var2=".urlencode($web)."";

                    $ch = curl_init();
                    $timeout = 90; // set to zero for no timeout
                    $ch = curl_init($url);
                    curl_setopt ($ch, CURLOPT_URL, $url);
                    curl_setopt ($ch, CURLOPT_HEADER, 0);
                    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
                    $res_data = curl_exec($ch);
                    $curl_error = curl_errno($ch);
                    curl_close($ch);
                    $rj = (json_decode($res_data, true));
                }
                    
            }
    
       
    
            // response for user
            $myObj = new \stdClass();
            $myObj->succ = $success_message;
            $myObj->error = $error_message;
            $myObj->log = $status;
            if(isset($_SESSION['type'])){
                $myObj->form = $_SESSION['type'];
            }
    
            $myJSON = json_encode($myObj);
    
            echo $myJSON;
        }
    }

    public function verify_otp_form()
    {
        /************************************/
        // loading common model
        $this
            ->load
            ->model('Common_model');
            
        /************************************/
        $column['id'] = '1';
        $table = 'tbl_settings';
        
        /************************************/
        // we are getting results from common modal
        $data = $this->Common_model->Result($column,$table);
        /************************************/
        
        $i = 1;
        $result = $this->Common_model->Results('tbl_language');
        foreach ($result as $row)
        {
            define('LANG_VALUE_' . $i, $row['lang_value']);
            $i++;
        }

        $status = 0;
        $success_message = '';
        $error_message = '';
        $header = '';
        
        if (isset($_POST['mobile_number']) && isset($_POST['otp'])){
           
                $_SESSION['type'] = 3;
                $mob = $_POST['mobile_number'];
                $otp = $_POST['otp'];
    
                if ($_SESSION['otp'] == $otp)
                {
                    $status = 1;
                    $success_message = 'Otp Verified Successfully.';
                }
                else
                {
                    $error_message = 'Expired or Invalid OTP.';
                    $status = 0;
                }
                
            // response for user
            $myObj = new \stdClass();
            $myObj->succ = $success_message;
            $myObj->error = $error_message;
            $myObj->log = $status;
            if(isset($_SESSION['type'])){
                $myObj->form = $_SESSION['type'];
            }
    
            $myJSON = json_encode($myObj);
    
            echo $myJSON;
        }
    }

    public function final_form()
    {
        /************************************/
        // loading common model
        $this
            ->load
            ->model('Common_model');
            
        /************************************/
        $column['id'] = '1';
        $table = 'tbl_settings';
        
        /************************************/
        // we are getting results from common modal
        $data = $this->Common_model->Result($column,$table);
        /************************************/
        
        $i = 1;
        $result = $this->Common_model->Results('tbl_language');
        foreach ($result as $row)
        {
            define('LANG_VALUE_' . $i, $row['lang_value']);
            $i++;
        }

        $status = 0;
        $success_message = '';
        $error_message = '';
        $header = '';
        
            if (isset($_POST['mobile_number']) && isset($_POST['otp']) && isset($_POST['cust_name']) && isset($_POST['cust_email']) && isset($_POST['reg_password']) && isset($_POST['reg_confirm_password']))
            {
    
                $mob = $_POST['mobile_number'];
                $uid=$mob;
                $rj=$uid;
                $income_id=$uid;
                $side=$_POST['side']?$_POST['side']:1;
                $otp = $_POST['otp'];
                $cust_name = $_POST['cust_name'];
                $name=$cust_name;
                $cust_email = $_POST['cust_email'];
                $email=$cust_email;
                $sid = $_POST['sid'];
                
                if(isset($_SESSION['nsponsor']))
                {
                $sponsor = $_SESSION['nsponsor'];
                }
                else
                {
                $sponsor = $_POST['sid'];
                }
                
                $upid=$sponsor;
                $reg_password = $_POST['reg_password'];
                $pass=$reg_password;
                $reg_confirm_password = $_POST['reg_confirm_password'];
                date_default_timezone_set("Asia/Kolkata");
                $addon=date('Y-m-d H:i:s');
                $date5=date('d-m-Y H:i:s');
               
                $column132['cust_phone'] = $_POST['mobile_number'];
                $number_of_rows = $this->Common_model->Count_Where($column132,'tbl_customer');
                if ($number_of_rows > 0)
                {
                    $status = 0;
                    $error_message = 'This Mobile is already Exits. Please Login..';
                }else{
                
              
                  
                if ($_POST['reg_password'] == $_POST['reg_confirm_password'])
                {
                    
                    
                    $query6 = $this->db->where('uid',$sponsor);
                    $query6 = $this->db->get('details');
                    if ($query6->num_rows() > 0)  
                    {
                    
                    
                        if($side=='1')
                        {
                            $sides='luid';
                        
                        }
                        else
                        {
                            $sides='ruid';
                        
                        }
                        
                        
                        $upid=$sponsor;
                        
                        for($x1=0;$x1<2000;$x1++)
                        {
                        
                            $res= $this->db->query("select * from details where uid='$upid'");
                            foreach ($res->result() as $row9)
                            {
                            
                                $upid_side=$row9->$sides;
                                if(empty($upid_side)){
                                    $upid=$row9->uid;
                                    $root1=$row9->root;
                                    $root=$root1.','.$upid; 
                                }
                                else
                                {
                                    $upid=$row9->$sides;
                                }
                            
                            }
                        }       
                        
                        
                        $datas=[
                            $sides=>$uid  
                        ];     
                        
                        $income_id=$uid;
                        
                        $datak12=[
                            'sponsor'=>$sponsor,
                            'upid'=>$upid,
                            'uid'=>$uid, 
                            'pass'=>$reg_password, 
                            'name'=>strip_tags($_POST['cust_name']), 
                            'mob'=>strip_tags($_POST['mobile_number']), 
                            'email'=>strip_tags($_POST['cust_email']),
                            'root'=>$root,
                            'tlm'=>0,
                            'tlpv'=>0,
                            'trpv'=>0,
                            'tlpv1'=>0,
                            'trpv1'=>0,
                            'trm'=>0,
                            'tpv'=>0,
                            'rpv'=>0,
                            'status'=>1
                        ];
                        
                        $this->db->insert('details',$datak12);
                        
                        $this->db->where('uid',$upid);
                        $this->db->update('details',$datas);     
                                        
                                        
                        $token = md5(time());
                        $cust_datetime = date('Y-m-d h:i:s');
                        $cust_timestamp = time();
        
                        $formArray['cust_name'] =  strip_tags($_POST['cust_name']);
                        $formArray['sponsor_id'] =  strip_tags($_POST['sid']);
                        $formArray['cust_email'] = strip_tags($_POST['cust_email']);
                        $formArray['cust_phone'] = strip_tags($_POST['mobile_number']);
                        $formArray['cust_password'] = md5($reg_password);
                        $formArray['cust_token'] = $token;
                        $formArray['cust_datetime'] = $cust_datetime;
                        $formArray['cust_timestamp'] = $cust_timestamp;
                        $formArray['cust_status'] = 1;
                        $formArray['user'] = 1;
                        $formArray['code'] = $otp;
                        $formArray['uid'] = $_POST['mobile_number'];
                        $formArray['pass'] = $reg_password;
                            
                        $this->Common_model->Create($formArray,'tbl_customer');
                        
                        
                        $date=date('Y-m-d H:i:s');
         
                        $uid= explode(",",$root); 
                        $i=count($uid,COUNT_NORMAL);
                        $x=0;
                        for($x=$i;$x>=0;$x--)
                        {  
                        
                            $upid_pv=$uid[$x];
                            
                            if($upid_pv!='')
                            {
                            
                            
                                $find=$rj;
                                
                                
                                $querygf11 = $this->db->query("SELECT * FROM details where uid='$upid_pv' ");
                                $rowgf11 = $querygf11->row();
                                $rj=$rowgf11->uid;
                                
                                
                                $my1=$rowgf11->luid;
                                $my2=$rowgf11->ruid;
                                
                                
                                if($my1==$find)
                                {
                                    $pv_sides='tlpv';
                                    $pv_sides1='tlpv1';
                                    $tm='tlm';
                                    $tsp='tlsp';
                                }  
                                
                                if($my2==$find)
                                { 
                                    $pv_sides='trpv';
                                    $pv_sides1='trpv1';
                                    $tm='trm';
                                    $tsp='trsp';
                                }
                                
                                
                                
                                
                                $queryf11 = $this->db->query("SELECT * FROM details where uid='$upid_pv' ");
                                $rowf11 = $queryf11->row();
                                $tm1=$rowf11->$tm+1;
                                $tsp1=$rowf11->$tsp+1;
                                $f=[
                                    $tsp=>$tsp1, 
                                    $tm=>$tm1
                                ];
                                
                                $this->db->where('uid',$upid_pv);
                                $this->db->update('details',$f);
                                
                                
                               
                            
                            }
                        
                        } 
                        
                     
                        $this->db->where('cust_phone',$_POST['mobile_number']);
                        $result2 = $this->db->get('tbl_customer')->result_array();
                        
                        foreach($result2 as $row) {
                            $cust_status = $row['cust_status'];
                            $row_password = $row['pass'];
                        }
                        
                        $_SESSION['customer'] = $row;
                        $this->session->set_userdata('customer',$row);
                        
                    
                        $status = 1;
                        $success_message = "Account Created Successfully.";
                        unset($_SESSION['type']);
                    }
                    else
                    {
                        $error_message = 'Invalid Sponsor ID.';
                        $status = 0; 
                    }
                }
                else
                {
                    $error_message = 'Password & Confirm Password Does Not Matched.';
                    $status = 0;
                }
                
             
                }
            }
    
            // response for user
            $myObj = new \stdClass();
            $myObj->succ = $success_message;
            $myObj->error = $error_message;
            $myObj->log = $status;
            if(isset($_SESSION['type'])){
                $myObj->form = $_SESSION['type'];
            }
            $myJSON = json_encode($myObj);
    
            echo $myJSON;
    }

}

