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

class OrdersItemChoice extends DatabaseObject {
	
  protected static $table_name="OrdersItemChoices_Opt";
 
  
  //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);
    
  }

  //we may need to create an special function 
  ///that will change ItemType_id to id

  public static function EditCheckedVal ($val="", $ItemChoices_Opt_id="" ) {
    global $database;
    error_log(print_r( "Inside EditCheckedVal" ,true));
   
    $sql = 'UPDATE `OrdersItemChoices_Opt` SET `checked`= "'.$val.'" WHERE `ItemChoices_Opt_id` = "'.$ItemChoices_Opt_id.'"';
    $result_set = $database->query($sql);

  }

  public static function TotalChoicesPrice ($orderID="") {
    global $database;
     $sql = "SELECT `OrderDishInfo_id`,`Price` FROM `OrdersItemChoices_Opt` WHERE `Orders_ID`='".$orderID."' AND `checked`='1' ";

    // error_log(print_r( "Total coices ARRAY" ,true));
    // error_log(print_r( $ExtrasForOrder,true));
    // foreach ( mysqli_fetch_all($database->query($sql),MYSQLI_ASSOC)  as $passval) {
    //     $Total = number_format($passval['Price']+ $Total,2,'.', '' ); 
   
    // }

    // error_log(print_r( "TOTAL Choices in functions",true));
   
    // error_log(print_r(  mysqli_fetch_all($database->query($sql),MYSQLI_ASSOC),true));

  
    return mysqli_fetch_all($database->query($sql),MYSQLI_ASSOC);   
  
  }
  
  


  public static function saveOrderItemChoices ($orderID="",$object="",$dishinfosaved="") {
    //error_log(print_r( $object->Options ,true));
         foreach(  $object->Options as $attribute){
            $option= new self ("Blank");


         
            foreach(  $attribute as  $attributePassed =>$value ){
               // error_log(print_r( $value,true));
            //     //error_log(print_r( $attributePassed,true));
            //     // error_log(print_r( $attribute,true));
            //     // error_log(print_r( "seee nowwww",true));
           //error_log(print_r( $attribute,true));
               

            if(property_exists($option,$attributePassed) && $attributePassed != "id" ) {
               $option->$attributePassed = $value;
 
              //  if ($object ->StoringAppOrder ==true) {
              //    $option->CustomType_id =  $attribute->id;
              //   } else {
              //     $option->CustomType_id =  $attribute->CustomType_id;
              //   }
                


              }  
    

              }


  // $option->CustomType_id =  $attribute->id;                       
   $option->Orders_id =  $orderID  ; 
   $option->OrderDishInfo_id = $dishinfosaved;
      
   if ($attribute ->StoringAppOrder ==true) {
     unset( $option->ItemChoices_Opt_id);
  } else {
    //you may need to check for id too
   // $option->CustomType_id=$attribute->id;
        if (isset($attribute->id) ) {
          $option->CustomType_id=$attribute->id;
        } else {
          $option->CustomType_id=$attribute->CustomType_id;
        }
    
   



  }
 

            // if ($object ->StoringAppOrder ==true) {
            //   unset( $option->ItemChoices_Opt_id);
            // }

          

              //unset( $option->ItemChoices_Opt_id);

              $option->save();
              //return $option;



          
         }
 // error_log(print_r( $option,true));

}

public static function GetDishChoices_TOTAL ($OrderDishInfo_id="", $OrderID="") {
  global $database;
  $sql = "SELECT `Price` FROM `OrdersItemChoices_Opt` WHERE `checked`=1 AND `Orders_ID`='".$OrderID."' AND `OrderDishInfo_id`= '".$OrderDishInfo_id."' ";

  $TOTAL_EXTRA_ForDish = mysqli_fetch_all( $database->query($sql),MYSQLI_ASSOC);
   
  $FINAL_TOTAL=0.00;

  foreach ($TOTAL_EXTRA_ForDish  as $key => $value) {
    //error_log(print_r("SHOW ME THE value",true));
    //error_log(print_r($value['Price']  ,true));
   $FINAL_TOTAL= $FINAL_TOTAL+$value['Price'];
  }

  error_log(print_r("OrdersItemChoices_Opt  ME THE total",true));
   error_log(print_r(number_format(round((float)$FINAL_TOTAL,2),2) ,true));
 return number_format(round((float)$FINAL_TOTAL,2),2) ;

  
}



