Option Explicit On
Imports System.IO
Public Class LoginForm1
    ' TODO: 插入程式碼，利用提供的使用者名稱和密碼執行自訂驗證
    ' (請參閱 https://go.microsoft.com/fwlink/?LinkId=35339)。
    ' 如此便可將自訂主體附加到目前執行緒的主體，如下所示: 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' 其中 CustomPrincipal 是用來執行驗證的 IPrincipal 實作。
    ' 接著，My.User 便會傳回封裝在 CustomPrincipal 物件中的識別資訊，
    ' 例如使用者名稱、顯示名稱等。
    Private Property MYSQL As String
    ReadOnly ds As Object
    ReadOnly ds2 As New DataSet
    ReadOnly screenWidth = Screen.PrimaryScreen.Bounds.Width
    ReadOnly screenHeight = Screen.PrimaryScreen.Bounds.Height
    Private Sub Set_清單1()
        FTP_dgv.DataSource = Nothing : ds2.Clear()
        FTP_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        FTP_dgv.ColumnHeadersHeight = 25
        FTP_dgv.AllowUserToAddRows = False
        ConnOpen()
        SQL1 = "SELECT 資料夾名稱, 指定 FROM FTP資料夾管理"
        CmdSet_For_DGV() : da.Fill(ds2) : FTP_dgv.DataSource = ds2.Tables(0) : conn.Close()
    End Sub
    Private Sub SQL_使用者下拉清單載入()
        SQL_使用者下拉清單() : 使用者_cb.Items.Clear()
        While (dr.Read()) : 使用者_cb.Items.Add(dr("UserName")) : End While : conn.Close()
    End Sub
    Private Sub LoginForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SQL_連線字串_考勤() : SQL_連線字串_ERP()
        '-----------------------登入準備-----------------------------------------------------------------------------------------------------------------------------------------------
        SQL_使用者下拉清單載入()
        '-----------------------調整視窗大小-----------------------------------------------------------------------------------------------------------------------------------------------
        Me.Size = New Size(470, 288)
        If screenWidth = 1920 And screenHeight = 1080 Then : Me.Location = New Point(725, 396)
        ElseIf screenWidth = 1366 And screenHeight = 768 Then : Me.Location = New Point(448, 240)
        ElseIf screenWidth = 1440 And screenHeight = 900 Then : Me.Location = New Point(485, 306)
        ElseIf screenWidth = 1280 And screenHeight = 1024 Then : Me.Location = New Point(405, 368)
        ElseIf screenWidth = 1600 And screenHeight = 900 Then : Me.Location = New Point(565, 306)
        End If
        '-----------------------系統版本號寫入-----------------------------------------------------------------------------------------------------------------------------------------------
        SQL_系統版本號讀取()
        If dr.Read() Then
            Label2.Text = "資料庫版本號 : " & dr("系統認證版本號") : 版本號 = dr("系統認證版本號")
        End If : conn.Close()
        Label1.Text = "執行檔版本號 : " & 版本
        '-----------------------FTP伺服器登入位置--------------------------------------------------------------------------------------------------------------------------------------------
        SQL_FTP伺服器登入位置()
        If dr.Read() Then : Target = dr("FTP地址") : FTP帳號 = dr("FTP帳號") : FTP密碼 = dr("FTP密碼") : FTP物理位置 = dr("FTP物理位置") : End If : conn.Close()
        Set_清單1()
        Dim HH As String = 0
        For i As Integer = 0 To FTP_dgv.Rows.Count - 1
            If i < 10 Then : HH = "0" & i : ElseIf i >= 10 Then : HH = i : End If
            If FTP_dgv("指定", i).Value.ToString = "AA" & HH Then : AA(i) = FTP_dgv("資料夾名稱", i).Value.ToString : 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 自動更新2()
        Dim Str As String = Application.StartupPath
        If File.Exists(Str + "\NOTAUTO.txt") Then : Else
            My.Computer.Network.DownloadFile(Target & AA(0) & "/" & "SYS-UPDATE.exe", Str + "\SYS-UPDATE.exe")
            Shell(Str + "\SYS-UPDATE.exe", vbNormalFocus)
            Me.Close()
        End If
    End Sub
    Private Sub 手動更新_cb_CheckedChanged(sender As Object, e As EventArgs) Handles 手動更新_cb.CheckedChanged
        If 手動更新_cb.Checked = False Then
            Me.Size = New Size(470, 288)
            If screenWidth = 1920 And screenHeight = 1080 Then : Me.Location = New Point(725, 396)
            ElseIf screenWidth = 1366 And screenHeight = 768 Then : Me.Location = New Point(448, 240)
            ElseIf screenWidth = 1440 And screenHeight = 900 Then : Me.Location = New Point(485, 306)
            ElseIf screenWidth = 1280 And screenHeight = 1024 Then : Me.Location = New Point(405, 368)
            ElseIf screenWidth = 1600 And screenHeight = 900 Then : Me.Location = New Point(565, 306)
            End If
        Else
            Me.Size = New Size(729, 288)
            If screenWidth = 1920 And screenHeight = 1080 Then : Me.Location = New Point(596, 396)
            ElseIf screenWidth = 1366 And screenHeight = 768 Then : Me.Location = New Point(319, 240)
            ElseIf screenWidth = 1440 And screenHeight = 900 Then : Me.Location = New Point(356, 306)
            ElseIf screenWidth = 1280 And screenHeight = 1024 Then : Me.Location = New Point(276, 368)
            ElseIf screenWidth = 1600 And screenHeight = 900 Then : Me.Location = New Point(436, 306)
            End If
        End If
    End Sub
    Private Sub 使用者_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 使用者_cb.SelectedIndexChanged
        PA = 使用者_cb.Text : SQL_使用者密碼讀取()
        If dr.Read() Then : 密碼_tb.Text = dr("Password") : End If : conn.Close()
        PasswordTextBox.Focus()
    End Sub
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        If 密碼_tb.Text = "" Then
            MsgBox("請選擇使用者!")
        ElseIf 密碼_tb.Text <> "" Then
            SQL_使用者權限讀取()
            If dr.Read() Then
                gUserName = 使用者_cb.Text
                For I As Integer = 1 To 權限數量
                    CC(I) = dr.Item(I)
                Next
            End If
            If PasswordTextBox.Text = 密碼_tb.Text Then
                If PasswordTextBox.Text = "" Then
                    MsgBox("帳號或密碼錯誤，請重新輸入!")
                Else
                    Me.Hide()
                    GRAMS_SYS.Show() : OK.Enabled = False : Cancel.Enabled = False
                End If
            ElseIf PasswordTextBox.Text <> 密碼_tb.Text Then
                MsgBox("帳號或密碼錯誤，請重新輸入!")
            End If
            dr.Close()
        End If
        conn.Close()
    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 適應症_bt_Click(sender As Object, e As EventArgs) Handles 適應症_bt.Click
        Me.Hide() : 醫師.Show()
    End Sub
End Class
