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

Partial Public Class ResetPW
    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 strPID As String = txtPID.Text
        Dim strEmail As String = txtEmail.Text

        'IDとメールアドレスが登録されているかをチェック
        Dim strOutput As String = ""
        strOutput = CHKアカウント(strPID, _
                                  strEmail)
        If Not String.IsNullOrEmpty(strOutput) Then
            lbl_errorMsg.Text = strOutput
            Exit Sub

        End If

        '送信
        If 送信処理(strPID, _
                    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 strPID As String, _
                                   ByVal strEmail As String) As String

        Dim cnStr As String = System.Configuration.ConfigurationManager.ConnectionStrings(DB_strCon).ConnectionString
        Using con As New SqlConnection(cnStr)

            con.Open()

            Try

                Dim cmd As SqlCommand = New SqlCommand("dbo.PW_アカウントCHK_OUTPUT", con)
                cmd.CommandType = CommandType.StoredProcedure

                Dim param1 As SqlParameter = cmd.Parameters.Add("@PID", SqlDbType.VarChar, 200)
                param1.Value = strPID

                Dim param2 As SqlParameter = cmd.Parameters.Add("@EMAIL", SqlDbType.VarChar, 100)
                param2.Value = strEmail

                Dim param10 As SqlParameter = cmd.Parameters.Add("@ERRMSG", Data.SqlDbType.VarChar, 200)
                param10.Direction = ParameterDirection.Output

                cmd.ExecuteNonQuery()

                If Not String.IsNullOrEmpty(param10.Value.ToString) Then
                    Return param10.Value.ToString
                Else
                    Return ""
                End If

            Catch ex As Exception
                con.Close()
                Return "エラーが発生しました。"
            End Try

            con.Close()

        End Using
    End Function

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

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

        Using spm As New StoredProcedureManager("PW_PW変更メール_INSERT")
            spm.AddPrmIn("@PID", Data.SqlDbType.VarChar, 200, strPID)
            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.SendPasswordResetMail(retNewCnt, mkey, strPID, strEmail)

        End If

        Return sucFlg

    End Function
#End Region

    
End Class