'use strict';

var AttendanceCtrl = ['$scope', '$location', 'asDateFilter', 'Attendance', 'attendanceTypes', 'info', function ($scope, $location, asDate, Attendance, attendanceTypes, info) {
    $scope.attendanceTypes = attendanceTypes;
    $scope.info = info;
    $scope.show = function () {
        $scope.info.Date = asDate($scope.info.Date);
        $location.path('/Attendance/' + $scope.info.Date);
    };
    $scope.save = function () {
        $scope.info.Date = asDate($scope.info.Date);
        return $scope.info.$save(function (u) {
            $scope.toasts.push({Type: 'Success', Message: u.Code});
        }, function (data) {
            $scope.toasts.push({Type: 'Danger', Message: data.data});
        });
    };
    $('#txtDate').focus();
}];

var AttendanceSubCtrl = ['$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);
    };
}];

AttendanceCtrl.resolve = {
    info: ['$route', 'Attendance', function ($route, Attendance) {
        var date = $route.current.params.date;
        if (angular.isUndefined(date)) {
            return Attendance.get({}).$promise;
        } else {
            return Attendance.get({date: date}).$promise;
        }
    }],
    attendanceTypes: ['AttendanceTypes', function (AttendanceTypes) {
        return AttendanceTypes.query({}).$promise;
    }]
};