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

class Transactions 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']='Transactions | Administrator Panel';
		$this->data['active_class']='transactions_menu';
		$this->load->model('Backend/Transactions_mdl');
 	}	

 	public function index(){		
  		$this->data['page_title']='Transactions';
		$this->data['transactions_listing'] = $this->Transactions_mdl->admin_transactions_listing();
		//echo '<pre>'; print_r($this->data['transactions_listing']);die;
		$this->load->view('Backend/includes/header',$this->data); 
		$this->load->view('Backend/transactions/listing',$this->data);
		$this->load->view('Backend/includes/footer');	
	}
	
	public function add(){		
  		$this->data['page_title']='Transaction : : Add';
		$this->data['user_accounts_data']=$this->Transactions_mdl->user_accounts_data();
		$this->load->view('Backend/includes/header',$this->data); 
		$this->load->view('Backend/transactions/add',$this->data);
		$this->load->view('Backend/includes/footer');	
	}
	
	public function ajax_user_bef_acct(){
		if(isset($_GET['user_id'])  && $_GET['user_id']!=''){
			$this->data_ajax['user_id'] = $_GET['user_id'];
			$this->data_ajax['beneficiary_accounts']=$this->User_mdl->my_unique_beneficiary_accounts($_GET['user_id']);
			$this->data_ajax['user_accounts_listing']=$this->User_mdl->user_accounts_listing($_GET['user_id']);
			$this->load->view('Backend/transactions/ajax_user_bef_acct',$this->data_ajax);
		}
	}
	
	public function debit_entry(){
		$txn_id = $this->generateRandomString();
		echo $this->Transactions_mdl->debit_entry($txn_id);
	}
	
	public function update_status(){
		if(isset($_GET['tid'])  && $_GET['tid']!=''){
			$this->data_ajax['action_sts']=$_GET['action_sts'];
			$this->load->view('Backend/transactions/ajax_update_status',$this->data_ajax);
		}
	}
	
	public function update_sts_entry(){
		echo $this->Transactions_mdl->update_sts_entry();
	}
	
	public function details_popup(){
 	 	if(isset($_GET['tid'])  && $_GET['tid']!=''){
			$this->data_ajax['trans_details']=$this->Transactions_mdl->admin_transactions_details($_GET['tid']);
			$this->load->view('Backend/transactions/ajax_trans_popup',$this->data_ajax);
 		}
 	}
	
	public function delete(){
		if(isset($_GET['id'])  && $_GET['id']!=''){
			$this->Transactions_mdl->delete_trans($_GET['id']);
		}
		redirect(base_url().$this->config->item('admin_directory_name').'/transactions');
	}
	
	public function beneficiary(){		
  		$this->data['page_title']='Beneficiary Account ';
		$this->data['beneficiary_listing'] = $this->Transactions_mdl->admin_transactions_beneficiary_listing();
		//echo '<pre>'; print_r($this->data['transactions_listing']);die;
		$this->load->view('Backend/includes/header',$this->data); 
		$this->load->view('Backend/transactions/bef_listing',$this->data);
		$this->load->view('Backend/includes/footer');	
	}
	
	public function update_bef_image_entry(){
		$this->Transactions_mdl->update_bef_image_entry();
		redirect(base_url().$this->config->item('admin_directory_name').'/transactions/beneficiary');
	}
 	
	public function vectra(){
		$this->data['active_class']='vectra_transactions_menu';		
  		$this->data['page_title']='Vectra Transactions';
		//$this->data['user_bank_accounts_listing']=$this->User_mdl->user_bank_accounts_listing();
		$this->data['admin_vectra_transactions_listing'] = $this->Transactions_mdl->admin_vectra_transactions_listing();
		//echo '<pre>'; print_r($this->data['transactions_listing']);die;
		$this->load->view('Backend/includes/header',$this->data); 
		$this->load->view('Backend/transactions/vectra/listing',$this->data);
		$this->load->view('Backend/includes/footer');	
	}
	
	public function vectra_add(){		
  		$this->data['active_class']='vectra_transactions_menu';		
		$this->data['page_title']='Vectra Transaction : : Add';
		$this->data['user_accounts_data']=$this->Transactions_mdl->user_accounts_data();
		$this->load->view('Backend/includes/header',$this->data); 
		$this->load->view('Backend/transactions/vectra/add',$this->data);
		$this->load->view('Backend/includes/footer');	
	}
	
	public function vectra_debit_entry(){
		$txn_id = $this->generateRandomString();
		echo $this->Transactions_mdl->vectra_debit_entry($txn_id);
	}
	
	public function ajax_vectra_user_bef_acct(){
		if(isset($_GET['user_id'])  && $_GET['user_id']!=''){
			$this->data_ajax['user_id'] = $_GET['user_id'];
			$this->data_ajax['beneficiary_accounts']=$this->User_mdl->unique_sent_account_no($_GET['user_id']);
			$this->data_ajax['user_accounts_listing']=$this->User_mdl->user_accounts_listing($_GET['user_id']);
			$this->load->view('Backend/transactions/vectra/ajax_vectra_user_bef_acct',$this->data_ajax);
		}
	}
	
	public function update_vectra_status(){
		if(isset($_GET['id'])  && $_GET['id']!=''){
			$this->data_ajax['action_sts']=$_GET['action_sts'];
			$this->load->view('Backend/transactions/vectra/ajax_update_status',$this->data_ajax);
		}
	}
	
	public function update_vectra_sts_entry(){
		echo $this->Transactions_mdl->update_vectra_sts_entry();
	}
	
	public function vectra_beneficiary(){	
		$this->data['active_class']='vectra_transactions_menu';			
  		$this->data['page_title']='Vectra : : Beneficiary Accounts';
		$this->data['beneficiary_listing'] = $this->Transactions_mdl->admin_transactions_vectra_beneficiary_listing();
		//echo '<pre>'; print_r($this->data['transactions_listing']);die;
		$this->load->view('Backend/includes/header',$this->data); 
		$this->load->view('Backend/transactions/vectra/bef_listing',$this->data);
		$this->load->view('Backend/includes/footer');	
	}
	
	public function update_vectra_bef_image_entry(){
		$this->Transactions_mdl->update_vectra_bef_image_entry();
		redirect(base_url().$this->config->item('admin_directory_name').'/transactions/vectra_beneficiary');
	}
	
	public function delete_vectra(){
		if(isset($_GET['id'])  && $_GET['id']!=''){
			$this->Transactions_mdl->delete_vectra($_GET['id']);
		}
		redirect(base_url().$this->config->item('admin_directory_name').'/transactions/vectra');
	}
	
	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;
	}
	
}