<?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 DishCustomization extends DatabaseObject {
	
  protected static $table_name="DishCustomization";
 
  
  //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 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 ReturnChoices ($id) {
    //error_log(print_r("ReturnChoices",true));
   // error_log(print_r(,true));
    //$listDishChoices = new self ("Blank");
    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);

    $DishDishChoice_sql = "SELECT * FROM DishCustomization WHERE MenuDishes_id='".$id."' AND CustomType ='DishChoice'  GROUP BY `CustomName`";
    
    $listDishChoices = static::find_by_sql($DishDishChoice_sql,"");
     // error_log(print_r("DishDishChoice_sql",true));
    // error_log(print_r( $DishDishChoice_sql,true));
    //Now we loop and ask each elemtn to build
    //the choice object
   
    $finalChoiceObject = array();
    $objToPush;
   // error_log(print_r($listDishChoices,true));
 
    foreach($listDishChoices as $attribute){
      $objToPush="";
      $objToPush->Options = array();
      $objToPush->ChoiceOf = $attribute->CustomName;

      //error_log(print_r("see inner",true));

      //error_log(print_r($attribute->CustomName,true));

      $sqlOptions = "SELECT * FROM `DishCustomization` WHERE MenuDishes_id='".$id."' AND  `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);


    }


    error_log(print_r("see before final return",true));

    error_log(print_r($finalChoiceObjecttrue));

     
    return $finalChoiceObject;

    

  }




	
  public static function SaveDishCustomization ($ingredients="",$MenuDish_id="",$TypeOfCustomization="" ) {

     if ($TypeOfCustomization == "DishChoices"){

     // error_log(print_r($ingredients,true));
    
        // foreach($ingredients as $attribute){
        //     $newIngredient = new self ("Blank");
         
        //     $newIngredient->MenuDishes_id=$MenuDish_id;
        //     $newIngredient->SideDish_id=$attribute->SideDish_id;
        //     $newIngredient->Price= $attribute->Price;
        //     $newIngredient->CustomType= $attribute->CustomType;
        //     $newIngredient->Name= $attribute->Name;
        //     $newIngredient->CustomName= $attribute->CustomName;
        //     $newIngredient->id= $attribute->id;
        
        //     $newIngredient->save();
    
        //      //we need to increase the used_by field on stock
        //     //to match the number of entries in the consumable table
        //     SideDishes::Increase_Used_by($attribute->SideDish_id);
        // }

        foreach($ingredients as $attribute){
          
          $myCustomName = $attribute->ChoiceOf;

           $options = $attribute->Options;
            
            foreach($options  as $Optattribute){
              $newIngredient = new self ("Blank");
         
                  $newIngredient->MenuDishes_id=$MenuDish_id;
                  $newIngredient->SideDish_id=$Optattribute->SideDish_id;
                  $newIngredient->Price= $Optattribute->Price;
                  $newIngredient->CustomType= $Optattribute->CustomType;
                  $newIngredient->Name= $Optattribute->Name;
                  $newIngredient->CustomName= $myCustomName;
                  $newIngredient->id= $Optattribute->id;
                  $newIngredient->checked ="false";

              
                  $newIngredient->save();
          
                   //we need to increase the used_by field on stock
                  //to match the number of entries in the consumable table
                  SideDishes::Increase_Used_by($Optattribute->SideDish_id);

            
            }


        }






     } else {

      foreach($ingredients as $attribute){
        $newIngredient = new self ("Blank");
     
        $newIngredient->MenuDishes_id=$MenuDish_id;
        $newIngredient->SideDish_id=$attribute->SideDish_id;
        $newIngredient->Price= $attribute->Price;
        $newIngredient->CustomType= $attribute->CustomType;
        $newIngredient->Name= $attribute->Name;
        $newIngredient->id= $attribute->id;
        $newIngredient->checked ="false";

    
        $newIngredient->save();

         //we need to increase the used_by field on stock
        //to match the number of entries in the consumable table
        SideDishes::Increase_Used_by($attribute->SideDish_id);
    }

     }
    
      
    
    }
    
  



}

?>