<?php 
Class Account extends AdminController
{
	function __construct(){
		parent::__construct();
	}
	
	function login()
	{
		if(isset($_GET['email']) && isset($_GET['password']))
		{
			$email = $_GET['email'];
			$password = md5($_GET['password']);

			$condition[] = array('name'=>'username','value'=>$email); 
			$condition[] = array('name'=>'password','value'=>$password);

			$res = $this->setting_model->get_setting('admin',$condition);

			if(!empty($res)){
				$this->session->set_userdata("admin_id",$res['admin_id']);

				if($this->session->userdata('admin_redirect_link')){
					$redirect_link = $this->session->userdata('admin_redirect_link');
					$this->session->unset_userdata('admin_redirect_link');
					echo $redirect_link;
					exit;
				}
				
				echo site_url('dashboard');
				exit;
			}else{
				echo "E";
				exit;
			}
		}

		$data['meta_title'] = 'Login';
		$this->load->view('account/login',$data);
	}
	
	function logout()
	{
		$this->session->unset_userdata("admin_id");
		redirect('login');
	}

	function dashboard()
	{
		if(!empty($this->session->userdata("admin_id"))){ 
			$data['meta_title'] = 'Dashboard';			 
     // $data['admin_perms']= get_admin_perms();

			$condition = array();
			$condition[] = array('name'=>'deleted','value'=>'0'); 
			$select =array("count(*) as tot_rec");
			$res = $this->setting_model->get_setting('entry',$condition,false,false,$select);

			if($res)
				$tot_entry=$res['tot_rec'];
			else
				$tot_entry=0;

			$condition = array();
			$condition[] = array('name'=>'deleted','value'=>'0'); 
			$select =array("sum(net_amt) as tot_amt");
			$res = $this->setting_model->get_setting('entry',$condition,false,false,$select);

			if($res)
				$tot_amt=$res['tot_amt'];
			else
				$tot_amt=0; 

			$data['tot_entry']=$tot_entry;
			$data['tot_amt']=$tot_amt;
			 
			$this->load->view('common/header',$data);
			$this->load->view('common/dashboard',$data);
			$this->load->view('common/footer',$data);	
		}		
	}

	function profile()
	{
		// echo $this->session->userdata("admin_id");exit;
		if($this->input->server('REQUEST_METHOD') === 'POST')
		{
			$save['admin_id'] = $this->session->userdata("admin_id");
			if (($this->input->post('email'))){
				$save['email'] = $this->input->post('email');
				$save['password'] = md5($this->input->post('password'));
			}			

			if (($this->input->post('name')))
				$save['name'] = $this->input->post('name');

			// if (isset($this->input->post('email_id')))
			// 	$save['email_id'] = $this->input->post('email_id');
			$err=0;
			if(!empty($_FILES['img']['name'])){
				$upload_path = ADMIN_USER_IMG_PATH. $this->session->userdata("admin_id");

				if(!file_exists($upload_path))
					mkdir($upload_path, 0777, true);

				$config['upload_path'] = $upload_path;
				$config['allowed_types'] = 'jpg|png|jpeg';
				$config['max_size']	= '100';
				$config['max_width']  = '700';
				$config['max_height']  = '700';
				$this->load->library('upload', $config);

				if ( ! $this->upload->do_upload('img')){
					$err=1;
					$this->session->set_flashdata("alert_error",$this->upload->display_errors());
				}else{
					$image_info = $this->upload->data();
					// resize_image($image_info['file_name'], $upload_path, admin_IMG_SIZE );
					$save['img'] = $image_info['file_name'];
				}
			}

			$this->setting_model->save_setting('admin',$save);

			if($err==0)
				$this->session->set_flashdata("alert_success","Changes saved Successfully!");

			redirect('profile');
		}

		$data['meta_title'] = 'My Profile';

		$admin_id = $this->session->userdata("admin_id");

		$condition = array();
		$condition[] = array('name'=>'admin_id','value'=>$admin_id);
		$data['user_info'] = $this->setting_model->get_setting('admin',$condition);
		//echo "<pre>";print_r(ADMIN_USER_IMG_PATH.$data['user_info']['admin_id'].'/'.$data['user_info']['img']);exit;
		if(!empty($data['user_info']['img']))
			$data['user_img'] = base_url(ADMIN_USER_IMG_PATH.$data['user_info']['admin_id'].'/'.$data['user_info']['img']);
		else
			$data['user_img'] = base_url('assets/img/admin.jpg'); 
		$this->load->view('common/header',$data);
		$this->load->view('account/profile',$data);
		$this->load->view('common/footer',$data);	
	}
}

?>