'use strict';

var AttendanceCtrl = ['$scope', '$location', 'dateFilter', 'Attendance', 'attendance_types', 'info', function ($scope, $location, dateFilter, Attendance, attendance_types, info) {
    $scope.attendance_types = attendance_types;
    $scope.info = info;
    $scope.show = function () {
        if (angular.isDate($scope.info.Date)) {
            $scope.info.Date = dateFilter($scope.info.Date, 'dd-MMM-yyyy');
        }
        $location.path('/Attendance/' + $scope.info.Date);
    };
    $scope.save = function () {
        if (angular.isDate($scope.info.Date)) {
            $scope.info.Date = dateFilter($scope.info.Date, 'dd-MMM-yyyy');
        }
        return $scope.info.$save(function (u, putResponseHeaders) {
            $scope.toasts.push({Type: 'Success', Message: u.Code});
        }, function (data, status) {
            $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 (typeof date === 'undefined') {
            return Attendance.get({}).$promise;
        } else {
            return Attendance.get({date: date}).$promise;
        }
    }],
    attendance_types: ['AttendanceTypes', function (AttendanceTypes) {
        return AttendanceTypes.query({}).$promise;
    }]
};