Public Class LoginForm1
    ReadOnly ds2 As New DataSet
    Dim Str As String = Application.StartupPath
    Dim MACKK As String 'MAC碼
    Dim diskId As String '數字ID
    Dim diskSerialNumber As String '硬碟序列
    Dim diskModel As String '磁盘型態
    Public Property Credentials As ICredentials
    ReadOnly screenWidth = Screen.PrimaryScreen.Bounds.Width
    ReadOnly screenHeight = Screen.PrimaryScreen.Bounds.Height
    Private Sub LoginForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '-----------------------控件基本設定-----------------------------------------------------------------------------------------------------------------------------------------------
        刷卡登入_ch.Checked = True : 刷卡登入_ch.Enabled = False : 登入綁定_ch.Checked = False : 登入綁定_ch.Enabled = False : 帳號_tb.Visible = False
        執行檔版本號_lb.Text = "執行檔版本號 : " & 版本
        網段.ShowDialog()
        '-----------------------調整視窗大小-----------------------------------------------------------------------------------------------------------------------------------------------
        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
        Me.ActiveControl = 使用者帳號_cb
        '-----------------------刪除自動更新項目-----------------------------------------------------------------------------------------------------------------------------------------------
        Dim 已開啟 As Boolean = False
        Dim Strs As String = Application.StartupPath
        If File.Exists(My.Computer.FileSystem.SpecialDirectories.Desktop & "\SYS-UPDATE.exe") Then
            Dim pro() As Process = System.Diagnostics.Process.GetProcesses()
            ' 判斷是否有符合的Process
            For Each process As Process In pro
                If ("SYS-UPDATE").Equals(process.ProcessName) Then
                    已開啟 = True
                End If
            Next
            If 已開啟 = False Then
                Timer1.Enabled = False
                If File.Exists(Str + "\SYS-UPDATE.exe") Then : File.Delete(Str + "\SYS-UPDATE.exe") : End If
            End If
        ElseIf File.Exists(Strs & "\SYS-UPDATE.exe") Then
            Dim pro() As Process = System.Diagnostics.Process.GetProcesses()
            ' 判斷是否有符合的Process
            For Each process As Process In pro
                If ("SYS-UPDATE").Equals(process.ProcessName) Then
                    已開啟 = True
                End If
            Next
            If 已開啟 = False Then
                Timer1.Enabled = False
                If File.Exists(Str + "\SYS-UPDATE.exe") Then : File.Delete(Str + "\SYS-UPDATE.exe") : End If
            End If
        End If
        Timer1.Enabled = True
    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        '-----------------------刪除自動更新項目-----------------------------------------------------------------------------------------------------------------------------------------------
        Dim 已開啟 As Boolean = False
        Dim Strs As String = Application.StartupPath
        If File.Exists(My.Computer.FileSystem.SpecialDirectories.Desktop & "\SYS-UPDATE.exe") Then
            Dim pro() As Process = System.Diagnostics.Process.GetProcesses()
            ' 判斷是否有符合的Process
            For Each process As Process In pro
                If ("SYS-UPDATE").Equals(process.ProcessName) Then
                    已開啟 = True
                End If
            Next
            If 已開啟 = False Then
                Timer1.Enabled = False
                If File.Exists(Str + "\SYS-UPDATE.exe") Then : File.Delete(Str + "\SYS-UPDATE.exe") : End If
            End If
        ElseIf File.Exists(Strs & "\SYS-UPDATE.exe") Then
            Dim pro() As Process = System.Diagnostics.Process.GetProcesses()
            ' 判斷是否有符合的Process
            For Each process As Process In pro
                If ("SYS-UPDATE").Equals(process.ProcessName) Then
                    已開啟 = True
                End If
            Next
            If 已開啟 = False Then
                Timer1.Enabled = False
                If File.Exists(Str + "\SYS-UPDATE.exe") Then : File.Delete(Str + "\SYS-UPDATE.exe") : End If
            End If
        End If
    End Sub
    Private Sub LoginForm1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        '-----------------------伺服器登入選擇-----------------------------------------------------------------------------------------------------------------------------------------------
        If 登入判斷 = True Then
            伺服器區域_ch.Text = "雅邸" : 確認_bt.Enabled = True : 取消_bt.Enabled = True : 刷卡登入_ch.Checked = True
        ElseIf 登入判斷 = False Then
            伺服器區域_ch.Text = "設計" : 確認_bt.Enabled = True : 取消_bt.Enabled = True : 刷卡登入_ch.Checked = True
        End If
        '-----MAC碼與硬碟資料-------------------------------------------------------------------------------------------------------------------------------------------------------------
        Dim Wmi As New Management.ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration")
        For Each WmiObj As Management.ManagementObject In Wmi.Get
            If CBool(WmiObj("IPEnabled")) Then
                MACKK = WmiObj("MACAddress")
            End If
        Next
        Dim cmicWmi As New Management.ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive")
        For Each cmicWmiObj As Management.ManagementObject In cmicWmi.Get
            diskId = cmicWmiObj("size")             'diskId = cmicWmiObj("signature")
            diskSerialNumber = cmicWmiObj("serialnumber")
            diskModel = cmicWmiObj("Model")
            Exit For
        Next
        '-----電腦綁定判斷-------------------------------------------------------------------------------------------------------------------------------------------------------------
        PA1 = MACKK : PA2 = diskId : PA3 = diskSerialNumber : PA4 = diskModel
        SQL_電腦綁定()
        If dr.Read() Then
            conn.Close()
        Else
            conn.Close() : MsgBox("該電腦沒有註冊，無法使用系統。") : Me.Close()
        End If
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 伺服器區域_ch.SelectedIndexChanged
        '-----------------------確認登入伺服器位置-----------------------------------------------------------------------------------------------------------------------------------------
        If 伺服器區域_ch.Text = "雅邸" Then
            登入人IP = "180.218.212.245,4567" : 登入() : 確認_bt.Enabled = True : 取消_bt.Enabled = True : 刷卡登入_ch.Checked = True
        ElseIf 伺服器區域_ch.Text = "設計" Then
            登入人IP = "106.1.48.106,4567" : 登入() : 確認_bt.Enabled = True : 取消_bt.Enabled = True : 刷卡登入_ch.Checked = True
        End If
    End Sub
    Private Sub 登入()
        '-----------------------寫入連線字串-----------------------------------------------------------------------------------------------------------------------------------------------
        If 伺服器區域_ch.Text = "設計" Then
            SQL_連線字串_測試()
        Else
            SQL_連線字串_正式()
        End If
        驗證_tb.Text = "1" : g_conn_type = 伺服器區域_ch.Text & "伺服器"
        '-----------------------系統版本號驗證-----------------------------------------------------------------------------------------------------------------------------------------------
        SQL_系統版本號驗證()
        If dr.Read() Then : 資料庫版本號_lb.Text = "資料庫版本號 : " & dr("系統認證版本號") : 版本號 = dr("系統認證版本號") : End If : conn.Close()
        '-----------------------FTP伺服器登入位置--------------------------------------------------------------------------------------------------------------------------------------------
        Target = "https://gcm-smart.com/ERP/YD-ERP/"
        '-----------------------帳號下拉清單----------------------------------------------------------------------------------------------------------------------------------------------------
        SQL_帳號讀取()
        使用者帳號_cb.Items.Clear() : While (dr.Read()) : 使用者帳號_cb.Items.Add(dr("帳號")) : End While : conn.Close()
        If Strings.Right(執行檔版本號_lb.Text, 10) <> Strings.Right(資料庫版本號_lb.Text, 10) Then
            自動更新2()
        Else : End If
    End Sub
    Private Sub 自動更新1()
        登入程序()
    End Sub
    Private Sub 自動更新2()
        Dim Str As String = Application.StartupPath
        If File.Exists(Str + "\NOTAUTO.txt") Then
            If 使用者帳號_cb.Text = "b70340" Or 使用者帳號_cb.Text = "B70340" Then : Else : End If
        Else
            My.Computer.Network.DownloadFile(Target & "SYS-UPDATE.exe", Str + "\SYS-UPDATE.exe")
            Shell(Str + "\SYS-UPDATE.exe", vbNormalFocus) : Me.Close()
        End If
    End Sub
    Private Sub 登入第一層()
        If 帳號_tb.Text = "b70340" Or 帳號_tb.Text = "B70340" Then
            If Strings.Right(執行檔版本號_lb.Text, 10) <> Strings.Right(資料庫版本號_lb.Text, 10) Then : 自動更新1() : Else : 登入程序() : End If
        Else
            If Strings.Right(執行檔版本號_lb.Text, 10) <> Strings.Right(資料庫版本號_lb.Text, 10) Then : 自動更新2() : Else : 登入程序() : End If
        End If
    End Sub
    Private Sub 登入程序()
        If 帳號_tb.Text = "" Then
            MsgBox("請輸入帳號!   如使用刷登入，請確定該卡片有合法註冊!")
        ElseIf 帳號_tb.Text <> "" Then
            If 帳號_tb.Text = "" Then
                MsgBox("請輸入帳號!")
            ElseIf 帳號_tb.Text <> "" Then
                If 驗證_tb.Text = "1" Then
                    SQL_使用者權限宣告()
                    If dr.Read() Then
                        密碼驗證_tb.Text = dr("密碼").ToString : gUserName = dr("姓名").ToString : 登入人職稱 = dr("職稱").ToString : 登入人級別 = dr("級別").ToString
                        For I As Integer = 1 To 權限數量
                            CC(I) = dr.Item(I + 4)
                        Next
                        If 登入人職稱 = "業務" Or 登入人職稱 = "經理" Or 登入人職稱 = "董事" Then
                            業務號碼 = Val(帳號_tb.Text)
                        End If
                        登入第二層()
                    End If : dr.Close()
                End If
            End If
        End If : conn.Close()
    End Sub
    Private Sub 登入第二層()
        If 密碼_tb.Text = 密碼驗證_tb.Text Then
            If 密碼驗證_tb.Text = "" Then
                MsgBox("帳號或密碼錯誤，請重新輸入!")
            Else
                Hide() : YD_ERP_SYS.Show() ': 密碼_tb.Text = "" : 帳號_tb.Text = ""
            End If
        ElseIf 密碼_tb.Text <> 密碼驗證_tb.Text Then
            MsgBox("帳號或密碼錯誤，請重新輸入!")
        End If
    End Sub
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 確認_bt.Click
        登入第一層()
    End Sub
    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 取消_bt.Click
        Me.Close()
    End Sub
    Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles 更新下載_ch.CheckedChanged
        If 更新下載_ch.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_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles 帳號_tb.KeyPress
        If e.KeyChar = ChrW(13) Then : Me.確認_bt.PerformClick() : End If
    End Sub
    Private Sub PasswordTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles 密碼_tb.KeyPress
        If e.KeyChar = ChrW(13) Then : Me.確認_bt.PerformClick() : End If
    End Sub
    Private Sub UsernameTextBox_TextChanged(sender As Object, e As EventArgs) Handles 帳號_tb.TextChanged
        If 刷卡登入_ch.Checked = False Then
        Else
            If IsNumeric(帳號_tb.Text) = True Then
                If 帳號_tb.Text.Length = 10 Then
                    SQL_刷卡登入()
                    If dr.Read() Then
                        帳號_tb.Text = dr("帳號").ToString : 密碼_tb.Text = dr("密碼").ToString
                    Else
                        帳號_tb.Text = "" : 密碼_tb.Text = ""
                    End If : conn.Close()
                    Me.確認_bt.PerformClick()
                End If
            End If
        End If
    End Sub
    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles 刷卡登入_ch.Click
        Me.ActiveControl = 使用者帳號_cb
    End Sub
    Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 使用者帳號_cb.TextChanged
        帳號_tb.Text = 使用者帳號_cb.Text
    End Sub
    Private Sub UPDATE_bt_Click(sender As Object, e As EventArgs) Handles UPDATE_bt.Click
        Dim Str As String = Application.StartupPath
        My.Computer.Network.DownloadFile(Target & "SYS-UPDATE.exe", Str + "\SYS-UPDATE.exe")
        Shell(Str + "\SYS-UPDATE.exe", vbNormalFocus)
        Me.Close()
    End Sub
    Private Sub 使用者帳號_cb_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles 使用者帳號_cb.GotFocus
        使用者帳號_cb.ImeMode = 2
    End Sub
End Class
