<?php

if(!defined('BASEPATH')) exit('Hacking Attempt : Get Out of the system ..!');

class Login_model extends CI_Model

{

public function __construct()

{
	parent::__construct();

}

public function checkUser($email)
{ 
	$this->db->select('*');
	$this->db->or_where('email',$email);
	$query = $this->db->get('user');
	return $query->num_rows();
}


public function takeUser($username, $password, $status)
{
	$this->db->select('*');
	$this->db->where('password', $password);
	$this->db->where('status', 1);
	$this->db->where('email',$username);
	$query = $this->db->get('user');
	return $query->num_rows();
}

public function insertUser($name,$email,$password)
{
	$data['name']= $name;
	$data['password']= md5($password);
	$data['email']=$email;
	$data['image']='userimg.jpg';
	$data['activationstatus']=0;
	$data['createdon']=date('Y-m-d H:i:s');
	
	$data['role']=1;
	$this->db->insert('user', $data);
	$userId = $this->db->insert_id();
	$this->insertplan($userId);
}

public function insertplan($userId)
{	
	$data['userId']= $userId;
	$data['plan']='FREE';
	$data['articles']= 0;
	$data['validity']=0;
	$data['activationdate']=date('Y-m-d H:i:s');
	$data['status']=1;			
	$this->db->insert('user_plan', $data);	
}

public function userData($useremail)

{
	$this->db->select('user.*');
	$this->db->or_where('user.email',$useremail);
	$query = $this->db->get('user');
	return $query->row();
}



public function checkForgotUser($value)
{ 
	$this->db->select('*');
	$this->db->from('user');
	$this->db->where('contact', $value);
	$this->db->or_where('email',$value);
	$query = $this->db->get();
	return $query->num_rows();
}

public function updatepassword($user,$password){
	$data = array( 'password'=>$password );
	$this->db->where('contact', $user);
	$this->db->or_where('email', $user);
	$this->db->update('user', $data);

}


public function userAddressData($id)

{
	$this->db->select('*');
	$this->db->where('userId',$id);
	$this->db->group_by('userId'); 
	$query = $this->db->get('user_address');
	return $query->row();
}



public function insertuserdetails($userId,$name,$email,$contact)
{	
	$data['userId']= $userId;
	$data['first_name']= $name;
	$data['contact']= $contact;
	$data['email']=$email;
	$data['avtar']='uploads/users/dummy.jpg';
	$data['referral_code']='ERT'.generateOTP(4);
	$data['updated_on']=date('Y-m-d H:i:s');
	
	if(isset($_SESSION['utm']) && @$_SESSION['utm']['utm_source'] !='')
	{
		$data['utm_source']=@$_SESSION['utm']['utm_source'];
		$data['utm_medium']=@$_SESSION['utm']['utm_medium'];
		$data['utm_campaign']=@$_SESSION['utm']['utm_campaign'];
	}else{
		$data['utm_source']='DIRECT';
	}
				
	$this->db->insert('user_details', $data);
	
	$this->createuserwallet($userId);
}

public function createuserwallet($userId)
{	
	$data['userId']= $userId;
	$data['amount']= '0.00';
	$data['prev_bal']= '0';
	$data['new_bal']='0.00';
	$data['last_updated']=date('Y-m-d H:i:s');
	
	$this->db->insert('user_wallet', $data);
	$walletId = $this->db->insert_id();
	$this->updatewallettxt($userId,$walletId,'New Wallet Creation',$data['amount']);
}

public function updatewallettxt($userId,$walletId,$description,$amount)
{	
	$data['userId']= $userId;
	$data['walletId']= $walletId;
	$data['amount']= $amount;
	$data['prev_bal']= '0';
	$data['new_bal']=$amount;
	$data['description']=$description;
	$data['created_on']=date('Y-m-d H:i:s');
	
	$this->db->insert('user_wallet_txn', $data);
}

public function checkpostcode($postcode){
	$this->db->select('*');
	$this->db->where('postcode',$postcode );
	$query = $this->db->get('delivery_zone_area');
	return $query->row_array();
}

public function getuserordercount($userId){
	$this->db->select('orders.orderId');
	$this->db->join('order_amount','order_amount.orderId=orders.orderId' ,'left');
	
	$this->db->where(array('userId='=>$userId,'order_amount.payment_status='=>'PAID'));
	$query = $this->db->get('orders');
	return $query->num_rows();
}

}
