/*! grafana - v4.5.1 - 2017-09-19
 * Copyright (c) 2017 Torkel Ödegaard; Licensed Apache-2.0 */

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