<?php
// If it's going to need the database, then it's 
// probably smart to require it before we start.
//require_once('initializer.php');

class Itinerary extends DatabaseObject {
	
  protected static $table_name="BVIS_Itineraries";
  
  //we will populate this array of attribure dynamically
	//protected static $db_fields = array('id','Username','Privilege','First_Name','Last_Name','Password');
  
  protected static $db_fields;
  protected static $exclu_fields = array ();

	// public  $id;
	// public $Username;
	// public $Privilege;
	// public $Last_Name;
  // public $First_Name;
  // public $Password;

  public function __construct($options=""){
    if ($options=="Blank") {
      $myFields = static::Get_Object_Fields ();
      foreach($myFields as $field) {
        $this->{$field} = "";
      }
    }

   // unset ($this->id);
    
  }


  

 
  public static function Verify_IF_UserItinerary ($user_id="", $packID = "" ) {

    if (isset($packID) && !empty($packID) ) {
        $sql = "SELECT * FROM `BVIS_Itineraries` WHERE  `Package_id`='{$packID}' AND `User_id`='{$user_id}' AND `Confirmation`='Pending' ";

    } else {
        $sql = "SELECT * FROM `BVIS_Itineraries` WHERE `User_id`='{$user_id}' AND `Confirmation`='Pending' ";

    }
    
    $result = self::find_by_sql ($sql, "");

    if (sizeof($result )>0) {
        return false;
    } else {
        return true;
    }
    


  }

  public static function getMyItinerary ($userID) {
    global $database;


     //fix for mysql 5.7
     $FIX_ONLY_FULL_GROUP_BY = "SET sql_mode=(SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''))";
     $database->query($FIX_ONLY_FULL_GROUP_BY);

     //we need to  find the date where users have activities
     $sqlDate = "SELECT `RequestedDate` FROM `BVIS_Itineraries` WHERE `User_id`='{$userID}' AND (`Confirmation`='Confirmed' OR `Confirmation`='Pending') GROUP BY DATE(`RequestedDate`)  ORDER BY `RequestedDate` ASC ";

     $dateFound = $database->query( $sqlDate );


     $collection = [];
     $Itinerary = [];

     while($row = $dateFound->fetch_assoc()) {
         $Date = new DateTime($row['RequestedDate']);
         $Date =   $Date->format('Y-m-d') ;
        //now we ask the database for the activity in this date
        $sql = "SELECT * FROM `BVIS_Itineraries` WHERE DATE(`RequestedDate`)='{$Date}' AND `User_id`='{$userID}' AND  (`Confirmation`='Confirmed' OR `Confirmation`='Pending')  ORDER BY `RequestedDate` ASC ";

        $activities = self::find_by_sql ( $sql, "myItineraryPack");
        array_push($Itinerary, $Date   ) ;
        array_push($Itinerary, $activities  ) ;

        array_push( $collection , $Itinerary ) ;
 $Itinerary = [];
   
       
    }

    //  while ($row = mysql_fetch_array($dateFound , MYSQL_ASSOC)) {
    //      $row;
    // }

return  $collection ;

  }

  
  public static function ShowSuggested_Date ($Bus_id="") { 

    $userData =self::find_by_id($Bus_id );
    //2020-07-26 19:43:23
    //Full_Name
    if (  $userData) {
      echo(  substr(str_replace(' ', 'T', $userData->RequestedDate), 0, -3) ) ;
    } else {
      echo( "N/A");
    }

  }


  public static function ShowTo_Date ($Bus_id="") { 

    $userData =self::find_by_id($Bus_id );
    //2020-07-26 19:43:23
    //Full_Name
    if (  $userData) {
      echo(  substr(str_replace(' ', 'T', $userData->To_Requested_Date), 0, -3) ) ;
    } else {
      echo( "N/A");
    }

  }

 
  public static function testGetField () {
    return self::$db_fields;
  }

  public static function createProperty( $name, $value ){
     $this->{$name} = $value;
}

  public static function setGetField ($idf) {
    return self::$db_fields['id'] = $idf;
  }


  public static function GetCustomerPrivilage ($staffid) {
    $user =self::find_by_id($staffid );
    return $user->Privilege ;
  }

  public static function GetCustomerEmail  ($staffid="", $source="") {

    if ( strtoupper ($source) == "LOCAL") {
      return LocalQueuer::GetEmail ($staffid);
      
      # code...
    } else {
     // $sql = 'SELECT * FROM `BVIS_User` WHERE `id` ="4"';
     $user =self::find_by_id($staffid );
  
      //Full_Name
      if (  $user && $user->Email  !="undefined" ) {
        return $user->Email ;
      } else {
      return "N/A";
      }

    }
    
    
  


  }


 
  



 
    
  


 



 
 

  public static function addPoints($id="", $amount="" ) {
    global $database;
     $points = floor ($amount/2);
     $sql = "UPDATE `BVIS_User` SET `Points`=`Points`+$points WHERE `id`='".$id."'";
     return $database->query($sql);

  }


 
 



 

 
	
 

}

?>