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

class Admin_login_ctrl extends admin_index {
 
	
	public function __construct(){
		parent::__construct(); 
		$data = array();
		$this->load->model('admin_model');
	}
	
	/*----------------- load dashboard -----------------*/
	public function index($d_type=''){	
		// var_dump($d_type);return;
		$data['sdata'] = array();
		if($d_type==''){			
			$this->load->view('common/admin_dashboard'); 
		}else{
			switch($d_type){
				case "lu_type":
					if($this->user['panel_type']=='0'){
						$data['subview'] = 'dashboard/ad_dashboard';
						$data['active_cls'] = 'dash';		
						$this->load->view('common/sidepanel',$data); 
					}
				break;

				case "icd_type":
					if($this->user['panel_type']=='0'){
						$data['subview'] = 'dashboard/ad_dashboard';
						$data['active_cls'] = 'dash';		
						$this->load->view('common/icd_sidepanel',$data); 
					}
				break;
			}
			
		}
	}

	/*----------------- loading or unloading module dashboard -----------------*/
	/*public function lu_module_dashboard(){
		$data['sdata'] = array();
		if($this->user['panel_type']=='0'){
			$data['subview'] = 'dashboard/ad_dashboard';
		}else if($this->user['panel_type']=='1'){
			$data['subview'] = 'dashboard/eo_dashboard';
		}else if($this->user['panel_type']=='2'){
			$data['subview'] = 'dashboard/ao_dashboard';
		}else if($this->user['panel_type']=='3'){
			$data['subview'] = 'dashboard/de_dashboard';
		}else if($this->user['panel_type']=='4'){
			$data['subview'] = 'dashboard/leo_dashboard';
		}else if($this->user['panel_type']=='5'){
			$data['subview'] = 'dashboard/lo_dashboard';
		}else if($this->user['panel_type']=='6'){
			$data['subview'] = 'dashboard/avso_dashboard';
		}
		$data['active_cls'] = 'dash';		
		$this->load->view('common/sidepanel',$data); 
	}*/


	/*----------------- load login page -----------------*/
	public function login(){
		$this->load->view('common/ad_login');
	}
	
	/*----------------- login submition -----------------*/
	
	public function submit(){
		$return = array('status'=>false);
		if(isset($_POST["u_name"])){
			 $user_name 			= $this->input->post("u_name");
			 $password 				= $this->input->post("pwd");
			 $l_data 				= $this->admin_model->signin($user_name,$password);
			 $l_data['panel_type']  = '0';
			// var_dump($l_data);//return;
			// echo $this->db->last_query(); return false;
			 if($l_data!=false){ 
				$this->session->set_userdata('login_state',$l_data );
				redirect('admin_login_ctrl/index');
				//return;
			}
			else redirect("admin");
		}
		else redirect("admin");
	}
	
	
	/*----------------- load priority settings page loading -----------------*/
	public function priority_settings(){
		$data['sdata'] = array();
		$data['subview'] 			= 'users/allow_report_menu';
		$data['active_cls'] = 'dash';		
		
		$data['sdata']['ul_list'] 	= $this->admin_model->user_list();
		$this->load->view('common/sidepanel',$data); 
	}
 

	/*----------------- add users  details and edit users  details -----------------*/
	public function insert_priority(){		
		// var_dump($_POST);return;
		$user_id 			= $this->input->post('user_id');
		$this->db->where('l_status','1')
					->set('l_status','0')
					->update('login');
		foreach($user_id as $u_rows){
			$i_user_id		= $u_rows;
			$data['l_status']		= '1';
			$edit_l = $this->admin_model->update_data('login','i_user_id',$i_user_id,$data);
				
		}

		$this->session->set_flashdata('tos_success', 'Priority setted');
		redirect('admin_login_ctrl/priority_settings');
			
					
	}
	

	
	
	
	
	
	public function change_password(){	
		$data['sdata'] = array();
		$data['subview'] = 'change_password';
		$this->load->view('admin/sidepanel',$data);
	}
	
	
	public function reset_password(){

		$login_user = $this->user['l_id'];
		$old_pwd = md5($this->input->post('old_pwd'));
		$new_pwd = md5($this->input->post('new_pwd'));
		$confirm_pwd = md5($this->input->post('confirm_pwd'));
		if(isset($login_user)){
			if($new_pwd==$confirm_pwd){
				$u_pwd = $this->login_model->update_pwd($login_user,$old_pwd,$confirm_pwd);
				if($u_pwd==1){
					$this->session->set_flashdata('tos_success', 'Successfully Reseted...');
					redirect('admin_ctrl/change_password');
				}else {
					$this->session->set_flashdata('tos_error', 'Try again !...');
					redirect('admin_ctrl/change_password');
				}
			}
			else {
				$this->session->set_flashdata('tos_error', 'New password and confirm password are mismatch');
				redirect('admin_ctrl/change_password');
			}
		}else {
			$this->session->set_flashdata('tos_error', 'Invalid user');
			redirect('admin_ctrl/change_password');
		}
	}	
	
	public function logout(){		
		$redirect_url = 'admin_login_ctrl/login';		
		$this->session->sess_destroy();
		redirect(''.$redirect_url.'');
	}
	
	
	
	
}
