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

class Profile extends Admin_Controller{

	function __construct()
	{
		parent::__construct();	
		$this->load->model("mod_profile");
		$this->load->library("form_validation");
		$this->load->helper(array('form','file'));
	}
	
	
	function index()
	{
		$tab['activeTab'] = "";
		
		$userId = $this->session->userdata('userId');
		$user = $this->mod_profile->getUser($userId);
		if(!$user)
		{
			redirect("admin/profile");
			exit();
		}
		
		$data['user'] = $user;
		$this->load->view("admin/header",$tab);
		$this->load->view("admin/profile",$data);
		$this->load->view("admin/footer");	
	}


	function changeName()
	{
		$this->form_validation->set_rules('fullname', 'Fullname', 'trim|required|max_length[45]');	
		//$this->form_validation->set_rules('phone', 'Phone', 'trim|required|max_length[45]|numeric');	
		$this->form_validation->set_rules('email', 'Email', 'trim|required|max_length[45]|valid_email');	
		if($this->form_validation->run() == TRUE)
		{
			//Altering Database
			$userId = $this->session->userdata('userId');
			$postdata = array(
				'fullname'	=>	$this->input->post('fullname'),
				'email'		=>	$this->input->post('email'),
			);
				
			$this->mod_profile->changeProfile($postdata,$userId);
			$this->session->set_flashdata('message',"Profile Update successfully");
			redirect('admin/profile');
			exit();
			//End Altering Database
		}else{
			$this->session->set_flashdata('errors', validation_errors());
			redirect('admin/profile');
			exit();
		}
	}
	
	
	function changePasswd()
	{
		$this->form_validation->set_rules('opassword', 'Old Password', 'required|max_length[45]');
		$this->form_validation->set_rules('npassword', 'New Password', 'required|max_length[45]');
		$this->form_validation->set_rules('cpassword', 'Confirm Password','required|max_length[45]|matches[npassword]');
		
		if($this->form_validation->run() == TRUE)
		{
			$agencyId = $this->session->userdata('agencyId');
			$userId = $this->session->userdata('userId');
			$password = sha1($this->input->post('opassword'));
			
			if($this->mod_profile->compPassword($userId,$password))
			{
				$postdata['password'] = sha1($this->input->post('npassword'));
				$this->mod_profile->changeProfile($postdata,$userId);
				$this->session->set_flashdata('message',"Password had Update successfully");
				redirect('admin/profile');
				exit();
			}else{
				$this->session->set_userdata('ms_error','Invalid password!');
				redirect('admin/profile');
				exit();
			}
			//End Altering Database
		}else{
			$this->session->set_flashdata('errors', validation_errors());
			redirect('admin/profile');
			exit();	
		}
	}


}