<?php


class Login_model extends CI_Model
{
	public function login($email,$pass)
	{
		$email = $this->db->escape($email);
		$select = $this->db->query("SELECT * FROM `users` WHERE email = $email" );
		$select2 = $this->db->query("SELECT * FROM `users` WHERE email = $email AND is_suspended = '1'" );

		if($select->num_rows() == 0)
		{
			return array(1,'Email Not Registered','');
		}
		else if($select2->num_rows() >= 1)
		{
			return array(1,'User is suspended','');
		}
		else
		{
			$data = $select->result_object();
			if($data[0]->password === $pass)
			{
				return array(0,'Login Successfull...',$data[0]->id,$data[0]->usertype);
			}
			else
			{
				return array(1,'Password does not Match','');
			}
			
		}
	}
	
	public function pass_change($currentpass,$pass,$cpass)
	{
		$currentpass = $this->db->escape($currentpass);
		$select = $this->db->query("SELECT * FROM `users` WHERE id= ".$this->session->userdata('id')." AND password = md5($currentpass)" );
        $data = $select->result_object();
       
        if($select->num_rows() == 0){
            
             return "Current Password is Wrong";
             
        }else{

             $password = $data[0]->password;
                      $data = [
            'password' => md5($pass),
        ];
        $this->db->where('id', $this->session->userdata('id'));
        $this->db->update('users', $data);
       return "Change password Successfully...";
        }

	}
	
	//staff
	public function staff_login($email,$pass)
	{
		$email = $this->db->escape($email);
		$select = $this->db->query("SELECT * FROM `staff` WHERE  emailid = $email" );
		$select2 = $this->db->query("SELECT * FROM `staff` WHERE emailid = $email AND is_suspended = '1'" );

		if($select->num_rows() == 0)
		{
			return array(1,'Email Not Registered','');
		}
		else if($select2->num_rows() >= 1)
		{
			return array(1,'Staff is suspended','');
		}
		else
		{
			$data = $select->result_object();
			if($data[0]->password === $pass)
			{
				return array(0,'Login Successfull...',$data[0]->id,'staff');
			}
			else
			{
				return array(1,'Password does not Match','');
			}
			
		}
	}
	public function staff_pass_change($currentpass,$pass,$cpass)
	{
		$currentpass = $this->db->escape($currentpass);
		$select = $this->db->query("SELECT * FROM `staff` WHERE id= ".$this->session->userdata('id')." AND password = md5($currentpass)" );
        $data = $select->result_object();
       
        if($select->num_rows() == 0){
            
             return "Current Password is Wrong";
             
        }else{

             $password = $data[0]->password;
                      $data = [
            'password' => md5($pass),
        ];
        $this->db->where('id', $this->session->userdata('id'));
        $this->db->update('staff', $data);
       return "<span class='text-success'>Password changed Successfully...</span>";
        }

	}

	//turf owner
	public function turf_owner_login($email,$pass)
	{
		$email = $this->db->escape($email);
		$select = $this->db->query("SELECT * FROM `turf_owner` WHERE  email = $email" );
		$select2 = $this->db->query("SELECT * FROM `turf_owner` WHERE email = $email AND is_suspended = '1'" );

		if($select->num_rows() == 0)
		{
			return array(1,'Email Not Registered','');
		}
		else if($select2->num_rows() >= 1)
		{
			return array(1,'Turf Owner is suspended','');
		}
		else
		{
			$data = $select->result_object();
			if($data[0]->password === $pass)
			{
				return array(0,'Login Successfull...',$data[0]->id,'turf_owner');
			}
			else
			{
				return array(1,'Password does not Match','');
			}
			
		}
	}
	public function turf_owner_pass_change($currentpass,$pass,$cpass)
	{
		$currentpass = $this->db->escape($currentpass);
		$select = $this->db->query("SELECT * FROM `turf_owner` WHERE id= ".$this->session->userdata('id')." AND password = md5($currentpass)" );
        $data = $select->result_object();
       
        if($select->num_rows() == 0){
            
             return "Current Password is Wrong";
             
        }else{

             $password = $data[0]->password;
                      $data = [
            'password' => md5($pass),
        ];
        $this->db->where('id', $this->session->userdata('id'));
        $this->db->update('turf_owner', $data);
       return "Password changed Successfully...";
        }

	}
	
}

?>