<?php



namespace App\Controllers;

use App\Models\Member_model;

use App\Models\User_model;

use App\Models\Dashboard_model;

use App\Models\Common_model;

use App\Models\Member_credits_model;

use App\Models\Packages_and_pricing_model;

use App\Models\Cart_model;

use App\Models\Credits_usage_tracking_model;

use Config\Services;

use App\Libraries\SquareService;

use App\Models\Cart_token_model;

use App\Models\Member_address_model;

use App\Models\Transaction_model;

use App\Models\Rule_model;

use App\Models\Zipcode_model;

use App\Models\Country_model;

class Member_controller extends BaseController

{

	public $session;

	public function __construct()

	{

	

		$this->session = session();

		$this->MemberModel = new Member_model();

		$this->UserModel = new User_model();

		$this->dModel = new Dashboard_model();

		$this->cModel = new Common_model();

		$this->creditsModel = new Member_credits_model();

		$this->PackagesModel = new Packages_and_pricing_model();

		$this->cartModel = new Cart_model();

		$this->CreditsUsageTrackingModel = new Credits_usage_tracking_model();

		$this->Cart_token_model = new Cart_token_model();

		$this->MemberAddressModel = new Member_address_model();

		$this->TransactionModel = new Transaction_model();

		$this->RuleModel = new Rule_model();

		$this->ZipcodeModel = new Zipcode_model();

		$this->CountryModel = new Country_model();

		helper('form');
		
	}

    public function index()

    {

		$data=[];

		

		

		if($this->request->getmethod() == 'post')

		{

			$email = $this->request->getVar('email');

			//echo $email; die();

			$password = $this->request->getVar('password');

			$memberdata = $this->MemberModel->verifyEmail($email);

			

			

			if($memberdata)

			{

				if ($memberdata['is_email_verified']) 

				{

					if ($memberdata['is_active'] == 0) 
					{
						$this->session->setTempdata('error','User id or password is invalid',3);
						$this->session->setTempdata('l_email',$this->request->getVar('email'),3);
						$this->session->setTempdata('l_password',$this->request->getVar('password'),3);
						return redirect()->to(base_url());
					}	

					if (password_verify($password, $memberdata['password'])) 

					{
						
						$token = $this->session->get("token");
						 
						$this->session->set('logged_user',$memberdata['id']);

						$this->session->set('logged_user_name',$memberdata['name']);

						$this->session->set('logged_user_email',$memberdata['email']);

						if ($token)

						{
							$tokendata =  $this->Cart_token_model->where('md5(token)', $token )->findAll();

							foreach ($tokendata as $value) 

							{  

                                $data =  $this->cartModel->where('pack_id', $value['primary_id'] )->where('member_id', $this->session->get('logged_user') )->find();
								if($data)
								{
									$successdata = $this->cartModel->where('pack_id', $value['primary_id'] )->where('member_id', $this->session->get('logged_user') )->set(array( 'count' => $data[0]['count'] + 1 ))->update(); 
									if ($successdata) 
									{
										$deletedata =  $this->Cart_token_model->where('id', $value['id'] )->delete();
									} 
								}
								else
								{
									$data['member_id'] = $memberdata['id'];

									$data['pack_id'] = $value['primary_id'];
	
									$data['count'] = $value['count'];
	
									$successdata = $this->cartModel->save($data);
									if ($successdata) 
									{
										$deletedata =  $this->Cart_token_model->where('id', $value['id'] )->delete();
									} 
								}

							}

							return redirect()->to(base_url().'payment');

						} 

						else 

						{

							// $this->member_dashboard();
                            
							 return redirect()->to(base_url().'member_home');

						}

						

					}

					else{

						$this->session->setTempdata('error','User id or password is invalid',3);
						$this->session->setTempdata('l_email',$this->request->getVar('email'),3);
						$this->session->setTempdata('l_password',$this->request->getVar('password'),3);
						return redirect()->to(base_url());

					}

				}

				else{

					$this->session->setTempdata('error','User id or password is invalid',3);
					$this->session->setTempdata('l_email',$this->request->getVar('email'),3);
					$this->session->setTempdata('l_password',$this->request->getVar('password'),3);
					return redirect()->to(base_url());

				}

				

			}else

			{

			  $this->session->setTempdata('error','User id or password is invalid',3);
			  $this->session->setTempdata('l_email',$this->request->getVar('email'),3);
			  $this->session->setTempdata('l_password',$this->request->getVar('password'),3);
			  return redirect()->to(base_url());				  

			}

	

		}else{ // get method

				//return view('member_login');

				if(session()->has('logged_user'))

				{

					return redirect()->to(base_url().'member_home');

					//$this->member_dashboard();

				}

				else

				{

					return view('myaccount');



				}

		}



		

    }



