Public Class LoginForm1
    Dim pMainForm As Form1
    Dim m_Log As New CLogManage

    ' TODO: 插入代码，以使用提供的用户名和密码执行自定义的身份验证
    ' (请参见 http://go.microsoft.com/fwlink/?LinkId=35339)。 
    ' 随后自定义主体可附加到当前线程的主体，如下所示: 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' 其中 CustomPrincipal 是用于执行身份验证的 IPrincipal 实现。 
    ' 随后，My.User 将返回 CustomPrincipal 对象中封装的标识信息
    ' 如用户名、显示名等

    Public LoginResult As Boolean = False
    Dim m_LastError As String = ""
    Dim m_InitOver As Boolean = False
    Dim r_Right(COL_RIGHTS.max - 1) As String

    '将字符串权限转换为对应的RIGHTS值
    Private Sub ConvertRights(ByRef strSrc() As String, ByRef rRight() As RIGHTS)
        For i As Integer = 0 To COL_RIGHTS.max - 1
            Select Case strSrc(i)
                Case "只读"
                    rRight(i) = RIGHTS.READ
                Case "读写"
                    rRight(i) = RIGHTS.READ_WRITE
                Case "管理"
                    rRight(i) = RIGHTS.MANAGE Or RIGHTS.READ_WRITE
                Case Else
                    rRight(i) = RIGHTS.NONE
            End Select
        Next
    End Sub

    ''' <summary>
    ''' 登录检测
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function LoginCheck(Optional domain As String = "") As Boolean

        Dim m_AccountManage As New CAccountManage

        Dim m_UserName As String = UsernameTextBox.Text
        Dim m_UserPassword As String = PasswordTextBox.Text
        Dim r_Password As String = ""
        Dim r_EmployeeID As Integer = 0

        '清除密码
        PasswordTextBox.Text = ""

        If m_UserName.Length = 0 Then
            m_LastError = "用户名不能为空"
            Return False
        End If

        Dim ipaddr As String
        'Dim ipaddr As String = GetIPFromNetUrl("uts-svr2.qicp.net") '
        'Dim ipaddr As String = GetIPFromNetUrl("uts-svr2.ticp.vip") '
        'Dim ipaddr As String = GetIPFromNetUrl("blv-rd.tech") '
        'Dim ipaddr As String = "172.16.4.182"

        '判断domian是否为有效域名
        If False Then
            m_LastError = "域名无效"
            Return False
        End If

        '如域名为空，则使用默认IP
        Dim isDomainvailable As Boolean = IsValidDomainUsingUri(Trim(domain))
        If isDomainvailable Then
            ipaddr = GetIPFromNetUrl(domain)
        Else
            MsgBox("域名无效，即将使用内网IP。")
            ipaddr = "172.16.4.185"
        End If

        SQL_ConnectionSetting(ipaddr, "uts_DMS", "uts_DMS_Manager", "A98ya9hu(*HU8hu(*HU(hu")

        'SQL_ConnectionSetting("10.46.213.199", "w&t database", "root", "3ZAn8dTtqxBTubWz")
        Current_Rights(COL_RIGHTS.用户管理) = RIGHTS.READ
        CAccountManage.AccessRight = RIGHTS.READ
        If m_AccountManage.QueryItem(m_UserName, r_Password, r_EmployeeID, r_Right) = ERROR_CODE.SUCCESS Then
            If Strings.StrComp(m_UserPassword, r_Password, Microsoft.VisualBasic.CompareMethod.Text) = 0 Then
                If r_Right(COL_RIGHTS.登录) = "开启" Then
                    Return True
                Else
                    m_LastError = "无登录权限"
                End If
            Else
                m_LastError = "用户密码不正确"
            End If
        Else
            m_LastError = "数据库连接失败，或者用户名不正确"
        End If

        Return False
    End Function



    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        If My.Settings.CHK_SAVEUSERNAME = True Then
            My.Settings.TXT_USERNAME = UsernameTextBox.Text
            My.Settings.strDomain = txtDomain.Text

            If My.Settings.CHK_AUTOLOGIN = True Then
                My.Settings.TXT_PASSWORD = PasswordTextBox.Text
            End If

            My.Settings.Save()
        End If

        'heck domain
        If txtDomain.TextLength > 0 Then
            LoginResult = LoginCheck(txtDomain.Text)
        Else
            LoginResult = LoginCheck()
        End If

        'LoginResult = LoginCheck()
        If LoginResult = True Then
            If m_Log.AddItem("系统", "登入", UsernameTextBox.Text) = ERROR_CODE.SUCCESS Then
                Me.Hide()
                pMainForm = New Form1
                Current_UserName = UsernameTextBox.Text
                ConvertRights(r_Right, Current_Rights)
                pMainForm.ShowDialog()

                m_Log.AddItem("系统", "退出", UsernameTextBox.Text)
                If pMainForm.QuitReson = QUIT_RESON.close Then
                    Me.Close()
                Else
                    Me.Show()
                End If
            Else
                MsgBox("保存登录信息失败!")
            End If
        Else
            MsgBox(m_LastError)
        End If
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        LoginResult = False
        Me.Close()
    End Sub

    Private Sub LoginForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If My.Settings.CHK_SAVEUSERNAME = True Then
            UsernameTextBox.Text = My.Settings.TXT_USERNAME
            txtDomain.Text = My.Settings.strDomain
        End If

        m_InitOver = True
    End Sub

    Private Sub chk_SaveUserName_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chk_SaveUserName.CheckedChanged
        If m_InitOver = False Then Return

        My.Settings.CHK_SAVEUSERNAME = chk_SaveUserName.Checked

        If My.Settings.CHK_SAVEUSERNAME = True Then
            My.Settings.TXT_USERNAME = UsernameTextBox.Text
            My.Settings.Save()
        Else
            If My.Settings.CHK_AUTOLOGIN = True Then
                chk_SaveUserName.Checked = True
            End If
        End If
    End Sub

    Private Sub LoginForm1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        If UsernameTextBox.Text.Length > 0 Then
            PasswordTextBox.Focus()
        End If

        If My.Settings.CHK_AUTOLOGIN = True Then
            PasswordTextBox.Text = My.Settings.TXT_PASSWORD
            PasswordTextBox.Refresh()

            Timer1.Start()
        End If
    End Sub

    Private Sub chk_AutoLogin_CheckedChanged(sender As Object, e As EventArgs) Handles chk_AutoLogin.CheckedChanged
        If m_InitOver = False Then Return

        My.Settings.CHK_AUTOLOGIN = chk_AutoLogin.Checked
        If My.Settings.CHK_AUTOLOGIN = True Then
            If chk_SaveUserName.Checked = False Then
                chk_SaveUserName.Checked = True
            End If

            My.Settings.TXT_PASSWORD = PasswordTextBox.Text
            My.Settings.Save()
        End If
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Timer1.Stop()
        If chk_AutoLogin.Checked Then
            OK.PerformClick()
        End If
    End Sub

    Private Sub PasswordTextBox_TextChanged(sender As Object, e As EventArgs) Handles PasswordTextBox.TextChanged

    End Sub
End Class
