Imports System.Data
Imports System.Data.SqlClient
Imports AppVariable
Imports AppAuth
Imports AppCommon

Partial Public Class Signup
    Inherits System.Web.UI.Page

#Region "******************** ページイベント ********************"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'ヘッダの左領域を設定
        SetHeaderLeft(lb_user, lb_title)

        '---------------------------------------------------------------
        'HTMLタグを追加
        '---------------------------------------------------------------
        Dim ctl As Control = Page.FindControl("login_header")
        'JS
        SetTagJs(ctl, "common.js")
        SetTagJs(ctl, "jquery-1.8.3.min.js")
        SetTagJs(ctl, "login.js")
        'CSS
        SetTagCss(ctl, "login.css?20230605")
        SetTagCss(ctl, "master.css")
        SetTagCss(ctl, "common.css")
        'ファビコン
        SetTagFavicon(ctl)

    End Sub

#End Region

#Region "***************** コントロールイベント *****************"

    Protected Sub btn送信_Click(sender As Object, e As EventArgs) Handles btn送信.Click

        Dim strEmail As String = txtEmail.Text
        Dim strAuthCD As String = txtAuthCD.Text

        'メールアドレスと認証CDのチェック
        Dim strOutput As String = ""
        strOutput = CHKアカウント(strEmail, strAuthCD)

        If Not String.IsNullOrEmpty(strOutput) Then
            lbl_errorMsg.Text = strOutput
            Exit Sub
        End If

        '送信
        If 送信処理(strEmail) = True Then

            lbl_errorMsg.Text = ""

            ScriptManager.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().GetHashCode().ToString, "javascript:alert('メールを送信しました。'); window.location='Login.aspx';", True)

        Else

            lbl_errorMsg.Text = "エラーが発生しました。"

        End If
    End Sub

    Protected Sub btnBackToLogin_Click(sender As Object, e As EventArgs)
        ' ログイン画面にリダイレクト
        Response.Redirect("Login.aspx")
    End Sub


#End Region

#Region "******************* Sub及びFunction ********************"
    Private Function CHKアカウント(ByVal strEmail As String,
                                   ByVal strAuthCd As String) As String
        Using spm As New StoredProcedureManager("PT_認証_SIGNUP_OUTPUT")
            spm.AddPrmIn("@EMAIL", Data.SqlDbType.NVarChar, 100, strEmail)
            spm.AddPrmIn("@AUTHCD", Data.SqlDbType.VarChar, 20, strAuthCd)
            spm.AddPrmOut("@ERRMSG", Data.SqlDbType.VarChar, 200)

            spm.Exec()

            Dim errMsg = spm.GetValue("@ERRMSG")
            Return errMsg
        End Using
    End Function

    Private Function 送信処理(strEmail As String) As Boolean

        Dim retNewCnt As Integer = 0
        Dim mkey As String = ""
        Dim sucFlg As Boolean = False

        Using spm As New StoredProcedureManager("PT_DBアカウント申請履歴_INSERT")
            spm.AddPrmIn("@EMAIL", Data.SqlDbType.VarChar, 100, strEmail)
            spm.AddPrmOut("@out_CNT", Data.SqlDbType.Int)
            spm.AddPrmOut("@out_MKEY", Data.SqlDbType.VarChar, 32)
            spm.Exec()

            retNewCnt = spm.GetValue("@out_CNT")
            mkey = spm.GetValue("@out_MKEY")
        End Using

        If retNewCnt > 0 Then

            'アカウント申請用URL通知メール発行
            sucFlg = AppMail.SendSignupFormMail(retNewCnt, mkey, strEmail)

        End If

        Return sucFlg

    End Function
#End Region


End Class