Option Strict Off
Imports System.Data.SqlClient
Imports System.IO
Imports System.Net
Public Class 驗貨報告資料庫
    Public Property Credentials As ICredentials
    ReadOnly conn As New SqlConnection : ReadOnly conn1 As New SqlConnection
    ReadOnly da As New SqlDataAdapter
    ReadOnly cmd As New SqlCommand : ReadOnly cmd1 As New SqlCommand
    ReadOnly ds As New DataSet : ReadOnly ds1 As New DataSet
    Dim dr As SqlDataReader
    Dim TT As String
    Private m_Leftx As Integer = 152
    Private m_Lefty As Integer = 0
    Dim m_MousePosX As Integer
    Dim m_MousePosY As Integer
    Dim m_DeltaX As Integer
    Dim m_DeltaY As Integer
    'Dim m_StrecthX As Double
    ' Dim m_StrecthY As Double
    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()
    End Sub
    Private Sub Set_圖片清單()
        Set_DGV1載入前設定()
        SQL1 = "SELECT 訂單控制表.訂單流水號, 訂單控制表.年份季度, 驗貨報告圖庫.客戶訂單號, 驗貨報告圖庫.型體名稱, 訂單控制表.APMA確認 AS 顏色,  驗貨報告圖庫.驗貨報告編號 " &
               "FROM 驗貨報告圖庫 LEFT OUTER JOIN 訂單控制表 ON 驗貨報告圖庫.客戶訂單號 = 訂單控制表.客戶訂單號 GROUP BY   訂單控制表.訂單流水號, 訂單控制表.年份季度, " &
               "驗貨報告圖庫.客戶訂單號, 驗貨報告圖庫.型體名稱, 訂單控制表.APMA確認, 驗貨報告圖庫.驗貨報告編號 ORDER BY 驗貨報告圖庫.客戶訂單號, 驗貨報告圖庫.型體名稱"
        Set_DGV1載入後設定()
    End Sub
    Private Sub Set_grid()
        DataGridView1.Columns(0).Width = 140 : DataGridView1.Columns(1).Width = 80 : DataGridView1.Columns(2).Width = 100
        DataGridView1.Columns(3).Width = 100 : DataGridView1.Columns(4).Width = 70 : DataGridView1.Columns(5).Width = 100
    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
        TextBox1.Enabled = False : TextBox2.Enabled = False : TextBox3.Enabled = False : TextBox4.Enabled = False : TextBox5.Visible = False : TextBox7.Visible = False : Button24.Enabled = False
        'If CC(92) = True Then : Button24.Enabled = True : End If

        Target1 = Target & AA(13) & "/"
        WebBrowser1.Url = New Uri(Target1)
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            WebBrowser2.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
        Set_圖片清單() : Set_grid()
    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox1.Text = DataGridView1(5, e.RowIndex).Value.ToString
            TextBox2.Text = DataGridView1(1, e.RowIndex).Value.ToString
            TextBox3.Text = DataGridView1(2, e.RowIndex).Value.ToString
            TextBox4.Text = DataGridView1(3, e.RowIndex).Value.ToString
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 驗貨報告 FROM 驗貨報告圖庫 WHERE 驗貨報告編號 LIKE '" & TextBox1.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 DataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = DataGridView1.Rows.Count - 1 Then
            Exit Sub
        Else
            If DataGridView1(2, e.RowIndex).Value.ToString <> DataGridView1(2, e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(DataGridView1.RowHeadersVisible, DataGridView1.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + DataGridView1.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - DataGridView1.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                Exit Sub
            End If
        End If
    End Sub
    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        PA = InputBox("請輸入關鍵字")
        Set_DGV1載入前設定()
        SQL1 = "SELECT 訂單控制表.訂單流水號, 訂單控制表.年份季度, 驗貨報告圖庫.客戶訂單號, 驗貨報告圖庫.型體名稱, 訂單控制表.APMA確認 AS 顏色,  驗貨報告圖庫.驗貨報告編號 
                FROM 驗貨報告圖庫 LEFT OUTER JOIN 訂單控制表 ON 驗貨報告圖庫.客戶訂單號 = 訂單控制表.客戶訂單號 
                WHERE 訂單控制表.訂單流水號 LIKE '%" & PA & "%' OR 訂單控制表.年份季度 LIKE '%" & PA & "%' OR 驗貨報告圖庫.客戶訂單號 LIKE '%" & PA & "%' OR 驗貨報告圖庫.型體名稱 LIKE '%" & PA & "%'
                GROUP BY   訂單控制表.訂單流水號, 訂單控制表.年份季度, 驗貨報告圖庫.客戶訂單號, 驗貨報告圖庫.型體名稱, 訂單控制表.APMA確認, 驗貨報告圖庫.驗貨報告編號 
                ORDER BY 驗貨報告圖庫.客戶訂單號, 驗貨報告圖庫.型體名稱"
        Set_DGV1載入後設定() : Set_grid()
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If TextBox1.Text = "" Then
            MsgBox("選擇要下載的圖片")
        Else
            With SaveFileDialog1 : .Filter = "所有文件(*.*)|*.*" : End With
            TT = "驗貨報告"

            SaveFileDialog1.FileName = TT & "-" & TextBox2.Text & "-" & TextBox3.Text & "-" & TextBox4.Text & ".jpg"

            SaveFileDialog1.ShowDialog() : TextBox5.Text = SaveFileDialog1.FileName
            ds1.Clear()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 驗貨報告 FROM 驗貨報告圖庫 WHERE 驗貨報告編號 LIKE '" & TextBox1.Text & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds1) : conn.Close()
            If ds1.Tables(0).Rows.Count > 0 Then
                Dim imgData() As Byte
                imgData = ds1.Tables(0).Rows(0).Item("驗貨報告")
                Dim fs As FileStream
                fs = File.Create(TextBox5.Text, imgData.Length - 1)
                fs.Write(imgData, 0, imgData.Length - 1)
                fs.Close()
            End If
            TextBox5.Text = ""
            MsgBox("下載完成")
        End If
    End Sub
    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        PictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone) : PictureBox1.Refresh() : PictureBox1.SizeMode = 4
    End Sub
    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        PictureBox1.Image.RotateFlip(RotateFlipType.Rotate270FlipNone) : PictureBox1.Refresh() : PictureBox1.SizeMode = 4
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        PictureBox1.Image.RotateFlip(RotateFlipType.Rotate180FlipY) : PictureBox1.Refresh() : PictureBox1.SizeMode = 4
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        PictureBox1.Image.RotateFlip(RotateFlipType.Rotate180FlipX) : PictureBox1.Refresh() : PictureBox1.SizeMode = 4
    End Sub
    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Dim image = PictureBox1.Image
        Dim g As Graphics = PictureBox1.CreateGraphics
        g.DrawImage(image, New Rectangle(0, 0, image.Width * 1.01, image.Height * 1.01))
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        PictureBox1.Refresh()
    End Sub
    '@#处理鼠标按键抬起的事件，根据鼠标按下时保存的鼠标位置，和当前鼠标的位置， 
    '计算鼠标移动偏移量，借此调用移动图片的函数，移动图片 
    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseUp
        m_DeltaX = m_MousePosX - e.X
        m_DeltaY = m_MousePosY - e.Y
        m_Leftx -= m_DeltaX
        m_Lefty -= m_DeltaY
        Picturemove(sender, e)
        Me.Cursor = Cursors.Arrow
    End Sub
    '@#当鼠标按下时，将鼠标变成手形，并且记录下当前鼠标的位置 
    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
        Me.Cursor = Cursors.Hand
        m_MousePosX = e.X
        m_MousePosY = e.Y
    End Sub
    '@#根据偏移量计算出的图片位置，重画图片 
    Private Sub Picturemove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim myBit As New Bitmap(PictureBox1.Image)
        Dim myPicGrh As Graphics = Me.PictureBox1.CreateGraphics
        myPicGrh.Clear(Me.PictureBox1.BackColor)
        myPicGrh.DrawImageUnscaled(myBit, m_Leftx - 152, m_Lefty)
        myBit.Dispose()
        myPicGrh.Dispose()
    End Sub
    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            WebBrowser2.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
    End Sub
    Private Sub TextBox6_DragEnter(sender As Object, e As DragEventArgs) Handles TextBox6.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim files As String()
            Try
                files = CType(e.Data.GetData(DataFormats.FileDrop), String())
                TextBox6.Text = files(files.Length - 1)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Return
            End Try
        End If

        Dim STR2 As Integer = 0
        TextBox7.Text = TextBox6.Text
        If TextBox6.Text = "" Then
        Else
            For i As Integer = 0 To 9999
                Dim STR1 As Integer = Strings.Len(TextBox7.Text)
                If Strings.Right((TextBox7.Text), 1) <> "\" Then
                    STR1 -= 1 : STR2 += 1
                    If STR1 = 0 Then
                        TextBox7.Text = ""
                        TextBox6.Text = ""
                        i = 9999
                    Else
                        TextBox7.Text = Strings.Left((TextBox7.Text), STR1)
                    End If
                Else
                    TextBox7.Text = Strings.Right((TextBox6.Text), STR2)
                    TextBox6.Text = TextBox7.Text
                    i = 9999
                End If
            Next
        End If
    End Sub
    Private Sub TextBox6_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles TextBox6.DragDrop
        Dim files As Array = e.Data.GetData(DataFormats.FileDrop)
        For Each file As String In files
            TextBox6.AppendText(file + Environment.NewLine)
        Next
    End Sub
    Private Sub Button24_Click(sender As Object, e As EventArgs) Handles Button24.Click
        conn.Close()
        If TextBox1.Text = "" Then
            MsgBox("請先選擇需要修改的圖片")
        Else
            If TextBox6.Text = "" Then
                MsgBox("請確認圖片已準備完畢")
            Else
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "UPDATE 驗貨報告圖庫 SET  驗貨報告 = BULKCOLUMN FROM OPENROWSET ( BULK 'E:\TENDENZA\0004\" & TextBox6.Text & "', SINGLE_BLOB) AS EMPLOYEEPICTURE " &
                       "WHERE (驗貨報告編號 = '" & TextBox1.Text & "')"
                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 & TextBox6.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()
                '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                TextBox6.Text = ""

                MsgBox("修改完成")
                Set_圖片清單() : Set_grid()
            End If
        End If
    End Sub
End Class