{"size": 2755, "ext": "js", "max_stars_repo_path": "public/js/controllers/ItemCtrl.js", "max_stars_repo_name": "monikagupta78/inventory_mgmt", "max_stars_repo_stars_event_min_datetime": "2018-07-09T14:00:16.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-06T05:09:34.000Z", "max_issues_repo_path": "public/js/controllers/ItemCtrl.js", "max_issues_repo_name": "monikagupta78/inventory_mgmt", "max_issues_repo_issues_event_min_datetime": "2019-01-02T09:08:37.000Z", "max_issues_repo_issues_event_max_datetime": "2019-01-02T09:08:37.000Z", "max_forks_repo_path": "public/js/controllers/ItemCtrl.js", "max_forks_repo_name": "monikagupta78/inventory_mgmt", "max_forks_repo_forks_event_min_datetime": "2018-06-20T12:18:28.000Z", "max_forks_repo_forks_event_max_datetime": "2021-03-01T05:32:25.000Z"}
{"max_stars_count": 5, "max_issues_count": 1, "max_forks_count": 6, "avg_line_length": 23.3474576271, "max_line_length": 114, "alphanum_fraction": 0.4820326679}
/**
 * Created by talha on 8/14/15.
 */

'use strict';

angular.module('inventoryApp').controller('ItemController', function($scope, Item, $mdDialog, $filter, $timeout) {

    Item.getItems("", function(result){
        $scope.items = result.data;
    },
    function(err){
        console.log(err)
    });


    //TODO Integrate database search
    $scope.searchTextChanged = function(){
      Item.getItems($scope.searchText, function(result){
        $scope.items = result.data;
    },
    function(err){
        console.log(err)
    })

    }


    $scope.openAddItemDialog = function(ev) {
        $mdDialog.show({
            controller: AddItemController,
            templateUrl: '/views/dialogs/add-item.html',
            targetEvent: ev
        })
            .then(function(result) {
                if(result.status == 200){
                    $scope.items.push(result.data);
                }

            }, function() {
                console.log("Dialog Cancelled");
            });
    };



    $scope.editItem = function(ev, item, index){
        $mdDialog.show({
            controller: EditItemController,
            templateUrl: '/views/dialogs/edit-item.html',
            locals:{
              "item": angular.copy(item)
            },
            targetEvent: ev
        })
            .then(function(result) {
                if(result.status == 200){
                    if(result.data){
                        $scope.items[index] = result.data;
                    }
                    else{
                        $scope.items.splice(index, 1);
                    }
                }            
            }, function(){
                console.log("Dialog Cancelled");

            });

    };


});


function AddItemController ($scope, $mdDialog, Item){

    $scope.addItem = function(){
        Item.addItem($scope.item, function(data){
            $mdDialog.hide(data);
        }, function(err){
            console.log(err);
            $mdDialog.hide();
        })
    }
    $scope.closeDialog = function () {
        $mdDialog.cancel();
    }
}


function EditItemController ($scope, $mdDialog, item, Item){
    $scope.item = item;
    $scope.deleteItem = function(ev, item){
        Item.deleteItem($scope.item._id, function(data){
                $mdDialog.hide(data);
            },
            function(err){
                console.log(err)
                $mdDialog.hide(err);
            })
    };


    $scope.editItem = function(){
        Item.editItem($scope.item, function(data){
            $mdDialog.hide(data);
        }, function(err){
            console.log(err)
            $mdDialog.hide(err);

        })
    }

    $scope.closeDialog = function () {
        $mdDialog.cancel();
    }
}