'use strict';

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

EmployeeListCtrl.resolve = {
    employees: ['$q', '$route', 'Employee', function ($q, $route, Employee) {
        var deferred = $q.defer();

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

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

var EmployeeCtrl = ['$scope', '$routeParams', '$location', 'dateFilter', '$modal', 'employee', 'cost_centers', function ($scope, $routeParams, $location, dateFilter, $modal, employee, cost_centers) {
    $scope.employee = employee;

    $scope.cost_centers = cost_centers;

    $scope.save = function () {
        if (angular.isDate($scope.employee.JoiningDate)) {
            $scope.employee.JoiningDate = dateFilter($scope.employee.JoiningDate, 'dd-MMM-yyyy');
        }
        if (angular.isDate($scope.employee.LeavingDate)) {
            $scope.employee.LeavingDate = dateFilter($scope.employee.LeavingDate, 'dd-MMM-yyyy');
        }
        $scope.employee.$save(function (u, putResponseHeaders) {
            $scope.toasts.push({Type: 'Success', Message: u.Code});
            $location.path('/Employees');
        }, function (data, status) {
            $scope.toasts.push({Type: 'Danger', Message: data.data});
        });
    };

    $scope.delete = function () {
        $scope.employee.$delete(function (u, putResponseHeaders) {
            $scope.toasts.push({Type: 'Success', Message: ''});
            $location.path('/Employees');
        }, function (data, status) {
            $scope.toasts.push({Type: 'Danger', Message: data.data});
        });
    };

    $scope.confirm = function () {
        var modalInstance = $modal.open({
            backdrop: true,
            templateUrl: '/template/modal/confirm.html',
            controller: ['$scope', '$modalInstance', function ($scope, $modalInstance) {
                $scope.title = "Delete Account";
                $scope.body = "Are you sure? This cannot be undone.";
                $scope.isDelete = true;
                $scope.ok = function () {
                    $modalInstance.close();
                };
                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            }]
        });
        modalInstance.result.then(function () {
            $scope.delete();
        });
    };

    $('#txtName').focus();
}];

EmployeeCtrl.resolve = {
    employee: ['$q', '$route', 'Employee', function ($q, $route, Employee) {
        var deferred = $q.defer();

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

        Employee.get({id: id}, successCb);
        return deferred.promise;
    }],
    cost_centers: ['$q', '$route', 'CostCenter', function ($q, $route, CostCenter) {
        var deferred = $q.defer();

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

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