<?php
	
	class Login extends CI_Controller {
		
		function __construct() {
			// Call the Model constructor
			parent::__construct();
			$this->load->database();
			$this->load->library('session');
			$this->load->helper('form');
			$this->load->library('form_validation');
			$this->load->model('Login_check');
			
			$this->load->helper('url');
			//$this->load->library('parser');
			//$this->load->library('encrypt');
			
		}
		
			function index() {

			  $username = $this->input->post("username");
			$this->load->helper(array('form'));
			
			/* Load form validation library */
			$this->load->library('form_validation');
			$this->form_validation->set_rules('username', 'username', 'callback_login_check');
			
			if ($this->form_validation->run() == false) {
				
				$this->load->view('login');
			}
			
			if ($this->form_validation->run() == true) {
				
				$login_type=$_SESSION['login_type'];
				
				if($login_type=='super_admin')
				{ 
					$session_rest_id=''; 
							redirect('admin_dashboard');
				}
				else
				{ 
					$session_rest_id=$_SESSION['user_id'];
					 
					redirect('admin_dashboard');
				}
			}
		
		}
		function submitLogin()
		{
				$this->load->view('dashboardView');
		}
		function duplicate_contact()
		{
			$custContact = $this->input->post("custContact");
			$cust_flag=$this->Login_check->duplicate_contact($custContact);
			echo $cust_flag;
			
		}
		
		function login_check($username) {
			$password = $this->input->post("password");
			
			if (!$this->Login_check->login($username, $password)) {
				
				$this->form_validation->set_message('login_check', 'The {field} field can not match"');
				return false;
			}
			return true;
		}
		
		function do_reset_password_notify()
		{	
			$curr_url = $this->input->post("curr_url");
			if($this->input->post('username_or_email'))
			{
				$custrow = $this->Login_check->get_details_username_or_email($this->input->post('username_or_email'));
				if ($custrow)
				{
					//$data = array();
					//$data['rest'] = $custrow;
					$cust_id=$custrow->customer_id;
					$rest_list_array=array();
					$chkdata=array();
					$rest_list_array['chkdata'] = base64_encode('rest|'.$cust_id.'|'.(time() + (2 * 24 * 60 * 60)));
					//	print_r($rest_list_array);
					
					$this->load->library('email');
					$config['mailtype'] = 'html';
					$this->email->initialize($config);
					$this->email->from('info@primebazar.in', 'INFO@123#');
					$this->email->to($custrow->cust_email); 
					$this->email->subject('Forgot Password');
					
					$message=$this->load->view('forgot_map_mail_formatview',$rest_list_array,TRUE);
					$this->email->message($message);
					$this->email->send();
					
					$succdata['success']='Reset Password Link has been sent.';
					//$this->load->view('reset_password',$succdata);
					redirect($curr_url);
				}
				else 
				{
					$errdata['error']='Username/Email ID does not exist';
					//$this->load->view('reset_password',$errdata);
				}
			}
			else
			{
				$errdata['error']= 'Field Can not be Empty';
				//$this->load->view('reset_password',$errdata);
			}
		}
		
		function reset_password_enter_password($key=false)
		{
			$url_param = rtrim($key, '=');
			$base_64a = $url_param . str_repeat('=', strlen($url_param) % 4);
			$data = base64_decode($base_64a);
			$data2 = explode("|",$data);
			$restdata['check_flag']=$data2[0];
			$restdata['check_id']=$data2[1];
			
			$this->load->view('reset_password_enter_password', $restdata, FALSE);			
			
		}
		
		function do_reset_password()	{ 
			 $check_id = $this->input->post('check_id');			 
		 	 $check_flag = $this->input->post('check_flag');			 
			 $password = $this->input->post('rest_new_password');
   $clinic_username = $this->input->post('clinic_username');			 
			 $confirm_password = $this->input->post('rest_confirm_password');
			 $this->Login_check->save_reset_password($check_id,$clinic_username,$password,$check_flag); 
		}
		
		
		public function logout()
		{
			$this->session->unset_userdata('login_id');
			$this->session->unset_userdata('login_type');
			
					$this->session->unset_userdata('session_onpage');
			$this->session->sess_destroy();
			redirect(base_url().'index.php'); 
		}
		
		
		public function customer_login()
		{
			$username = $this->input->post("username");
			$password = $this->input->post("password");
			$curr_url = $this->input->post("curr_url");
			$this->load->helper(array('form'));
		
			if ($this->Login_check->cust_login_method($username, $password)) {
			
				  echo '1';
			}
			  else
			  {
                echo '0';
            }
			
		}
		public function guess_login()
		{
			$username = $this->input->post("guest_username");
			
			$curr_url = $this->input->post("curr_url");
			$this->load->helper(array('form'));
			
			$res =$this->Login_check->guest_login_method($username);
			
			
		}
		
		
		
		
	}
	
?>	