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

class Users_ctrl extends user_index {
 
	
	public function __construct(){ 
		parent::__construct();
		$data = array();
		$this->load->model('admin_model');	
	}
	
	/*----------------- load users list page -----------------*/
	public function users_list(){	
		$data['sdata'] 				= array();
		$data['subview'] 			= 'users/users_list';
		$data['active_cls'] 		= 'userrr_cls';
		$data['sdata']['ul_list'] 	= $this->admin_model->get_result_array('users_list');			
		$this->load->view('common/sidepanel',$data);
	}
	

	/*----------------- load add users details page ----------------*/
	public function add_user_details($ul_id=''){	
		$data['sdata'] 						= array();
		$data['subview'] 					= 'users/add_users_details'; 
		$data['active_cls'] 				= 'userrr_cls';	
		if($ul_id!=''){
			$data['sdata']['view_type'] 	= "Edit";
			$data['sdata']['ul_data'] = $this->admin_model->get_result_array('users_list','ul_id',$ul_id);	
			$data['sdata']['aurl_list'] = $this->admin_model->get_result_array_with_two_where('assigned_user_roles_list','aurl_ul_id',$ul_id,'aurl_status','1');				
			
		}else{
			$data['sdata']['view_type'] 	= "Add";
		}
		$this->load->view('common/sidepanel',$data);
	}
	
	
	/*----------------- add users  details and edit users  details -----------------*/
	public function insert_users($ul_id= false){		
		$this->form_validation->set_rules('ul_name', 'Name', 'trim|required');
		// $this->form_validation->set_rules('ul_designation', 'Designation', 'trim|required');
		$this->form_validation->set_rules('ul_operation_level', 'Operation Level', 'trim|required');
		$this->form_validation->set_rules('ul_mobile', 'Mobile number', 'trim|required');
		$this->form_validation->set_rules('ul_email', 'Emailid', 'trim|required');
		$this->form_validation->set_rules('ul_state', 'State', 'trim|required');
		$this->form_validation->set_rules('ul_district', 'District', 'trim|required');
		$this->form_validation->set_rules('ul_place', 'Place', 'trim|required');
		$this->form_validation->set_rules('ul_pincode', 'Pincode', 'trim|required');
		$this->form_validation->set_rules('ul_address', 'Address', 'trim|required');
		
		if ($this->form_validation->run() === FALSE) {
			$e_msg = '';
			$e_msg .= form_error('ul_name');  				$e_msg .= form_error('ul_pincode');  			
			$e_msg .= form_error('ul_mobile');   			$e_msg .= form_error('ul_address'); 
			$e_msg .= form_error('ul_email');   			$e_msg .= form_error('ul_state');   
			$e_msg .= form_error('ul_district');   			$e_msg .= form_error('ul_place');    
			// $e_msg .= form_error('ul_designation');			
			$e_msg .= form_error('ul_operation_level');
			$this->session->set_flashdata('tos_error', $e_msg);
			redirect('users_ctrl/add_user_details');	
		}	
		$data['ul_details'] = array();
		foreach($_POST as $post_key=>$post_rows){
			if($post_key!='aurl_roles'){
				$data['ul_details'][$post_key] 		= $this->input->post($post_key);
			}
		} 
		

		if($ul_id==false){
			$check_email 								= $this->admin_model->get_row_array_with_one_where('users_list','ul_email',$data['ul_details']['ul_email']);
			if(sizeof($check_email)==0){								
				$add_ul 								= $this->admin_model->insert_item('users_list',$data['ul_details']);		
				$data['ul_details']['ul_code'] 			= 'US-'.$add_ul;
				$code_ul 								= $this->admin_model->update_data('users_list','ul_id',$add_ul,$data['ul_details']);

				$data['lg_details']['i_user_id'] 		= $add_ul;
				$data['lg_details']['i_type'] 			= $this->input->post('ul_designation');
				$data['lg_details']['l_user'] 			= $this->input->post('ul_email');
				$data['lg_details']['l_user_name'] 		= $this->input->post('ul_name');
				$data['lg_details']['l_pwd'] 			= md5($this->input->post('ul_mobile'));
				
				$add_lg 								= $this->admin_model->insert_item('login',$data['lg_details']);		

				$aurl_roles = $this->db->set('aurl_status','0')->where('aurl_ul_id',$add_ul)->update('assigned_user_roles_list');
				
				foreach($_POST['aurl_roles'] as $aurl_rows){
					$aurl_data 						= $this->admin_model->get_row_array_with_two_where('assigned_user_roles_list','aurl_ul_id',$add_ul,'aurl_roles',$aurl_rows);	
					
					$data['aurl']['aurl_ul_id'] 	= $add_ul;
					$data['aurl']['aurl_type'] 		= 'l_u';
					$data['aurl']['aurl_roles'] 	= $aurl_rows;
					
					if(empty($aurl_data )){
						$add_aurl 					= $this->admin_model->insert_item('assigned_user_roles_list',$data['aurl']);						
					}else{
						$edit_aurl = $this->db->set('aurl_status','1')->where('aurl_ul_id',$add_ul)->where('aurl_roles',$aurl_rows)->update('assigned_user_roles_list');
					}
				}
				
				

				if($add_ul!=false){
					$this->session->set_flashdata('tos_success', 'user details added.');
					redirect('users_ctrl/users_list');
				}else {
					$this->session->set_flashdata('tos_error', 'Try again !...');
					redirect('users_ctrl/users_list');
				}	
			}else{
				$this->session->set_flashdata('tos_warning', 'This email id already exist . <br> Try again !...');
				redirect('users_ctrl/users_list');
			}		
		}else{
			$check_email1 								= $this->admin_model->get_row_array_with_one_where('users_list','ul_email',$data['ul_details']['ul_email']);
			// var_dump($check_email1['ul_id']);return;
			// var_dump($check_email1);return;
			if((sizeof($check_email1)!=0 && $check_email1['ul_id']==$ul_id) || sizeof($check_email1)==0){
				$edit_ul = $this->admin_model->update_data('users_list','ul_id',$ul_id,$data['ul_details']);

			
				$data['lg']['i_type'] 			= $this->input->post('ul_designation');
				$data['lg']['l_user'] 			= $this->input->post('ul_email');
				$data['lg']['l_user_name'] 		= $this->input->post('ul_name');
					
				$edit_lg 								= $this->admin_model->update_data('login','i_user_id',$ul_id,$data['lg']);
			

				$aurl_roles = $this->db->set('aurl_status','0')->where('aurl_ul_id',$ul_id)->update('assigned_user_roles_list');

				$aurl_s = 0;

				foreach($_POST['aurl_roles'] as $aurl_rows){
					$aurl_data 						= $this->admin_model->get_row_array_with_two_where('assigned_user_roles_list','aurl_ul_id',$ul_id,'aurl_roles',$aurl_rows);	
						
					$data['aurl']['aurl_ul_id'] 	= $ul_id;
					$data['aurl']['aurl_type'] 		= 'l_u';
					$data['aurl']['aurl_roles'] 	= $aurl_rows;
						
					if(empty($aurl_data )){
						$add_aurl 					= $this->admin_model->insert_item('assigned_user_roles_list',$data['aurl']);	
						$aurl_s = 1;					
					}else{
						$edit_aurl = $this->db->set('aurl_status','1')->where('aurl_ul_id',$ul_id)->where('aurl_roles',$aurl_rows)->update('assigned_user_roles_list');
						$aurl_s =1;
					}
				}



				if($edit_ul=='1'){
					$this->session->set_flashdata('tos_success', 'user details updated');
					redirect('users_ctrl/users_list');
				}else if($aurl_s=='1' && $edit_ul!='1'){
					$this->session->set_flashdata('tos_success', 'user details updated');
					redirect('users_ctrl/users_list');
				}else {
					$this->session->set_flashdata('tos_warning', 'There is no changes!...');
					redirect('users_ctrl/users_list');
				}
			}else{
				$this->session->set_flashdata('tos_warning', 'This email id already exist . <br> Try again !...');
				redirect('users_ctrl/users_list');
			}
		}		
	}

	
	

	
}