	public function logout()

	{

		session()->remove('logged_user');
		session()->remove('token');
		if(!session()->get('logged_admin_user'))
		{
			session()->destroy();
		}

		return redirect()->to(base_url());

	}



	public function register()

	{

		if($this->request->getmethod() == 'post')
		{

			$check_if_email_exist =  $this->MemberModel->where('email',$this->request->getVar('email'))->find();
			if($check_if_email_exist)
			{
				return redirect()->to(base_url());

			}else{

				//insert member details
				$MemberData = $this->request->getVar();
				$MemberData['is_email_verified'] = 1;
				// Generate a random verification code
				$verification_code = rand(10 , 99 ).rand(10 , 99 ).rand(10 , 99 );
				$MemberData['verification_code'] = $verification_code;
				$insert_data = $this->MemberModel->save($MemberData);
				if($insert_data)
				{
					$sendmail_controller = new Sendmail_controller();
					$sendmail_controller->index($this->request->getVar('email') , $MemberData['verification_code'] , 1);
					
					$admin_email = $this->RuleModel->select('value')->where('rule_key', 9 )->get()->getRow()->value;
					$sendmail_controller = new Sendmail_controller();
					$sendmail_controller->index($admin_email , $this->request->getVar('email') , 8 ); 

					$this->session->setTempdata('mail_success', "Registration Successfull",3);
					return redirect()->to(base_url());
				}
				else 
				{
					// Show error message
					echo 'Error in registering user. Please try again.';
				}
			}
				// return redirect()->to(base_url());
		}
		// $data['country']=$this->cModel->fetch_country();
		// return view('member_registration',$data);
	}





	public function members_list()
	{
		$id = session()->get('logged_admin_user');
		$userdata['userdata']=$this->dModel->getLoggedInUserData($id);
		$data['Membersdata'] = [];
	
		$Membersdata =  $this->MemberModel->findAll();
		foreach ($Membersdata as $Membersdataindex => $MembersdataValue) {

			$credits = $this->creditsModel->selectSum('running_balance')->groupBy('member_id')->where('member_id',$MembersdataValue['id'])->where('is_active',1)->find();
			if(count($credits)){ $MembersdataValue['credits'] = $credits[0]['running_balance']; }else{ $MembersdataValue['credits'] = 0; }
			
			array_push($data['Membersdata'] , $MembersdataValue);
		}
		// echo '<pre>';
		// print_r($data['Membersdata']);
		// echo '</pre>'; die();
		//$data['Membersdata'] =  $this->MemberModel->findAll();
		echo view('layout/header',$userdata);
		echo view('members_list',$data);
		echo view('layout/footer');

	}



	public function member_deactivate($id = null)
	{
		$this->MemberModel->where('md5(id)', $id )->set(array( 'is_active' => 0))->update();
		return redirect()->to(base_url()."members_list");

	}	



	public function member_activate($id = null)
	{
		$this->MemberModel->where('md5(id)', $id )->set(array( 'is_active' => 1))->update();
		return redirect()->to(base_url()."members_list");
	}





	public function member_edit($member_id = null)

	{



		if($this->request->getmethod() == 'post')

		{
			
				//update member details
               if($this->request->getVar('country') == 'Canada'){ $state = $this->request->getVar('state_canada'); }else{ $state = $this->request->getVar('state');}
			   $MemberData = $this->request->getVar();
			   unset($MemberData['state']);
			   unset($MemberData['state_canada']);
			   $MemberData['state'] = $state;
			   $member_id =  $this->request->getVar('id');
			   $this->MemberModel->where('id', $member_id )->set($MemberData)->update();
			   return redirect()->to(base_url()."members_list");

			

		}



		$logged_user_id = session()->get('logged_admin_user');

		$userdata['userdata']=$this->dModel->getLoggedInUserData($logged_user_id);

		$zip = $this->ZipcodeModel->findAll();

		$output = '<option value="">Select Zipcode</option>';

		foreach($zip as $row)

		{
		$output .= '<option value="'.$row['id'].'">'.$row['code'].'</option>';

		}

		$data['zipcode'] = $output;

		$data['Memberdata'] =  $this->MemberModel->select('member.* , zipcode.code as zipcode')

												->join('zipcode', 'member.pincode = zipcode.id', 'left')

												->where('md5(member.id)', $member_id )

												->find();
// print_r($data['Memberdata']);die();
											
		$data['country'] = $this->CountryModel->findAll();

		echo view('layout/header',$userdata);

		echo view('member_form',$data);

		echo view('layout/footer');

		

	}



