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

/**
* User
*/
class Users extends CI_Controller
{

/*	public function index()
	{
		$this->data['users'] = $this->user_model->get();
		$this->load->view('users/index',$this->data);
	}

    public function user_types()
    {

    }
*/
    public function  login_user($username,$password)
    {             
               //login user , after successful login we will get user_id
               $user_id=$this->users_model->user_login($username,$password);
               $data['user_details']=$this->users_model->get_user_by_user_pass($username,$password); 
               $user_type_code=$this->users_model->get_user_type_code($data['user_details']['stakeholder_type']);
               if($data['user_details']['stakeholder_type']=='admin'&&$user_type_code=='11'):
                    $admin_data= array(			//passing data of user in array to be available throughout session
                        'admin_id' =>$data['user_details']['emp_id'],
                        'admin_username' =>$data['user_details']['username'],
                        'admin_firstname' =>$data['user_details']['firstname'],
                        'admin_lastname' =>$data['user_details']['lastname'],
                        'admin_email_id' =>$data['user_details']['email_id'],
                        'admin_phone_no' =>$data['user_details']['phone_no'],
                        'admin_profile_pic'=>$data['user_details']['profile_photo'],
                        'admin_logged_in' =>true,
                        'admin_date'=>date("Y-m-d H:i:s"),
                        'admin_usertype'=>$data['user_details']['stakeholder_type']
                        );

                        $this->session->set_userdata($admin_data);//setting the user data array to the session
                        //set flash message
                        $this->session->set_flashdata('admin_loggedin','You are now logged in');
                        redirect('admin/index');//redirecting to the posts index page     

                elseif($data['user_details']['stakeholder_type']=='hr'&&$user_type_code=='33'):

                    $hr_data= array(			//passing data of user in array to be available throughout session
                        'hr_id' =>$data['user_details']['emp_id'],
                        'hr_username' =>$data['user_details']['username'],
                        'hr_firstname' =>$data['user_details']['firstname'],
                        'hr_lastname' =>$data['user_details']['lastname'],
                        'hr_email_id' =>$data['user_details']['email_id'],
                        'hr_phone_no' =>$data['user_details']['phone_no'],
                        'hr_profile_pic'=>$data['user_details']['profile_photo'],
                        'hr_logged_in' =>true,
                        'hr_date'=>date("Y-m-d H:i:s"),
                        'hr_usertype'=>$data['user_details']['stakeholder_type']
                         );
                    
                    $this->session->set_userdata($hr_data);//setting the user data array to the session
                    //set flash message
                    $this->session->set_flashdata('hr_loggedin','You are now logged in');
                    redirect('humanresource/index');//redirecting to the posts index page
 
                elseif($data['user_details']['stakeholder_type']=='pd'&&$user_type_code=='22'):
 
                    $pd_data= array(			//passing data of user in array to be available throughout session
                        'pd_id' =>$data['user_details']['emp_id'],
                        'pd_username' =>$data['user_details']['username'],
                        'pd_firstname' =>$data['user_details']['firstname'],
                        'pd_lastname' =>$data['user_details']['lastname'],
                        'pd_email_id' =>$data['user_details']['email_id'],
                        'pd_phone_no' =>$data['user_details']['phone_no'],
                        'pd_profile_pic'=>$data['user_details']['profile_photo'],
                        'pd_logged_in' =>true,
                        'pd_date'=>date("Y-m-d H:i:s"),
                        'pd_usertype'=>$data['user_details']['stakeholder_type']
                         );
                    
                    $this->session->set_userdata($pd_data);//setting the user data array to the session
                    //set flash message
                    $this->session->set_flashdata('pd_loggedin','You are now logged in');
                    redirect('projectdirector/index');//redirecting to the posts index page
 
                elseif($data['user_details']['stakeholder_type']=='acc'&&$user_type_code=='44'):

                    $acc_data= array(			//passing data of user in array to be available throughout session
                        'acc_id' =>$data['user_details']['emp_id'],
                        'acc_username' =>$data['user_details']['username'],
                        'acc_firstname' =>$data['user_details']['firstname'],
                        'acc_lastname' =>$data['user_details']['lastname'],
                        'acc_email_id' =>$data['user_details']['email_id'],
                        'acc_phone_no' =>$data['user_details']['phone_no'],
                        'acc_profile_pic'=>$data['user_details']['profile_photo'],
                        'acc_logged_in' =>true,
                        'acc_date'=>date("Y-m-d H:i:s"),
                        'acc_usertype'=>$data['user_details']['stakeholder_type']
                         );
                    
                    $this->session->set_userdata($acc_data);//setting the user data array to the session
                    //set flash message
                    $this->session->set_flashdata('acc_loggedin','You are now logged in');
                    redirect('accountant/index');//redirecting to the posts index page
 
                elseif($data['user_details']['stakeholder_type']=='emp'&&$user_type_code=='55'):

                    $emp_data= array(			//passing data of user in array to be available throughout session
                        'emp_id' =>$data['user_details']['emp_id'],
                        'emp_username' =>$data['user_details']['username'],
                        'emp_firstname' =>$data['user_details']['firstname'],
                        'emp_lastname' =>$data['user_details']['lastname'],
                        'emp_email_id' =>$data['user_details']['email_id'],
                        'emp_phone_no' =>$data['user_details']['phone_no'],
                        'emp_profile_pic'=>$data['user_details']['profile_photo'],
                        'emp_logged_in' =>true,
                        'emp_date'=>date("Y-m-d H:i:s"),
                        'emp_usertype'=>$data['user_details']['stakeholder_type']
                         );
                    
                    $this->session->set_userdata($emp_data);//setting the user data array to the session
                    //set flash message
                    $this->session->set_flashdata('emp_loggedin','You are now logged in');
                    redirect('employee/index');//redirecting to the posts index page
 
                else:

                    $this->session->set_flashdata('login_failed','Login is invalid');
                    redirect('home/login');

                endif;
    }

/*	public function create()
	{
		
		$this->form_validation->set_rules(array(
            array(
                'field' => 'f_name',
                'label' => 'First Name',
                'rules' => 'required',
                'errors' => array(
                        'required' => '%s cannot be left blank.',
                ),
            ),
            array(
                'field' => 'l_name',
                'label' => 'Last Name',
                'rules' => 'required',
                'errors' => array(
                        'required' => '%s cannot be left blank.',
                ),
			),
			array(
                'field' => 'username',
                'label' => 'User Name',
                'rules' => 'required|is_unique[user.username]',
                'errors' => array(
                        'required' => '%s cannot be left blank.',
                        'is_unique' => '%s already exists.'
                ),
			),
			array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'required',
                'errors' => array(
                        'required' => '%s cannot be left blank.',
                ),
			),
			array(
                'field' => 'phone_no',
                'label' => 'Phone Number',
                'rules' => 'required|numeric',
                'errors' => array(
                        'required'  => '%s cannot be left blank.',
                        'numeric'   => '%s must contain only numbers.' 
                ),
			),
			array(
                'field' => 'address',
                'label' => 'Address',
                'rules' => 'required',
                'errors' => array(
                        'required' => '%s cannot be left blank.',
                ),
			),
			array(
                'field' => 'city',
                'label' => 'City',
                'rules' => 'required',
                'errors' => array(
                        'required' => '%s cannot be left blank.',
                ),
			)
        ));

        if($this->form_validation->run()){
        	if($this->user_model->insert()) {
        		set_alert('success','New User Created Successfully.' );
        		redirect(base_url('users'));
        	} else {
        		set_alert('error','Error while creating New User.' );
        	}
        }
		$this->load->view('users/create',$this->data);
	}

    public function view()
    {
        $this->data['user'] = $this->user_model->get($this->input->post('view_id'));
        $this->data['user_resource_type'] = $this->user_resource_model->getUserResourceTypes($this->data['user']->role_ID);
        $this->data['user_resource_data'] = $this->user_resource_model->getUserResourceData($this->data['user']->user_ID);
        $this->load->view('users/view',$this->data);
    }

	public function edit()
	{
        $this->data['user'] = $this->user_model->get($this->input->post('edit_id'));
        $this->data['user_resource_type'] = $this->user_resource_model->getUserResourceTypes($this->data['user']->role_ID);
        $this->data['user_resource_data'] = $this->user_resource_model->getUserResourceData($this->data['user']->user_ID);
		$this->load->view('users/edit',$this->data);
	}

    public function update()
    {
        if($this->user_model->update()!==false) {
            if($this->user_resource_model->setUserResourceData()) {
                set_alert('success','User Updated successfully');
            }
        } else {
            set_alert('error','Error While updating User');
        }
        redirect(base_url('users'));
    }

	public function delete()
	{
        if( $this->user_model->delete($this->input->post('delete_id')) ){
            set_alert('success','User deleted successfully.');
        } else {
            set_alert('error','Error while deleting User .');
        }
        redirect('users');
    }
*/    




}