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

class Login extends CI_Controller {  
     public function __construct() {
        parent::__construct();  
        date_default_timezone_set('Asia/Kolkata');
         $this->load->database();
         $this->load->model('Login_model');
        
              
    }
    
    
    
    public function user_registration()
    {
        if (!empty($_POST))
        {
            $sql = "SELECT user_id FROM tbl_user ORDER BY id DESC LIMIT 1";
                    $code = $this->Login_model->query($sql);
                    
                    if (!empty($code)) 
                    {
                        $prefix = 'User';
                        $string_with_number = $code[0]['user_id'];
                        preg_match_all('/([a-zA-Z]+)(\d+)/', $string_with_number, $matches);
                        $string_part = $matches[1][0]; 
                        $number_part = $matches[2][0]; 
                        $userrid = $number_part + 1;
                        $user_id = $prefix . $userrid ;
                    } 
                    else 
                    {
                        $user_id='User1';
                    }
            $data = array (
                
                'user_id'=>$user_id,
                'f_name'=>$this->input->post('f_name'),
                'password'=>$this->input->post('password'),
                'mobile_no'=>$this->input->post('mobile_no'),
                'email_id'=>$this->input->post('email_id'),
                'pincode'=>$this->input->post('pincode'),
                'address'=>$this->input->post('address'),
                'status'=> 1,
                'created_date' => date('Y-m-d_H-i')
                
                );
                
                $this->Login_model->insert('tbl_user',$data);
                $this->session->set_flashdata('message_name', 'Registration Successfull..! ');
                redirect('Base');  
        }
         else  
        {
            $this->session->set_flashdata('message_name','Something Wrong try Again..!');
            redirect('Base');
        }
    }
    
    
    public function login_admin(){
        // $password = 1234;
        // $encrypted_password = $this->encryption->encrypt($password);
        // print_r($encrypted_password);exit;
        if(!empty($_POST)){
        
        
        $validate1 = $this->Login_model->select_column_where(['id','admin_id','phone','email_id','password','status'],
        'tbl_admin',['admin_id' => $this->input->post('user_id')]);
    
        
        if(!empty($validate1)){
        
        $validate1 = $validate1[0]; 
        // print_r($this->encryption->decrypt($validate1['password']));exit;
        if ($this->input->post('password') == $validate1['password']) {
        
        if($validate1['status'] == 1 ){   
        $user_login_id =$validate1['admin_id']; 
        $email =$validate1['email_id']; 
        $phone =$validate1['phone']; 
        $id =$validate1['id']; 
        $user_type = 'Admin';  
        
        $this->session->set_flashdata('msg','Login Successful.');
        $this->session->set_userdata('admin_login_id',$user_login_id);
        $this->session->set_userdata('email',$email);
        $this->session->set_userdata('phone',$phone);
        $this->session->set_userdata('id',$id);
        $this->session->set_userdata('user_role',$user_type);
        
        
        redirect('Admin/dashboard');
        
        
        }else{
        $this->session->set_flashdata('error','Your account not active..! contact admin');
        redirect('Base/login');
        }
        } else{
        $this->session->set_flashdata('error','Invalid password..Try again');
        redirect('Base/login');
        }
        }
        else{
        $this->session->set_flashdata('error','Invalid Username');
        redirect('Base/login');
        } 
  
        
        
        // }
        } else  {
        $this->session->set_flashdata('error','Invalid Username And Password..');
        redirect('Base/login');
        }
        
        
        
    }
    
    
    
    
    
    
    
    
}