Public Class LoginForm1
    Dim pMainForm As Form1

    ' 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(CAccountManage.COL_RIGHTS.max - 1) As String

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

    ''' <summary>
    ''' 登录检测
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function LoginCheck() 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 = GetIPFromNetUrl("inhaos-server.synology.me")
        If ipaddr.Length = 0 Then Return False
        SQL_ConnectionSetting(ipaddr, "w&t database", "Tony", "123")
        'SQL_ConnectionSetting("10.46.213.199", "w&t database", "Tony", "123")

        'SQL_ConnectionSetting("127.0.0.1", "W&T Database", "Tony", "123")

        m_AccountManage.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(CAccountManage.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.Save()
        End If

        LoginResult = LoginCheck()
        If LoginResult = True Then
            Me.Hide()
            pMainForm = New Form1
            pMainForm.Current_UserName = My.Settings.TXT_USERNAME
            ConvertRights(r_Right, pMainForm.Current_Rights)
            pMainForm.ShowDialog()
            If pMainForm.QuitReson = QUIT_RESON.close Then
                Me.Close()
            Else
                Me.Show()
            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
        End If

        PasswordTextBox.Focus()

        m_InitOver = True
    End Sub

    Private Sub chk_SaveUserName_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chk_SaveUserName.CheckedChanged
        My.Settings.CHK_SAVEUSERNAME = chk_SaveUserName.Checked
        If m_InitOver = False Then Return
        If My.Settings.CHK_SAVEUSERNAME = True Then
            My.Settings.TXT_USERNAME = UsernameTextBox.Text
            My.Settings.Save()
        End If
    End Sub
End Class