	public function member_add()

	{



		if($this->request->getmethod() == 'post')

		{
			$check_if_email_exist =  $this->MemberModel->where('email',$this->request->getVar('email'))->find();
			if($check_if_email_exist)
			{
				return redirect()->to(base_url()."members_list");

			}else{
				//insert member details

				if($this->request->getVar('country') == 'Canada'){ $state = $this->request->getVar('state_canada'); }else{ $state = $this->request->getVar('state');}
				$MemberData = $this->request->getVar();
				unset($MemberData['state']);
				unset($MemberData['state_canada']);
				$MemberData['state'] = $state;

				// Generate a random verification code

				$verification_code = rand(10 , 99 ).rand(10 , 99 ).rand(10 , 99 );

				$MemberData['verification_code'] = $verification_code;

				$insert_data = $this->MemberModel->save($MemberData);

				if($insert_data)

				{

					// Send the verification email

					$sendmail_controller = new Sendmail_controller();

					$sendmail_controller->index($this->request->getVar('email') , $MemberData['verification_code'] , 1);

				}

				return redirect()->to(base_url()."members_list");

		}

		}

		

		$userdata['userdata']=$this->dModel->getLoggedInUserData(session()->get('logged_admin_user'));

		$zip = $this->ZipcodeModel->findAll();

		$output = '<option value="">Select Zipcode</option>';

		foreach($zip as $row)

		{

		$output .= '<option value="'.$row['id'].'">'.$row['code'].'</option>';

		}

		$data['zipcode'] = $output;

		$data['country'] = $this->CountryModel->findAll();

		echo view('layout/header',$userdata);

		echo view('member_form',$data);

		echo view('layout/footer');

		

	}



	public function forgot_password()

	{

		if($this->request->getmethod() == 'get')

		{

		   return view('forgot_password.php');

		}

		else if($this->request->getmethod() == 'post')

		{         



			$email = $this->request->getVar('email');

			$user_data = $this->MemberModel->where('email', $email)->find();

			echo $user_data;

			// echo $user_data; 

			if ($user_data) 

			{

				$otp = rand(1000 , 9999 );

				// $sendmail_controller = new Sendmail_controller();

				$get_data = $Sendmail_controller->index($this->request->getVar('email') , $otp , 2 ); 

				$data['status'] = 'success';

				$data['otp'] = $otp;

				echo json_encode($data);

			} 

			else 

			{

				$data['status'] = 'failed';

				echo json_encode($data);

			}

			 

		}

	}



	public function change_password_form()

	{

		if($this->request->getmethod() == 'post')

		{

			$otp = $this->request->getVar('otp');

			$verify_otp = $this->request->getVar('verify_otp');

			if ($this->request->getVar('user') == 'user') 

			{

				if ($otp == $verify_otp) 

				{

					$email =  $this->request->getVar('email');

					$data = $this->UserModel->where('email', $email)->find();

					if ($data) 

					{

						$this->session->setTempdata('id',$data[0]['id'],3);

						$this->session->setTempdata('email',$data[0]['email'],3);

						$this->session->setTempdata('mail_success', "OTP verified sucessfully",3);

						return view('set_password.php');

					} 

				} 

				else 

				{

					$this->session->setTempdata('error', "You entered wrong OTP.Please retry",3);

					return view('forgot_password.php');

				}

			} 

			else 

			{

				if ($otp == $verify_otp) 

				{

					$email =  $this->request->getVar('email');

					$data = $this->MemberModel->where('email', $email)->find();

					if ($data) 

					{

						$this->session->setTempdata('id',$data[0]['id'],3);

						$this->session->setTempdata('email',$data[0]['email'],3);

						$this->session->setTempdata('mail_success', "OTP verified sucessfully",3);

						return view('changepassword.php');

					} 

				} 

				else 

				{

					$this->session->setTempdata('error', "You entered wrong OTP.Please retry",3);

					$this->session->setTempdata('email', $this->request->getVar('email') ,3);
					$this->session->setTempdata('otp', $this->request->getVar('verify_otp') ,3);

					return view('password_match.php');

				}

			}

		}

	}





