'use strict';

var ProductGroupListCtrl = ['$scope', 'product_groups', function ($scope, product_groups) {
    $scope.info = product_groups;

    $scope.move = function (location, distance) {
        var result = Array($scope.info.length);
        _.forEach($scope.info, function (value, index) {
            if (index === location) {
                result[location + distance] = value;
            } else if (index == location + distance) {
                result[location] = value;
            } else {
                result[index] = value;
            }
        });
        $scope.info = result;
    }
}];

ProductGroupListCtrl.resolve = {
    product_groups: ['ProductGroup', function (ProductGroup) {
        return ProductGroup.query({}).$promise;
    }]
};

var ProductGroupCtrl = ['$scope', '$location', 'product_group', function ($scope, $location, product_group) {
    $scope.product_group = product_group;

    $scope.save = function () {
        $scope.product_group.$save(function (u, putResponseHeaders) {
            $scope.$emit('notification',{Type: 'Success', Message: ''});
            $location.path('/ProductGroups')
        }, function (data, status) {
            $scope.$emit('notification',{Type: 'Danger', Message: data.data});
        });
    };

    $scope.delete = function () {
        $scope.product_group.$delete(function (u, putResponseHeaders) {
            $scope.$emit('notification',{Type: 'Success', Message: ''});
            $location.path('/ProductGroups')
        }, function (data, status) {
            $scope.$emit('notification',{Type: 'Danger', Message: data.data});
        });
    };
    $('#txtName').focus();
}];

ProductGroupCtrl.resolve = {
    product_group: ['$route', 'ProductGroup', function ($route, ProductGroup) {
        var id = $route.current.params.id;
        return ProductGroup.get({id: id}).$promise;
    }]
};
