<?php
class admin extends CI_Controller {
	public function index()
	{
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
			
			$this->load->view('admin/index',array("username" =>$this->session->userdata("username"),"fullname"=>$this->session->userdata("fullname")));	
			}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
	}
	public function test(){
$this->load->model("telegram_model");
//$this->telegram_model->drop_telegram_table("ptyuaygbje");
$this->telegram_model->delete_telegram_db("telebot_UaferEjB8F");
//foreach()
//print_r($decoded);
	}
	public function userslist(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
			$this->load->model("finance_model");
			$this->load->model("users_model");
			$this->load->model("telegram_model");
			$this->load->model("botgroup_model");
			$bazaryabes = $this->finance_model->get_shokra_bazaryab(2);
			$bots_group = $this->botgroup_model->get_botgroup();
						if($this->input->get("page") && $this->input->get("limit")){
			$page = $this->input->get("page");
			$limit = $this->input->get("limit");
			}else{
				$page = 1;
				$limit = 10;
			}
			$offset = ($limit * ($page-1));
			if($this->input->get("search")){
			$results = $this->users_model->users_list($limit,$offset,$this->input->get("search"));
			}else{
			$results = $this->users_model->users_list($limit,$offset+1);
			}
			foreach($results as $value){
				$finance = $this->finance_model->get_finance_main($value->id);
				$telegram_token = $this->telegram_model->get_bot_url($value->id);
				if(count($telegram_token) > 0){
				$value->tokenkey = $telegram_token[0]->tokenkey;
				$value->botgroup = $telegram_token[0]->botgroup;	
				}else{
				$value->tokenkey = "";
				$value->botgroup = "-1";	
				}
				
				if(count($finance)>0){
				$bazaryab_Code = $finance[0]->bazar_yab_code;
				}else{
					$bazaryab_Code =0;
				}
				if($bazaryab_Code == 0){
				$value->bazaryab = "بدون بازاریاب";	
				}else{
				$bazaryab_name = $this->finance_model->get_bazaryab_byid($bazaryab_Code);
				if(count($bazaryab_name) > 0){
				$value->bazaryab = $bazaryab_name[0]->name;
				}else{
				$value->bazaryab = "بدون بازاریاب";
				}
				}
				

				
	
			}
			if($this->input->get("search")){
				$count = $this->users_model->users_list("null","null",$this->input->get("search"));
			}else{
			$count = $this->users_model->users_list();
			$count--;	
			}
			
			$this->load->view('admin/listsuser',array("username" =>$this->session->userdata("username"),"fullname"=>$this->session->userdata("fullname")
							,"result"=>$results,"limit"=>$limit,"offset"=>$offset,"counter"=>$count,"today"=>$this->persiandate->jdate("Ymd","","",'Asia/Tehran',"en")
													  ,"bazaryabs"=>$bazaryabes,"robats"=>$bots_group
													  ));	
			}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
	}
	public function createuser(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
			$this->load->model("users_model");
			$this->load->model("botgroup_model");
			$this->load->model("finance_model");
			$bazaryabes = $this->finance_model->get_shokra_bazaryab(2);
			$bots_group = $this->botgroup_model->get_botgroup();
			$drs = $this->users_model->get_doctors();
			$this->load->view('admin/create_user',array("username" =>$this->session->userdata("username"),"fullname"=>$this->session->userdata("fullname")
							,"doctors" =>$drs,"bogroup"=>$bots_group,"bazaryab"=>$bazaryabes
													  ));	
			}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
	}
	public function create_users_control(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
				$this->load->model("users_model");
				$this->load->model("telegram_model");
				$this->load->model("finance_model");
				$expire = $this->input->post("expdate");
				@(string)$expire = $expire[6].$expire[7].$expire[8].$expire[9].$expire[3].$expire[4].$expire[0].$expire[1];
				$expire = (int)$expire;
				if($this->input->post("whatbot") == -1 || $this->input->post("permision") == 3){
					$bot_group = -1;
					$bot_address = "null";
					$internal_permision = $this->input->post("drs_add");
					if($internal_permision != ""){
						$internal_permision = rtrim($internal_permision,",");
					}else{
						$internal_permision = "null";
					}
				}else{
					$bot_address = $this->input->post("address_bot");
					$bot_group =$this->input->post("whatbot");
					$internal_permision = "null";
					
				}
				if($this->input->post("typeofpay") == 0 && $this->input->post("permision") != 3){
					$expire = $expire;
				}else{
					if($this->input->post("typeofpay") == 2 && $this->input->post("permision") != 3){
					$expire = $expire;
					}else{
					$expire = 2000099999;	
					}
				}
				if($this->input->post("permision") == 3){
					$typeofpay = -1;
				}else{
					$typeofpay = $this->input->post("typeofpay");
				}
				
				$data =array("username"=>$this->input->post("username"),"password"=>md5($this->input->post("pass")),"full_name"=>$this->input->post("fullname"),"expire"=>$expire,"internal_permision"=>$internal_permision,"permision"=>$this->input->post("permision"),"sybmitdate"=>$this->persiandate->jdate("Y/m/d","","",'Asia/Tehran',"en"),
							 "mo_number"=>$this->input->post("mo_number"),"abuneman_type"=>$typeofpay);
			 @$type_id = $this->users_model->insert_user($data);
			 $lastid = $this->users_model->getLastInserted();
			 $startdate = $this->persiandate->jdate("Y/m/d","","",'Asia/Tehran',"en");
			 $price = $this->input->post("price");
			 $percent = $this->input->post("bazaryab_percent");
			 $biziryab_price = ($price * $percent)/100;
			 $shoraka_price = $price - $biziryab_price;
			 $data_finance =array("uid"=>$lastid,"date"=>$startdate,"bazar_yab_code"=>$this->input->post("bazaryab_code"),"marketer_reward"=>$biziryab_price,"partners_profit"=>$shoraka_price,"bazaryab_percent"=>$percent);
			 $this->finance_model->set_finance_main($data_finance);
			 if($this->input->post("permision") == 2){
						$data = array("uid"=>$lastid,"about_us"=>"","phone"=>"","site"=>"","address"=>"","gioX"=>"","gioY"=>"");
						$this->telegram_model->set_user_info($data);
						mkdir("./assets/telegrams/".($lastid));
						$result = $this->telegram_model->get_telegram_bd_dymanic('telebot_'.$bot_group);
						foreach($result as $array){
							if($array->func == 1 || $array->func == 4){
								mkdir("./assets/telegrams/".($lastid)."/".$array->command);
							}
						}
						mkdir("./assets/telegrams/".($lastid)."/"."wellcome");
					}
			 if($this->input->post("permision") == 4 || $this->input->post("permision") == 1){
				mkdir("assets/data_images/drs/dr_galery/".$this->users_model->getLastInserted());
			 }
			 if($this->input->post("permision") == 4 || $this->input->post("permision") == 2 || $this->input->post("permision") == 1){
				if($bot_group != "-1"){
				$this->load->helper('string');
				$randstring = random_string('alnum', 30);
				$data = array("uid"=>$this->users_model->getLastInserted(),"tokenkey"=>$bot_address,"boturl"=>$randstring,"botgroup"=>$bot_group);
				$this->telegram_model->set_telinfo($data);
				$havebot = "yes";
				}
			 }
			 if(@$havebot == "yes"){
				if($bot_group == 1){
			$this->session->set_flashdata(array("type"=>"succ","isbot"=>"yes","active_link"=>"https://api.telegram.org/bot".$bot_address."/setWebhook?url=".base_url()."drtelegram/".$randstring));				
				}else{
			$this->session->set_flashdata(array("type"=>"succ","isbot"=>"yes","active_link"=>"https://api.telegram.org/bot".$bot_address."/setWebhook?url=".base_url()."telegram/".$randstring));		
				}
			
			 }else{
			$this->session->set_flashdata(array("type"=>"succ","isbot"=>"no"));	
			 }
			redirect("./admin/userslist?page=1&limit=10");	
			
			}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
	}
	public function update_user(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
		$this->load->model("users_model");
				$this->load->model("telegram_model");
				$this->load->model("finance_model");
				$changetoken = true;
				$expire = $this->input->post("expdate");
				$bot_group = $this->input->post("whatbot");
				$this->load->helper('directory');
				$typeofpay = $this->input->post("typeofpay");
				$id = $this->input->post("id");
				if($typeofpay == 0){
				@(string)$expire = $expire[6].$expire[7].$expire[8].$expire[9].$expire[3].$expire[4].$expire[0].$expire[1];
				$expire = (int)$expire;
				}elseif($typeofpay == 1){
					$expire = 2000099999;
				}elseif($typeofpay == 2){
					if($this->input->post("expdate") == ""){
					$timestamp = time();
					$timestamp = strtotime('+7 days', $timestamp);
					$expire = $this->persiandate->jdate("Ymd",$timestamp,"",'Asia/Tehran',"en");
					}else{
				@(string)$expire = $expire[6].$expire[7].$expire[8].$expire[9].$expire[3].$expire[4].$expire[0].$expire[1];
				$expire = (int)$expire;
					}
				}
				if($this->input->post("rotoken") != ""){
					
					if(count($this->telegram_model->get_bot_url($id)) > 0){
					$data = array("tokenkey"=>$this->input->post("rotoken"),"botgroup"=>$bot_group);	
					$this->telegram_model->update_token($id,$data);
					$changetoken = true;
					$token = $this->telegram_model->get_bot_url($id);
					$randstring = $token[0]->boturl;
					$bot_address = $this->input->post("rotoken");
					}else{
						$this->load->helper('string');
					$randstring = random_string('alnum', 30);
				$bot_address = $this->input->post("rotoken");
				$data = array("uid"=>$id,"tokenkey"=>$bot_address,"boturl"=>$randstring,"botgroup"=>$bot_group);
				$this->telegram_model->set_telinfo($data);
					}
				}else{
					$data = array("botgroup"=>$bot_group,"tokenkey"=>"");
					$this->telegram_model->update_token($id,$data);
				}
$this->load->helper("file");
				$last_user_data = $this->users_model->get_user_by_id($id);
				if($last_user_data[0]->permision == $this->input->post("permision")){
					
				}else{
					if($last_user_data[0]->permision == 4 || $last_user_data[0]->permision == 1){
						chmod("assets/data_images/drs/dr_galery/".$last_user_data[0]->id, 0777);   
							delete_files("assets/data_images/drs/dr_galery/".$last_user_data[0]->id, true);
					}elseif($last_user_data[0]->permision == 2){
						delete_files("./assets/telegrams/".$last_user_data[0]->id,true);
					}
					if($this->input->post("permision") == 4 || $this->input->post("permision") == 1){
					if (!file_exists("assets/data_images/drs/dr_galery/".$last_user_data[0]->id)) {
					mkdir("assets/data_images/drs/dr_galery/".$last_user_data[0]->id);
					}
					}else if($this->input->post("permision") == 2){
						if (!file_exists("./assets/telegrams/".$last_user_data[0]->id)) {
						mkdir("./assets/telegrams/".$last_user_data[0]->id);
						}
						$result = $this->telegram_model->get_telegram_bd_dymanic('telebot_'.$bot_group);
						foreach($result as $array){
							if($array->func == 1 || $array->func == 4){
								if (!file_exists("./assets/telegrams/".$last_user_data[0]->id."/".$array->command)) {
								mkdir("./assets/telegrams/".$last_user_data[0]->id."/".$array->command);
								}
							}
						}
						if (!file_exists("./assets/telegrams/".$last_user_data[0]->id."/"."wellcome")) {
						mkdir("./assets/telegrams/".$last_user_data[0]->id."/"."wellcome");
						}
					}
				}
				if($this->input->post("password") == ""){
					$data =array("expire"=>$expire,"permision"=>$this->input->post("permision"),
							"abuneman_type"=>$typeofpay);
				}else{
				$data =array("password"=>md5($this->input->post("password")),"permision"=>$this->input->post("permision"),"expire"=>$expire
							,"abuneman_type"=>$typeofpay);	
				}
				
			 @$type_id = $this->users_model->update_users($id,$data);
			  if(@$changetoken){
				if($bot_group == 1){
					if($bot_address != "" && $randstring != ""){
			$this->session->set_flashdata(array("type"=>"succup","isbot"=>"yes","active_link"=>"https://api.telegram.org/bot".$bot_address."/setWebhook?url=".base_url()."drtelegram/".$randstring));				
					}
				}else{
					if($bot_address != "" && $randstring != ""){
			$this->session->set_flashdata(array("type"=>"succup","isbot"=>"yes","active_link"=>"https://api.telegram.org/bot".$bot_address."/setWebhook?url=".base_url()."telegram/".$randstring));
					}
					else{
						$this->session->set_flashdata(array("type"=>"succup"));
					}
				}
			
			 }else{
			$this->session->set_flashdata(array("type"=>"succ","isbot"=>"no"));	
			 }
			redirect("./admin/userslist?page=1&limit=10");
			}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
	}
	public function delete_user_control(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
		$this->load->model("users_model");
		$this->load->model("telegram_model");
		if($this->input->post("id") == 1){
		echo "error";
		}else{
		$this->users_model->delete_users($this->input->post("id"));
		$this->telegram_model->delete_telegram_token($this->input->post("id"));
		echo "done";	
		}
		}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
		
	}
	public function create_bot(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
		$this->load->view('admin/createbot',array("username" =>$this->session->userdata("username"),"fullname"=>$this->session->userdata("fullname")
							));
		}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
	}
	public function group_list(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
		$this->load->model("botgroup_model");
		$result =  $this->botgroup_model->get_botgroup();
		$this->load->view('admin/listsbot',array("username" =>$this->session->userdata("username"),"fullname"=>$this->session->userdata("fullname"),
												 "botgroup"=>$result
							));
		}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
	}
	public function json_controler(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
		$this->load->model("botgroup_model");
		$this->load->model("telegram_model");
		$data = array("name"=>$this->input->post("name"),"name_id"=>$this->input->post("nameid"));
		$this->botgroup_model->add_botgroup($data);
		$this->load->helper('file');
		$data_post = json_decode($this->input->post("jsonfile"));
		$this->telegram_model->insert_telegram_table($this->input->post("nameid"));
		foreach($data_post as $value){
			if($value != ""){
			foreach($value as $val){
				if($val->btype == 1 || $val->btype == 2 || $val->btype == 4 ){
				$daynamic = 1;
				}else{
				$daynamic = 0;
				}
				$matreial = str_replace("option","",$val->bmatrial);
				if($val->bfunc == "null"){
					$command = $val->blink;
				}else{
					$command = $val->bfunc;
				}
				$data = array("name"=> $val->name,"level"=>$val->level,"command"=>$command,"func"=>$val->btype,"locate"=>$val->bpos,"isdiynamic"=>$daynamic,"material"=>$matreial,"text"=>"dsadsa");
				$this->telegram_model->set_telegram_db("telebot_".$this->input->post("nameid"),$data);
			}
			}
		}
    if ( ! write_file('./assets/jbot/'.$this->input->post("nameid").'.json', $this->input->post("jsonfile")))
    {
            echo 'Unable to write the file';
    }
    else
    {
            echo 'File written!';
    }
		echo "done";
		}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
	}
	public function delete_gbot_control(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
		$this->load->helper("file");
		$this->load->model("botgroup_model");
		$this->load->model("telegram_model");
		$id = $this->input->post("id");
		$idname = $this->botgroup_model->get_botgroup_file($id).".json";
		$idtable = $this->botgroup_model->get_botgroup_file($id);
		$path = "./assets/jbot/".$idname;
		@unlink($path);
		$this->botgroup_model->delete_bot($id);
		$this->telegram_model->drop_telegram_table($idtable);
		echo "done";
		}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
	}
	public function json_controler_edit(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
		$this->load->model("botgroup_model");
		$this->load->model("telegram_model");
		$id = $this->input->post("id");
		$file_name = $this->botgroup_model->get_botgroup_file($id).".json";
		$nameid = $this->botgroup_model->get_botgroup_file($id);
		
		$data = array("name"=>$this->input->post('name'));
		$this->botgroup_model->update_botgroup($id,$data);
		$data = array("name"=>$this->input->post("name"),"name_id"=>$this->input->post("nameid"));
		$data_post = json_decode($this->input->post("jsonfile"));
		
		$this->telegram_model->delete_telegram_db("telebot_".$nameid);
		$users_use_this_bot = $this->telegram_model->get_users_group($nameid);
		foreach($data_post as $value){
			if($value != ""){
			foreach($value as $val){
				if($val->btype == 1 || $val->btype == 2 || $val->btype == 4){
				$daynamic = 1;
				if($val->btype == 1 ||  $val->btype == 4){
				foreach($users_use_this_bot as $value){
					if (!is_dir('./assets/telegrams/'.$value->uid."/".$val->bfunc)) {
               mkdir('./assets/telegrams/'.$value->uid."/".$val->bfunc, 0777, TRUE);

            }
				}
				}
				}else{
				$daynamic = 0;
				}
				$matreial = str_replace("option","",$val->bmatrial);
				if($val->bfunc == "null"){
					$command = $val->blink;
				}else{
					$command = $val->bfunc;
				}
				$data = array("name"=> $val->name,"level"=>$val->level,"command"=>$command,"func"=>$val->btype,"locate"=>$val->bpos,"isdiynamic"=>$daynamic,"material"=>$matreial,"text"=>"dsadsa");
				$this->telegram_model->set_telegram_db("telebot_".$nameid,$data);
			}
			}
		}
		
		$this->load->helper('file');
    if ( ! write_file('./assets/jbot/'.$file_name, $this->input->post("jsonfile")))
    {
            echo 'Unable to write the file';
    }
    else
    {
            echo 'File written!';
    }
		echo "done";
		}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
	}
	public function edit_bot(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
		$id = $this->input->get("id");
		$this->load->model("botgroup_model");
		$filename = $this->botgroup_model->get_botgroup_file($id);
		$responseJSON = file_get_contents("./assets/jbot/".$filename.".json");
		$decoded=json_decode($responseJSON);
		$result = $this->botgroup_model->get_botgroup_id($id);
		$this->load->view('admin/editbot',array("username" =>$this->session->userdata("username"),"fullname"=>$this->session->userdata("fullname"),
												 "botinfo"=>$result,"botmenuinfo"=>$decoded,"jsonfile"=> preg_replace('/\s\s+/', ' ',$responseJSON)
							));
		}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
	}
	public function create_instanse(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
		$id = $this->input->get("id");
		$this->load->model("botgroup_model");
		$filename = $this->botgroup_model->get_botgroup_file($id);
		$responseJSON = file_get_contents("./assets/jbot/".$filename.".json");
		$decoded=json_decode($responseJSON);
		$result = $this->botgroup_model->get_botgroup_id($id);
		$this->load->view('admin/create_instense',array("username" =>$this->session->userdata("username"),"fullname"=>$this->session->userdata("fullname"),
												 "botinfo"=>$result,"botmenuinfo"=>$decoded,"jsonfile"=> preg_replace('/\s\s+/', ' ',$responseJSON)
						));
				}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
	}
	public function send_finance(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
		$this->load->model("finance_model");
		$uid = $this->input->post("uid");
		$result = $this->finance_model->get_finance_main($uid);
		
		if(count($result) > 0){
			
			foreach($result as $value){
		$code_number = $value->bazar_yab_code;
		if($code_number != "0"){
		$bazaryabname = $this->finance_model->get_bazaryab_byid($code_number);
		if(count($bazaryabname) > 0){
		$bazaryabname = $bazaryabname[0]->name;
		
		$value->bazaryab_name = $bazaryabname;
		}else{
		$value->bazaryab_name = "بدون بازاریاب";
		}
		}else{
			$value->bazaryab_name = "بدون بازار یاب";
		}
		
			}
		}
		
		echo json_encode($result);
		}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
	}
	public function recharge_account(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
		$this->load->model("users_model");
		$this->load->model("finance_model");
		$id = $this->input->post("id");
		$date = $this->input->post("date");
		$price = $this->input->post("price");
		$bid =  $this->input->post("bid");
		if($bid != "0"){
		$result = $this->finance_model->get_bazaryab_byid($bid);
		$percent = $result[0]->percent;
		$bazaryab_price = ($price * $percent)/100;
		$shoraka_price = $price - $bazaryab_price;
		}else{
			$percent = 0;
			$bazaryab_price = 0;
			$shoraka_price = $price;
		}
		$date = $date[6].$date[7].$date[8].$date[9].$date[3].$date[4].$date[0].$date[1];
		$data = array("expire"=>$date);
		$this->users_model->update_users($id,$data);
		$data_finance = array("uid"=>$id,"date"=>$this->persiandate->jdate("Y/m/d","","",'Asia/Tehran',"en"),"bazar_yab_code"=>$bid,"bazaryab_percent"=>$percent,"marketer_reward"=>$bazaryab_price
							  ,"partners_profit"=>$shoraka_price
							  );
		$this->finance_model->set_finance_main($data_finance);
		echo "done";
		}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
	}
	public function set_members_partner(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
		$this->load->model("finance_model");
		$bazaryab = $this->finance_model->get_shokra_bazaryab(2);
		$shoraka = $this->finance_model->get_shokra_bazaryab(1);
		$this->load->view('admin/set_shokra',array("username" =>$this->session->userdata("username"),"fullname"=>$this->session->userdata("fullname"),
						"shoraka"=>$shoraka
												 
						));
		}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
	}
	public function set_members_marcketer(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
		$this->load->model("finance_model");
		$bazaryab = $this->finance_model->get_shokra_bazaryab(2);
		$shoraka = $this->finance_model->get_shokra_bazaryab(1);
		$this->load->view('admin/set_bazaryab',array("username" =>$this->session->userdata("username"),"fullname"=>$this->session->userdata("fullname"),
						"bazaryab"=>$bazaryab
												 
						));
		}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
	}
	public function set_sharik(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
		$this->load->model("finance_model");
		$this->finance_model->clear_shokra_bazaryab(1);
		for($i=1; $i <= $this->input->post("number");$i++ ){
		if($this->input->post("sharikname".$i) !="" && $this->input->post("sahmpercent".$i) !=""){
			$data = array("type"=>1,"name"=>$this->input->post("sharikname".$i),"percent"=>$this->input->post("sahmpercent".$i));
			$this->finance_model->add_shokra_bazaryab($data);
		}
		
	}
	redirect("./admin/set_members_partner");
	}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
	}
	public function set_bazaryab(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
		$this->load->model("finance_model");
		$this->finance_model->clear_shokra_bazaryab(2);
		for($i=1; $i <= $this->input->post("number");$i++ ){
		if($this->input->post("sharikname".$i) !="" && $this->input->post("sahmpercent".$i) !=""){
			$data = array("type"=>2,"name"=>$this->input->post("sharikname".$i),"percent"=>$this->input->post("sahmpercent".$i));
			$this->finance_model->add_shokra_bazaryab($data);
		}
		
	}
	redirect("./admin/set_members_marcketer");
	}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}
	}
	public function comming_soon(){
		if($this->session->userdata("canlogin") == "yes"){
			if($this->session->userdata("browser") == $this->agent->browser() && $this->session->userdata("version") == $this->agent->version() && $this->session->userdata("ip") == $this->input->ip_address() && $this->session->userdata("per") == "5"){
				$this->load->view('plugins/cunstroct');
				}else{
				$this->session->sess_destroy();
				redirect("./");
			}
			
		}else{
			$this->session->sess_destroy();
			redirect("./");
		}

	}

}
?>