	public function set_password($id)

	{

		if($this->request->getmethod() == 'get')

		{

		   $user_data = $this->MemberModel->where('md5(id)', $id)->find();

		   $this->session->setTempdata('id',$user_data[0]['id'],3);

		   $this->session->setTempdata('email',$user_data[0]['email'],3);

		   if(!$user_data[0]['password'] == null)

		   $this->session->setTempdata('mail_success', "Email verification sucessfull",3);

		   else

		   $this->session->setTempdata('mail_success', "Email verified . Please set password",3);

		   return view('changepassword.php');

		}

	}



	public function update_password()

	{

		if($this->request->getmethod() == 'post')

		{

			if ($this->request->getVar('user') == 'user') 

			{

				$userData = $this->request->getVar();

				$user_id =  $this->request->getVar('id');

				$this->UserModel->where('id', $user_id )->set($userData)->update();

				$this->session->setTempdata('mail_success', "Password set sucessfully",3);

				return redirect()->to(base_url()."admin");

			}

			else

			{

				$MemberData = $this->request->getVar();

				$member_id =  $this->request->getVar('id');

				$this->MemberModel->where('id', $member_id )->set($MemberData)->update();

				$this->session->setTempdata('mail_success', "Password set sucessfully",3);

				return redirect()->to(base_url());

			}

		}

	}









	public function buy_pack()

	{

		//insert member details

		$array = $this->request->getVar();

		//print_r($array);die();

		$newArray = array();

		foreach ($array as $key => $value) {

		$newKey = substr($key, 0, -2);

		$newArray[$newKey] = $value;

		}

		//print_r($newArray);die();

		$packageData = $this->PackagesModel->where('id',$newArray['pack_id'])->find();

		//print_r($packageData);die();

		$today = date("Y-m-d");

		

		$credits_data['member_id'] = $newArray['member_id'];

		$credits_data['package_name'] = $packageData[0]['package_name'];

		$credits_data['package_type'] = $packageData[0]['package_type'];

		$credits_data['credit_points'] = $packageData[0]['credit'] * $newArray['pack_count'];

		$credits_data['running_balance'] = $packageData[0]['credit'] * $newArray['pack_count'];

		$credits_data['expired_points'] = 0;

		$credits_data['expiring_date'] = date('Y-m-d', strtotime('+1 month', strtotime($today)));

		$credits_data['cost'] = $packageData[0]['cost'];

	   //print_r($credits_data);die();

		$insert_data = $this->creditsModel->save($credits_data);

		if($insert_data)

			{

				echo true;

			}else{

				echo false;

			  }

		

		

	}





	

	public function use_points()

	{

		

		$FormData = $this->request->getVar();

		



		//print_r($FormData);die();

		$needed_points = $FormData['points'];

		

		$count = $this->creditsModel->selectSum('running_balance')->where('member_id',$FormData['member_id'])->get()->getRow()->running_balance;

		if($count > $needed_points)

		{

			$creditsData = $this->creditsModel->where('member_id',$FormData['member_id'])->findAll();

			//$newArray = array();

			foreach ($creditsData as $creditsDataindex => $creditsDataValue) {

				if($creditsDataValue['expiring_date'] > date("Y-m-d"))

				{

					$temp_needed_points = $needed_points;

					if($creditsDataValue['running_balance'] != 0 && $needed_points > 0)

					{

	

						$needed_points = ($needed_points - $creditsDataValue['running_balance']);

						if($needed_points < 0 ){ 

							$running_balance =    $creditsDataValue['running_balance'] - $temp_needed_points;

							$this->update_credit_package_data($creditsDataValue['id'] ,  array( 'running_balance'=>$running_balance , 'used_points'=> $creditsDataValue['used_points'] + $creditsDataValue['running_balance'] - $running_balance));

							$this->CreditsUsageTrackingModel->save(array('member_id'=>$FormData['member_id'],'member_credits_id'=>$creditsDataValue['id'], 'used_points'=>($creditsDataValue['running_balance'] - $running_balance),'slot_date_time'=>date("Y-m-d")));

						 }else { $running_balance = 0; 

							$this->update_credit_package_data($creditsDataValue['id'] ,  array( 'running_balance'=>$running_balance , 'used_points'=> $creditsDataValue['used_points'] + $creditsDataValue['running_balance'] - $running_balance));

							$this->CreditsUsageTrackingModel->save(array('member_id'=>$FormData['member_id'],'member_credits_id'=>$creditsDataValue['id'], 'used_points'=>($creditsDataValue['running_balance'] - $running_balance),'slot_date_time'=>date("Y-m-d")));

						}

						//$newArray[$creditsDataValue['running_balance']] = array('needed_points'=>$needed_points , 'running_balance'=>$running_balance , 'used_points'=> $creditsDataValue['running_balance'] - $running_balance);

	

					}else{

					

					}

	

				}else{

					

				}

	

			}

			

			 echo true;



		}else{

          echo "u need to purchase points";

		}



	

		

		

	}



