 <?php
class Webservice_model extends CI_Model {
    function __construct() {
        parent::__construct();
        date_default_timezone_set('Asia/Kolkata');
    }

    public function createCustomer($postData = array()){
        if(empty($postData))
            return 0;
        if(isset($postData['user_email']) && !empty($postData['user_email'])){
            $this->db->join('customer AS CUST','CUST.id = USR.user_id');
            $emailChk = $this->db->get_where('users AS USR',array('CUST.email_id'=>$postData['user_email'], 'USR.status !='=>'2','USR.user_type'=>'1'));
            if(!empty($emailChk) && $emailChk->num_rows() > 0){
                return 2;
            }
        }
        if(isset($postData['user_phonenumber']) && !empty($postData['user_phonenumber'])){
            $phoneChk = $this->db->get_where('users',array('phone_no'=>$postData['user_phonenumber'],'status !='=>'2'));
            if(!empty($phoneChk) && $phoneChk->num_rows() > 0){
                return 3;
            }
        }
        $insert_array = array(
                            'name'=>$postData['user_name'],
                            'email_id'=>$postData['user_email'],
                            'code'=>$postData['user_phonecode'],
                            'phone_no'=>$postData['user_phonenumber'],
                            'device_id'=>$postData['device_id'],
                            'address'=>$postData['user_address'],
                            'country'=>$postData['user_country'],
                            'region'=>$postData['user_region'],
                            'city'=>$postData['user_city'],
                            'area'=>$postData['user_area'],
                            'lat'=>$postData['user_lat'],
                            'lng'=>$postData['user_lng'],
                            'password'=>$postData['user_password'],
                        );
        $status = $this->db->insert('customer',$insert_array);
        $last_id = $this->db->insert_id();

        $status = $this->db->insert('users',array('user_type'=>'1','user_id'=>$last_id,'username'=>$postData['user_email'],'phone_no'=>$postData['user_phonenumber'],'password'=>$postData['user_password'],'status'=>'1'));
        return ($status)?1:0;
    }

    public function checkCustomerLogin($userLogData){
        $respArr = array('status'=>'error');
        if(empty($userLogData)){
            return $respArr;
        }
        $this->db->join('customer AS CUST','CUST.id = USR.user_id');
        $result = $this->db->get_where('users AS USR',array('CUST.email_id'=>$userLogData['user_email'], 'USR.status'=>'1','USR.user_type'=>'1'));

        if(empty($result) || $result->num_rows() < 1 || empty($custData = $result->row())){
            $respArr['message'] = "Invalid Email Address";
            return $respArr;
        }
        $this->db->join('customer AS CUST','CUST.id = USR.user_id');
        $result = $this->db->get_where('users AS USR',array('CUST.email_id'=>$userLogData['user_email'], 'USR.status'=>'1','USR.user_type'=>'1','USR.password'=>$userLogData['user_password']));

        $respArr['message'] = "Invalid Password";

        if(!empty($result) && $result->num_rows() == 1 && !empty($custData = $result->row())){
            $respArr['data'] = $custData;
            $respArr['status'] = 1;
            $respArr['message'] = "success";
        }
        return $respArr;
    }

    public function userLogin($postData = array()){
        $respArr = array('status'=>'error');
        if(empty($postData)){
            return $respArr;
        }
        $result = $this->checkCustomerLogin($postData);
        if($result['status'] == 'error'){
            return $result;
        }
        if($this->db->update('customer',array('lat'=>$postData['user_lat'],'lng'=>$postData['user_lng'],'device_id'=>$postData['device_id']),array('id'=>$result['data']->user_id))){
            $respArr['status'] = "success";
            $respArr['message'] = "success";
            $respArr['data'] = $result['data'];
        } 
        return $respArr;
    }

}
?>
