<?php 

namespace App\Models;
use CodeIgniter\Model;
use App\Models\CommonModel;

Class Rank_update extends Model {
	        // Declare a class variable (static)
	public function __construct(){
		$this->db = \Config\Database::connect();
		$db      = \Config\Database::connect();
		

		$this->common_model = model('CommonModel');
	}
	public function rank_update($userid,$recvied){

		$userid = $userid;
		$db      = \Config\Database::connect();
		$uusers_matrix = $db->table('uusers_matrix');
		$uusers_matrix =  $uusers_matrix->where('uusersid',$userid)->get()->getRow();

		if ($uusers_matrix->uusersid) {

			$referral_id = $uusers_matrix->referral_id;
			
			$referral_table = $db->table('uusers');
			$referral_details = $referral_table->where('uusersid',$referral_id)->get()->getRow();

			$referral_rank = $referral_details->rankid;

			if(empty($referral_rank))
				$referral_rank = 4;

			if ($referral_details) {
				$this->rank_check($referral_details->uusersid);
			}
			
			$referral_details = $referral_table->where('uusersid',$referral_id)->get()->getRow();
			$referral_rank = $referral_details->rankid;




			// if ($referral_rank) {

			// 	$rewardsettings = $db->table('rewardsettings');
			// 	$rewardsettings = $rewardsettings->where('id',$referral_rank)->get()->getRow();

			// 	// Direct commission check

			// 	if ($rewardsettings->direct_push_award > 0) {

			// 		$commission = $recvied * $rewardsettings->direct_push_award / 100;
			// 		//$this->directcommission($referral_details->uusersid,$commission,$userid);

			// 	}

			// }
		}


		return true;
	}

	public function rank_check($userid){
		$db      = \Config\Database::connect();
		$this->session = session();
		
		$uusers_matrix_data = $db->table('uusers_matrix');
		$uusers_matrix_data =  $uusers_matrix_data->where('uusersid',$userid)->get()->getRow();

		$rank = $uusers_matrix_data->rankid;

		if(empty($rank))
			$rank = 4;

		$db->query("UPDATE `uusers_matrix` set rankid=4 WHERE rankid is NULL");

	 //    echo"<pre>";
		// print_r($userid);
		// print_r("rank \n");
		// print_r($rank);
		// echo"</pre>";

		if ($rank) {
			
			$next_rank_get =$db->table('rewardsettings')->where('id >',$rank)->get()->getRow();
			$last_rank_get =$db->table('rewardsettings')->orderBy('id','DESC')->get()->getRow();


			$next_rank_id = $next_rank_get->id;
			$last_rank_id = $last_rank_get->id;


			$direct_ref_count = $next_rank_get->direct_ref_count;
			$team_mem_count = $next_rank_get->team_mem_count; 
			$pre_line_count = $next_rank_get->pre_line_count;
	

			$direct_ref_count_flag = '0';
			$team_mem_count_flag = '0';
			$pre_line_count_flag = '0';

			if ($direct_ref_count == 0)  {
				$direct_ref_count_flag = '1';
			}else{

				$uusers_matrix = $db->table('uusers_matrix');
				$uusers_matrix =  $uusers_matrix->where('referral_id',$userid)->where('rankid !=','')->get();
				$check_direct = $uusers_matrix->getResult();
                $check_direct_num_rows = 0;
				foreach ($check_direct as $key => $value) {
				$confirm = $db->table('history')->where('uusersid',$value->uusersid)->where('type','activation')->where('status',1)->get()->getRow();
				// echo"<pre>";
				// print_r($confirm);
				// echo"</pre>";
				if($confirm && $confirm->payment_id != 1)
				$check_direct_num_rows += 1;
				}

				if ($direct_ref_count <= $check_direct_num_rows) {
					$direct_ref_count_flag = '1';
				}else{
					$direct_ref_count_flag = '0';
				}
			}

			if ($team_mem_count == 0)  {
				$team_mem_count_flag = '1';
			}else{

				$downlinescount = $this->downlinescount($userid);
				
				if ($team_mem_count <= $downlinescount) {
					$team_mem_count_flag = '1';
				}else{
					$team_mem_count_flag = '0';
				}
			}


		

			if ($pre_line_count == 0) {
				$pre_line_count_flag = '1';
			}else{

				$uusers_matrix = $db->table('uusers_matrix');
				$uusers_matrix =  $uusers_matrix->where('referral_id',$userid)->where('rankid',$rank)->get();
				$check_pre_line = $uusers_matrix->getResult();

                $check_pre_line_num_rows = 0;
				foreach ($check_pre_line as $key => $value) {
				$confirm = $db->table('history')->where('uusersid',$value->uusersid)->where('type','activation')->where('status',1)->get()->getRow();
				if($confirm  && $confirm->payment_id != 1)
				$check_pre_line_num_rows = $check_pre_line_num_rows + 1;
				}



			


				if ($pre_line_count <= $check_direct_num_rows) {
					if ($pre_line_count <= $check_pre_line_num_rows)
					$pre_line_count_flag = '1';
				    else{
				    	$check_pre_line_num_rows_nxt = 0;
			            if(!empty($check_direct)) { 
				    	foreach ($check_direct as $key => $value) {
				    		$uusers_matrix1 = $db->table('uusers_matrix');
				    		$uusers_matrix_one =  $uusers_matrix1->where('uusersid',$value->uusersid)->get()->getRow();
		
				    		if($uusers_matrix_one->rankid >= $rank){
				    			$confirm1 = $db->table('history')->where('uusersid',$uusers_matrix_one->uusersid)->where('type','activation')->where('status',1)->get()->getRow();
				                if($confirm1  && $confirm1->payment_id != 1){
				    			$check_pre_line_num_rows_nxt = $check_pre_line_num_rows_nxt + 1;
				    		}
				    		}
				    		else{
			
				    		$team_members = explode(',', $uusers_matrix_one->members_cnt);
				    	    foreach ($team_members as $key => $value1) {
				    	    	$uusers_matrix2 = $db->table('uusers_matrix');
				    	    	 $uusers_matrix_two =  $uusers_matrix2->where('uusersid',$value1)->get()->getRow();
				    	    	 if($uusers_matrix_two->rankid >= $rank){
				    	    	 	$confirm2 = $db->table('history')->where('uusersid',$uusers_matrix_two->uusersid)->where('type','activation')->where('status',1)->get()->getRow();
				                   if($confirm2  && $confirm2->payment_id != 1){
				    	    	 	$check_pre_line_num_rows_nxt = $check_pre_line_num_rows_nxt + 1;
				    	    	 	break;
				    	    	 }
				    	    }
				    	}
				    }
				}
			}else{
				    	$check_pre_line_num_rows_nxt = 0;
				    }
				    	if($pre_line_count <= $check_pre_line_num_rows_nxt){
				    		$pre_line_count_flag = '1';
				    	}else{
				    		$pre_line_count_flag = '0';
				    	}
				    }
				}else{
					$pre_line_count_flag = '0';
				}
			}
             
           if($userid == 158) { 
            echo"<pre>";
            print_r("Direct Referral : ");
			print_r($check_direct_num_rows);
			print_r("\n Direct Flag : ");
			print_r($direct_ref_count_flag);
            print_r("\n Team referral : ");
            // print_r($downlinescount);
            print_r("\n Team Flag : ");
            print_r($team_mem_count_flag);
            print_r("\n Pre line count : ");
            print_r($check_pre_line_num_rows);
            print_r("\n Pre line count nxt : ");
            print_r($check_pre_line_num_rows_nxt);
            print_r("\n pre line count flag : ");
            print_r($pre_line_count_flag);
			echo"</pre>";
			// die;
           }
            
			

	


	


			if ($direct_ref_count_flag == '1' && $team_mem_count_flag == '1' && $pre_line_count_flag == '1') {

				$rank_data = array(
					'rankid' => $next_rank_id
				);

				

				if($rank != $last_rank_id){ 
		
				$rank_update_user_table = $db->table('uusers')->where('uusersid',$userid)->update($rank_data);
				$rank_update = $db->table('uusers_matrix')->where('uusersid',$userid)->update($rank_data);

				}
				
		
				return true;
			}
		}
	}


	// Rank update setup

	public function rank_change($userid,$rankid){
		$rank_data = array(
			'rankid' => $rankid
		);
		$uusers = $db->table('uusers');
		$uusers = $uusers->where('uusersid',$pay_address->userid)->update($rank_data);
	}






	public function downlinescount($userid)
	{
		$db  = \Config\Database::connect();
		$this->session = session();

		$ucondition = "uusersid = '".$userid."' and rankid != ''";
		$table = "uusers_matrix";

		$select_count = $this->common_model->GetRow($table,$ucondition);


		$members_cnt = explode(',', $select_count->members_cnt);
        $arraycount = 0;
		foreach ($members_cnt as $key => $value) {
			 $confirm = $db->table('history')->where('uusersid',$value)->where('type','activation')->where('status',1)->get()->getRow();
			 if($confirm  && $confirm->payment_id != 1)
			 	$arraycount += 1;
		}

		return $arraycount;
	}

}


?>