Option Explicit On
Public Class 簽名檔管理
    Public Property Credentials As ICredentials
    ReadOnly ds As New DataSet
    ReadOnly ds1 As New DataSet
    Dim EDR As Integer = 0 : Dim ESTR As String
    Private Sub Set_DGV1載入前設定()
        簽名檔人名_dgv.DataSource = Nothing : ds.Clear()
        簽名檔人名_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        簽名檔人名_dgv.ColumnHeadersHeight = 25
        簽名檔人名_dgv.AllowUserToAddRows = False
        簽名檔人名_dgv.RowTemplate.Height = 30
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV1載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : 簽名檔人名_dgv.DataSource = ds.Tables(0) : conn.Close()
        Set_grid()
    End Sub
    Private Sub Set_清單()
        Set_DGV1載入前設定()
        SQL_簽名檔管理_圖片流水號查詢()
        Set_DGV1載入後設定()
    End Sub
    Private Sub Set_grid()
        簽名檔人名_dgv.Columns(0).FillWeight = 221 : 簽名檔人名_dgv.Columns(1).FillWeight = 220
    End Sub
    Private Sub ComboBox1下拉表單資料載入()
        conn.Close()
        SQL_簽名檔管理_使用者權限管理表姓名查詢()
        簽名檔持有人_cb.Items.Clear()
        While (dr.Read()) : 簽名檔持有人_cb.Items.Add(dr("姓名")) : End While
        conn.Close()
    End Sub
    Private Sub 簽名檔管理_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = YD_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        Set_清單() : 檔案名稱tb.Visible = False
        ComboBox1下拉表單資料載入()
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            本機資料夾_wb.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
        Target1 = Target & AA(1) & "/"
        伺服器FTP文件夾_wb.Url = New Uri(Target1)
    End Sub
    Private Sub 簽名檔人名_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 簽名檔人名_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            圖檔編號_tb.Text = 簽名檔人名_dgv(0, e.RowIndex).Value.ToString : 簽名檔持有人_cb.Text = 簽名檔人名_dgv(1, e.RowIndex).Value.ToString
            SQL_簽名檔管理_主管簽名檔查詢()
            While dr.Read() = True
                Dim unused As Byte() = New Byte(-1) {}
                Dim bytes As Byte() = DirectCast(dr.Item("簽名檔圖片"), Byte())
                Dim oStream As New MemoryStream(bytes)
                圖片顯示_vb.Image = Bitmap.FromStream(oStream)
            End While
            conn.Close()
            圖片顯示_vb.SizeMode = 4
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles 簽名檔入系統_bt.Click
        If 圖檔編號_tb.Text = "" Then
            MsgBox("請先將要入庫的編號準備好")
        Else
            If 檔案名稱_tb.Text = "" Then
                MsgBox("請先確定圖片資料已準備完畢")
            Else
                SQL_簽名檔管理_主管簽名檔新增() : conn.Close()
                '---FTP方式刪除檔案----------------------------------------------------------------------------------------------------------------------------------------------------------------------
                Dim frq As FtpWebRequest, frp As FtpWebResponse, fcr As NetworkCredential
                frq = CType(WebRequest.Create(New Uri(Target1 & 檔案名稱_tb.Text)), FtpWebRequest)
                fcr = New NetworkCredential(FTP帳號, FTP密碼)
                frq.Credentials = fcr
                frq.Method = WebRequestMethods.Ftp.DeleteFile
                frq.UseBinary = True
                frp = CType(frq.GetResponse, FtpWebResponse)
                frp.Close()
                伺服器FTP文件夾_wb.Refresh()
                '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                檔案名稱_tb.Text = ""
                SQL_簽名檔管理_主管簽名檔查詢()
                While dr.Read() = True
                    Dim unused As Byte() = New Byte(-1) {}
                    Dim bytes As Byte() = DirectCast(dr.Item("簽名檔圖片"), Byte())
                    Dim oStream As New MemoryStream(bytes)
                    圖片顯示_vb.Image = Bitmap.FromStream(oStream)
                End While
                conn.Close()
                圖片顯示_vb.SizeMode = 4
                Set_清單() : 圖檔編號_tb.Text = ""
            End If
        End If
    End Sub
    Private Sub 生成圖檔編號_bt_Click(sender As Object, e As EventArgs) Handles 生成圖檔編號_bt.Click
        SQL_簽名檔管理_主管簽名檔最後一筆查詢()
        If dr.Read() Then : EDR = Double.Parse(Strings.Right(dr("圖片流水號").ToString, 8)) : Else : EDR = 0 : End If
        conn.Close()
        EDR += 1
        If EDR < 10 Then : ESTR = "PG" & "0000000" & EDR
        ElseIf EDR > 9 And EDR < 100 Then : ESTR = "PG" & "000000" & EDR
        ElseIf EDR > 99 And EDR < 1000 Then : ESTR = "PG" & "00000" & EDR
        ElseIf EDR > 999 And EDR < 10000 Then : ESTR = "PG" & "0000" & EDR
        ElseIf EDR > 9999 And EDR < 100000 Then : ESTR = "PG" & "000" & EDR
        ElseIf EDR > 99999 And EDR < 100000 Then : ESTR = "PG" & "00" & EDR
        ElseIf EDR > 999999 And EDR < 1000000 Then : ESTR = "PG" & "0" & EDR
        ElseIf EDR > 9999999 Then : ESTR = "PG" & EDR
        End If
        圖檔編號_tb.Text = ESTR
    End Sub
    Private Sub 檔案名稱_tb_DragEnter(sender As Object, e As DragEventArgs) Handles 檔案名稱_tb.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim files As String()
            files = CType(e.Data.GetData(DataFormats.FileDrop), String())
            檔案名稱_tb.Text = files(files.Length - 1)
        End If
        Dim STR2 As Integer = 0
        檔案名稱tb.Text = 檔案名稱_tb.Text
        If 檔案名稱_tb.Text = "" Then
        Else
            For i As Integer = 0 To 9999
                Dim STR1 As Integer = Strings.Len(檔案名稱tb.Text)
                If Strings.Right((檔案名稱tb.Text), 1) <> "\" Then
                    STR1 -= 1 : STR2 += 1
                    If STR1 = 0 Then
                        檔案名稱tb.Text = ""
                        檔案名稱_tb.Text = ""
                        i = 9999
                    Else
                        檔案名稱tb.Text = Strings.Left((檔案名稱tb.Text), STR1)
                    End If
                Else
                    檔案名稱tb.Text = Strings.Right((檔案名稱_tb.Text), STR2)
                    檔案名稱_tb.Text = 檔案名稱tb.Text
                    i = 9999
                End If
            Next
        End If
    End Sub
    Private Sub 檔案名稱_tb_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles 檔案名稱_tb.DragDrop
        Dim files As Array = e.Data.GetData(DataFormats.FileDrop)
        For Each file As String In files
            檔案名稱_tb.AppendText(file + Environment.NewLine)
        Next
    End Sub
    Private Sub 刪除簽名檔_bt_Click(sender As Object, e As EventArgs) Handles 刪除簽名檔_bt.Click
        Dim aa As MsgBoxResult
        aa = MsgBox("確定要刪除該筆資料?", MsgBoxStyle.OkCancel)
        If aa = MsgBoxResult.Ok Then
            SQL_簽名檔管理_主管簽名檔刪除()
            Set_清單() : 圖檔編號_tb.Text = ""
        End If
    End Sub
    Private Sub 讀取料夾路徑_bt_Click(sender As Object, e As EventArgs) Handles 讀取料夾路徑_bt.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            本機資料夾_wb.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
    End Sub
    Private Sub 字體_NUD_ValueChanged(sender As Object, e As EventArgs) Handles 字體_NUD.ValueChanged
        簽名檔人名_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
    End Sub
End Class