<?php

class EVENTS {

    private $db;

    function __construct($DB_con) {
        $this->db = $DB_con;
    } 
    
    public function insert($event_name, $organizer, $start_date, $end_date, $categories, $venue, $city, $country, $document, $add_by,$assign_agents,$market_status=0) {
        try {

            $stmt = $this->db->prepare("INSERT INTO events(
                            event_name,
                            organizer,
                            start_date,
                            end_date,
                            categories,
                            venue,
                            city,
                            country,
                            document,
                            add_by ,assign_agent,
							market_status
                            ) 
		            VALUES(
                            :event_name,
                            :organizer,
                            :start_date,
                            :end_date,
                            :categories,
                            :venue,
                            :city,
                            :country,
                            :document,
                            :add_by , :assign_agent,
							:market_status
                            )");
            
            $stmt->bindparam(":event_name", $event_name);
            $stmt->bindparam(":organizer", $organizer);
            $stmt->bindparam(":start_date", $start_date);
            $stmt->bindparam(":end_date", $end_date);
            $stmt->bindparam(":categories", $categories);
            $stmt->bindparam(":venue", $venue); 
            $stmt->bindparam(":city", $city); 
            $stmt->bindparam(":country", $country); 
            $stmt->bindparam(":document", $document); 
            $stmt->bindparam(":add_by", $add_by); 
            $stmt->bindparam(":assign_agent", $assign_agents); 
            $stmt->bindparam(":market_status", $market_status); 
            
            
            
            
            $stmt->execute();

            return $this->db->lastInsertId();
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

    
    public function AddCampaign($campaign_name, $event_id, $cam_id, $agent_id) {
        try {
            
                $stmt1 = $this->db->prepare("SELECT * FROM event_campaign WHERE event_id = :e_Id and agent_id =  :a_id");
                $stmt1->execute(array(':e_Id' => $event_id,':a_id' => $agent_id));
                $rr = $stmt1->rowCount();
                
                if($rr < 1){
                    
                    $stmt = $this->db->prepare("INSERT INTO event_campaign( event_id, agent_id, campaign_code, campaign_name)
		            VALUES( :event_id, :agent_id, :campaign_id, :campaign_name )");
                    $stmt->bindparam(":event_id", $event_id);
                    $stmt->bindparam(":agent_id", $agent_id);
                    $stmt->bindparam(":campaign_id", $cam_id);
                    $stmt->bindparam(":campaign_name", $campaign_name);
                    
                    $stmt->execute();
                    return  $this->db->lastInsertId();

                }else {

                    $return = $stmt1->fetch(PDO::FETCH_ASSOC);
                    return $return['id'];
                    
                }
            

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

    

public function getEvent($id=''){
        try {
            
            $return = array();
            if(!empty($id)){
                $stmt = $this->db->prepare("SELECT * FROM events WHERE event_id=:uId ");
                $stmt->execute(array(':uId' => $id));
                $return = $stmt->fetch(PDO::FETCH_ASSOC);
                
            }else {
                $stmt = $this->db->prepare("SELECT * FROM events order by `market_status` desc" );
                $stmt->execute();
                
                while($Row = $stmt->fetch(PDO::FETCH_ASSOC)){
                      $return[] = $Row;  
                }
            }
            return $return;
            
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    
    
public function getCampaign($id=''){
    try {
            $return = array();
            if(!empty($id)){
                $stmt = $this->db->prepare("SELECT * FROM event_campaign WHERE id =:uId ");
                $stmt->execute(array(':uId' => $id));
                $return = $stmt->fetch(PDO::FETCH_ASSOC);
            }else {
                $stmt = $this->db->prepare("SELECT * FROM event_campaign order by id desc" );
                $stmt->execute();
                while($Row = $stmt->fetch(PDO::FETCH_ASSOC)){
                      $return[] = $Row;  
                }
            }
            return $return;
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}
    
    
public function getAgetEvent($agent_id ='',$status=false){
        try {
            
        $return = array();
        if(!empty($agent_id )){
        	
        if ($status){ $eve_status="and status='$status'";  }
      		$stmt = $this->db->prepare("SELECT * FROM events WHERE (add_by = :uId or FIND_IN_SET(:uId,`assign_agent`) ) $eve_status order by `market_status` desc");
                    $stmt->execute(array(':uId' => $agent_id)); 
               
        }else {
                $stmt = $this->db->prepare("SELECT * FROM events  order by `market_status` desc");
                $stmt->execute(array(':uId' => $agent_id));    
        }
            while($Row = $stmt->fetch(PDO::FETCH_ASSOC)){
                $return[] = $Row;  
            }
            return $return;
            
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    
    
     
public function getEventByDate($start_date= '', $end_date='',$agent_id=0 ){
        try {
               
                
            $return = array();
			
            if(!empty($agent_id )){
                    $stmt = $this->db->prepare("SELECT * FROM events WHERE add_by = :uId or FIND_IN_SET(:uId,`assign_agent`)");
                    $stmt->execute(array(':uId' => $agent_id));    
            }else {
                    $stmt = $this->db->prepare("SELECT * FROM events");
                    $stmt->execute(array(':uId' => $agent_id));    
            }
           	
			
            if(!empty($start_date) && !empty($end_date) ) {
                  
            if(!empty($agent_id )){
					 
		$stmt = $this->db->prepare("SELECT * FROM `events` WHERE start_date >=:s_date and start_date <=:e_date and add_by = :uId or FIND_IN_SET(:uId,`assign_agent`)" );
                $stmt->execute(array(':s_date' => $start_date, ':e_date' => $end_date, ':uId' => $agent_id ));
	    }
            else {
                    $stmt = $this->db->prepare("SELECT * FROM `events` WHERE start_date >=:s_date and start_date <=:e_date" );
                    $stmt->execute(array(':s_date' => $start_date, ':e_date' => $end_date ));   
                    echo "<br><br><br>SELECT * FROM `events` WHERE start_date >= $start_date and start_date <= $end_date" ;
            }
             
				  
				  
               
           

		   }else if(!empty($start_date)){
				 if(!empty($agent_id )){
				$stmt = $this->db->prepare("SELECT * FROM `events` WHERE start_date >=:s_date  and add_by = :uId or FIND_IN_SET(:uId,`assign_agent`)");
                $stmt->execute(array(':s_date' => $start_date, ':uId' => $agent_id ));

				}else {
                 $stmt = $this->db->prepare("SELECT * FROM `events` WHERE start_date >=:s_date  ");
                $stmt->execute(array(':s_date' => $start_date));
  
		   }}
                while($Row = $stmt->fetch(PDO::FETCH_ASSOC)){
                      $return[] = $Row['event_id'];
                }            
            
            return $return;
            
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    
    
 public function delEvent($id=''){
        try {
            $return = array();
            if(!empty($id)){
                  $q = "update events set `status` = 'Deactive' WHERE event_id= $id  ";
                $stmt = $this->db->prepare($q);
                $stmt->execute();
            }
        } catch (PDOException $e) {
            echo $e->getMessage();
            die();
        }
        
 }
  
 public function updateMarketStatus($id='', $status){
        try {
            $return = array();
            if(!empty($id)){
                  $q = "update events set `market_status` = '".$status."' WHERE event_id = $id  ";
                $stmt = $this->db->prepare($q);
                $stmt->execute();
            }
        } catch (PDOException $e) {
            echo $e->getMessage();
            die();
        }
        
 }
 
    
    public function update($event_id, $event_name, $organizer, $start_date, $end_date, $categories, $venue, $city, $country, $document, $add_by, $assign_agents) {
        try {
            $stmt = $this->db->prepare("Update events
                            Set
                            event_name = :event_name,
                            organizer = :organizer,
                            start_date = :start_date ,
                            end_date = :end_date,
                            categories = :categories ,
                            venue = :venue ,
                            city = :city ,
                            country = :country ,
                            document = :document, 
                            add_by = :add_by , last_update = :last_update,
                            assign_agent = :assign_agent
                            where event_id = :event_id
                            "
                            );
             
            $stmt->bindparam(":event_name", $event_name);
            $stmt->bindparam(":organizer", $organizer);
            $stmt->bindparam(":start_date", $start_date );
            $stmt->bindparam(":end_date", $end_date);
            $stmt->bindparam(":categories", $categories );
            $stmt->bindparam(":venue", $venue );
            $stmt->bindparam(":city", $city );
            $stmt->bindparam(":country", $country );
            $stmt->bindparam(":document", $document );
            $stmt->bindparam(":add_by", $add_by );
            $stmt->bindparam(":last_update", date("Y-m-d H:i:s") ); 
            $stmt->bindparam(":assign_agent", $assign_agents ); 

            $stmt->bindparam(":event_id", $event_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 getEventAssignClient($event_id, $agent_id) {
        try { $event_id.' - '.$agent_id;
			$return = array();
			  $sql= "SELECT * FROM clients_events_assigning WHERE event_id = $event_id and agent_id= $agent_id";
            $stmt = $this->db->prepare($sql);
            $stmt->execute();
             while($userRow = $stmt->fetch(PDO::FETCH_ASSOC)){
				 $return[] = $userRow;
			 }
            return $return;
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    
    
    
     public function AddAssignEventClient($event_id, $client_id, $agent_id) {
        try {
                    $stmt = $this->db->prepare("INSERT INTO clients_events_assigning( event_id, agent_id, client_id)
		            VALUES( :event_id, :agent_id, :client_id)");
                    $stmt->bindparam(":event_id", $event_id);
                    $stmt->bindparam(":agent_id", $agent_id);
                    $stmt->bindparam(":client_id", $client_id);
                    $stmt->execute();
                    return  $this->db->lastInsertId();

            
        } 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;
    }

}

?>