<?php

class Candidate_model extends CI_Model
{
    	//check username exists
		public function check_username_exists($username)
		{
			$query=$this->db->get_where('candidate',array('uname'=>$username));
			
			if(empty($query->row_array()))
			{
				return true;
			}
			else
			{
				return false;
			}
			return $query->row();
		}

		//check email exists
		public function check_email_exists($email)
		{
			$query=$this->db->get_where('candidate',array('email'=>$email));
			
			if(empty($query->row_array()))
			{
				return true;
			}
			else
			{
				return false;
			}
			return $query->row();
        }
        
		//check phone exists
		public function check_phone_exists($phone)
		{
			$query=$this->db->get_where('candidate',array('phone_no'=>$phone));
			
			if(empty($query->row_array()))
			{
				return true;
			}
			else
			{
				return false;
			}
			return $query->row();
		}

        public function retrive_payment_status($username,$password)
        {
            //$this->load->database;
            $this->db->where('uname',$username);
            $this->db->where('pword',md5($password));
            
            $result=$this->db->get('candidate');
            
            if($result->num_rows()==1)
            {
                return $result->row(0)->payment_status;
            }
            else
            {
                return "Payment Status Can't be fetched.";
            }
        }

        public function retrive_adm_no($username,$password)
        {
            
            //$this->load->database;
            $this->db->where('uname',$username);
            $this->db->where('pword',md5($password));
            
            $result=$this->db->get('candidate');
            
            if($result->num_rows()==1)
            {
                return $result->row(0)->admission_no;
            }
            else
            {
                return "Admission No Can't be fetched.";
            }
        }
        

        public function login($username,$password)
        {
            //validate
            $this->db->where('uname',$username);
            $this->db->where('pword',md5($password));
            
            $result=$this->db->get('candidate');
            
            if($result->num_rows()==1)
            {
                return $result->row(0)->id;
            }
            else
            {
                return false;
            }
        }

        public function retrive_password($username,$email)
        {
            //$this->load->database;
            $this->db->where('uname',$username);
            $this->db->where('email',$email);
            
            $result=$this->db->get('candidate');
            
            if($result->num_rows()==1)
            {
                return $result->row(0)->pwd;
            }
            else
            {
                return false;
            }
        }
        
    
        public function validate_username_email($username,$email)
        {
            //$this->load->databse;
            $this->db->where('uname',$username);
            $this->db->where('email',$email);
            
            $result=$this->db->get('candidate');
            
            if($result->num_rows()==1)
            {
                return $result->row(0)->email;
            }
            else
            {
                return false;
            }
        }

       public function register($enc_password,$admission_no)
        {
            //candidate data array
            $data=array(
                'uname'=>$this->input->post('username'),
                'pword'=>$enc_password,
                'fname'=>$this->input->post('firstname'),
                'lname'=>$this->input->post('lastname'),
                'gender'=>$this->input->post('gender'),
                'email'=>$this->input->post('email'),
                'phone_no'=>$this->input->post('phone'),
                'payment_status	'=>'UNPAID',
                'admission_no'=>$admission_no,
                'pwd'=>$this->input->post('password'),    //storing original password so that at the time of retrival it can be retrived
            );
            //insert user
            return $this->db->insert('candidate',$data);
        }


}