'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');
        }
        $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: ['$q', '$route', 'Attendance', function ($q, $route, Attendance) {
        var deferred = $q.defer();
        var date = $route.current.params.date;
        var successCb = function (result) {
            deferred.resolve(result);
        };

        if (typeof date === 'undefined') {
            Attendance.get({}, successCb);
        } else {
            Attendance.get({date: date}, successCb);
        }
        return deferred.promise;
    }],
    attendance_types: ['$q', 'AttendanceTypes', function ($q, AttendanceTypes) {
        var deferred = $q.defer();
        var successCb = function (result) {
            deferred.resolve(result);
        };
        AttendanceTypes.query({}, successCb);
        return deferred.promise;
    }]
};