<?php

class AGENTS   {

    private $db;

    function __construct($DB_con) {
        $this->db = $DB_con;
    }
/*
full_name      = '';
father_name    = '';
          = '';
       = '';
        = '';
         = '';
         = '';
        = '';
           = '';
      = '';
 *  */
    public function insert($full_name, $father_name, $email, $password, $contact,$mobile, $address, $gender, $cnic, $join_date, $profile_img, $agent_type ) {
        try {
            echo $profile_img; 
            
            $stmt = $this->db->prepare("INSERT INTO agents(
                            name,
                            father_name,
                            email,
                            password,
                            contact_no,
                            mobile_no,
                            gender,
                            address,
                            cnic,
                            join_date,
                            profile_img,
                            agent_type
                            ) 
		            VALUES(
                            :full_name,
                            :father_name,
                            :email,
                            :password,
                            :contact,
                            :mobile,
                            :gender,
                            :address,
                            :cnic,
                            :join_date,
                            :profile_img,
                            :agent_type
                            
                            )");
            
            $stmt->bindparam(":full_name", $full_name);
            $stmt->bindparam(":father_name", $father_name);
            $stmt->bindparam(":email", $email);
            $stmt->bindparam(":password", $password);
            $stmt->bindparam(":contact", $contact); 
            $stmt->bindparam(":mobile", $mobile); 
            $stmt->bindparam(":address", $address); 
            $stmt->bindparam(":gender", $gender); 
            $stmt->bindparam(":cnic", $cnic); 
            $stmt->bindparam(":join_date", $join_date); 
            $stmt->bindparam(":profile_img", $profile_img);
            $stmt->bindparam(":agent_type", $agent_type);
            
            $stmt->execute();

            return $stmt;
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

public function getAgent($id=''){
    
        try {
            
            $return = array();
            if(!empty($id)){
                $stmt = $this->db->prepare("SELECT * FROM agents WHERE agent_id=:uId ");
                $stmt->execute(array(':uId' => $id));
                $return = $stmt->fetch(PDO::FETCH_ASSOC);
                
            }else {
                $stmt = $this->db->prepare("SELECT * FROM agents" );
                $stmt->execute();
                
                while($Row = $stmt->fetch(PDO::FETCH_ASSOC)){
                      $return[] = $Row;  
                }
            }
            return $return;
            
        } catch (PDOException $e) {
                echo $e->getMessage();
        }
    }
    
public function checkAgent($email=''){
        try {
            
            $return = '';
            
                $stmt = $this->db->prepare("SELECT * FROM agents WHERE email=:email ");
                $stmt->execute(array(':email' => $email));
                $return = $stmt->fetch(PDO::FETCH_ASSOC);
                
                if(count($return)>1){
                    $return = 'exists'; 
                }
            
            return $return;
            
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    
    
    
    
    
    
    
    public function update($agent_id, $full_name, $father_name,$password, $contact,$mobile, $address, $gender, $cnic, $join_date, $profile_img, $agent_type) {
        try {
            //echo "here";
            $stmt = $this->db->prepare("Update agents
                            Set
                            name = :full_name,
                            father_name = :father_name,
                            password = :password,
                            contact_no = :contact,
                            mobile_no = :mobile,
                            gender = :gender,
                            address = :address,
                            cnic = :cnic,
                            join_date = :join_date,
                            profile_img = :profile_img,
                            agent_type = :agent_type                            
                            where agent_id = :ag_id
                            "
                            );
            
            $stmt->bindparam(":full_name", $full_name);
            $stmt->bindparam(":father_name", $father_name);
            $stmt->bindparam(":password", $password);
            $stmt->bindparam(":contact", $contact);
            $stmt->bindparam(":mobile", $mobile);
            $stmt->bindparam(":address", $address);
            $stmt->bindparam(":gender", $gender);
            $stmt->bindparam(":cnic", $cnic);
            $stmt->bindparam(":join_date", $join_date);
            $stmt->bindparam(":profile_img", $profile_img); 
            $stmt->bindparam(":agent_type", $agent_type); 
            
            $stmt->bindparam(":ag_id", $agent_id);
 
            $stmt->execute();

            return $stmt;
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

      public function updateStatus($agent_id, $status) {
        try {
            //echo "here";
            $stmt = $this->db->prepare("Update agents
                            Set status = :status                            
                            where agent_id = :ag_id"
                            );
            
            $stmt->bindparam(":status", $status);
            $stmt->bindparam(":ag_id", $agent_id);
 
            $stmt->execute();

            return $stmt;
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

    
    
    public function getParentCategories(){
        try {
            
            $return = array();
            
                $stmt = $this->db->prepare("SELECT * FROM categories where cat_parentId = 0" );
                $stmt->execute();
                
                while($Row = $stmt->fetch(PDO::FETCH_ASSOC)){
                      $return[] = $Row;  
                }
            return $return;
            
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    
     
    public function getAllCategories(){
        try {
            
            $return = array();
            
                $stmt = $this->db->prepare("SELECT * FROM categories WHERE cat_parentId = 0" );
                $stmt->execute();
                $i=0;
                while($Row = $stmt->fetch(PDO::FETCH_ASSOC)){
                      //$return[] = $Row;
                      $stmt2 = $this->db->prepare("SELECT * FROM categories WHERE cat_parentId = ".$Row['cat_id'] ."  " );
                      $stmt2->execute();
                      
                        $return[$i]['cat_id'] =$Row['cat_id'];
                        $return[$i]['category'] =$Row['cat_title'];
                        $return[$i]['desc'] =$Row['cat_desc'];
                        $return[$i]['status'] =$Row['cat_status'];
                        $j = 0 ;
                      while($Row2 = $stmt2->fetch(PDO::FETCH_ASSOC)){
                          
                                $return[$i]['sub_cat'][$j]['category'] =$Row2['cat_title'];
                                $return[$i]['sub_cat'][$j]['desc'] =$Row2['cat_desc'];
                                $return[$i]['sub_cat'][$j]['status'] =$Row2['cat_status'];    
                                $return[$i]['sub_cat'][$j]['main'] =$Row['cat_title'];
                                $return[$i]['sub_cat'][$j]['cat_id'] =$Row2['cat_id'];
                                
                            $j++;
                      }
                     $i++; 
                }
            
            return $return;
            
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    
    
    
    
    
    
    
    
    
    public function getProfile($id) {
        try {
            $stmt = $this->db->prepare("SELECT * FROM users WHERE uId=:uId");
            $stmt->execute(array(':uId' => $id));
            $userRow = $stmt->fetch(PDO::FETCH_ASSOC);
            if ($stmt->rowCount() > 0) {
                return $userRow;
            }
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

    public function is_loggedin() {
        if (isset($_SESSION['user_session'])) {
            return true;
        }
    }

    public function redirect($url) {
        header("Location: $url");
    }

    public function logout() {
        session_destroy();
        unset($_SESSION['user_session']);
        return true;
    }

}

?>