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

class User_account extends CI_Controller {
 	 
	function __construct(){
		parent::__construct();
		$this->load->helper('adminlogin');
		checkIfLoggedIn($this->session->userdata('userid'));
		$this->data['session_details'] = $this->Login_mdl->adminlogin_details($this->session->userdata('userid'));
		$this->data['admin_type']=$this->data['session_details']->admin_type;
 		$this->data['success_msg']=$this->session->flashdata('success');
		$this->data['error_msg']=$this->session->flashdata('error');
		$this->data['title']='Users Account | Administrator Panel';
		$this->data['active_class']='user_account_menu';
		$this->load->model('Backend/User_account_mdl');
 	}	
	
	public function testing_sent_mail(){
		$message = 'Testing bosy message by ankit';
		$user_name='Ankit';
		$subject='testig subject';
		send_mail('info@savetrusts.online',$message,$user_name,'',$subject);
	}

 	public function index(){		
  		$this->data['page_title']='Users Listing';
		$this->data['student_listing'] = $this->User_account_mdl->admin_users_listing();
		$this->load->view('Backend/includes/header',$this->data); 
		$this->load->view('Backend/user_account/listing',$this->data);
		$this->load->view('Backend/includes/footer');	
	}
	
	public function edit(){		
 		$user_encrypt_id = $this->uri->segment(4);
		$this->form_validation->set_rules('customer_name','Customer Name','trim|required',array('required' => '%s cannot be blank.'));
 		if($this->form_validation->run() == FALSE){ 
 			$this->data['page_title']='User Account : : Edit';
			$this->data['user_details'] = $this->User_account_mdl->user_fulldetails($user_encrypt_id);
			$this->data['user_accounts_listing'] = $this->User_account_mdl->user_accounts_listing($this->data['user_details']->user_id);
			$this->load->view('Backend/includes/header',$this->data); 
			$this->load->view('Backend/user_account/edit',$this->data);
			$this->load->view('Backend/includes/footer');
 		}else{		
			$this->User_account_mdl->update_user_information($user_encrypt_id);
			redirect(base_url().$this->config->item('admin_directory_name').'/user_account');
		}				
	}
	
	public function create(){		
		$this->form_validation->set_rules('customer_name','Customer Name','trim|required',array('required' => '%s cannot be blank.'));		
		if($this->form_validation->run() == FALSE){ 		
			$this->data['page_title']='User Account : : Create'; 
			$this->load->view('Backend/includes/header',$this->data);
			$this->load->view('Backend/user_account/add',$this->data);
			$this->load->view('Backend/includes/footer');		
		}else{		
			$this->User_account_mdl->user_entry();
		}			
	}
	
	public function acct_entry(){
		$this->User_account_mdl->acct_entry();
		$user_encrypt_id = $this->input->post('h_user_encrypt_id');
		redirect(base_url().$this->config->item('admin_directory_name').'/user_account/edit/'.$user_encrypt_id);
	}
	
	public function generateRandomString($length = 10) {
		$characters = '123456789ABCDEFGHIJKLMNPQRSTUVWXYZ';
		$charactersLength = strlen($characters);
		$randomString = '';
		for ($i = 0; $i < $length; $i++) {
			$randomString .= $characters[rand(0, $charactersLength - 1)];
		}
		return $randomString;
	}
	
	public function credit_amt_entry(){
		$txn_id = $this->generateRandomString();
		$this->User_account_mdl->credit_amt_entry($txn_id);
		$user_encrypt_id = $this->input->post('h_user_encrypt_id');
		redirect(base_url().$this->config->item('admin_directory_name').'/user_account/edit/'.$user_encrypt_id);
	}
 	
	public function banks_details_popup(){
 	 	if(isset($_GET['user_id'])  && $_GET['user_id']!=''){
			$this->data_ajax['user_accounts_listing']=$this->User_account_mdl->user_accounts_listing($_GET['user_id']);
			$this->load->view('Backend/user_account/ajax_bank_accounts_popup',$this->data_ajax);
 		}
 	}
 	
	public function delete(){
		if(isset($_GET['id'])  && $_GET['id']!=''){
			$this->User_account_mdl->delete_account($_GET['id']);
		}
		redirect(base_url().$this->config->item('admin_directory_name').'/user_account');
	}
	
}