public static function FindOrderChoices ($orderID="", $OrderDishInfo_id="") {
  //error_log(print_r( "OrderDishInfo_id fron inside extra"  ,true));
  // error_log(print_r("solving choices",true));
  // error_log(print_r($OrderDishInfo_id,true));
  // error_log(print_r($orderID,true));


  $finalArrat=[];
  $orderDetails;

  $finalChoiceObject = [];
  $objToPush;

  $options = self::find_by_sql(' SELECT * FROM `OrdersItemChoices_Opt` WHERE `Orders_ID` = "'.$orderID.'"  AND `OrderDishInfo_id`="'.$OrderDishInfo_id.'"  ORDER BY `CustomName`',""  )  ;
  // foreach(  $options as $attribute =>$value){
  //   $value ->id =  $value ->CustomType_id;
  //   unset($value ->CustomType_id);  

  // }
  // error_log(print_r("option here",true));

  //   error_log(print_r($options,true));
 
  foreach(  $options as $attribute=>$value ){
    // $orderDetails=array("ChoiceOf"=>$value ->ChoiceTittle,
    //                     "Options"=> $options );
   $objToPush="";
    $objToPush->Options = array(); 
    
   
    //&& array_search($attribute->CustomName, array_column($finalChoiceObject, 'ChoiceOf')) !== false
      foreach(  $options as $attribute ){ 
         $objToPush->ChoiceOf = $value->CustomName;
        // error_log(print_r("Printing Attribute",true));
        // error_log(print_r($objToPush->ChoiceOf ." compared to ". $attribute->CustomName ,true));
        if ( $value->CustomName == $attribute->CustomName ) {
         array_push( $objToPush->Options,$attribute );
         }   
 

      }

      //  error_log(print_r("count of objToPush->Options",true));
      //  error_log(print_r( $objToPush->Options ,true));
        
    //  if (count($objToPush->Options )>0 || !empty($objToPush->Options)){ 
    //       array_push( $finalChoiceObject,$objToPush);   
    //     }

   
     array_push( $finalChoiceObject,$objToPush);  
    

    
  }

  // error_log(print_r($finalChoiceObject,true));

 
//making sure there is not duplicates
  $choices = array_map("unserialize", array_unique(array_map("serialize", $finalChoiceObject)));

  return  array_values($choices );


}

// public static function ReturnOrderChoices ($orderID="", $OrderDishInfo_id="") {
  //error_log(print_r( "OrderDishInfo_id fron inside extra"  ,true));
  // error_log(print_r("solving choices",true));
  // error_log(print_r($OrderDishInfo_id,true));
  // error_log(print_r($orderID,true));


  // $finalArrat=[];
  // $options = self::find_by_sql(' SELECT * FROM `OrdersItemChoices_Opt` WHERE `Orders_ID` = "'.$orderID.'" AND `OrderDishInfo_id` = "'.$OrderDishInfo_id.'" '  )  ;
 
  // $finalChoiceObject = array();
  //   $objToPush;
  //   error_log(print_r("getting thid",true));
  //   error_log(print_r($orderID." ".$OrderDishInfo_id,true));
 
    // foreach($listDishChoices as $attribute){
    //   $objToPush="";
    //   $objToPush->Options = array();
    //   $objToPush->ChoiceOf = $attribute->CustomName;

    //   $sqlOptions = "SELECT * FROM `DishCustomization` WHERE `CustomName` = '".$attribute->CustomName."' ";
    //    $options = static::find_by_sql($sqlOptions);
       
    //    foreach($options as $Optattribute){
    //     $OptionDetails="";
    //    $OptionDetails->CustomType = $Optattribute->CustomType;
    //    $OptionDetails->Name = $Optattribute->Name;
    //    $OptionDetails->Price = $Optattribute->Price;
    //    $OptionDetails->SideDish_id = $Optattribute->SideDish_id;
    //    $OptionDetails->id = $Optattribute->id;
    //    $OptionDetails->CustomName = $attribute->CustomName;
    //    $OptionDetails->ChoiceTittle = $attribute->CustomName;
    //    $OptionDetails->checked =$Optattribute->checked;

    //    // $objToPush->Options[$i]=$OptionDetails;
    //     array_push( $objToPush->Options,$OptionDetails);

    //    // error_log(print_r($options,true));
    //    }

     
    //    array_push( $finalChoiceObject,$objToPush);


    // }

     
    // return $finalChoiceObject;


// }




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

  public static function setGetField ($idf) {
    return self::$db_fields['id'] = $idf;
  }
	
  public function full_name() {
    if(isset($this->First_Name) && isset($this->Last_Name)) {
      return $this->First_Name . " " . $this->Last_Name;
    } else {
      return "";
    }
  }




}

?>