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

class Login_ctrl extends user_index {
 
	
	public function __construct(){
		parent::__construct(); 
		$data = array();
		$this->load->model('admin_model');
	}
	
	/*----------------- load dashboard -----------------*/
	public function index(){
		// var_dump($this->u_roles);return;
		
		$data['sdata'] = array();
		if($this->user['panel_type']=='0'){
			$data['subview'] = 'dashboard/ad_dashboard';
		}else if($this->user['panel_type']=='1' && in_array("1", $this->u_roles)){
			$data['subview'] = 'dashboard/eo_dashboard';
		}else if($this->user['panel_type']=='2' && in_array("2", $this->u_roles)){
			$data['subview'] = 'dashboard/ao_dashboard';
		}else if($this->user['panel_type']=='3' && in_array("3", $this->u_roles)){
			$data['subview'] = 'dashboard/de_dashboard';
		}else if($this->user['panel_type']=='20' && in_array("20", $this->u_roles)){
			redirect('reports_ctrl/reports_list');
		}else{
			// $this->session->set_flashdata('tos_error', 'Access Denied');			
			redirect('login_ctrl/logout');
		}
		
		// else if($this->user['panel_type']=='4' && in_array("4", $this->u_roles)){
		// 	$data['subview'] = 'dashboard/leo_dashboard';
		// }else if($this->user['panel_type']=='5' && in_array("5", $this->u_roles)){
		// 	$data['subview'] = 'dashboard/lo_dashboard';
		// }else if($this->user['panel_type']=='6' && in_array("6", $this->u_roles)){
		// 	$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/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");
			$ul_type 				= $this->input->post("ul_type");
			$l_data 				= $this->admin_model->signin($user_name,$password);
			$l_data['panel_type']  = $ul_type;
			// echo $this->db->last_query(); //return false;
			// var_dump($l_data);return;
			$aurl_list 				= $this->admin_model->get_result_array_with_two_where('assigned_user_roles_list','aurl_ul_id',$l_data['i_user_id'],'aurl_status','1');	
			
			
			$aurl_data 				= array();
			foreach($aurl_list as $aurl_rows){
				$aurl_data[] = $aurl_rows['aurl_roles'];
			}
			 
			// var_dump($ul_type);return;
			// var_dump($aurl_data);return;
			// echo $this->db->last_query(); return false;
			if($l_data!=false && in_array($ul_type, $aurl_data)){ 
				// echo "yes";return;
				$this->session->set_userdata('login_state',$l_data );
				$this->session->set_userdata('lg_user_role',$aurl_data );
				redirect('login_ctrl/index');
					//return;
			}
			else{
				$this->session->set_flashdata('tos_error', 'Access Denied');				
				redirect("login_ctrl/login");
			}
		}
		else redirect("admin");
	}
	
	


	/*----------------- load Study Material category page -----------------*/
	public function study_material_category_list($smcl_id=''){	
		$data['sdata'] = array();
		$data['subview'] = 'study_material_category_list';
		$data['active_cls'] = 's_mat';
		$data['sdata']['smcl_list'] = $this->admin_model->get_result_array('study_material_category_list');
		if($smcl_id!=''){
			$data['sdata']['smcl_data'] = $this->admin_model->get_result_array('study_material_category_list','smcl_id',$smcl_id);			
			$data['sdata']['v_type'] = 'Edit';
		}if($smcl_id==''){
			$data['sdata']['v_type'] = 'Add';
		}
		$this->load->view('admin/sidepanel',$data);
	}

	/*------------- add Study Material category details and edit Study Material category details --------------*/
	public function insert_material_category_details($smcl_id= false){		
		$this->form_validation->set_rules('smcl_category', 'Category', 'trim|required');
		
		if ($this->form_validation->run() === FALSE) {
			$e_msg = ''; 
			$e_msg .= form_error('smcl_category');  			
			
			$this->session->set_flashdata('tos_error', $e_msg);
			redirect('login_ctrl/study_material_category_list');
		}	
		foreach($_POST as $post_key=>$post_rows){
			$data[$post_key] 		= $this->input->post($post_key);					
		}
		
		if($smcl_id==''){	
			$add_smcl = $this->admin_model->insert_item('study_material_category_list',$data);			
			if($add_smcl!='0'){
				$this->session->set_flashdata('tos_success', 'Category details added.');
				redirect('login_ctrl/study_material_category_list');
			}else {
				$this->session->set_flashdata('tos_error', 'Try again !...');
				redirect('login_ctrl/study_material_category_list');
			}
		}else{
			$edit_smcl = $this->admin_model->update_data('study_material_category_list','smcl_id',$smcl_id,$data);
			if($edit_smcl=='1'){
				$this->session->set_flashdata('tos_success', 'Category details updated');
				redirect('login_ctrl/study_material_category_list');
			}else {
				$this->session->set_flashdata('tos_warning', 'There is no changes!...');
				redirect('login_ctrl/study_material_category_list');
			}
		}		
	}


	/*----------------- load Study Material list page -----------------*/
	public function study_material_list($smcl_id){	
		$data['sdata'] = array();
		$data['subview'] = 'study_material_list';
		$data['active_cls'] = 's_mat';
		$data['sdata']['sml_list'] = $this->admin_model->get_result_array_with_one_where('study_material_list','sml_smcl_id',$smcl_id);		
		$this->load->view('admin/sidepanel',$data);
	}


	/*----------------- load add Study Material page -----------------*/
	public function add_study_material_details($sml_id=''){	
		$data['sdata'] = array();
		$data['subview'] = 'add_study_material_details';
		$data['active_cls'] = 's_mat';
		$data['sdata']['smcl_list'] = $this->admin_model->get_result_array('study_material_category_list');
		if($sml_id!=''){
			$data['sdata']['sml_data'] = $this->admin_model->get_result_array('study_material_list','sml_id',$sml_id);			
			$data['sdata']['v_type'] = 'Edit';
		}if($sml_id==''){
			$data['sdata']['v_type'] = 'Add';
		}
		$this->load->view('admin/sidepanel',$data);
	}
	

	/*------------- add Study Material details and edit Study Material details --------------*/
	public function insert_study_material_details($sml_id= false){		
		$this->form_validation->set_rules('sml_title', 'Tilte', 'trim|required');
		$this->form_validation->set_rules('sml_smcl_id', 'Category', 'trim|required');
		$this->form_validation->set_rules('sml_pdf_file', 'PDF File', 'trim|required');
		
		if ($this->form_validation->run() === FALSE) {
			$e_msg = ''; 
			$e_msg .= form_error('sml_title');  			
			$e_msg .= form_error('sml_smcl_id');  			
			$e_msg .= form_error('sml_pdf_file');  			
			
			$this->session->set_flashdata('tos_error', $e_msg);
			redirect('login_ctrl/add_study_material_details');
		}	
		$data['sml_title'] 			= $this->input->post('sml_title');					
		$data['sml_smcl_id'] 		= $this->input->post('sml_smcl_id');					
		$data['sml_pdf_file'] 		= $this->input->post('sml_pdf_file');					
		
		if($sml_id==''){	
			$add_sml = $this->admin_model->insert_item('study_material_list',$data);			
			if($add_sml!='0'){
				$this->session->set_flashdata('tos_success', 'Study Material details added.');
				redirect('login_ctrl/study_material_list/'.$data['sml_smcl_id']);
			}else {
				$this->session->set_flashdata('tos_error', 'Try again !...');
				redirect('login_ctrl/add_study_material_details');
			}
		}else{
			$edit_sml = $this->admin_model->update_data('study_material_list','sml_id',$sml_id,$data);
			if($edit_sml=='1'){
				$this->session->set_flashdata('tos_success', 'Study Material details updated');
				redirect('login_ctrl/study_material_list/'.$data['sml_smcl_id']);
			}else {
				$this->session->set_flashdata('tos_warning', 'There is no changes!...');
				redirect('login_ctrl/study_material_category_list');
			}
		}		
	}
	
	
	
	public function change_password(){ 
        $data['sdata'] = array();
        $data['subview'] = 'common/change_password';
        $data['active_cls'] = 'dash';
        $this->load->view('common/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->admin_model->update_pwd($login_user,$old_pwd,$confirm_pwd);
                if($u_pwd==1){
                    $this->session->set_flashdata('tos_success', 'Successfully Reseted...');
                    redirect('login_ctrl/change_password');
                }else {
                    $this->session->set_flashdata('tos_error', 'Try again !...');
                    redirect('login_ctrl/change_password');
                }
            }
            else {
                $this->session->set_flashdata('tos_error', 'New password and confirm password are mismatch');
                redirect('login_ctrl/change_password');
            }
        }else {
            $this->session->set_flashdata('tos_error', 'Invalid user');
            redirect('login_ctrl/change_password');
        }
    }

	


	
	public function logout(){		
		$redirect_url = 'login_ctrl/login';		
		$this->session->sess_destroy();
		redirect(''.$redirect_url.'');
	}
	
	
	
	
}
