Imports System.Data.SqlClient
Imports System.Net
Public Class LoginForm1
    ' TODO: 插入程式碼，利用提供的使用者名稱和密碼執行自訂驗證
    ' (請參閱 https://go.microsoft.com/fwlink/?LinkId=35339)。
    ' 如此便可將自訂主體附加到目前執行緒的主體，如下所示: 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' 其中 CustomPrincipal 是用來執行驗證的 IPrincipal 實作。
    ' 接著，My.User 便會傳回封裝在 CustomPrincipal 物件中的識別資訊，
    ' 例如使用者名稱、顯示名稱等。
    Public Property Credentials As ICredentials
    Private Property MYSQL As String
    ReadOnly da As Object
    ReadOnly ds As Object
    ReadOnly conn As New SqlConnection
    ReadOnly cmd As New SqlCommand
    Dim dr As SqlDataReader
    ReadOnly da1 As New SqlDataAdapter
    ReadOnly ds2 As New DataSet
    ReadOnly screenWidth = Screen.PrimaryScreen.Bounds.Width
    ReadOnly screenHeight = Screen.PrimaryScreen.Bounds.Height
    Dim HH As String
    Private Sub Set_清單1()
        DataGridView1.DataSource = Nothing : ds2.Clear()
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView1.ColumnHeadersHeight = 25
        DataGridView1.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 資料夾名稱, 指定 FROM FTP資料夾管理 WHERE (指定 NOT LIKE 'NA') ORDER BY 指定"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da1.SelectCommand = cmd : da1.Fill(ds2) : DataGridView1.DataSource = ds2.Tables(0) : conn.Close()
    End Sub
    <Obsolete>
    Private Sub LoginForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '-----------------------控件基本設定-----------------------------------------------------------------------------------------------------------------------------------------------
        CheckBox1.Checked = True : CheckBox1.Enabled = False
        Label1.Text = "執行檔版本號 : " & 版本
        TextBox4.Text = "1"
        網段.ShowDialog()
        If 登入判斷 = True Then
            ComboBox1.Text = "巨益線路"
            OK.Enabled = True : Cancel.Enabled = True : CheckBox1.Checked = True
        ElseIf 登入判斷 = False Then
            ComboBox1.Text = "開發線路"
            OK.Enabled = True : Cancel.Enabled = True : CheckBox1.Checked = True
        End If
        '-----------------------調整視窗大小-----------------------------------------------------------------------------------------------------------------------------------------------
        Me.Size = New Size(466, 279)
        If screenWidth = 1920 And screenHeight = 1080 Then : Me.Location = New Point(727, 401)
        ElseIf screenWidth = 1366 And screenHeight = 768 Then : Me.Location = New Point(450, 245)
        ElseIf screenWidth = 1440 And screenHeight = 900 Then : Me.Location = New Point(487, 311)
        ElseIf screenWidth = 1280 And screenHeight = 1024 Then : Me.Location = New Point(407, 373)
        ElseIf screenWidth = 1600 And screenHeight = 900 Then : Me.Location = New Point(567, 311)
        End If
    End Sub
    <Obsolete>
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        '-----------------------確認登入伺服器位置-----------------------------------------------------------------------------------------------------------------------------------------
        If ComboBox1.Text = "巨益線路" Then
            登入人IP = "192.168.10.5"
            登入()
            OK.Enabled = True : Cancel.Enabled = True : CheckBox1.Checked = True
        ElseIf ComboBox1.Text = "開發線路" Then
            登入人IP = "106.1.50.79"
            登入()
            OK.Enabled = True : Cancel.Enabled = True : CheckBox1.Checked = True
        End If
        UsernameTextBox.Focus()
    End Sub
    Private Sub 登入()
        '-----------------------寫入連線字串-----------------------------------------------------------------------------------------------------------------------------------------------
        If ComboBox1.Text = "開發線路" Then
            ConString = "Data Source=" & 登入人IP & ";Initial Catalog=METO-MES-SYS;Persist Security Info=True;User ID=B70340;Password=Lee0911274990;Max pool size = 200;Connection Timeout=0"
        Else
            ConString = "Data Source=" & 登入人IP & ";Initial Catalog=METO-MES-SYS;Persist Security Info=True;User ID=b70340;Password=0911274990;Max pool size = 200;Connection Timeout=0"
        End If
        g_conn_type = ComboBox1.Text
        '-----------------------系統版本號寫入-----------------------------------------------------------------------------------------------------------------------------------------------
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 系統認證版本號 FROM 版本號管理 "
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        If dr.Read() Then
            Label2.Text = "資料庫版本號 : " & dr("系統認證版本號") : 版本號 = dr("系統認證版本號")
        End If : conn.Close()
        '-----------------------FTP資料夾登入位置宣告--------------------------------------------------------------------------------------------------------------------------------------
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT  FTP地址, FTP帳號, FTP密碼, FTP實體位置 FROM FTP資料庫管理 WHERE 地區 LIKE N'" & ComboBox1.Text & "'"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        If dr.Read() Then
            Target = dr("FTP地址") : FTP帳號 = dr("FTP帳號") : FTP密碼 = dr("FTP密碼") : FTP實體位置 = dr("FTP實體位置")
        End If : conn.Close()
        Set_清單1()
        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            If i < 10 Then : HH = "0" & i : ElseIf i >= 10 Then : HH = i : End If
            If DataGridView1(1, i).Value.ToString = "AA" & HH Then : AA(i) = DataGridView1(0, i).Value : End If
        Next
        Target1 = Target & AA(0) & "/"
        WebBrowser3.Url = New Uri(Target1)
        '-----------------------自動更新--------------------------------------------------------------------------------------------------------------------------------------
        If Strings.Right(Label1.Text, 10) <> Strings.Right(Label2.Text, 10) Then
            自動更新2()
        Else : End If
    End Sub
    Private Sub 登入程序()
        If UsernameTextBox.Text = "" Then
            MsgBox("請輸入帳號!   如使用刷登入，請確定該卡片有合法註冊!")
        ElseIf UsernameTextBox.Text <> "" Then
            If TextBox4.Text = "1" Then
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                MYSQL = "SELECT " & 登入權限 & " 使用者權限管理表 WHERE (帳號 = '" & UsernameTextBox.Text & "')"
                cmd.Connection = conn : cmd.CommandText = MYSQL : dr = cmd.ExecuteReader

                If dr.Read() Then
                    TextBox1.Text = dr("密碼").ToString
                    gUserName = dr("姓名").ToString
                    登入人部門 = dr("部門").ToString
                    For I As Integer = 0 To 95
                        CC(I) = dr.Item(I + 5)
                    Next
                End If
                If PasswordTextBox.Text = TextBox1.Text Then
                    If TextBox1.Text = "" Then
                        MsgBox("帳號或密碼錯誤，請重新輸入!")
                    Else
                        Me.Hide()
                        WINPROFIT_ERP_SYS.Show()
                        OK.Enabled = False : Cancel.Enabled = False
                    End If
                ElseIf PasswordTextBox.Text <> TextBox1.Text Then
                    MsgBox("帳號或密碼錯誤，請重新輸入!")
                End If
                dr.Close()
            End If
        End If
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        MYSQL = "SELECT 員工編號 FROM 使用者權限管理表 WHERE (帳號 = '" & UsernameTextBox.Text & "')"
        cmd.Connection = conn : cmd.CommandText = MYSQL : dr = cmd.ExecuteReader
        If dr.Read() Then
            員工編號 = dr("員工編號").ToString
        End If
        conn.Close()
    End Sub
    Private Sub 自動更新1()
        Dim aa1 As MsgBoxResult
        aa1 = MsgBox("系統版本號 與 資料庫版本號 對應不上，請開發人員注意是否有其他人員修改。並決定是否下載最新的執行檔?", MsgBoxStyle.OkCancel)
        If aa1 = MsgBoxResult.Ok Then
            With SaveFileDialog1 : .Filter = "所有文件(*.exe)|*.exe" : End With
            SaveFileDialog1.FileName = "WINPROFIT-ERP-SYS - " & 版本號 & ".exe"
            SaveFileDialog1.ShowDialog()
            If Strings.Right(SaveFileDialog1.FileName, 34) = "WINPROFIT-ERP-SYS - " & 版本號 & ".exe" Then
                My.Computer.Network.DownloadFile(Target & AA(0) & "/" & "WINPROFIT-ERP-SYS - " & 版本號 & ".exe", SaveFileDialog1.FileName, "", "", True, 10000, True)
                MsgBox("下載完成，請手動刪除就的執行檔，並且用新的執行檔開啟。")
                Me.Close()
            Else
                MsgBox("操作不正確，請用手動方式下載更新。")
            End If
        Else
            登入程序()
        End If
    End Sub
    Private Sub 自動更新2()
        Dim aa1 As MsgBoxResult
        aa1 = MsgBox("系統版本號 與 資料庫版本號 對應不上，請下載最新的執行檔。", MsgBoxStyle.OkCancel)
        If aa1 = MsgBoxResult.Ok Then
            With SaveFileDialog1 : .Filter = "所有文件(*.exe)|*.exe" : End With
            SaveFileDialog1.FileName = "WINPROFIT-ERP-SYS - " & 版本號 & ".exe"
            SaveFileDialog1.ShowDialog()
            If Strings.Right(SaveFileDialog1.FileName, 34) = "WINPROFIT-ERP-SYS - " & 版本號 & ".exe" Then
                My.Computer.Network.DownloadFile(Target & AA(0) & "/" & "WINPROFIT-ERP-SYS - " & 版本號 & ".exe", SaveFileDialog1.FileName, "", "", True, 10000, True)
                MsgBox("下載完成，請手動刪除就的執行檔，並且用新的執行檔開啟。")
                Me.Close()
            Else
                MsgBox("操作不正確，請用手動方式下載更新。")
            End If
        End If
    End Sub
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        If UsernameTextBox.Text = "b70340" Or UsernameTextBox.Text = "B70340" Then
            If Strings.Right(Label1.Text, 10) <> Strings.Right(Label2.Text, 10) Then
                自動更新1()
            Else
                登入程序()
            End If
        Else
            If Strings.Right(Label1.Text, 10) <> Strings.Right(Label2.Text, 10) Then
                自動更新2()
            Else
                登入程序()
            End If
        End If
    End Sub
    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub
    Private Sub CheckBox2_Click(sender As Object, e As EventArgs) Handles CheckBox2.Click
        If CheckBox2.Checked = False Then
            Me.Size = New Size(466, 279)
            If screenWidth = 1920 And screenHeight = 1080 Then : Me.Location = New Point(727, 401)
            ElseIf screenWidth = 1366 And screenHeight = 768 Then : Me.Location = New Point(450, 245)
            ElseIf screenWidth = 1440 And screenHeight = 900 Then : Me.Location = New Point(487, 311)
            ElseIf screenWidth = 1280 And screenHeight = 1024 Then : Me.Location = New Point(407, 373)
            ElseIf screenWidth = 1600 And screenHeight = 900 Then : Me.Location = New Point(567, 311)
            End If
        Else
            Me.Size = New Size(705, 279)
            If screenWidth = 1920 And screenHeight = 1080 Then : Me.Location = New Point(608, 401)
            ElseIf screenWidth = 1366 And screenHeight = 768 Then : Me.Location = New Point(331, 245)
            ElseIf screenWidth = 1440 And screenHeight = 900 Then : Me.Location = New Point(368, 311)
            ElseIf screenWidth = 1280 And screenHeight = 1024 Then : Me.Location = New Point(288, 373)
            ElseIf screenWidth = 1600 And screenHeight = 900 Then : Me.Location = New Point(448, 311)
            End If
        End If
    End Sub
    Private Sub UsernameTextBox_TextChanged(sender As Object, e As EventArgs) Handles UsernameTextBox.TextChanged
        If CheckBox1.Checked = False Then
        Else
            If IsNumeric(UsernameTextBox.Text) = True Then
                If UsernameTextBox.Text.Length = 10 Then
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    MYSQL = "SELECT 帳號, 密碼 FROM 使用者權限管理表 WHERE (ID卡號 = '" & UsernameTextBox.Text & "')"
                    cmd.Connection = conn : cmd.CommandText = MYSQL : dr = cmd.ExecuteReader
                    If dr.Read() Then
                        UsernameTextBox.Text = dr("帳號").ToString
                        PasswordTextBox.Text = dr("密碼").ToString
                        conn.Close()
                    Else
                        UsernameTextBox.Text = "" : PasswordTextBox.Text = "" : conn.Close()
                    End If
                    Exit Sub : Me.OK.PerformClick()
                End If
            End If
        End If
    End Sub
    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.Click
        UsernameTextBox.Focus()
    End Sub
End Class
