'use strict';

define(['app'], function(app) {
	app.controller('CategoryCtrl', ['$http', '$scope', '$routeParams',
        function($http, $scope, $routeParams) {
        $scope.categories = [];

        $scope.getFeed = function(_jsonPath){


            $http({
                method: 'GET', 
                url: globals.RootPath + '/' + _jsonPath + '?parent=' + $routeParams.categoryID
            })

            .success(function(res, status){               
                if (status !== 200) return false;

                $scope.categories = [];

                for (var i=0; i<res.data.length; i++) {
                    var category = res.data[i];
                    // var catClass = '';
                    // switch (news.category) {
                    //     case 'Dešavanja':
                    //         catClass = 'events';
                    //     break;

                    //     case 'Aktuelno':
                    //         catClass = 'current';
                    //     break;

                    //     case 'Blog':
                    //         catClass = 'blog';
                    //     break;

                    //     case 'Vijesti':
                    //         catClass = 'news';
                    //     break;
                    // }
                    // news.catClass = catClass;
                    // var pubDate = moment(news.published).format("DD.MM.YYYY.");;
                    // news.pubDate = pubDate;
                    
                    $scope.categories.push(category);
                    // if (category.parent == 0) {
                    // }
                }
            })

            .error(function(res, status){
                console.log('error status: ' + status);
                console.debug(res);
            });
        }

        $scope.searchStores = function() {
            var query = $scope.search;
            
            $http({
                method: 'GET', 
                url: globals.RootPath + '/search.json?search=' + query + '&category=' + $routeParams.categoryID
            })
            .success(function(res, status) {

                // console.log('i am in success');
              
                $scope.searchResult = res.data;
                // console.log($scope.searchResult);


                $scope.searchResults = [];

                for (var i=0; i<res.data.length; i++) {
                    var term = res.data[i];
                    $scope.searchResults.push(term);
                    // if (category.parent == 0) {
                    // }
                }

                if($scope.searchResult.length >0){
                    $('.beforeSearch').hide();
                    $('.searchSuccess').show();
                }else{
                    $('.searchSuccess').hide();
                    $('.beforeSearch').show();
                }

                if($scope.searchResult.length == 0){
                    $('.searchSuccess').hide();
                    $('.beforeSearch').hide();
                    $('.noSearchResult').show();
                }


            })
            .error(function(res, status) {
                 console.log('error status: ' + status);
                console.debug(res);
            });
        }
            
        $scope.getFeed('store_categories.json');
        
	}]);
});