	public function update_credit_package_data($id , $array)

	{

		$this->creditsModel->where('id', $id )->set($array)->update();

	}





	public function add_to_cart()

	{

		//insert member details

		$array = $this->request->getVar();

		//print_r($array);die();

		$newArray = array();

		foreach ($array as $key => $value) {

		$newKey = substr($key, 0, -2);

		$newArray[$newKey] = $value;

		}

		//print_r($newArray);die();

		$insert_data = $this->cartModel->save($newArray);

		if($insert_data)

			{

				echo true;

			}else{

				echo false;

			  }

	}



	public function cart_checkout()

	{



		$CartData = $this->cartModel->where('member_id',session()->get('logged_user'))->findAll();



		

		$today = date("Y-m-d");

		foreach ($CartData as $creditsDataindex => $creditsDataValue) 

		{

		$packageData = $this->PackagesModel->where('id',$creditsDataValue['pack_id'])->find();

		

		$credits_data['member_id'] = session()->get('logged_user');

		$credits_data['package_name'] = $packageData[0]['package_name'];

		$credits_data['package_type'] = $packageData[0]['package_type'];

		$credits_data['credit_points'] = $packageData[0]['credit'] ;

		$credits_data['running_balance'] = $packageData[0]['credit'] ;

		$credits_data['expired_points'] = 0;

		$credits_data['expiring_date'] = date('Y-m-d', strtotime('+1 month', strtotime($today)));

		$credits_data['cost'] = $packageData[0]['cost'];

		$insert_data = $this->creditsModel->save($credits_data);

		if($insert_data)

			{

				$this->cartModel->where('id',$creditsDataValue['id'])->delete();

			}

	    }

		echo true;

	}





	public function scheduler()

	{

		

		$Data = $this->creditsModel->select('member_credits.* , member.name as member_name , member.email as member_email ')

		                           ->join('member', 'member_credits.member_id = member.id', 'left')

		                           ->findAll();

								 

		foreach ($Data as $index => $Value) 

	    {

			if(strtotime('-3 days', strtotime($Value['expiring_date'])) == strtotime(date("Y-m-d")))

			{

				$sendmail_controller = new Sendmail_controller();

				$message = 'Hai '. $Value['member_name']. ' Your pack will expire with in three days';

				$sendmail_controller->index($Value['member_email'] ,  null , 5); 

			}

	    }

		

	}







	public function member_address_form()

    {

		

      $id = $this->request->getVar('id');

	  $data['userAddress'] = $this->MemberAddressModel->where('member_id',session()->get('logged_user'))->where('id',$id)->find();

	  echo view('member_address_form',$data);



    }



	public function member_address_edit()

    {

		

		$array = $this->request->getVar();

		$id =  $this->request->getVar('id');

		$update =$this->MemberModel->where('id', $id )->set($array)->update();

	    echo true;

	 



    }

	public function member_password_edit()

    {	
		$id =  $this->session->get('logged_user');
		$array = $this->request->getVar();
		$old_password = $this->request->getVar('old_password');
		$data =$this->MemberModel->where('id', $id )->find();

		if(password_verify($old_password , $data[0]['password']))
		{
			$update =$this->MemberModel->where('id', $id )->set($array)->update();
			// return redirect()->to(base_url()."member_logout");
			echo 1;
		}
		else
		{
			echo 0;
		}
    }



