'use strict';

var EmployeeFunctionsCtrl = ['$scope', '$http', 'dateFilter', function ($scope, $http, dateFilter) {
    $scope.record = {};
    $scope.creditSalary = function () {
        if (!angular.isDate($scope.salaryMonth)) {
            return;
        }
        var salary_month = dateFilter($scope.salaryMonth, 'dd-MMM-yyyy');
        return $http.
            post('/api/CreditSalary', {Month: salary_month}).
            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 start_date = angular.isDate($scope.record.StartDate) ? dateFilter($scope.record.StartDate, 'dd-MMM-yyyy') : $scope.record.StartDate;
        var finish_date = angular.isDate($scope.record.FinishDate) ? dateFilter($scope.record.FinishDate, 'dd-MMM-yyyy') : $scope.record.FinishDate;

        var url = '/AttendanceReport?StartDate=' + start_date + "&FinishDate=" + finish_date;
        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, status, headers, config) {
                $scope.toasts.push({Type: 'Danger', Message: data});
            });
    };
}];

