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

class Authentication extends CI_Controller {
	function __construct()
	{
		parent::__construct();

		$this->load->model('master_model');
    $this->load->helper('email_helper');
    $this->load->library('encryption');
		//$this->load->library("phpmailer");
  
		//$mail = $this->phpmailer_lib->load();

	}
	function verification(){
 
		/* $this->form_validation->set_rules('login', 'Login', 'trim|required|xss_clean');
		//$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|md5');
		$this->form_validation->set_rules('remember', 'Remember me', 'integer');
        
   		if ($this->form_validation->run()) {
			
			 */

            $login = $this->input->post('login');
            $password= $this->input->post('password');
            //$password="08fa0a7e19b1eaaa7655d54fabf8ec61";
            $pass=md5($password);
        

                        	   $condition[] = array('where','user_name',$login);                            
                        	   $condition[] = array('where','user_password',$pass);
                             $condition[] = array('where',TABLE_USERS.'.is_active','1');
                             $condition[] = array('join',TABLE_USER_TYPE,TABLE_USERS.'.user_type_id ='.TABLE_USER_TYPE.'.user_type_id');

    			 $select_array=array(
                                'table_name'=>TABLE_USERS,
                                'table_fields'=>'user_name,user_password,user_id,'.TABLE_USERS.'.user_type_id,user_type,module_ids,first_name,last_name,company_branch_id,accessible_branches',
                                'condition'=>$condition,
                                'limit'=>'',
                                'offset'=>''
                            );
                $field = $this->master_model->select_data($select_array);            
                         
                 foreach ($field as  $value) {

                    $user_type_id=$value['user_type_id'];
                    $module_ids=$value['module_ids'];
                    $user_type=$value['user_type'];
                    $user_id=$value['user_id'];
                    $session_first_name=$value['first_name'];
                    $session_last_name=$value['last_name'];
                    $company_branch_id=$value['company_branch_id'];
                    $access_branch=$value['accessible_branches'];
                 }
               $newdata = array( 
                            'user_type_id'  => $user_type_id, 
                            'module_ids'     => $module_ids, 
                            'user_type' => $user_type,
                            'user_id' => $user_id,
                            'session_first_name' => $session_first_name,
                            'session_last_name' => $session_last_name,
                            'company_branch_id'=>$company_branch_id,
                            'access_branch'=>$access_branch
               ); 
              $this->session->set_userdata($newdata);
              $users['user']=$this->master_model->select_data($select_array);
                        
                if(!empty($users['user'])&& is_array($users['user'])){

                  //$this->session->set_flashdata('message',' data updated  successfully');
				  redirect('gc/Dashboard');
				  // $this->session->set_userdata(array('user'=>$user)); 
  
  	            }else{
                    $this->session->set_flashdata('message','Invalid Username and Password');
  	            	redirect('Main/index');
  	            }
        /* }else{
          redirect('main/index');
        } */
	      

	//}
   }

