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

class track extends CI_Controller {
	var $arrLogin;
	  public function __construct(){
  		parent::__construct();//$this->load->model(array('admin/adm_track_model','admin/adm_auth_model','admin/admin_model' ));
		$this->load->model(array('admin/admin_model','admin/admin_mail_model', 'admin/adm_track_model' ));
		 if($this->admin_model->is_login === false){  redirect('admin/login', 'refresh'); exit; }
		 $this->admin_model->authenticateAdmin('track'); 
   	}
	function user(){
		if($this->input->post('usertrack_id', TRUE) && $this->input->post('doAction', TRUE) ){
			$action = $this->input->post('doAction', TRUE);
			if($action == 'deleteAll'){
				$this->admin_model->checkAdminPrivilage('DELETE','admin/track/user');
			}else{
				$this->admin_model->checkAdminPrivilage('UPDATE','admin/track/user');
			}
 			 $successMsg = $this->adm_track_model->updateUsertrackAction($this->input->post('doAction', TRUE),$this->input->post('usertrack_id', TRUE));
			if($successMsg){
				$this->session->set_flashdata('successMsg',$successMsg);
			}
			redirect('admin/track/user');exit;
  		}
		if($this->input->get('searchBy', TRUE)) $data['searchBy'] = $this->input->get('searchBy', TRUE); 
		if($this->input->get('searchKey', TRUE)) $data['searchKey'] = $this->input->get('searchKey', TRUE); 
		if($this->input->get('userTypeBy', TRUE)) $data['userTypeBy'] = $this->input->get('userTypeBy', TRUE); 
		if($this->input->get('sortBy', TRUE)) $data['sortBy'] = $this->input->get('sortBy', TRUE); 
 		if($this->input->get('orderBy', TRUE)) $data['orderBy'] = $this->input->get('orderBy', TRUE); 
		$this->load->view('admin/user_track_list',$data);
 	}
	function user_doAction($action,$id){
		if($action == 'delete'){
			$this->admin_model->checkAdminPrivilage('DELETE','aadmin/track/user');
		}else{
			$this->admin_model->checkAdminPrivilage('UPDATE','admin/track/user');
		}
		$successMsg = $this->adm_track_model->updateUsertrackAction($action,$id);
		if($successMsg){
			$this->session->set_flashdata('successMsg',$successMsg);
		}
		redirect('admin/track/user');exit;
	}
	function user_view($id = false){
		$this->admin_model->checkAdminPrivilage('VIEW','admin/track/user'); 
		$data['usertrack_id'] = $id;
		$this->load->view('admin/usertrack_view',$data);
	}
	function blacklist_manager($black_id=false){
		if($this->input->post('blacklist_type',true)){
 			 $res = $this->adm_track_model->addEditIPTrack($black_id);
			 if($res){
				 if($black_id)$this->session->set_flashdata('successMsg','Blacklist updated successfully');
				 else $this->session->set_flashdata('successMsg','Blacklist created successfully');
			 }else{
				 if($black_id)$this->session->set_flashdata('dangerMsg','Failed to update blacklist ');
				 else $this->session->set_flashdata('dangerMsg','Failed to create blacklist');
			 }
			 redirect('admin/track/black_list');exit;
		}
	  
		$data = array('action'=>'add');
		if($black_id){$data['action'] = 'edit'; $data['black_id'] = $black_id; }
		$this->load->view('admin/blacklist_manager',$data);
	}
	function black_list($action=false,$black_id=false){
		  
 		$data = array();
		if($this->input->post('doActionId', TRUE) && $this->input->post('doAction', TRUE) ){
			$action = $this->input->post('doAction', TRUE);
			$black_id= $this->input->post('doActionId', TRUE);
			$log_note = $this->input->post('log_note', TRUE);
 			$this->admin_model->checkAdminPrivilage('UPDATE','admin/track/black_list');
			$successMsg = $this->adm_track_model->updateBlacklist($action,$black_id,$log_note);
 			if($successMsg){
				$this->session->set_flashdata('successMsg',$successMsg);
 			}
			redirect('admin/track/black_list');exit;
  		}
  		if($action !== false && $black_id !== false){
			 $data['action'] = 	$action;$data['black_id'] = 	$black_id;
			 if($action == 'delete'){
					$this->admin_model->checkAdminPrivilage('DELETE','admin/track/black_list');
					$successMsg = $this->adm_track_model->updateBlacklist($action,$black_id);
					if($successMsg){ $this->session->set_flashdata('successMsg',$successMsg); }
					redirect('admin/track/black_list');exit;
			 }
 		}
		 
 		$this->load->view('admin/black_list',$data);
 	}
	function log_history($action=false,$black_id=false){
		$data = array();
  		 
 		
		
  		$this->load->view('admin/log_history',$data);
 	}
	
	function admin(){
		if($this->input->post('admintrack_id', TRUE) && $this->input->post('doAction', TRUE) ){
			$action = $this->input->post('doAction', TRUE);
			if($action == 'deleteAll'){
				$this->admin_model->checkAdminPrivilage('DELETE','admin/track/admin');
			}else{
				$this->admin_model->checkAdminPrivilage('UPDATE','admin/track/admin');
			}
 			 $successMsg = $this->adm_track_model->updateAdmintrackAction($this->input->post('doAction', TRUE),$this->input->post('admintrack_id', TRUE));
			if($successMsg){
				$this->session->set_flashdata('successMsg',$successMsg);
			}
			redirect('admin/track/admin');exit;
  		}
		if($this->input->get('searchBy', TRUE)) $data['searchBy'] = $this->input->get('searchBy', TRUE); 
		if($this->input->get('searchKey', TRUE)) $data['searchKey'] = $this->input->get('searchKey', TRUE); 
 		$this->load->view('admin/admin_track_list',$data);
 	}
	function admin_doAction($action,$id){
		if($action == 'delete'){
			$this->admin_model->checkAdminPrivilage('DELETE','aadmin/track/admin');
		}else{
			$this->admin_model->checkAdminPrivilage('UPDATE','admin/track/admin');
		}
		$successMsg = $this->adm_track_model->updateAdmintrackAction($action,$id);
		if($successMsg){
			$this->session->set_flashdata('successMsg',$successMsg);
		}
		redirect('admin/track/admin');exit;
	}

	function checkAdminPrivilage($privilage,$goToUrl){
		$privilageAccess = $this->adm_auth_model->adminPrivilege($privilage);
		if($privilageAccess === false){
			 
 						$this->session->set_flashdata('errMsg', "You don't have enough privilege to process the request");    
					redirect($goToUrl);
		}
	}
}
?>