<%@ Page Title="Title" Language="C#" MasterPageFile="~/Site.Master" %>
<%@ Import Namespace="eDocument.Server.Administrator.User" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Sjekk om brukeren er her pga. låst konto
            var isLocked = Request.QueryString["locked"];
            if (isLocked == "true")
            {
                lblMessage.Visible = true; // En melding om at brukeren må låse opp kontoen
                lblMessage.Text = "Din konto er låst. Vennligst tilbakestill passordet for å låse opp kontoen.";
            }
        }
    }

    private void btnResetPassword_OnClick(object sender, EventArgs e)
    {

        //var username = txtUsername.Text.Trim();
        var mail = txtMail.Text.Trim().ToLower();

        if(string.IsNullOrWhiteSpace(mail))
        {
            lblMessage.Text = "Oppgi e-post.";
            return;
        }


        var sendt = LogIn.SendPasswordMail(mail);

        if(sendt)
        {
            lblMessage.Text = string.Empty;
            lblMessageOk.Text = "En e-post med instruksjoner om hvordan du nullstiller passordet ditt er sendt til '" + mail + "'.";
        }
        else
        {
            lblMessageOk.Text = string.Empty;
            lblMessage.Text = "Fant ikke bruker med oppgitt e-postadresse";
        }
    }


</script>
<asp:Content runat="server" ID="Main" ContentPlaceHolderID="MainContent">
    <div class="row">
        <div class="col-12 col-lg-6 offset-lg-3">
            <h1 class="skjema-admin__page-title skjema-admin__text-center d-block">
                Nullstill passord
            </h1>
                
            <div class="skjema-admin__mb-20">
                <label class="skjema-admin__page__label">
                    E-postadresse (brukernavn)
                    <asp:RequiredFieldValidator runat="server" ControlToValidate="txtMail" ForeColor="Red" Display="Dynamic">*</asp:RequiredFieldValidator>
                </label>

                <asp:TextBox 
                    ID="txtMail" 
                    CssClass="skjema-admin__form__input"
                    runat="server" 
                    MaxLength="300">
                </asp:TextBox>
                
                <asp:RegularExpressionValidator runat="server" ControlToValidate="txtMail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic" ErrorMessage="Ugyldig e-postadresse" ForeColor="Red" />
                <asp:Label runat="server" ID="lblEmail" ForeColor="Red"></asp:Label>
            </div>

            <br />
            
            
            <asp:Button 
                ID="btnResetPassword" 
                CssClass="skjema-admin__button skjema-admin__button--primary mb-5"
                runat="server" 
                Text="Nullstill passord" 
                OnClick="btnResetPassword_OnClick">
            </asp:Button>
            
            <br/>
            <asp:Label runat="server" id="lblMessage" ForeColor="Red"></asp:Label><br/>
            <asp:Label runat="server" id="lblMessageOk" ForeColor="Green"></asp:Label>
        </div>
    </div>
</asp:Content>
