Option Explicit On
Imports System.Net
Imports System.IO
Imports System.Data.SqlClient
Public Class 簽名檔管理
    Public Property Credentials As ICredentials
    ReadOnly conn As New SqlConnection
    ReadOnly da As New SqlDataAdapter
    ReadOnly cmd As New SqlCommand
    ReadOnly ds As New DataSet : ReadOnly ds1 As New DataSet
    Dim dr As SqlDataReader
    Dim EDR As Integer = 0 : Dim ESTR As String
    Private Sub Set_DGV1載入前設定()
        DataGridView1.DataSource = Nothing : ds.Clear()
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView1.ColumnHeadersHeight = 25
        DataGridView1.AllowUserToAddRows = False
        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) : DataGridView1.DataSource = ds.Tables(0) : conn.Close() : Set_grid()
    End Sub
    Private Sub Set_清單()
        Set_DGV1載入前設定()
        SQL1 = "SELECT 圖片流水號 AS 圖檔編碼, 簽名檔人名 FROM 簽名檔管理 ORDER BY 圖片流水號"
        Set_DGV1載入後設定()
    End Sub
    Private Sub Set_grid()
        DataGridView1.Columns(0).Width = 221 : DataGridView1.Columns(1).Width = 220
    End Sub
    Private Sub ComboBox1下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 姓名 FROM 使用者權限管理表 ORDER BY 職稱"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox1.Items.Clear()
        While (dr.Read()) : ComboBox1.Items.Add(dr("姓名")) : End While
        conn.Close()
    End Sub
    Private Sub 簽名檔管理_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = WINPROFIT_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        Set_清單() : TextBox3.Visible = False
        ComboBox1下拉表單資料載入()
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            WebBrowser2.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
        Target1 = Target & AA(1) & "/"
        WebBrowser1.Url = New Uri(Target1)
    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox2.Text = DataGridView1(0, e.RowIndex).Value.ToString : ComboBox1.Text = DataGridView1(1, e.RowIndex).Value.ToString
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 簽名檔圖片 FROM 簽名檔管理 WHERE 圖片流水號 LIKE N'" & TextBox2.Text & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            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)
                PictureBox1.Image = Bitmap.FromStream(oStream)
            End While
            conn.Close()
            PictureBox1.SizeMode = 4
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If TextBox2.Text = "" Then
            MsgBox("請先將要入庫的編號準備好")
        Else
            If TextBox4.Text = "" Then
                MsgBox("請先確定圖片資料已準備完畢")
            Else
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "INSERT INTO 簽名檔管理 (圖片流水號, 簽名檔人名, 簽名檔圖片) SELECT '" & TextBox2.Text & "', '" & ComboBox1.Text & "', " &
                       "BULKCOLUMN FROM OPENROWSET ( BULK '" & FTP實體位置 & AA(1) & "\" & TextBox4.Text & "', SINGLE_BLOB) AS EMPLOYEEPICTURE"
                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                '---FTP方式刪除檔案----------------------------------------------------------------------------------------------------------------------------------------------------------------------
                Dim frq As FtpWebRequest, frp As FtpWebResponse, fcr As NetworkCredential
                frq = CType(WebRequest.Create(New Uri(Target1 & TextBox4.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()
                WebBrowser1.Refresh()
                '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                TextBox4.Text = ""
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "SELECT 簽名檔圖片 FROM 簽名檔管理 WHERE 圖片流水號 LIKE N'" & TextBox2.Text & "'"
                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                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)
                    PictureBox1.Image = Bitmap.FromStream(oStream)
                End While
                conn.Close()
                PictureBox1.SizeMode = 4
                Set_清單() : TextBox2.Text = ""
            End If
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT TOP (1) 圖片流水號 FROM 簽名檔管理 ORDER BY 圖片流水號 DESC"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        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
        TextBox2.Text = ESTR
    End Sub
    Private Sub TextBox4_DragEnter(sender As Object, e As DragEventArgs) Handles TextBox4.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim files As String()
            Try
                files = CType(e.Data.GetData(DataFormats.FileDrop), String())
                TextBox4.Text = files(files.Length - 1)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Return
            End Try
        End If

        Dim STR2 As Integer = 0
        TextBox3.Text = TextBox4.Text
        If TextBox4.Text = "" Then
        Else
            For i As Integer = 0 To 9999
                Dim STR1 As Integer = Strings.Len(TextBox3.Text)
                If Strings.Right((TextBox3.Text), 1) <> "\" Then
                    STR1 -= 1 : STR2 += 1
                    If STR1 = 0 Then
                        TextBox3.Text = ""
                        TextBox4.Text = ""
                        i = 9999
                    Else
                        TextBox3.Text = Strings.Left((TextBox3.Text), STR1)
                    End If
                Else
                    TextBox3.Text = Strings.Right((TextBox4.Text), STR2)
                    TextBox4.Text = TextBox3.Text
                    i = 9999
                End If
            Next
        End If
    End Sub
    Private Sub TextBox4_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles TextBox4.DragDrop
        Dim files As Array = e.Data.GetData(DataFormats.FileDrop)
        For Each file As String In files
            TextBox4.AppendText(file + Environment.NewLine)
        Next
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        SQL1 = "DELETE FROM 簽名檔管理 WHERE (圖片流水號 = N'" & TextBox2.Text & "')"
        cmd.CommandText = SQL1 : cmd.Connection = conn
        If conn.State = ConnectionState.Closed Then : conn.Open() : End If
        Dim aa As MsgBoxResult
        aa = MsgBox("確定要刪除該筆資料?", MsgBoxStyle.OkCancel)
        If aa = MsgBoxResult.Ok Then
            cmd.ExecuteNonQuery()
            Set_清單() : TextBox2.Text = ""
        End If
    End Sub
End Class