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 da As New SqlDataAdapter
    ReadOnly cmd As New SqlCommand
    ReadOnly ds As New DataSet : ReadOnly ds1 As New DataSet : ReadOnly ds2 As New DataSet : ReadOnly ds3 As New DataSet
    Dim dr As SqlDataReader
    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載入前設定()
        If PA = "" Then
            SQL1 = "SELECT 楦頭編號, 楦頭名 FROM 楦頭規格控制表 ORDER BY 楦頭編號"
        Else
            SQL1 = "SELECT 楦頭編號, 楦頭名 FROM 楦頭規格控制表 WHERE 楦頭編號 LIKE N'%" & PA & "%' ORDER BY 楦頭編號"
        End If
        Set_DGV1載入後設定() : Set_grid()
    End Sub
    Private Sub Set_grid()
        DataGridView1.Columns(0).Width = 142 : DataGridView1.Columns(1).Width = 142
    End Sub
    Private Sub Set_DGV2載入前設定()
        DataGridView2.DataSource = Nothing : ds1.Clear()
        DataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView2.ColumnHeadersHeight = 25
        DataGridView2.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV2載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds1) : DataGridView2.DataSource = ds1.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_模具開發表清單()
        Set_DGV2載入前設定()
        SQL1 = "SELECT 模具編號, 結構, 材質 FROM 模具開發控制表 WHERE (楦頭編號 LIKE N'" & TextBox1.Text & "')"
        Set_DGV2載入後設定() : Set_grid1()
    End Sub
    Private Sub Set_grid1()
        DataGridView2.Columns(0).Width = 120 : DataGridView2.Columns(1).Width = 120 : DataGridView2.Columns(2).Width = 120
    End Sub
    Private Sub Set_DGV3載入前設定()
        DataGridView3.DataSource = Nothing : ds2.Clear()
        DataGridView3.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView3.ColumnHeadersHeight = 25
        DataGridView3.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV3載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds2) : DataGridView3.DataSource = ds2.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_型體表清單()
        Set_DGV3載入前設定()
        SQL1 = "SELECT 型體號碼, 型體名稱 FROM 型體控制表 WHERE (客戶簡稱 LIKE N'" & ComboBox1.Text & "') AND (型體號碼 LIKE N'%" & TextBox1.Text & "%') ORDER BY 型體號碼"
        Set_DGV3載入後設定() : Set_grid2()
    End Sub
    Private Sub Set_grid2()
        DataGridView3.Columns(0).Width = 120 : DataGridView3.Columns(1).Width = 240
    End Sub
    Private Sub Set_DGV4載入前設定()
        DataGridView4.DataSource = Nothing : ds3.Clear()
        DataGridView4.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView4.ColumnHeadersHeight = 25
        DataGridView4.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV4載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds3) : DataGridView4.DataSource = ds3.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_鞋樣素描清單()
        Set_DGV4載入前設定()
        If PA = "" Then
            SQL1 = "SELECT 品牌, 鞋樣素描圖編號, 楦頭編號, 模具編號, 型體編號, 型體名稱 FROM 鞋樣素描圖控制表"
        Else
            SQL1 = "SELECT 品牌, 鞋樣素描圖編號, 楦頭編號, 模具編號, 型體編號, 型體名稱 FROM 鞋樣素描圖控制表 WHERE 品牌 LIKE N'" & PA & "' OR 鞋樣素描圖編號 LIKE N'" & PA & "' OR 楦頭編號 LIKE N'" & PA &
                    "' OR 模具編號 LIKE N'" & PA & "' OR 型體編號 LIKE N'" & PA & "' OR 型體名稱 LIKE N'" & PA & "'"
        End If
        Set_DGV4載入後設定() : Set_grid3()
    End Sub
    Private Sub Set_grid3()
        DataGridView4.Columns(0).Width = 339 : DataGridView4.Columns(1).Width = 120 : DataGridView4.Columns(2).Width = 120 : DataGridView4.Columns(3).Width = 120 : DataGridView4.Columns(4).Width = 120
        DataGridView4.Columns(5).Width = 340
    End Sub
    Private Sub ComboBox1下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 客戶簡稱 FROM 型體控制表 GROUP BY   客戶簡稱 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_楦頭規格表清單() : ComboBox1下拉表單資料載入() : Set_鞋樣素描清單()
        TextBox4.Enabled = False : TextBox7.Visible = False
        TextBox6.Enabled = False : TextBox1.Enabled = False : TextBox2.Enabled = False : TextBox3.Enabled = False : Button3.Enabled = False : Button6.Enabled = False
        Button2.Enabled = True : Button5.Enabled = True
        Target1 = Target & AA(13) & "/"
        WebBrowser1.Url = New Uri(Target1)
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            WebBrowser2.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
    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.Rows(e.RowIndex).Cells("楦頭編號").Value
            Set_模具開發表清單()
            Set_型體表清單()
        End If
    End Sub
    Private Sub DataGridView2_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView2.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox4.Text = DataGridView2.Rows(e.RowIndex).Cells("模具編號").Value
        End If
    End Sub
    Private Sub DataGridView3_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView3.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox2.Text = DataGridView3.Rows(e.RowIndex).Cells("型體號碼").Value
            TextBox3.Text = DataGridView3.Rows(e.RowIndex).Cells("型體名稱").Value
        End If
    End Sub
    Private Sub DataGridView4_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView4.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox6.Text = DataGridView4.Rows(e.RowIndex).Cells("鞋樣素描圖編號").Value
            TextBox1.Text = DataGridView4.Rows(e.RowIndex).Cells("楦頭編號").Value
            TextBox4.Text = DataGridView4.Rows(e.RowIndex).Cells("模具編號").Value
            TextBox2.Text = DataGridView4.Rows(e.RowIndex).Cells("型體編號").Value
            TextBox3.Text = DataGridView4.Rows(e.RowIndex).Cells("型體名稱").Value
            ComboBox1.Text = DataGridView4.Rows(e.RowIndex).Cells("品牌").Value
            圖片導入()
        End If
    End Sub
    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        PA = InputBox("請輸入要查詢的楦頭編號") : Set_楦頭規格表清單() : PA = ""
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Set_型體表清單()
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ComboBox1.Enabled = True
        Button3.Enabled = True : Button6.Enabled = False : Button2.Enabled = False : Button5.Enabled = False
        ComboBox1下拉表單資料載入()
        TextBox6.Text = "" : TextBox6.Text = "" : TextBox1.Text = "" : TextBox2.Text = "" : TextBox3.Text = "" : PictureBox1.Image = Nothing
        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 : TextBox6.Text = dr("鞋樣素描圖編號").ToString : Else : TextBox6.Text = "SS00000000" : End If
        conn.Close()
        Dim NUM1 As Integer
        NUM1 = Double.Parse(Strings.Right(TextBox6.Text, 8)) + 1
        If NUM1 < 10 Then : TextBox6.Text = "SS" & "0000000" & NUM1
        ElseIf NUM1 > 9 And NUM1 < 100 Then : TextBox6.Text = "SS" & "000000" & NUM1
        ElseIf NUM1 > 99 And NUM1 < 1000 Then : TextBox6.Text = "SS" & "00000" & NUM1
        ElseIf NUM1 > 999 And NUM1 < 10000 Then : TextBox6.Text = "SS" & "0000" & NUM1
        ElseIf NUM1 > 9999 And NUM1 < 100000 Then : TextBox6.Text = "SS" & "000" & NUM1
        ElseIf NUM1 > 99999 And NUM1 < 100000 Then : TextBox6.Text = "SS" & "00" & NUM1
        ElseIf NUM1 > 999999 And NUM1 < 1000000 Then : TextBox6.Text = "SS" & "0" & NUM1
        ElseIf NUM1 > 9999999 Then : TextBox6.Text = "SS" & NUM1
        End If
    End Sub
    Private Sub 圖片導入()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 圖片 FROM 鞋樣素描圖控制表 WHERE 鞋樣素描圖編號 LIKE N'" & TextBox6.Text & "'"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        PictureBox1.Image = Nothing
        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 Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If ComboBox1.Text = "" Or TextBox4.Text = "" Or TextBox6.Text = "" Or TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox5.Text = "" Then
            MsgBox("資料不齊全，請注意每個資料欄位與圖片是否有準備好。")
        Else
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 鞋樣素描圖編號 FROM 鞋樣素描圖控制表 
                    WHERE 楦頭編號 LIKE N'" & TextBox1.Text & "' AND 模具編號 LIKE N'" & TextBox4.Text & "' AND 型體編號 LIKE N'" & TextBox2.Text & "' AND 型體名稱 LIKE N'" & TextBox3.Text & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            If dr.Read() Then
                conn.Close()
                MsgBox("資料重複，無法新增。")
            Else
                conn.Close()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "SELECT 鞋樣素描圖編號 FROM 鞋樣素描圖控制表 WHERE 鞋樣素描圖編號 LIKE N'" & TextBox6.Text & "'"
                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                If dr.Read() Then
                    conn.Close()
                    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 : TextBox6.Text = dr("鞋樣素描圖編號").ToString : Else : TextBox6.Text = "SS00000000" : End If
                    conn.Close()
                    Dim NUM1 As Integer
                    NUM1 = Double.Parse(Strings.Right(TextBox6.Text, 8)) + 1
                    If NUM1 < 10 Then : TextBox6.Text = "SS" & "0000000" & NUM1
                    ElseIf NUM1 > 9 And NUM1 < 100 Then : TextBox6.Text = "SS" & "000000" & NUM1
                    ElseIf NUM1 > 99 And NUM1 < 1000 Then : TextBox6.Text = "SS" & "00000" & NUM1
                    ElseIf NUM1 > 999 And NUM1 < 10000 Then : TextBox6.Text = "SS" & "0000" & NUM1
                    ElseIf NUM1 > 9999 And NUM1 < 100000 Then : TextBox6.Text = "SS" & "000" & NUM1
                    ElseIf NUM1 > 99999 And NUM1 < 100000 Then : TextBox6.Text = "SS" & "00" & NUM1
                    ElseIf NUM1 > 999999 And NUM1 < 1000000 Then : TextBox6.Text = "SS" & "0" & NUM1
                    ElseIf NUM1 > 9999999 Then : TextBox6.Text = "SS" & NUM1
                    End If

                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "INSERT INTO 鞋樣素描圖控制表 (鞋樣素描圖編號, 品牌, 楦頭編號, 模具編號, 型體編號, 型體名稱, 圖片) SELECT N'" & TextBox6.Text & "',N'" & ComboBox1.Text & "',N'" & TextBox1.Text &
                    "',N'" & TextBox4.Text & "',N'" & TextBox2.Text & "',N'" & TextBox3.Text & "', BULKCOLUMN FROM OPENROWSET ( BULK '" & FTP實體位置 & AA(13) & "\" & TextBox5.Text & "', SINGLE_BLOB) AS EMPLOYEEPICTURE"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                Else
                    conn.Close()
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "INSERT INTO 鞋樣素描圖控制表 (鞋樣素描圖編號, 品牌, 楦頭編號, 模具編號, 型體編號, 型體名稱, 圖片) SELECT N'" & TextBox6.Text & "',N'" & ComboBox1.Text & "',N'" & TextBox1.Text &
                   "',N'" & TextBox4.Text & "',N'" & TextBox2.Text & "',N'" & TextBox3.Text & "', BULKCOLUMN FROM OPENROWSET ( BULK '" & FTP實體位置 & AA(13) & "\" & TextBox5.Text & "', SINGLE_BLOB) AS EMPLOYEEPICTURE"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                End If

                '---FTP方式刪除檔案----------------------------------------------------------------------------------------------------------------------------------------------------------------------
                Dim frq As FtpWebRequest, frp As FtpWebResponse, fcr As NetworkCredential
                frq = CType(WebRequest.Create(New Uri(Target1 & TextBox5.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()
                '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                圖片導入() : Set_鞋樣素描清單() : MsgBox("新增完成")
                Button3.Enabled = False : Button6.Enabled = False : Button2.Enabled = True : Button5.Enabled = True : TextBox5.Text = ""
            End If
        End If
    End Sub
    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        ComboBox1.Enabled = True : Button2.Enabled = False : Button5.Enabled = False : Button6.Enabled = True
    End Sub
    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "UPDATE 鞋樣素描圖控制表 SET 品牌 = N'" & ComboBox1.Text & "', 楦頭編號 = N'" & TextBox1.Text & "', 模具編號 = N'" & TextBox4.Text & "', 型體編號 = N'" & TextBox2.Text &
               "', 型體名稱 = N'" & TextBox3.Text & "' WHERE 鞋樣素描圖編號 LIKE N'" & TextBox6.Text & "'"
        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        Set_鞋樣素描清單()
        MsgBox("修改完成")
        Button3.Enabled = False : Button6.Enabled = False : Button2.Enabled = True : Button5.Enabled = True
    End Sub
    Private Sub TextBox5_DragEnter(sender As Object, e As DragEventArgs) Handles TextBox5.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim files As String()
            Try
                files = CType(e.Data.GetData(DataFormats.FileDrop), String())
                TextBox5.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 = TextBox5.Text
        If TextBox5.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 = ""
                        TextBox5.Text = ""
                        i = 9999
                    Else
                        TextBox7.Text = Strings.Left((TextBox7.Text), STR1)
                    End If
                Else
                    TextBox7.Text = Strings.Right((TextBox5.Text), STR2)
                    TextBox5.Text = TextBox7.Text
                    i = 9999
                End If
            Next
        End If
    End Sub
    Private Sub TextBox5_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles TextBox5.DragDrop
        Dim files As Array = e.Data.GetData(DataFormats.FileDrop)
        For Each file As String In files
            TextBox5.AppendText(file + Environment.NewLine)
        Next
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            WebBrowser2.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
    End Sub
    Private Sub Button24_Click(sender As Object, e As EventArgs) Handles Button24.Click
        If TextBox6.Text = "" Or TextBox5.Text = "" Then
            MsgBox("請確定需要修改圖片的模具資料已選擇與圖片資料已準備完畢。")
        Else
            conn.Close()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 鞋樣素描圖控制表 SET 圖片 = BULKCOLUMN FROM OPENROWSET ( BULK '" & FTP實體位置 & AA(13) & "\" & TextBox5.Text & "', SINGLE_BLOB) AS 
                    EMPLOYEEPICTURE WHERE (鞋樣素描圖編號 = '" & TextBox6.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 & TextBox5.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()
            '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
            TextBox5.Text = ""
            圖片導入()
            MsgBox("修改完成")
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        PA = InputBox("請輸入要查詢的關鍵字") : Set_鞋樣素描清單() : PA = ""
    End Sub
End Class