Imports System.IO
Imports System.Text
Public Class LoginForm1
    Dim MACKK As String 'MAC碼
    Dim diskId As String '數字ID
    Dim diskSerialNumber As String '硬碟序列
    Dim diskModel As String '磁盘型態
    Dim Str As String = Application.StartupPath
    Dim FFGG, GGHH As Integer
    Dim 內網不通 As Boolean
    Dim FTPIP As String
    Private Sub Set_清單2()
        Dim ds3 As New DataSet
        系統登入_dgv.DataSource = Nothing : ds3.Clear()
        系統登入_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        系統登入_dgv.ColumnHeadersHeight = 25
        系統登入_dgv.AllowUserToAddRows = False
        SQL_系統使用者卡號讀取()
        da.Fill(ds3) : 系統登入_dgv.DataSource = ds3.Tables(0) : conn.Close()
        系統登入_dgv.Columns(0).FillWeight = 120 : 系統登入_dgv.Columns(1).FillWeight = 30 : 系統登入_dgv.Columns(2).Visible = False : 系統登入_dgv.Columns(3).FillWeight = 50
    End Sub
    Private Sub LoginForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        首次開啟 = True
    End Sub
    Private Sub LoginForm1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        '-----調整視窗大小-------------------------------------------------------------------------------------------------------------------------------------------------------------
        Me.Size = New Size(420, 289)
        Dim screenBounds As Rectangle = Screen.PrimaryScreen.Bounds
        Me.Left = (screenBounds.Width - Me.Width) / 2
        Me.Top = (screenBounds.Height - Me.Height) / 2
        系統登入_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 7.6)
        '-----版本比對與更新-------------------------------------------------------------------------------------------------------------------------------------------------------------
        計算分流.Enabled = True
        '-----圖片低網速-------------------------------------------------------------------------------------------------------------------------------------------------------------
        If File.Exists(Str + "\GASTYLE.txt") Then
            Dim filereader = My.Computer.FileSystem.OpenTextFileReader(Str + "\GASTYLE.txt", System.Text.Encoding.Default)
            Dim Text As String = "" : Dim line As String = filereader.ReadLine()
            While Not (line Is Nothing) : Text = Text & line & vbCrLf : line = filereader.ReadLine() : End While : filereader.Close()
            If Strings.Left(Text, 1) = "B" Then : 低網速模式_ch.Checked = False : 低網速模式 = False
            ElseIf Strings.Left(Text, 1) = "W" Then : 低網速模式_ch.Checked = True : 低網速模式 = True : End If
        Else : 低網速模式_ch.Checked = False : End If
    End Sub
    Private Sub 計算分流_Tick(sender As Object, e As EventArgs) Handles 計算分流.Tick
        計算分流.Enabled = False
        IP比對() : SQL_系統版本號驗證() : If dr.Read() Then : 資料庫版本 = dr("系統認證版本號") : 版本號 = dr("系統認證版本號") : End If : conn.Close() : Timer1.Enabled = True
    End Sub
    Private Sub 後續動作()
        執行檔版本號_lb.Text = "執行檔版本號 : " & 版本 : 資料庫版本號_lb.Text = "資料庫版本號 : " & 資料庫版本
        If Strings.Right(執行檔版本號_lb.Text, 10) <> Strings.Right(資料庫版本號_lb.Text, 10) Then : 登入() : 自動更新2() : Else : 登入() : End If
        '-----電腦綁定判斷-------------------------------------------------------------------------------------------------------------------------------------------------------------
        MAC碼與硬碟資料() : PA1 = MACKK : PA2 = diskId : PA3 = diskSerialNumber : PA4 = diskModel : SQL_電腦綁定()
        If dr.Read() Then : Else : 輸入密碼.ShowDialog()
            If PA = "0003065795" Then : 系統綁定電腦資訊.ShowDialog() : SQL_電腦綁定() : If dr.Read() Then : Else : MsgBox("該電腦尚未進行認證，無法使用系統!!") : Me.Close() : End If
            Else : MsgBox("該電腦尚未進行認證，無法使用系統!!") : Me.Close() : End If
        End If
    End Sub
    Private Sub 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
    End Sub
    Private Sub IP比對()
        If SQL_連線狀態模組1.ConnectTest1("192.168.0.187,1433") = True Then
            '---主伺服器導通-------------------------------------------
            公司線路_rb.Checked = True : 測試線路_rb.Checked = False : 內網不通 = False
        Else
            If SQL_連線狀態模組2.ConnectTest2("106.1.49.218,2433") = True Then
                '---主伺服器導通-------------------------------------------
                公司線路_rb.Checked = False : 測試線路_rb.Checked = True : 內網不通 = True
            Else : MsgBox("無法連線到任何伺服器，請聯繫系館管理員!!") : End If
        End If : 連線字串()
    End Sub
    Private Sub 連線字串()
        '-----------------------寫入連線字串-----------------------------------------------------------------------------------------------------------------------------------------------
        If 公司線路_rb.Checked = True Then
            登入人IP = "192.168.0.187,1433" : g_conn_type = "公司伺服器" : PA = "公司"
        Else : 登入人IP = "106.1.49.218,2433" : g_conn_type = "測試伺服器" : PA = "測試" : End If
        SQL_連線字串_主機() : SQL_連線字串_正式_工程SQL()
    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
        Else : Timer1.Enabled = False : 後續動作() : End If
    End Sub
    Private Sub 登入()
        '-----------------------系統版本號寫入-----------------------------------------------------------------------------------------------------------------------------------------------
        SQL_系統版本號驗證()
        If dr.Read() Then : 資料庫版本 = dr("系統認證版本號") : 版本號 = dr("系統認證版本號") : End If : conn.Close()
        '-----------------------FTP資料夾登入位置宣告--------------------------------------------------------------------------------------------------------------------------------------
        Target = "https://gcm-smart.com/ERP/FH-ERP/"
    End Sub
    Private Sub 自動更新2()
        Dim Str As String = Application.StartupPath
        If File.Exists(Str + "\NOTAUTO.txt") Then : 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 Strings.Right(執行檔版本號_lb.Text, 10) <> Strings.Right(資料庫版本號_lb.Text, 10) Then
            自動更新2() : 登入程序()
        Else
            登入程序()
        End If
    End Sub
    Private Sub 登入程序()
        If 帳號_rtb.Text = "" Then : MsgBox("沒有輸入帳號!!") : ElseIf 帳號_rtb.Text <> "" Then
            SQL_使用者級別宣告() : If dr.Read() Then : 登入人級別 = dr("級別").ToString : End If
            SQL_系統預設值()
            If dr.Read() Then
                密碼時限 = dr("密碼重設天數") : 行政採購簽核級別基數 = dr("行政採購二級") : 建築採購簽核級別基數 = dr("建築採購二級")
                比例1 = dr("比例1") : 比例2 = dr("比例2") : 比例3 = dr("比例3") : 分母 = dr("圖片伺服器上限") : 工程圖庫限制 = dr("圖片伺服器上限")
            End If
            SQL_使用者權限宣告()
            If dr.Read() Then
                密碼驗證_tb.Text = dr("密碼").ToString : 密碼日期_tb.Text = dr("密碼日期").ToString
                gUserName = dr("姓名").ToString : 登入人部門 = dr("部門").ToString : 登入人課別 = dr("課別").ToString
                登入人組別 = dr("組別").ToString : gUser帳號 = 帳號_rtb.Text
                For I As Integer = 1 To 權限數量 : CC(I) = dr.Item(I + 8 - 1) : Next
                FFGG = DateDiff("d", 密碼日期_tb.Text, Format(Today(), "yyyy/MM/dd"))
                If CC(45) = True Then : GGHH = 密碼時限 - FFGG
                    If GGHH <= 0 Then
                        MsgBox("密碼過期，請重新設定!!") : 個人帳號管理.ShowDialog()
                    Else : 登入第二層() : End If
                Else : 登入第二層() : End If
            Else : MsgBox("帳號有誤，請重新輸入!!") : End If : dr.Close()
        End If
        conn.Close()
    End Sub
    Private Sub 登入第二層()
        If 密碼_rtb.Text = 密碼驗證_tb.Text Then
            If 密碼驗證_tb.Text = "" Then : MsgBox("沒有輸入密碼!!")
            Else
                Me.Hide() : Timer1.Enabled = False
                FH_ERP_SYS.Show() : 密碼_rtb.Text = "" : 帳號_rtb.Text = ""
            End If
        ElseIf 密碼_rtb.Text <> 密碼驗證_tb.Text Then : MsgBox("密碼錯誤!!")
        End If
    End Sub
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        '-----電腦綁定判斷-------------------------------------------------------------------------------------------------------------------------------------------------------------
        If 登入綁定_ch.Checked = True Then
            PA1 = MACKK : PA2 = diskId : PA3 = diskSerialNumber : PA4 = diskModel
            SQL_電腦綁定()
            If dr.Read() Then
                conn.Close() : 登入第一層()
            Else
                If 帳號_rtb.Text = "b70340" Or 帳號_rtb.Text = "B70340" Then
                    conn.Close() : 登入第一層()
                Else
                    conn.Close() : MsgBox("該電腦沒有註冊，無法使用系統!!") : Me.Close()
                End If
            End If
        Else
            conn.Close() : 登入第一層()
        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 系統登入_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 系統登入_dgv.CellClick
        If e.RowIndex = -1 Then : Else : 帳號_rtb.Text = 系統登入_dgv("ID卡號", e.RowIndex).Value.ToString : OK.PerformClick() : End If
    End Sub
    Private Sub UsernameTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles 帳號_rtb.KeyPress
        If e.KeyChar = ChrW(13) Then
            Me.OK.PerformClick()
        End If
    End Sub
    Private Sub PasswordTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles 密碼_rtb.KeyPress
        If e.KeyChar = ChrW(13) Then
            Me.OK.PerformClick()
        End If
    End Sub
    Private Sub UsernameTextBox_TextChanged(sender As Object, e As EventArgs) Handles 帳號_rtb.TextChanged
        If IsNumeric(帳號_rtb.Text) = True Then
            If 帳號_rtb.Text.Length = 10 Then
                SQL_刷卡登入()
                If dr.Read() Then
                    帳號_rtb.Text = dr("帳號").ToString : 密碼_rtb.Text = dr("密碼").ToString : conn.Close()
                Else
                    帳號_rtb.Text = "" : 密碼_rtb.Text = "" : conn.Close()
                End If
                Exit Sub : Me.OK.PerformClick()
            End If
        End If
    End Sub
    Private Sub 隱藏開關_lb_Click(sender As Object, e As EventArgs) Handles 隱藏開關_lb.Click
        輸入密碼.ShowDialog()
        If PA = "0003065795" Then
            Me.Size = New Size(671, 289)
            Dim screenBounds As Rectangle = Screen.PrimaryScreen.Bounds
            Me.Left = (screenBounds.Width - Me.Width) / 2
            Me.Top = (screenBounds.Height - Me.Height) / 2 : Set_清單2()
        Else
            Me.Size = New Size(420, 289)
            Dim screenBounds As Rectangle = Screen.PrimaryScreen.Bounds
            Me.Left = (screenBounds.Width - Me.Width) / 2
            Me.Top = (screenBounds.Height - Me.Height) / 2
        End If
    End Sub
    Private Sub 執行更新_bt_Click(sender As Object, e As EventArgs) Handles 執行更新_bt.Click
        自動更新2()
    End Sub
    Private Sub LogoPictureBox_Click(sender As Object, e As EventArgs) Handles LogoPictureBox.Click
        輸入密碼.ShowDialog()
        If PA = "0003065795" Then
            Me.Size = New Size(671, 289)
            Dim screenBounds As Rectangle = Screen.PrimaryScreen.Bounds
            Me.Left = (screenBounds.Width - Me.Width) / 2
            Me.Top = (screenBounds.Height - Me.Height) / 2 : Set_清單2()
        Else
            Me.Size = New Size(420, 289)
            Dim screenBounds As Rectangle = Screen.PrimaryScreen.Bounds
            Me.Left = (screenBounds.Width - Me.Width) / 2
            Me.Top = (screenBounds.Height - Me.Height) / 2
        End If
    End Sub
    Private Sub 低網速模式_ch_Click(sender As Object, e As EventArgs) Handles 低網速模式_ch.Click
        If 低網速模式_ch.Checked = True Then : 低網速模式 = True
            Dim strDirName As String : Dim Strs As String = Application.StartupPath
            If File.Exists(Strs & "\GASTYLE.txt") Then : File.Delete(Strs & "\GASTYLE.txt") : End If
            strDirName = Strs & "\GASTYLE.txt"
            Try
                Using fs As IO.FileStream = New IO.FileStream(strDirName, FileMode.Append)
                    Using sw As StreamWriter = New StreamWriter(fs, Encoding.Default)
                        Dim str1 As String = "W"
                        sw.WriteLine(str1)   '插入一整行
                        sw.Flush()
                    End Using
                End Using
            Catch ex As Exception
            End Try
        Else : 低網速模式 = False
            Dim strDirName As String : Dim Strs As String = Application.StartupPath
            If File.Exists(Strs & "\GASTYLE.txt") Then : File.Delete(Strs & "\GASTYLE.txt") : End If
            strDirName = Strs & "\GASTYLE.txt"
            Try
                Using fs As IO.FileStream = New IO.FileStream(strDirName, FileMode.Append)
                    Using sw As StreamWriter = New StreamWriter(fs, Encoding.Default)
                        Dim str1 As String = "B"
                        sw.WriteLine(str1)   '插入一整行
                        sw.Flush()
                    End Using
                End Using
            Catch ex As Exception
            End Try
        End If
    End Sub
End Class
