Imports AppVariable
Imports AppCommon
Imports System.Data
Imports AppMail

Partial Class ChangeUserInfo
    Inherits System.Web.UI.Page

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

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ChangeMode("input")
            '既存のユーザ情報を初期表示
            SetUserInfo()
            'パスワードNGリストの追加
            SetHdfPassNgList()
        End If
    End Sub

#End Region


#Region "***************** コントロールイベント *****************"
    Protected Sub btnTestMail_Click(sender As Object, e As EventArgs) Handles btnテスト送信.Click
        ' 手動でメールアドレスのみチェック
        If String.IsNullOrEmpty(txtEMail.Text.Trim()) Then
            lblTestMailError.Text = "メールアドレスを入力してください"
            lblTestMailError.Visible = True
            Return
        End If

        ' メールアドレス形式チェック
        If Not IsValidEmail(txtEMail.Text) Then
            lblTestMailError.Text = "メールアドレスの形式が正しくありません"
            lblTestMailError.Visible = True
            Return
        End If
        'メールアドレスの重複チェック
        Dim mes As String = ChkMail(txtEMail.Text)
        If Not String.IsNullOrEmpty(mes) Then
            lbl_errMsg_Email.Text = "<br />" & mes
            Return
        End If

        ' テストメール送信
        Try
            lblTestMailError.Visible = False
            SendTestMail(txtEMail.Text)
            lblTestMailSuccess.Text = "テストメールを送信しました"
            lblTestMailSuccess.Visible = True
            btnテスト送信.Enabled = False '送信成功時はボタン無効化
        Catch ex As Exception

        End Try
    End Sub

    Private Function IsValidEmail(email As String) As Boolean
        Try
            Dim addr = New System.Net.Mail.MailAddress(email)
            Return addr.Address = email
        Catch
            Return False
        End Try
    End Function
    Protected Sub btn確認_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn確認.Click
        If Not IsValid Then
            Return
        End If

        'すべて未入力の場合は処理しない 20260306 S.Iguchi
        Dim all未入力Flg = String.IsNullOrEmpty(txtEMail.Text) And String.IsNullOrEmpty(txtDeptName.Text) And String.IsNullOrEmpty(txtStaffName.Text) And String.IsNullOrEmpty(txtPw.Text)
        If all未入力Flg Then
            lbl_errorMsg.Text = "入力された項目がありません。"
            lbl_errorMsg.Visible = True
            Return
        End If
        lbl_errMsg_Email.Text = ""
        lbl_errMsg_Pass.Text = ""
        lbl_errorMsg.Visible = False
        'メールアドレスの重複チェック
        Dim mes As String = ChkMail(txtEMail.Text)
        If Not String.IsNullOrEmpty(mes) Then
            lbl_errMsg_Email.Text = "<br />" & mes
        End If
        'パスワード確認
        Dim pw As String = txtPw.Text
        Dim pwChk As String = txtPwChk.Text
        If Not pw = pwChk Then
            lbl_errMsg_Pass.Text = "<br />入力されたパスワードが一致しません。もう一度確認してください。"
        End If

        lbl_errMsg_Email.Visible = Not String.IsNullOrEmpty(lbl_errMsg_Email.Text)
        lbl_errMsg_Pass.Visible = Not String.IsNullOrEmpty(lbl_errMsg_Pass.Text)

        If String.IsNullOrEmpty(lbl_errMsg_Email.Text) And String.IsNullOrEmpty(lbl_errMsg_Pass.Text) Then
            ChangeMode("Confirm")
        End If
    End Sub
    Protected Sub btn戻る_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn戻る.Click
        If Not IsValid Then
            Return
        End If

        ChangeMode("input")
    End Sub

    Protected Sub btn登録_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn登録.Click
        'メールアドレスの重複チェック(直前)
        Dim mes As String = ChkMail(lblEMail.Text)
        If Not String.IsNullOrEmpty(mes) Then
            lbl_errorMsg.Text = "既に登録済みのメールアドレスです。"
            lbl_errorMsg.Visible = True
            Return
        End If

        Try
            'ユーザー情報の更新
            UpdateUserInfo()

            Dim email As String = lblEMail.Text
            'メールアドレス未入力時は、アカウントのメールアドレス宛にメール送付 20260306 S.Iguchi
            If String.IsNullOrEmpty(email) Then
                email = txtOrgEMail.Text
            End If
            Dim portalId As String = Session(S_COM_PortalID)
            Dim userName As String = lblStaffName.Text
            'アカウント更新通知メールの送信
            SendUpdateUserInfoMail(email, portalId, userName)

            '完了画面を表示
            ChangeMode("complete")

        Catch ex As Exception
            span_demo_mes.InnerText = "送信に失敗しました。"
        End Try
    End Sub
    Protected Sub btn戻る2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn戻る2.Click
        Response.Redirect(Com_Get_Content("menu_cm_003")) 'アカウント情報変更トップに戻る
    End Sub
    Protected Sub CustomValidator1_ServerValidate(source As Object, args As ServerValidateEventArgs)
        Dim inputPassword As String = args.Value.Trim()

        'NGパスワードリスト
        Dim ngPasswordList As String = hdfPassNgList.Value

        If Not String.IsNullOrEmpty(ngPasswordList) AndAlso Not String.IsNullOrEmpty(inputPassword) Then
            Dim ngPasswords() As String = ngPasswordList.Split(","c)

            For Each ngPassword As String In ngPasswords
                If inputPassword.ToLower() = ngPassword.ToLower() Then ' 大文字小文字を区別しない
                    args.IsValid = False
                    Return
                End If
            Next
        End If

        args.IsValid = True
    End Sub