   function forgot_password(){
		
    //error_reporting(E_ALL);
		$this->_page_data['page_title']="Forgot Password";
		$this->_page_data['bredcrum']=array();
		$this->load->view('forgot_password_form',$this->_page_data);
	}
	function mail_forgot_passoword(){
    //error_reporting(E_ALL);
    $email_id = check_injections_filter($this->input->post('email'));
	$condition[] = array('where','user_name',$email_id);
      $selected_array = array( 
                         'table_name' =>TABLE_USERS,
                         'table_fields' => 'user_id,user_name',
                         'order_by_field'=>'Asc',
                         'condition'=>$condition,
                         'limit'=>'',
                         'offset'=>''
    );
    $send = $this->master_model->select_data($selected_array); 
	$cnt=count($send);
	if($cnt==0)
	{
		$this->_page_data['page_title']="Forgot Password";
		$this->_page_data['bredcrum']=array();
		 $this->session->set_flashdata('message','Invalid Email id');
		$this->load->view('forgot_password_form',$this->_page_data);
	}
	else
	{
		//$this->session->set_userdata($email_id)
		$this->send($email_id); 
	}
}

function send($email_id){
   $condition[] = array('where','user_name',$email_id);
      $selected_array = array( 
                         'table_name' =>TABLE_USERS,
                         'table_fields' => 'user_id,user_name,first_name,last_name',
                         'order_by_field'=>'Asc',
                         'condition'=>$condition,
                         'limit'=>'',
                         'offset'=>''
    );

 
    $send = $this->master_model->select_data($selected_array); 
    foreach ($send as  $value) {
      $userid = $value['user_id'];
      $user_name = $value['user_name'];
      $fname = $value['first_name'];
      $lname = $value['last_name'];
	  
	  $name=$fname." ".$lname;
    }
	
	
	
   // $userid=$this->encrypt->encode($userid);
	 $userid=encrypt_decrypt_parameters($userid,'encrypt');
    $url="https://shagoon.nityamhygiene.in/dev1/index.php/authentication/change_password/".$userid;
    //$success_message='Your request for forgot password submitted successfully.<br>The e-mail has been sent. Use the link contained in the email to change your password.';

    $mailContent = "
                    <table align=center border=0 cellpadding=8 cellspacing=0 width=600>    <tbody>        <tr>            <td align=left bgcolor=#f26e2e style=font-family:Arial, Helvetica, sans-serif; font-size:12px; line-height:18px; color:#666666; text-align:justify; font-family:Arial, Helvetica, sans-serif; font-size:12px; line-height:18px; color:#ffffff; padding:4px; valign=top><img src=https://shagoon.nityamhygiene.in/assets/images/logohygiene.jpg></td>
      </tr>
      <tr>
          <td align=left style=border-left:4px solid #CBE4EC; border-right:4px solid #CBE4EC; font-family:Arial, Helvetica, sans-serif; font-size:12px; line-height:18px; color:#666666; text-align:justify; padding:10px; valign=top><p><strong>Dear ".$name.",</strong></p>

  
        <p>Welcome to Nityam Hygiene - Refill Management System.</p> 
        <p>Click on this link to reset your password</p> 
            <p><a href='".$url."'>Link</a></p>
            
          <p>Warm Regards,</p>

          <p>Nityam Hygiene</p>
        </td>
       </tr>
     
  </tbody>
</table>
                    ";
	$subject="Password Reset";
	eval("\$mailContent = \"$mailContent\";");
  //   $mail->Body = $mailContent;
  $config['charset'] = 'UTF-8';
$config['encoding'] = '8bit'; // or 'base64'
$this->load->library('email', $config);
  //  $this->load->library('email'); 
    $this->email->from('no-reply@shagoon.nityamhygiene.in','Nityam Hygiene');
    $this->email->to($email_id);
   // $this->email->subject("asdsdasd");
   $this->email->set_header('Subject', '=?UTF-8?B?'.base64_encode($subject).'?=');
    $this->email->message($mailContent);
    //$this->email->set_newline("\r\n");
   // $this->email->crlf = "\n";
    // $this->email->send();
	
     if (!$this->email->send()) {
		show_error($this->email->print_debugger()); 
  }
     else {
       $this->session->set_flashdata('message','link has been sent to your email');
      redirect('main/index');
     }
  }
    function change_password($userid=""){
      
      // $userid=$this->encrypt->decode($userid);
     // error_reporting(E_ALL);
        $this->_page_data['page_title']="Change Password";
        // $this->session->set_flashdata('user_id',$userid);
        $this->_page_data['user']=$userid;
        $this->_page_data['bredcrum']=array(			                           
										'Change Password'=>'#',
	 									);
        $this->load->view('change_password',$this->_page_data);      
    }
    function Changing_password($userid=""){
    //error_reporting(E_ALL);
   
     //$userid=$this->encrypt->decode($userid);
	  $userid=encrypt_decrypt_parameters($userid,'decrypt');
     $new_password = check_injections_filter($this->input->post('password'));
     $confirm_password= check_injections_filter($this->input->post('confirm_password'));
     $confirm_password=md5($confirm_password);
        $change_data=array(
                'user_password'=>$confirm_password              
              );

      //$condition[] = array('where','user_id',$user_id);
           $this->session->set_flashdata('message',' Password changed successfully');
            $condition=array('user_id'=>$userid);
            $this->master_model->update_record('users', $change_data,$condition);
            redirect('main/index');
         }

    function User_change_password(){

      $user_id = $this->session->userdata('user_id');
      //$userid=$this->encrypt->encode($user_id);
	  $userid=encrypt_decrypt_parameters($user_id,'encrypt');
      error_reporting(E_ALL);
        $this->_page_data['page_title']="Change Password";
        // $this->session->set_flashdata('user_id',$userid);
        $this->_page_data['user']=$userid;
        $this->_page_data['bredcrum']=array(			                           
										'Change Password'=>'#',
	 									);
        $this->load->view('user_changed_password',$this->_page_data);      
    }
    function User_changing_password($userid){

    // error_reporting(E_ALL);
      
      //$userid=$this->encrypt->decode($userid);
        $userid=encrypt_decrypt_parameters($userid,'decrypt');
      $new_password = check_injections_filter($this->input->post('password'));
      $confirm_password= check_injections_filter($this->input->post('confirm_password'));
      //$confirm_password1=md5($confirm_password);
      $old_password = check_injections_filter($this->input->post('old_password'));
      $old_password=md5($old_password);

     
       $condition[] = array('where','user_id',$userid);
       $condition[] = array('where','user_password',$old_password);

      $select_array=array(
                                'table_name'=>'users',
                                'table_fields'=>'user_password,user_id',
                                'condition'=>$condition,
                                'limit'=>'',
                                'offset'=>''
                            );
      $old['old']=$this->master_model->select_data($select_array);
	  //dump($this->db->last_query());
	$cnt=count($old['old']);
     if($cnt>0){

        $change_data=array(
                'user_password'=>md5($confirm_password)             
             );

         
          $this->session->set_flashdata('message',' Pasword changes successfully');
          $condition=array('user_id'=>$userid);
         // $condition= array('where','user_id',$userid);
           $this->master_model->update_record('users', $change_data,$condition);
           redirect('main/index');

         }else{
             
             
            $this->session->set_flashdata('message','Incorrect Old Password');
            redirect('authentication/User_change_password');
           
         }

     
     
    }

}
   
           
			//header('location:index.php');
	