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

class User extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->library('layouts');
		
		checkDashboard();
		
	}
	
	public function index(){
		
		
		$this->lang->load("user",$this->session->userdata('language'));
		
		$this->load->model('user_model');
		
		$data['userList'] = $this->user_model->user_all();
		
		
		$this->layouts->view('listUser', array(), $data);
		
		
	}
	
	
	public function saveNewUser(){
	
		$this->load->model('user_model');
	
		$email = $this->input->post('email');
		$password = $this->input->post('password');
		$confirmPassword = $this->input->post('confirmPassword');
		$groupName = $this->input->post('groupName');
		
		
		if($password == $confirmPassword){
			
			$data = array(
						'email' => $email,
						'password' => md5($password),
						'groupID' => $groupName
					);
				
				
			$insert = $this->user_model->insertUser('user', $data);
		
		
			echo "<script>alert('Data saved');window.location='".base_url()."user'</script>";
		
		}
		else{
			
			echo"<script>alert('Confirm password not matched');history.go(-1);</script>";
			
		}
		
		
	
	}
	
	
	public function saveUpdateUser(){
	
		$this->load->model('user_model');
	
		$email = $this->input->post('email');
		$password = $this->input->post('password');
		$confirmPassword = $this->input->post('confirmPassword');
		$groupName = $this->input->post('groupName');
		
		
		if($password != NULL){
			if($password == $confirmPassword){
				
				$userID = element('userid', $this->session->userdata('logged_in'));
				
				$data = array(
							'email' => $email,
							'password' => md5($password),
							'groupID' => $groupName
						);
					
					
				$insert = $this->user_model->updateUser('user', $userID, $data);
			
			
				echo "<script>alert('Data saved');window.location='".base_url()."user/editUser'</script>";
			
			}
			else{
				
				echo"<script>alert('Confirm password not matched');history.go(-1);</script>";
				
			}
		
		}
		else{
		
				$userID = element('userid', $this->session->userdata('logged_in'));
				
				$data = array(
							'email' => $email,
							'groupID' => $groupName
						);
					
					
				$insert = $this->user_model->updateUser('user', $userID, $data);
			
			
				echo "<script>alert('Data saved');window.location='".base_url()."user/editUser'</script>";
		
		}
		
		
	
	}
	
	
	public function saveEditUser(){
	
		$this->load->model('user_model');
	
		$ID = $this->input->post('ID2');
		$email = $this->input->post('email2');
		$password = $this->input->post('password2');
		$confirmPassword = $this->input->post('confirmPassword2');
		$groupName = $this->input->post('groupName2');
		
		
		if($password){
			
			if($password == $confirmPassword){
				$data = array(
							'email' => $email,
							'password' => md5($password),
							'groupID' => $groupName
						);
				
				
				$update = $this->user_model->updateUser('user', $ID, $data);
		
		
				print json_encode(array("status"=>"success"));
		
			}
			else{
			
				print json_encode(array("status"=>"failed"));
				
			}
		
		}
		else{
			
				$data = array(
							'email' => $email,
							'groupID' => $groupName
						);
				
				
				$update = $this->user_model->updateUser('user', $ID, $data);
		
		
				print json_encode(array("status"=>"success"));
			
		}
		
		
	
	}
	
	
	public function deleteUser()
	{
		$this->load->model('user_model');
	
		$ID = $this->input->post('ID2');
		
		$data = array(
					
					'is_delete' => 1
					
				);
				
		$update = $this->user_model->updateUser('user', $ID, $data);
		
		if($update){
			print json_encode(array("status"=>"success"));
		}
		else{
			print json_encode(array("status"=>"failed"));
		}		
	
	}
	
	
	public function checkPassword(){
		
		$this->load->model('user_model');
		
		$ID = $this->input->post('ID');
		$adminPassword = md5($this->input->post('adminPassword'));
				
		$query = $this->user_model->checkAdminPassword($ID, $adminPassword);		
		
		if($query){
   			print json_encode(array("status"=>"success"));
		}else{
   			print json_encode(array("status"=>"failed"));
		}		
	}
	
	
	public function editUser(){
		
		
		$this->lang->load("user",$this->session->userdata('language'));
		
		$this->load->model('user_model');
		
		$userID = element('userid', $this->session->userdata('logged_in'));
		
		
		$data['userGroup'] = $this->user_model->get_userGroup();
		$data['getUser'] = $this->user_model->get_userbyID($userID);
		
		
		$this->layouts->view('editUser', array(), $data);
		
		
	}
	
	
}

?>