'use strict';

var EmployeeFunctionsController = ['$scope', '$http', 'asDateFilter', function ($scope, $http, asDate) {
    $scope.record = {};
    $scope.creditSalary = function () {
        if (!angular.isDate($scope.salaryMonth)) {
            return;
        }
        var SalaryMonth = asDate($scope.salaryMonth);
        return $http.
            post('/api/CreditSalary', {Month: SalaryMonth}).
            success(function (data) {
                $scope.toasts.push({Type: 'Success', Message: data.message});
            }).
            error(function (errorMessage) {
                $scope.toasts.push({Type: 'Danger', Message: errorMessage});
            });
    };

    $scope.attendanceRecordUrl = function () {
        if (!$scope.record.StartDate || !$scope.record.FinishDate) {
            return;
        }
        var startDate = asDate($scope.record.StartDate);
        var finishDate = asDate($scope.record.FinishDate);

        var url = '/AttendanceReport?StartDate=' + startDate + "&FinishDate=" + finishDate;
        return url;
    };
    $scope.setFile = function (element) {
        $scope.uploadedFile = element.files[0];
    };
    $scope.uploadFingerprints = function () {
        if (!$scope.uploadedFile) {
            $scope.toasts.push({Type: 'Danger', Message: 'Please choose a file first!'});
            return;
        }
        var fd = new FormData();
        fd.append("uploadedFile", $scope.uploadedFile);
        return $http
            .post('/api/Fingerprint', fd, {
                headers: {
                    'Content-Type': undefined
                },
                transformRequest: angular.identity
            }).success(function () {
                $scope.toasts.push({Type: 'Success', Message: 'Data uploaded'});
            }).error(function (data) {
                $scope.toasts.push({Type: 'Danger', Message: data});
            });
    };
}];

