'use strict';

var RawMaterialCostCtrl = ['$scope', '$location', 'dateFilter', 'raw_material_cost', function ($scope, $location, dateFilter, raw_material_cost) {
    $scope.info = raw_material_cost;
    $scope.show = function () {
        if (angular.isDate($scope.info.StartDate)) {
            $scope.info.StartDate = dateFilter($scope.info.StartDate, 'dd-MMM-yyyy');
        }
        if (angular.isDate($scope.info.FinishDate)) {
            $scope.info.FinishDate = dateFilter($scope.info.FinishDate, 'dd-MMM-yyyy');
        }
        $location.path('/RawMaterialCost').search({StartDate: $scope.info.StartDate, FinishDate: $scope.info.FinishDate});
    };
    $('#txtStartDate').focus();
}];

RawMaterialCostCtrl.resolve = {
    raw_material_cost: ['$route', 'RawMaterialCost', function ($route, RawMaterialCost) {
        var id = $route.current.params.id,
            start_date = $route.current.params.StartDate,
            finish_date = $route.current.params.FinishDate;

        if (typeof start_date === 'undefined' || typeof finish_date === 'undefined') {
           return RawMaterialCost.get({}).$promise;
        } else if (typeof id === 'undefined') {
           return RawMaterialCost.get({StartDate: start_date, FinishDate: finish_date}).$promise;
        } else {
            return RawMaterialCost.get({id: id, StartDate: start_date, FinishDate: finish_date}).$promise;
        }
    }]
};
