<%
Class RequestOrderController_Class
    Private m_useLayout
    Private m_title

    Private Sub Class_Initialize()
        m_useLayout = True
        m_title = "Request an Order"
    End Sub

    Public Property Get useLayout
        useLayout = m_useLayout
    End Property

    Public Property Let useLayout(v)
        m_useLayout = v
    End Property

    Public Property Get Title
        Title = m_title
    End Property

    Public Property Let Title(v)
        m_title = v
    End Property

    '-------------------------------------------------------------------------------------------------------------------
    ' GET: show the "request an order" form (email + jurisdiction number)
    '-------------------------------------------------------------------------------------------------------------------
    Public Sub Index()
        Dim formValues, emailValue, jurisdictionValue, csrfToken, turnstileSiteKey

        Set formValues = FormCache().DeserializeForm("RequestOrder")
        FormCache().ClearForm "RequestOrder"

        emailValue = ""
        jurisdictionValue = ""
        If Not (formValues Is Nothing) Then
            If formValues.Exists("Email") Then emailValue = formValues("Email")
            If formValues.Exists("JurisdictionNumber") Then jurisdictionValue = formValues("JurisdictionNumber")
        End If

        csrfToken = HTMLSecurity().GetAntiCSRFToken("RequestOrder")
        turnstileSiteKey = GetAppSetting("TurnstileSiteKey")
    %>
    <!--#include file="../views/RequestOrder/index.asp" -->
    <%
    End Sub

    '-------------------------------------------------------------------------------------------------------------------
    ' POST: validate submission, create the order record, email a continuation link
    '-------------------------------------------------------------------------------------------------------------------
    Public Sub Create()
        MVC.RequirePost

        Dim csrfToken : csrfToken = Request.Form("csrf_token")
        If Not HTMLSecurity().IsValidAntiCSRFToken("RequestOrder", csrfToken) Then
            HTMLSecurity().ClearAntiCSRFToken "RequestOrder"
            Flash().AddError "Your form session expired. Please try again."
            Response.Redirect Routes().AppURL & "request-order"
            Exit Sub
        End If
        HTMLSecurity().ClearAntiCSRFToken "RequestOrder"

        Dim email, jurisdictionNumber
        email = Trim(Request.Form("Email"))
        jurisdictionNumber = Trim(Request.Form("JurisdictionNumber"))

        Dim errorList() : ReDim errorList(-1)
        If Not IsValidEmail(email) Then AddValidationError errorList, "Please enter a valid email address."
        If Len(jurisdictionNumber) = 0 Then
            AddValidationError errorList, "Please enter your jurisdiction number."
        ElseIf Not IsValidJurisdictionNumber(jurisdictionNumber) Then
            AddValidationError errorList, "That jurisdiction number could not be verified."
        End If

        Dim turnstileToken : turnstileToken = Request.Form("cf-turnstile-response")
        If Not VerifyTurnstileToken(turnstileToken, Request.ServerVariables("REMOTE_ADDR")) Then
            AddValidationError errorList, "Please complete the verification challenge."
        End If

        If UBound(errorList) >= 0 Then
            Dim i
            For i = 0 To UBound(errorList)
                Flash().AddError errorList(i)
            Next
            FormCache().SerializeForm "RequestOrder", Request.Form
            Response.Redirect Routes().AppURL & "request-order"
            Exit Sub
        End If

        Dim token : token = OrdersRepository().CreateOrder(email, jurisdictionNumber)
        OrderMailer().SendContinuationEmail email, token

        Flash().Success = "Check your email for a link to continue your order."
        Response.Redirect Routes().AppURL
    End Sub

End Class

Dim RequestOrderController_Class__Singleton
Function RequestOrderController()
    If IsEmpty(RequestOrderController_Class__Singleton) Then
        Set RequestOrderController_Class__Singleton = New RequestOrderController_Class
    End If
    Set RequestOrderController = RequestOrderController_Class__Singleton
End Function
%>
