<?php if(!defined('BASEPATH')) exit('No direct script access allowed');   
class User_Model extends CI_Model {
    
    public function  __construct(){      
        parent::__construct();        
    }

     public function  login($data) {
        $condition = "(email = '".$data['email']."' ||   student_id = '".$data['email']."' ) AND (password ='".md5($data['password'])." ')";
        $this->db->select('*');
        $this->db->from('student');
        $this->db->where($condition);
        $this->db->limit(1);
        $query = $this->db->get();
        //echo $this->db->last_query();exit;
        if ($query->num_rows() == 1) {
            return $query->row();
        } else {
            return false;
        }
    }

    public function  get_login($student_code) {
        $this->db->where('student_code', $student_code);
        $query = $this->db->get('student');
        if($query->num_rows()>0) {
            return $query->row();
        } else {
            return false;
        }
    }

    function  get_student_history_id($id,$from="",$to="") {
        if($from){
            $this->db->where("sh.update_on BETWEEN '$from' AND '$to'");
        }
         $this->db->select('sh.*,c.category_name as cname,n.category_name as notes_name,d.category_name as discount_name,a.award_name');
        $this->db->join('category c', 'c.id = sh.category','left');
        $this->db->join('category n', 'n.id = sh.notes','left');
        $this->db->join('category d', 'd.id = sh.discount','left');
        $this->db->join('award a', 'a.id = sh.award_id','left');
        $this->db->where('stud_id',$id);
        $this->db->order_by('sh.history_id','desc');
        $query = $this->db->get('student_history sh');
        //echo $this->db->last_query();
        if($query->num_rows()>0) {
            return $query->result();
        } else {
            return false;
        }
    }

    public function  get_profile($id) {
        $this->db->where('id', $id);
        $query = $this->db->get('bills_user');
        if($query->num_rows()>0) {
            return $query->row();
        } else {
            return false;
        }
    }

    public function update($id,$data) {
        $this->db->where('id', $id);
        $query = $this->db->update('student', $data); 
        if($query){
            return true;
        } else {
            return false;
        }
    }

    function  get_award_history($id) {
        $this->db->where('aw.user_id',$id);
        $this->db->select('aw.*,s.username as student_name,a.name as memeber_name,aa.award_name');
        $this->db->join('bills_user s','s.id = aw.user_id','left');
        $this->db->join('pts_award aa','aa.id = aw.award_id','left');
        $this->db->join('user a','a.id = aw.member_id','left');
        $query = $this->db->get('pts_award_history aw');

        if($query->num_rows()>0) {
            return $query->result();
        } else {
            return false;
        }
    }

    function Change_password($id){   
        $this->db->select('id');
        $this->db->where('id',$id);
        $this->db->where('password',md5($this->input->post('old_password')));
        $query=$this->db->get('student');   
        if ($query->num_rows() > 0){
            $data = array( 'password' => md5($this->input->post('password')));
            $this->db->where('id',$id);
            $query1 = $this->db->update('student', $data);
            if($query1) {
                return "Password Changed Successfully";
            }
            else{
                return "Something Went Wrong, Password Not Changed";
            }
        }else{
                return "Wrong Old Password";
            }
    }

     public function  get_points($points) {
        $this->db->where('scan_code', $points);
        $query = $this->db->get('pts_barcode');
        if($query->num_rows()>0) {
            return $query->row();
        } else {
            return false;
        }
    }

}