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

class Login extends CI_Controller {

	function __construct() 
	{
       error_reporting(0);
        parent::__construct();
        $this->load->model('CommonModel');
        $this->load->helper('Security');
        $this->load->library('form_validation');       
        $this->load->library('cart');
    }

	
	public function verify() {
        $input_data = $this->input->post();
        $input_data = $this->security->xss_clean($input_data);
        $user_id = $input_data['username'];
        $password =base64_encode($input_data['password']);
        
          
        $query = $this->db->query("select * from `customer` where  (`mobile`='".$user_id."' or `email`='".$user_id."') and `pass`='".$password."'"); 
  $user_exsist=$query->result_array();

        
        if (!empty($user_exsist) && count($user_exsist) > 0) 
        {
            
            $this->session->set_userdata('user_id', $user_exsist[0]['c_id']);  
            $this->session->set_userdata('customer', $user_exsist[0]['c_id']);          
            $this->session->set_userdata('name', $user_exsist[0]['name']);
            $this->session->set_userdata('email', $user_exsist[0]['email']);
            $this->session->set_userdata('mobile', $user_exsist[0]['mobile']);
            $this->session->set_userdata('state', $user_exsist[0]['state']);
            $this->session->set_userdata('city', $user_exsist[0]['city']);
            $this->session->set_userdata('address', $user_exsist[0]['address']);
            $this->session->set_userdata('login_session', true);
            
            echo 'success'; 
        } 
        else 
        {
            echo 'failed';
        }
    }

    public function register_save()
  {

  $input_data = $this->input->post();
  $input_data = $this->security->xss_clean($input_data);
  
  $query = $this->db->query("select * from `customer` where `email`='".$input_data['r_email']."'"); 
  $row=$query->result_array();

  if ($row) {
   echo "emailexist";die;
  }
  else {
  
        
  
  $customer_data['name'] = $input_data['name'];
  $customer_data['mobile'] = $input_data['r_mobile'];
  $customer_data['email'] = $input_data['r_email'];
  $customer_data['address'] = $input_data['address'];
  $customer_data['pass'] = base64_encode($input_data['r_password']);
  $customer_data['veryfied'] = '1'; 
  $this->db->insert('customer',$customer_data);
  $customer_id=$this->db->insert_id();
  $query = $this->db->query("select * from `customer` where `c_id`='".$customer_id."' "); 
  $user_exsist=$query->result_array();

  $this->session->set_userdata('user_id', $user_exsist[0]['c_id']);            
  $this->session->set_userdata('name', $user_exsist[0]['name']);
  $this->session->set_userdata('email', $user_exsist[0]['email']);
  $this->session->set_userdata('mobile', $user_exsist[0]['mobile']);
  $this->session->set_userdata('login_session', true);
  
  echo "success";

  }


  }
}
?>