app.controller('AddItemPageController',['$scope','$timeout',function($scope,$timeout){
  $scope.resObj = {
    mealName: '',
    type: '',
    weight: '',
    ingredients: [,,,],
    imageUrl: '',
    price: '',
    isSelected: false
  };

  $scope.resObj2 = {};

  $scope.isFirstTab = true;

  var databaseMeals = firebase.database().ref().child('meals');
  var databaseDiets = firebase.database().ref().child('diets');

  //functions

  $scope.deleteIngredient = function(i){
    $scope.resObj.ingredients.splice(i,1);
  }
  $scope.addIngredient = function(){
    $scope.resObj.ingredients.push('');
  }

  var firebaseUsersTest = firebase.database().ref().child('users');
  firebaseUsersTest.on('value', function(snap){
    $scope.usersTest = snap.val();

  })

  $scope.sendMeal = function(){
    var userKeys = Object.keys($scope.usersTest);
    console.log(userKeys)
    for (let i = 0; i < userKeys.length; i++) {
      firebaseUsersTest.child(userKeys[i]).child('meals').push().set(Object.assign($scope.resObj));
    }
    var databaseMeals2 = firebase.database().ref().child('meals');
    databaseMeals2.push().set($scope.resObj);

  }

  $scope.sendDiet = function(){
    databaseDiets.push().set($scope.resObj2);
  }
}])
