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

class Login extends CI_Controller {
    
    public function index(){
        /************************************/
        // loading common model
        $this->load->model('Common_model');
        
        /************************************/
        /************************************/
       	    $column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
		$i = 1;
        $result23456789 = $this->Common_model->Results('tbl_language');
        foreach ($result23456789 as $row)
        {
            define('LANG_VALUE_' . $i, $row['lang_value']);
            $i++;
        }
        
	    $columntop_cities['status'] = '1';
		$tabletop_cities = 'tbl_top_cities';
        // we are getting results from common modal
		$resulttop_cities = $this->Common_model->ResultsWhere2($columntop_cities,$tabletop_cities);
		
		
	    $column3['status'] = '1';
	    $column3['status_home'] = '1';
		$table3 = 'tbl_top_category';
        // we are getting results from common modal
		$top_category = $this->Common_model->ResultsWhere2($column3,$table3);
		
	    $column3['status'] = '1';
	    $column3['status_home'] = '1';
		$table3 = 'tbl_mid_category';
        // we are getting results from common modal
		$mid_category = $this->Common_model->ResultsWhere2($column3,$table3);
		
	    $column3['status'] = '1';
	    $column3['status_home'] = '1';
		$table3 = 'tbl_end_category';
        // we are getting results from common modal
		$end_category = $this->Common_model->ResultsWhere2($column3,$table3);
		
		
	    $column3['status'] = '1';
	    $column3['status_home'] = '1';
		$table3 = 'tbl_top_category';
        // we are getting results from common modal
		$top_category = $this->Common_model->ResultsWhere2($column3,$table3);
		
	    $column32['status'] = '1';
		$table32 = 'tbl_mid_category';
        // we are getting results from common modal
		$data['mid_category2'] = $this->Common_model->ResultsWhere2($column32,$table32);
		
	    $column33['status'] = '1';
		$table33 = 'tbl_end_category';
        // we are getting results from common modal
		$data['end_category2'] = $this->Common_model->ResultsWhere2($column33,$table33);
		
	    $column31['status'] = 1;
		$table31 = 'tbl_top_cities';
        // we are getting results from common modal
		$top_cities = $this->Common_model->ResultsWhere2($column31,$table31);
		
		
	    $column31['status'] = 1;
		$table31 = 'tbl_social';
        // we are getting results from common modal
		$social = $this->Common_model->ResultsWhere2($column31,$table31);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['top_cities'] = $top_cities;
		$data['top_category'] = $top_category;
		$data['mid_category'] = $mid_category;
		$data['end_category'] = $end_category;
		$data['social'] = $social;
        
        /************************************/
        $data['page'] = 'Login';
        
        /************************************/
        $column31['status'] = 1;
        $table31 = 'tbl_social';
        // we are getting results from common modal
        $data['social'] = $this->Common_model->ResultsWhere2($column31,$table31);
        
        /************************************/
        // we are loading admin dashboard
        $this->load->view('website/login',$data);	
    }

    public function verify()
    {
        if(isset($_POST['cust_email'])){
            /************************************/
            // loading common model
            $this
                ->load
                ->model('Common_model');
                
            /************************************/
            $column['id'] = '1';
            $table = 'tbl_settings';
            
            /************************************/
            // we are getting results from common modal
            $data = $this->Common_model->Result($column,$table);
            /************************************/
            
            $i = 1;
            $result = $this->Common_model->Results('tbl_language');
            foreach ($result as $row)
            {
                define('LANG_VALUE_' . $i, $row['lang_value']);
                $i++;
            }
    
            $status = 0;
            $success_message = '';
            $error_message = '';
            $header = '';
            
            if(!isset($_POST['cust_email']) || empty($_POST['cust_email'])){
                $error_message = 'Email Or Mobile Number Can not be empty' ;
            }
            else
            {   
                if(!isset($_POST['cust_password']) || empty($_POST['cust_password'])){
                    $error_message = LANG_VALUE_138;
                }
                else
                {
                    $cust_email = strip_tags($_POST['cust_email']);
                    $cust_password = strip_tags($_POST['cust_password']);
            
                    $column14['cust_email'] = $cust_email;
                    $column15['cust_phone'] = $cust_email;
                    $table16 = 'tbl_customer';
                    $result2 = $this->Common_model->Results_or_Where3($column14,$column15,$table16);
                        
                    $total = count($result2);
                    foreach($result2 as $row) {
                        $cust_status = $row['cust_status'];
                        $row_password = $row['pass'];
                    }
                    if($total==0) {
                        $error_message = 'Invalid User Details.';
                    } else {
                        if( $row_password != $cust_password) {
                            $error_message = LANG_VALUE_139;
                        } else {
                            if($cust_status == 0) {
                                $error_message = LANG_VALUE_148;
                            } else{
                                $_SESSION['customer'] = $row;
                                $this->session->set_userdata('customer',$row);
                                $status = 1;
                                // $this->session->set_flashdata('success','Logged in successfully.');
                                $success_message = 'Logged in successfully.';
                            }
                        }
                        
                    }
                }
            }
            
            // response for user
            $myObj = new \stdClass();
            $myObj->succ = $success_message;
            $myObj->error = $error_message;
            $myObj->log = $status;
            if(isset($_SESSION['type'])){
                $myObj->form = $_SESSION['type'];
            }
            $myJSON = json_encode($myObj);
            echo $myJSON;
        }
        else{
            redirect(base_url());
        }
    }
    
    public function logout(){
        if(isset($_SESSION['customer'])){
            unset($_SESSION['customer']);
            $_SESSION = array();
            $_SESSION['kkks']=2;
        }
        $this->session->set_flashdata('success','Logged out successfully.');
		redirect(base_url());
    }
    
    
    
    
    
     public function verifys()
    {
        if(isset($_POST['kush'])){
            /************************************/
            // loading common model
            $this
                ->load
                ->model('Common_model');
                
            /************************************/
            $column['id'] = '1';
            $table = 'tbl_settings';
            
            /************************************/
            // we are getting results from common modal
            $data = $this->Common_model->Result($column,$table);
            /************************************/
            
            $i = 1;
            $result = $this->Common_model->Results('tbl_language');
            foreach ($result as $row)
            {
                define('LANG_VALUE_' . $i, $row['lang_value']);
                $i++;
            }
    
            $status = 0;
            $success_message = '';
            $error_message = '';
            $header = '';
            
            
             
                    $cust_email = strip_tags($_POST['kush']);
                
                    $column14['uid'] = $cust_email;
                
                    $table16 = 'tbl_customer';
                    $result2 = $this->Common_model->ResultsWhere2($column14,$table16);
                        
                    $total = count($result2);
                    
                    
                    foreach($result2 as $row) {
                        $cust_status = $row['cust_status'];
                        $row_password = $row['pass'];
                    }
                    
                    
                    if($total==0) {
                        $error_message = 'Invalid User Details.';
                    } else {
                      
                            if($cust_status == 0) {
                                $error_message = LANG_VALUE_148;
                            } else{
                                $_SESSION['customer'] = $row;
                                $this->session->set_userdata('customer',$row);
                                $status = 1;
                                $_SESSION['kkks']=1;
                             
                            }
                        
                        
                    }
                
           
            redirect(base_url()); 
           
           
        }
        else{
            redirect(base_url());
        }
    }

}

