app.directive('zlLogin', function () {
  return {
    restrict: 'E',
    scope: true,
    replace: false,
    templateUrl: 'directives/login/login.html',
    controller: function (blockUI, $auth, $location, $scope, $uibModalStack) {
      $scope.login = function () {
        if ($scope.validateLogin()) {
          return;
        }

        $auth.login($scope.username, $scope.password, function (err) {
          if (err) {
            reset();
          }
          else {
            $location.path('/profile');
            $uibModalStack.dismissAll();
          }
        });
      };

      $scope.validateLogin = function () {
        if (!$scope.username) {
          return true;
        }

        if (!$scope.password) {
          return true;
        }

        return false;
      };

      function reset() {
        $scope.username = "";
        $scope.password = "";
      }
    }
  };
});


