app.controller('mealsConstructor', ['$window','$scope','$interval',function($window,$scope, $interval) { // $scope.list1 = [ // {'title': 'Br_1', imageUrl:'./img/1.jpg', price: 10},{'title': 'Br_2', imageUrl:'./img/1.jpg', price: 10}, // {'title': 'Br_3', imageUrl:'./img/1.jpg', price: 10},{'title': 'Br_4', imageUrl:'./img/1.jpg', price: 10} // ]; // $scope.list2 = [ // {'title': 'Lu_1', imageUrl:'./img/2.jpg', price: 10},{'title': 'Lu_2', imageUrl:'./img/2.jpg', price: 10}, // {'title': 'Lu_3', imageUrl:'./img/2.jpg', price: 10},{'title': 'Lu_4', imageUrl:'./img/2.jpg', price: 10} // ]; // $scope.list3 = [ // {'title': 'Di_1', imageUrl:'./img/3.jpg', price: 10},{'title': 'Di_2', imageUrl:'./img/3.jpg', price: 10}, // {'title': 'Di_3', imageUrl:'./img/3.jpg', price: 10},{'title': 'Di_4', imageUrl:'./img/3.jpg', price: 10} // ]; // $scope.list4 = [ // {'title': 'Sn_1', imageUrl:'./img/4.jpg', price: 10},{'title': 'Sn_2', imageUrl:'./img/4.jpg', price: 10}, // {'title': 'Sn_3', imageUrl:'./img/4.jpg', price: 10},{'title': 'Sn_4', imageUrl:'./img/4.jpg', price: 10} // ]; $scope.list1 = []; $scope.list2 = []; $scope.list3 = []; $scope.list4 = []; $scope.firebaseMeals = firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('meals'); $scope.firebaseGlobalMeals = firebase.database().ref().child('meals'); $scope.firebaseMeals.on('value', function(snap){ $scope.userMeals = snap.val(); var userMealsKeys = Object.keys($scope.userMeals); for (var i = 0; i < userMealsKeys.length; i++) { if($scope.userMeals[userMealsKeys[i]].type == 'breakfast' && $scope.userMeals[userMealsKeys[i]].isSelected == true){ $scope.list1.push($scope.userMeals[userMealsKeys[i]]); } if($scope.userMeals[userMealsKeys[i]].type == 'lunch' && $scope.userMeals[userMealsKeys[i]].isSelected == true){ $scope.list2.push($scope.userMeals[userMealsKeys[i]]); } if($scope.userMeals[userMealsKeys[i]].type == 'dinner' && $scope.userMeals[userMealsKeys[i]].isSelected == true){ $scope.list3.push($scope.userMeals[userMealsKeys[i]]); } if($scope.userMeals[userMealsKeys[i]].type == 'snacks' && $scope.userMeals[userMealsKeys[i]].isSelected == true){ $scope.list4.push($scope.userMeals[userMealsKeys[i]]); } } }) $scope.arrDays = [ {caption: 'DAY 1', day: 'day1'}, {caption: 'DAY 2', day: 'day2'}, {caption: 'DAY 3', day: 'day3'}, {caption: 'DAY 4', day: 'day4'}, {caption: 'DAY 5', day: 'day5'}, {caption: 'DAY 6', day: 'day6'}, {caption: 'DAY 7', day: 'day7'}, ]; //$scope.todayDay = new Date().getDate() + 1; //$scope.todayMonthYear = ( new Date().getMonth() + 1 + '/' + new Date().getFullYear() ); $scope.order = { day1: { breakfast: [], lunch: [], dinner: [], snacks: [] }, day2: { breakfast: [], lunch: [], dinner: [], snacks: [] }, day3: { breakfast: [], lunch: [], dinner: [], snacks: [] }, day4: { breakfast: [], lunch: [], dinner: [], snacks: [] }, day5: { breakfast: [], lunch: [], dinner: [], snacks: [] }, day6: { breakfast: [], lunch: [], dinner: [], snacks: [] }, day7: { breakfast: [], lunch: [], dinner: [], snacks: [] }, }; $scope.sumToBacket = 0; $interval(function(){ let keyDay = ['breakfast','lunch','dinner','snacks'] let keyObj = Object.keys($scope.order); var sum = 0; for(let i = 0; i < keyObj.length; i++) { //console.log($scope.order[keyObj[i]]); for(let j=0; j < keyDay.length; j++) { //console.log($scope.order[keyObj[i]][keyDay[j]]); for(let l=0; l < $scope.order[keyObj[i]][keyDay[j]].length; l++) { //console.log($scope.order[keyObj[i]][keyDay[j]][l]['price']); sum += +$scope.order[keyObj[i]][keyDay[j]][l]['price']; } } } $scope.sumToBacket = sum; },500); $scope.deleteMeal = function(day, typeMeal,index) { //console.log($scope.order[day][typeMeal][index]); $scope.order[day][typeMeal].splice(index,1); } $scope.hideMe = function(day,typeMeal) { return $scope.order[day][typeMeal].length > 0 }; $scope.sendObject = { imageUrl: 'content/images/ready-menu/ready-menu-item/custom-menu-item.jpg', title: 'Custom menu ', weekCount: 1, order: $scope.order } $scope.firebaseShoppingCart = firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('shoppingCart'); $scope.firebaseShoppingCart.on('value', function(snap){ $scope.shoppingCartItems = snap.val(); $scope.customMenuCount = 1; angular.forEach($scope.shoppingCartItems, function(item,key){ if(item.title.indexOf('Custom menu') > -1){ $scope.customMenuCount++; } }) }) $scope.sendCustomDiet = function(){ $scope.sendObject.overallPrice = $scope.sumToBacket; $scope.sendObject.title += $scope.customMenuCount; $scope.firebaseShoppingCart.push().set(angular.copy($scope.sendObject)); $scope.firebaseGlobalMeals.on('value', function(snap){ $scope.globalMeals = snap.val(); firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('meals').set($scope.globalMeals); $window.location.assign('#!/shopping-cart'); }) } }]);