/*! grafana - v3.1.0 - 2016-07-18
 * Copyright (c) 2016 Torkel Ödegaard; Licensed Apache-2.0 */

define(["angular","../core_module","app/core/config"],function(a,b,c){"use strict";b["default"].controller("LoginCtrl",["$scope","backendSrv","contextSrv","$location",function(a,b,d,e){a.formModel={user:"",email:"",password:""},d.sidemenu=!1,a.googleAuthEnabled=c.googleAuthEnabled,a.githubAuthEnabled=c.githubAuthEnabled,a.oauthEnabled=c.githubAuthEnabled||c.googleAuthEnabled,a.disableUserSignUp=c.disableUserSignUp,a.loginHint=c.loginHint,a.loginMode=!0,a.submitBtnText="Log in",a.init=function(){a.$watch("loginMode",a.loginModeChanged);var b=e.search();b.failedMsg&&(a.appEvent("alert-warning",["Login Failed",b.failedMsg]),delete b.failedMsg,e.search(b))},a.submit=function(){a.loginMode?a.login():a.signUp()},a.loginModeChanged=function(b){a.submitBtnText=b?"Log in":"Sign up"},a.signUp=function(){a.loginForm.$valid&&b.post("/api/user/signup",a.formModel).then(function(b){"SignUpCreated"===b.status?e.path("/signup").search({email:a.formModel.email}):window.location.href=c.appSubUrl+"/"})},a.login=function(){delete a.loginError,a.loginForm.$valid&&b.post("/login",a.formModel).then(function(a){var b=e.search();b.redirect&&"/"===b.redirect[0]?window.location.href=c.appSubUrl+b.redirect:a.redirectUrl?window.location.href=a.redirectUrl:window.location.href=c.appSubUrl+"/"})},a.init()}])});