'use strict';

var SettingsCtrl = ['$scope', '$http', 'dateFilter', 'lockDate', function ($scope, $http, dateFilter, lockDate) {
    $scope.lockDate = lockDate.data['Lock Date'];
    $scope.setLockDate = function () {
        if (angular.isDate($scope.lockDate)) {
            $scope.lockDate = dateFilter($scope.lockDate, 'dd-MMM-yyyy');
        }

        $http({method: 'POST', url: '/api/LockDate', params: {Date: $scope.lockDate}}).
            success(function (data) {
                $scope.lockDate = data['Lock Date'];
                $scope.toasts.push({Type: 'Success', Message: ''});
            }).
            error(function (errorMessage) {
                $scope.toasts.push({Type: 'Danger', Message: errorMessage});
            });
    };
    $scope.clearLockDate = function () {
        $http({method: 'POST', url: '/api/LockDate', params: {Clear: ''}}).
            success(function (data) {
                $scope.lockDate = data['Lock Date'];
                $scope.toasts.push({Type: 'Success', Message: ''});
            }).
            error(function (errorMessage) {
                $scope.toasts.push({Type: 'Danger', Message: errorMessage});
            });
    };
}]

SettingsCtrl.resolve = {
    lockDate: ['$http', function ($http) {
        return $http.get('/api/LockDate', {});
    }]
};

