'use strict';

var EmployeeAttendanceCtrl = ['$scope', '$location', '$routeParams', 'dateFilter', 'EmployeeAttendance', 'attendance_types', 'info', 'Employee', function ($scope, $location, $routeParams, dateFilter, EmployeeAttendance, attendance_types, info, Employee) {
    $scope.attendance_types = attendance_types;
    $scope.info = info;
    $scope.show = function () {
        var id = $scope.info.Employee.LedgerID;
        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');
        }
        var start_date = $scope.info.StartDate;
        var finish_date = $scope.info.FinishDate;
        if (id == $routeParams.id && start_date == $routeParams.StartDate && finish_date == $routeParams.FinishDate) {
            EmployeeAttendance.get({id: id, StartDate: start_date, FinishDate: finish_date}, function (data) {
                $scope.info = data;
            });
        }
        else {
            $location.path('/EmployeeAttendance/' + $scope.info.Employee.LedgerID).search({StartDate: $scope.info.StartDate, FinishDate: $scope.info.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,
            start_date = $route.current.params.StartDate,
            finish_date = $route.current.params.FinishDate;

        if (typeof id === 'undefined') {
            return EmployeeAttendance.get({}).$promise;
        } else {
            return EmployeeAttendance.get({id: id, StartDate: start_date, FinishDate: finish_date}).$promise;
        }
    }],
    attendance_types: ['AttendanceTypes', function (AttendanceTypes) {
        return AttendanceTypes.query({}).$promise;
    }]
};