'use strict';

var EmployeeAttendanceCtrl = ['$scope', '$location', '$routeParams', 'asDateFilter', 'EmployeeAttendance', 'attendanceTypes', 'info', 'Employee', function ($scope, $location, $routeParams, asDate, EmployeeAttendance, attendanceTypes, info, Employee) {
    $scope.attendanceTypes = attendanceTypes;
    $scope.info = info;
    $scope.show = function () {
        var id = $scope.info.Employee.LedgerID;
        var startDate = asDate($scope.info.StartDate);
        var finishDate = asDate($scope.info.FinishDate);
        if (id === $routeParams.id && startDate === $routeParams.StartDate && finishDate === $routeParams.FinishDate) {
            EmployeeAttendance.get({id: id, StartDate: startDate, FinishDate: finishDate}, function (data) {
                $scope.info = data;
            });
        }
        else {
            $location.path('/EmployeeAttendance/' + id).search({StartDate: startDate, FinishDate: finishDate});
        }
    };

    $scope.employees = function ($viewValue) {
        return Employee.autocomplete({term: $viewValue, count: 20}).$promise;
    };

    $scope.save = function () {
        return $scope.info.$save(function (u, putResponseHeaders) {
            $scope.toasts.push({Type: 'Success', Message: 'Saved!'});
        }, function (data, status) {
            $scope.toasts.push({Type: 'Danger', Message: data.data});
        });
    };
    $('#txtEmployee').focus();
}];

var EmployeeAttendanceSubCtrl = ['$scope', function ($scope) {
    $scope.original = {};
    angular.copy($scope.item, $scope.original);

    $scope.isLabel = function () {
        return true;
    };

    $scope.isError = function () {
        return $scope.item.Worked === 'Error';
    };

    $scope.isGood = function () {
        return $scope.item.Worked === true;
    };

    $scope.isBad = function () {
        return $scope.item.Worked === false;
    };

    $scope.isDirty = function () {
        return !angular.equals($scope.original, $scope.item)
    };
}];
EmployeeAttendanceCtrl.resolve = {
    info: ['$route', 'EmployeeAttendance', function ($route, EmployeeAttendance) {
        var id = $route.current.params.id,
            startDate = $route.current.params.StartDate,
            finishDate = $route.current.params.FinishDate;

        if (angular.isUndefined(id)) {
            return EmployeeAttendance.get({}).$promise;
        } else {
            return EmployeeAttendance.get({id: id, StartDate: startDate, FinishDate: finishDate}).$promise;
        }
    }],
    attendanceTypes: ['AttendanceTypes', function (AttendanceTypes) {
        return AttendanceTypes.query({}).$promise;
    }]
};