<?php

class Login_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        
       
       $this->load->database('grocery_database');    
    }
    function check_user_exist($email,$phone)
    {
       $sql="SELECT * FROM `grocery_users` WHERE  (`phone`= '$phone' OR `email`= '$email') AND `status`= '1'";
       $que= $this->db->query($sql);
       $row=$que->row_array();
       
       
       return $row;
    }
     
     
     
      public function get_api_url() {
        $query = $this->db->get('api_config', 1);
        return $query->row()->api_url;
    }
     
     
     
     
     
     
     
     
     
     
     
     
     
     public function check_selleruser_exist($email,$phone)
     {
         $sql="SELECT * FROM `grocery_seller` WHERE  (`phone`= '$phone' OR `email`= '$email')";
       $que= $this->db->query($sql);
       $row=$que->row_array();
       
        return $row;
     }
    function selectusersdata($phone)
    {
         $sql="SELECT * FROM `grocery_users` WHERE  (`phone`= '$phone' OR `email`= '$phone') AND `status`= '1'";
           $que= $this->db->query($sql);
           $regcust = $que->row();
          
          if ($regcust) {
                 return $regcust;
          } else {
            return false;
         }
         
    }
    
    function selectdeliverydata($phone)
    {
         $sql="SELECT * FROM `grocery_deliveryboy` WHERE  (`mobile_no`= '$phone') AND `status`= '1'";
           $que= $this->db->query($sql);
           $regcust = $que->row();
          
          if ($regcust) {
                 return $regcust;
          } else {
            return false;
         }
         
    }
    
    
    function set_f_token_web($user_id, $token) 
    {
        $this->db->where('user_id', $user_id);
        $this->db->update('grocery_users', array('f_token_web' => $token));
    }
    
    function set_f_token__delgorcery_web($user_id, $token) 
    {
        $this->db->where('deli_user_id', $user_id);
        $this->db->update('grocery_deliveryboy', array('f_token_web' => $token));
    }
    
    function is_registered($phone)
    {   
        $sql="SELECT `phone`,id,name,user_id FROM `grocery_users` WHERE `phone`= '$phone' AND status=1";
        $que1= $this->db->query($sql);
        return $que1->row_array();
           
    }
    
    function login_with_otp_validate($mobile)
    {   
            //$mob=$_COOKIE['Mobile'];
      $sql="SELECT * FROM `all_otp` WHERE phone='$mobile' AND status=0 ORDER BY id DESC LIMIT 1";
      $que= $this->db->query($sql);
      return $que->row_array();
    }
    
        
             
             
    function register($register)
    {
        $this->db->insert('grocery_users', $register);
        return $this->db->insert_id();
    }   
    
    
    public function get_selleridforemail($id)
    {
        $this->db->select('*');
        $this->db->from('grocery_seller');
        $this->db->where('email', $id);
        $query = $this->db->get();
        return $query->row();
    }
    
     public function wluserdomain($domain)
    {
        $this->db->select('user_id');
        $this->db->from('grocery_wluser');
        $this->db->where('domain', $domain);
        $query = $this->db->get();
        return $query->row();
    }
    
    function set_f_token_web_seller($user_id, $token) {
        $this->db->where('seller_id', $user_id);
        $this->db->update('grocery_seller', array('f_token_web' => $token));
    }
    
    
    function selectsellerdata($phone)
    {
         $sql="SELECT * FROM `grocery_seller` WHERE  (`phone`= '$phone' OR `email`= '$phone')";
           $que= $this->db->query($sql);
           $regcust = $que->row();
          
          if ($regcust) {
                 return $regcust;
          } else {
            return false;
         }
         
    }
    
    function is_registered_seller($phone)
    {   
        $sql="SELECT * FROM `grocery_seller` WHERE `phone`= '$phone'";
        $que1= $this->db->query($sql);
        return $que1->row_array();
           
    }
   
    /**********End from latest***********/
    public function SelectData($table = null, $field = null, $cond = null, $limit = null, $order = null)
    {
        $this->db->select($field);
        $this->db->from($table);
        $this->db->where($cond);
        $query = $this->db->get();
        $data = $query->result();
        if ($data) {
            return $data;
        } else {
            return false;
        }
    }
    
    function selectsellerdata1($phone)
    {
     $sql="SELECT * FROM `grocery_seller` WHERE (`phone`= '$phone' OR `email`= '$phone') AND `status`= '1'";
           $que= $this->db->query($sql);
           $regcust = $que->result();
          if ($regcust) {
        return $regcust;
    } else {
        return false;
    }
    } 
    

    function add_seller($salary)
    {
        $this->db->insert('grocery_seller', $salary);
         return $this->db->insert_id();
    } 
    function add_wluser($salary)
    {
        $this->db->insert('grocery_wluser', $salary);
         return $this->db->insert_id();
    }
   
    function add_otp($otp)
    {
        $this->db->insert('grocery_sellervierifotp', $otp);
        return $this->db->insert_id();
    }
    
    function seller_otpcheck($phone_no,$otp)
                {   
                    //$mob=$_COOKIE['Mobile'];
                    
                      $sql="SELECT `otp` FROM `grocery_sellervierifotp` WHERE `phone`= '$phone_no' AND `otp`= '$otp'";
                       $que1= $this->db->query($sql);
                       $output = $que1->num_rows();
                      return $output;
                }

    function is_registered_user($phone_no)
    {
        //$mob=$_COOKIE['Mobile'];

      
        $sql = "SELECT `phone` FROM `grocery_users` WHERE `phone`= '$phone_no'";
        $que = $this->db->query($sql);
        $output2 = $que->num_rows();
        if ($output2 > 0) {
            return true;
        } else {
            return false;
        }
    
    }
    
    
    
    function getname($phone)
                {
                 $sql="SELECT `name` FROM `grocery_seller` WHERE `phone`= '$phone'";
                       $que= $this->db->query($sql);
                       $output= $que->row();
                       if ($output == '') {
            $sql = "SELECT `name` FROM `grocery_users` WHERE `phone`= '$phone'";
            $que = $this->db->query($sql);
            $output2 = $que->row();
            if ($output2 != '') {
                return $output2;
            } else {
                return false;
            }
        } else {
            return $output;
        }
                     
    } 

    function is_registeredE($email_id)
    {
        //$mob=$_COOKIE['Mobile'];

        $sql = "SELECT `email` FROM `grocery_seller` WHERE `email`= '$email_id'";
        $que1 = $this->db->query($sql);
        $output = $que1->num_rows();
        if ($output == 0) {
            $sql = "SELECT `email` FROM `grocery_users` WHERE `email`= '$email_id'";
            $que = $this->db->query($sql);
            $output2 = $que->num_rows();
            if ($output2 == 1) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }
    function is_registeredwlE($email_id)
    {
        //$mob=$_COOKIE['Mobile'];

        $sql = "SELECT `email` FROM `grocery_wluser` WHERE `email`= '$email_id'";
        $que1 = $this->db->query($sql);
        $output = $que1->num_rows();
        if ($output == 0) {
            return false;
        } else {
            return true;
        }
    } 
    
      function is_registereddelivery($email_id)
    {
        //$mob=$_COOKIE['Mobile'];

        $sql = "SELECT * FROM `grocery_deliveryboy` WHERE `email_id`= '$email_id'";
        $que1 = $this->db->query($sql);
        $output = $que1->num_rows();
        if ($output == 0) {
            return false;
        } else {
            return true;
        }
    }  
    function is_registered1($phone)
    {
        //$mob=$_COOKIE['Mobile'];

        $sql = "SELECT * FROM `grocery_deliveryboy` WHERE `mobile_no`= '$phone'";
        $que1 = $this->db->query($sql);
        $output = $que1->num_rows();
        if ($output != 0) {
                return true;
            } else {
                return false;
            }
    } 
    
     function emailcheckdeliv($email_id)
    {
        //$mob=$_COOKIE['Mobile'];

            $sql = "SELECT * FROM `grocery_deliveryboy` WHERE `email_id`= '$email_id'";
            $que = $this->db->query($sql);
            $output = $que->num_rows();
        if ($output != 0) {
                return true;
            } else {
                return false;
            }
            
    }
    function is_registeredwlp($phone_no)
    {
        //$mob=$_COOKIE['Mobile'];

        $sql = "SELECT `email` FROM `grocery_wluser` WHERE `phone`= '$phone_no'";
        $que1 = $this->db->query($sql);
        $output = $que1->num_rows();
        if ($output == 0) {
            return false;
        } else {
            return true;
        }
    }
    
     public function select_column($columns, $table_name) {
        return $this->db->select($columns)->get($table_name)->result_array();
    }
    
       public function select_column_where($columns, $table_name, $condition) {
        return $this->db->select($columns)->where($condition)->get($table_name)->result_array();
    }
    
      public function update($table_name, $condition, $data) {              
        return $this->db->where($condition)->update($table_name, $data);
    } 
    
    


                 function get_otp($mobile)
                {

                  $otp=rand(400000,800000);
                  $sql="SELECT `phone` FROM `grocery_users` WHERE `phone`= '$mobile'";
                 
                       $que1= $this->db->query($sql);
                       $output = $que1->num_rows();
                       $otps="UPDATE `grocery_users` SET user_otp = '$otp' WHERE `phone`= '$mobile'";
                       $ques= $this->db->query($otps);
                   
                        //$mob=$_COOKIE['Mobile'];
                       $sql="SELECT * FROM `grocery_users` WHERE `phone`='$mobile'";
                       $que= $this->db->query($sql); 
                       $arr=$que->result();                      
                       foreach($arr as $row ) {
                               $row->user_otp;
                       }
                      if($output==0) {
                        $sql="SELECT `phone` FROM `grocery_seller` WHERE `phone`= '$mobile'";
                       $que= $this->db->query($sql);
                      $output2 = $que->num_rows();
                      $otps="UPDATE `grocery_seller` SET otp = '$otp' WHERE `phone`= '$mobile'";
                       $ques= $this->db->query($otps);
                   
                        //$mob=$_COOKIE['Mobile'];
                       $sql="SELECT * FROM `grocery_seller` WHERE `phone`='$mobile'";
                       $que= $this->db->query($sql); 
                       $arr=$que->result();                      
                       foreach($arr as $row ) {
                               $row->otp;
                       }
                      if($output2==1){
                        return $row->otp;
                      }else{
                        return false;
                      }}else{
                        return $row->user_otp;
                      }
                }

                 function snd_otp($number,$otp)
                {
                    
                     $sql="SELECT * FROM `grocery_users` WHERE `phone`= '$number' ";
                    $que1= $this->db->query($sql);
                    $output = $que1->num_rows();
                    $nameshoe=$que1->result();
                    if($output==1){ 
                         $sql="SELECT * FROM `grocery_users` WHERE `phone`='$number'";
                       $que= $this->db->query($sql); 
                       $arr=$que->result();                      
                       foreach($arr as $row ) {
                              $otp= $row->user_otp;
                       }
        
                          return $otp;
                      }
                    
                else{
                    $sql="SELECT * FROM `grocery_seller` WHERE `phone`= '$number'";
                    $que1= $this->db->query($sql);
                    $output = $que1->num_rows();
                    $nameshoe=$que1->result();
                    if($output==1){ 
                       
                         $sql="SELECT * FROM `grocery_seller` WHERE `phone`='$number'";
                       $que= $this->db->query($sql); 
                       $arr=$que->result();                      
                       foreach($arr as $row ) {
                              $otp= $row->otp;
                       }
                                      return $otp;
                    }
                }
                }

                

                 function make_login_create_session($phone_no_otp,$mob)
                {   
                       // $mob=$_COOKIE['Mobile'];
                        $sql="SELECT * FROM `grocery_users` WHERE `phone`='$mob' AND user_otp= '$phone_no_otp' "  ;
                      $que1= $this->db->query($sql);
                      $output = $que1->num_rows();
                        if($output==1) {
                       $sql="SELECT * FROM `grocery_users` WHERE `phone`='$mob' AND user_otp= '$phone_no_otp' "  ;
                       $que= $this->db->query($sql); 
                       $arr=$que->result(); 
                       //print_r($arr);exit();
                       foreach($arr as $row) {
                                
                        if($row->status== 0){echo '<b class="">sorry.. ! Your Account Not Verified...</b>';exit();}else{ //echo //'hiiiii.............';exit;
                        $session_data  = array('user_id' => $row->id,
                                            'name' => $row->name, 
                                            'email' => $row->email, 
                                            'phone' => $row->phone,
                                            );}
                     }
                      return $session_data;
                       }
                       else{
                           $sql="SELECT * FROM `grocery_seller` WHERE `phone`='$mob' AND otp= '$phone_no_otp' "  ;
                       $que= $this->db->query($sql); 
                       $arr=$que->result(); 
                       //print_r($arr);exit();
                       foreach($arr as $row) {
                                
                        if($row->status== 0){echo '<b class="">sorry..!  Your Account Not Verified...</b>';exit();}else{ //echo //'hiiiii.............';exit;
                       $session_data  = array('user_login_id' => $row->seller_id,
                                            'name' => $row->seller_name, 
                                            'email' => $row->email, 
                                            'phone' => $row->phone,
                                            'seller_role' => $row->seller_role,
                                            );}
                     }
                      return $session_data;
                       }
                    
                      
                       
                       
                }
    
    
  
                
}
