/*! grafana - v4.2.0 - 2017-03-24
 * Copyright (c) 2017 Torkel Ödegaard; Licensed Apache-2.0 */

define(["angular","lodash","../core_module","app/core/config"],function(a,b,c,d){"use strict";var e={1000:"Required team membership not fulfilled",1001:"Required organization membership not fulfilled",1002:"Required email domain not fulfilled",1003:"Login provider denied login request"};c.default.controller("LoginCtrl",["$scope","backendSrv","contextSrv","$location",function(a,c,f,g){a.formModel={user:"",email:"",password:""},f.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);var b=g.search();b.failCode&&(a.appEvent("alert-warning",["Login Failed",e[b.failCode]]),delete b.failedMsg,g.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&&c.post("/api/user/signup",a.formModel).then(function(b){"SignUpCreated"===b.status?g.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=g.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()}])});