'use strict';

function ProductListCtrl($scope, products) {
    $scope.info = products;
}

ProductListCtrl.resolve = {
    products:function ($q, $route, Product) {
        var deferred = $q.defer();

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

        Product.query({}, successCb);
        return deferred.promise;
    }
};

function ProductCtrl($scope, $location, product, product_groups) {
    $scope.product = product;
    $scope.product_groups = product_groups;

    $scope.save = function () {
        $scope.product.$save(function (u, putResponseHeaders) {
            $scope.toasts.push({Type:'Success', Message:u.Code});
            $location.path('/Products')
        }, function (data, status) {
            $scope.toasts.push({Type:'Error', Message:data.data});
        });
    };

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

}

ProductCtrl.resolve = {
    product:function ($q, $route, Product) {
        var deferred = $q.defer();

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

        Product.get({id: id}, successCb);
        return deferred.promise;
    },
    product_groups:function ($q, ProductGroup) {
        var deferred = $q.defer();

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

        ProductGroup.query({}, successCb);
        return deferred.promise;
    }
};