	public function check_old_password()

    {

		$id =  $this->request->getVar('id');

		$old_password = $this->request->getVar('old_password');

		$data =$this->MemberModel->where('id', $id )->find();

	     if(password_verify($old_password , $data[0]['password']))
			echo 1;
		 else
		    echo 0;
    }



	public function member_dashboard()

	{

		if(!session()->has('logged_user'))

		{

		 return redirect()->to(base_url());

		}

		$creditsData = $this->creditsModel->where('member_id',$this->session->get('logged_user'))->where('is_active',1)->orderBy('id' , 'ASC')->findAll();

		$recent_expiry = $this->creditsModel->select('expiring_date')->selectSum('running_balance', 'no_of_credits')->groupBy('expiring_date')->orderBy('expiring_date', 'asc')->where('member_id',$this->session->get('logged_user'))->where('is_active',1)->first();

		if($recent_expiry){

			$data['no_of_credits'] = $recent_expiry['no_of_credits'];

			$data['expiring_date'] = $recent_expiry['expiring_date'];

		}else{

			$data['no_of_credits'] = '';

			$data['expiring_date'] = '';

		}

		

		

		$data['credit_data'] = [];

		$data['avaiable_credit'] = 0;

		// $data['no_of_credits'];

		// $data['expiry_date'];

		$data['profile'] = $this->MemberModel->select('member.* , zipcode.code as zipcode')

											->join('zipcode', 'member.pincode = zipcode.id', 'left')

											->where('member.id',  session()->get('logged_user'))

											->find();

		$data['country'] = $this->CountryModel->findAll();

	    foreach ($creditsData as $creditsDataindex => $creditsDataValue) 

		{

			

			if($creditsDataValue['running_balance'] + $creditsDataValue['used_points'] == 0)
			{
				$data['avaiable_credit'] = $data['avaiable_credit'] - $creditsDataValue['credit_points'];
				$creditsDataValue['avaiable_credit'] = $data['avaiable_credit'];
				array_push($data['credit_data'] , $creditsDataValue);
			}
			else
			{
				$data['avaiable_credit'] = $data['avaiable_credit'] + $creditsDataValue['credit_points'];
				$creditsDataValue['avaiable_credit'] = $data['avaiable_credit'];
				array_push($data['credit_data'] , $creditsDataValue);
			}

	    	

	    }



		$TransactionData = $this->TransactionModel->where('member_id', session()->get('logged_user'))->orderBy('created_at','desc')->where('status', 'success')->findAll();

		$data['TransactionData'] = []; 

		foreach ($TransactionData as $TransactionDatakey => $TransactionDatavalue) {

			$credit_data =  $this->creditsModel->where('member_id', session()->get('logged_user'))

							->select('package_name')

							->select('cost')

							->select('credit_points')

							->select('expiring_date')

							->select('remark')
							
							->select('method')

							->selectSum('credit_points', 'creditPointsSum')

							->selectSum('cost', 'costSum')

							->selectCount('package_name', 'pack_count')

							->groupBy('package_name')

                            ->where('order_id',$TransactionDatavalue['order_id'])

						    ->where('is_active',1)

							->findAll();

			$TransactionDatavalue['credit_data'] = $credit_data;

			array_push($data['TransactionData'] ,$TransactionDatavalue);

		}

		$data['gst'] = $this->RuleModel->select('value')->where('rule_key', 3 )->get()->getRow()->value;

		$data['pst'] = $this->RuleModel->select('value')->where('rule_key', 4 )->get()->getRow()->value;

		$zip = $this->ZipcodeModel->findAll();

		$output = '<option value="">Select Zipcode</option>';

		foreach($zip as $row)

		{

		$output .= '<option value="'.$row['id'].'">'.$row['code'].'</option>';

		}

		$data['zipcode'] = $output;

		echo view('memberaccess',$data); //dashboard

	}



	public function zipcode_data()

    {

		$id =  $this->request->getVar('id');

		$data = $this->ZipcodeModel->where('id' , $id)->find();

	     if($data)

			echo json_encode($data);

		 else

		    echo 0;

    }





	

	

	// -----------------------------------------------

}