#End Region

#Region "******************* Sub及びFunction ********************"
    Private Sub SetUserInfo()
        Dim portalId As String = Session(S_COM_PortalID)
        Dim dt As DataTable = GetUserData(portalId, False)

        If dt.Rows.Count > 0 Then
            txtEMail.Text = Convert.ToString(dt.Rows(0)("EMAIL"))
            txtOrgEmail.Text = Convert.ToString(dt.Rows(0)("EMAIL")) '元アドレスを保持 メール空で提出された際はここを参照して送信
            txtStaffName.Text = Convert.ToString(dt.Rows(0)("名前"))
            txtDeptName.Text = Convert.ToString(dt.Rows(0)("所属部課"))
            txtTel.Text = Convert.ToString(dt.Rows(0)("電話番号"))
        End If
    End Sub

    Private Sub UpdateUserInfo()
        Using spm As New StoredProcedureManager("PT_USER_UPDATE")
            spm.AddPrmIn("@PORTALID", Data.SqlDbType.VarChar, 200, Session(S_COM_PortalID))
            spm.AddPrmIn("@PASSWORD", Data.SqlDbType.VarChar, 200, txtPw.Text)
            spm.AddPrmIn("@EMAIL", Data.SqlDbType.VarChar, 50, lblEMail.Text)
            spm.AddPrmIn("@氏名", Data.SqlDbType.NVarChar, 50, lblStaffName.Text)
            spm.AddPrmIn("@所属部課", Data.SqlDbType.VarChar, 200, lblDeptName.Text)
            spm.AddPrmIn("@TEL", Data.SqlDbType.VarChar, 200, lblTel.Text)
            spm.Exec()
        End Using
    End Sub

    Private Function ChkMail(email As String) As String
        Dim errMes As String = ""
        Using spm As New StoredProcedureManager("PT_USER_MAIL_CHK_OUTPUT")
            spm.AddPrmIn("@EMAIL", Data.SqlDbType.VarChar, 50, email)
            spm.AddPrmIn("@PORTALID", Data.SqlDbType.VarChar, 200, Session(S_COM_PortalID))
            spm.AddPrmOut("@ERRMES", Data.SqlDbType.VarChar, 200)
            spm.Exec()

            errMes = Convert.ToString(spm.GetValue("@ERRMES"))
        End Using

        Return errMes
    End Function
    Private Sub ChangeMode(mode As String)
        If mode = "Confirm" Then
            txtEMail.Visible = False
            lblTestMailSuccess.Visible = False
            lblTestMailError.Visible = False
            btnテスト送信.Visible = False
            lblTestMailDiscription.Visible = False
            txtDeptName.Visible = False
            txtStaffName.Visible = False
            txtTel.Visible = False
            txtPw.Visible = False
            txtPwChk.Visible = False

            lblEMail.Text = txtEMail.Text
            lblDeptName.Text = txtDeptName.Text
            lblStaffName.Text = txtStaffName.Text
            lblTel.Text = txtTel.Text
            If Not String.IsNullOrEmpty(txtPw.Text) Then
                lblPw.Text = "***********"
                lblPwChk.Text = "***********"
            Else
                lblPw.Text = ""
                lblPwChk.Text = ""
            End If

            lblEMail.Visible = True
            lblDeptName.Visible = True
            lblStaffName.Visible = True
            lblTel.Visible = True
            lblPw.Visible = True
            lblPwChk.Visible = True

            btn確認.Visible = False
            btn登録.Visible = True
            btn戻る.Visible = True
            div_confirm_btn_area.Visible = True
            span_demo_mes.InnerText = "以下の項目を確認後、登録ボタンを押してください。"
        ElseIf mode = "input" Then
            'span_demo_mes.InnerText = "変更する項目を入力後、確認画面へのボタンを押してください。"
            span_demo_mes.InnerHtml = "変更する項目を入力後、確認画面へのボタンを押してください。<br /><span class=""error_msg"">※研修申込済みの「研修担当」の情報は変更されません。今後の申込時の情報に反映されます。</span>"
            txtEMail.Visible = True
                btnテスト送信.Visible = True
                lblTestMailDiscription.Visible = True
                If Not btnテスト送信.Enabled Then
                    lblTestMailSuccess.Visible = True
                    lblTestMailDiscription.Visible = False
                End If
                txtDeptName.Visible = True
                txtStaffName.Visible = True
                txtTel.Visible = True
                txtPw.Visible = True
                txtPwChk.Visible = True

                txtEMail.Text = lblEMail.Text
                txtDeptName.Text = lblDeptName.Text
                txtStaffName.Text = lblStaffName.Text
                txtTel.Text = lblTel.Text

                lblEMail.Visible = False
                lblDeptName.Visible = False
                lblStaffName.Visible = False
                lblTel.Visible = False
                lblPw.Visible = False
                lblPwChk.Visible = False

                btn確認.Visible = True
                btn登録.Visible = False
                btn戻る.Visible = False
                div_confirm_btn_area.Visible = False
            ElseIf mode = "complete" Then
                span_demo_mes.InnerHtml = "アカウント情報が変更されました。<br />アカウント情報変更メールを送信しましたので、ご確認ください。"
            btn登録.Visible = False
            btn戻る.Visible = False
            btn戻る2.Visible = True
            tbl_input.Visible = False
            tbl_input2.Visible = False
            tbl_input3.Visible = False
            tbl_input4.Visible = False
        End If
    End Sub
    Private Sub SetHdfPassNgList()
        Dim dt As DataTable

        Using spm As New StoredProcedureManager("共通_変数_DDL")
            spm.AddPrmIn("@変数", Data.SqlDbType.VarChar, "PASSWORD_NG")
            spm.Fill()
            dt = spm.ds.Tables(0)
        End Using

        ' カラム[内容]の値をカンマ区切り文字列に変換
        Dim valueList As New List(Of String)

        For Each row As DataRow In dt.Rows
            If Not IsDBNull(row("内容")) Then
                valueList.Add(row("内容").ToString())
            End If
        Next

        ' カンマ区切り文字列をHiddenFieldに設定
        Dim commaSeparatedValues As String = String.Join(",", valueList)
        hdfPassNgList.Value = commaSeparatedValues
    End Sub
#End Region

End Class
