'use strict';

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

ProductGroupListCtrl.resolve = {
    product_groups: ['$q', 'ProductGroup', function ($q, ProductGroup) {
        var deferred = $q.defer();

        var successCb = function (result) {
            deferred.resolve(result);
        };

        ProductGroup.query({}, successCb);
        return deferred.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.toasts.push({Type: 'Success', Message: ''});
            $location.path('/ProductGroups')
        }, function (data, status) {
            $scope.toasts.push({Type: 'Danger', Message: data.data});
        });
    };

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

ProductGroupCtrl.resolve = {
    product_group: ['$q', '$route', 'ProductGroup', function ($q, $route, ProductGroup) {
        var deferred = $q.defer();

        var id = $route.current.params.id;
        var successCb = function (result) {
            deferred.resolve(result);
        };

        ProductGroup.get({id: id}, successCb);
        return deferred.promise;
    }]
};
