Option Strict Off
Imports System.Data.SqlClient
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 : 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() : Set_grid1()
    End Sub
    Private Sub Set_清單1()
        Set_DGV1載入前設定()
        SQL1 = "SELECT 客戶簡稱 FROM 客戶控制表 ORDER BY 客戶簡稱"
        Set_DGV1載入後設定()
    End Sub
    Private Sub Set_grid1()
        DataGridView1.Columns(0).Width = 185
    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() : Set_grid2()
    End Sub
    Private Sub Set_grid2()
        DataGridView2.Columns(0).Width = 185
    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() : Set_grid3()
    End Sub
    Private Sub Set_grid3()
        DataGridView3.Columns(0).Width = 285
    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() : Set_grid4()
    End Sub
    Private Sub Set_清單2()
        Set_DGV4載入前設定()
        If PA = "" Then
            SQL1 = "SELECT 顏色 FROM 顏色清單 ORDER BY 顏色"
        Else
            SQL1 = "SELECT 顏色 FROM 顏色清單 WHERE 顏色 LIKE N'%" & PA & "%' ORDER BY 顏色"
        End If
        Set_DGV4載入後設定()
    End Sub
    Private Sub Set_grid4()
        DataGridView4.Columns(0).Width = 285
    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_清單1() : Set_清單2()
        TextBox1.Visible = False : TextBox7.Visible = False
        TextBox8.Enabled = False : TextBox3.Enabled = False : TextBox5.Enabled = False : TextBox6.Enabled = False
        Target1 = Target & AA(1) & "/"
        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
            TextBox8.Text = DataGridView1(0, e.RowIndex).Value.ToString
            Set_DGV2載入前設定()
            SQL1 = "SELECT 型體號碼 FROM 型體控制表 WHERE (客戶簡稱 LIKE N'" & DataGridView1.Rows(e.RowIndex).Cells("客戶簡稱").Value & "') GROUP BY 型體號碼 ORDER BY 型體號碼"
            Set_DGV2載入後設定()
        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
            TextBox3.Text = DataGridView2(0, e.RowIndex).Value.ToString
            Set_DGV3載入前設定()
            SQL1 = "SELECT 型體名稱 FROM 型體控制表 WHERE (型體號碼 LIKE N'" & DataGridView2.Rows(e.RowIndex).Cells("型體號碼").Value & "') GROUP BY 型體名稱 ORDER BY 型體名稱"
            Set_DGV3載入後設定()
        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
            TextBox5.Text = DataGridView3(0, e.RowIndex).Value.ToString
        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
            If ComboBox2.Text <> "設計圖" Then
                TextBox6.Text = DataGridView4(0, e.RowIndex).Value.ToString
            End If
        End If
    End Sub
    Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
        If ComboBox2.Text = "設計圖" Then : TextBox6.Text = "" : End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        PA = InputBox("請輸入要查詢的關鍵字") : Set_清單2() : PA = ""
    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
        TextBox7.Text = TextBox4.Text
        If TextBox4.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 = ""
                        TextBox4.Text = ""
                        i = 9999
                    Else
                        TextBox7.Text = Strings.Left((TextBox7.Text), STR1)
                    End If
                Else
                    TextBox7.Text = Strings.Right((TextBox4.Text), STR2)
                    TextBox4.Text = TextBox7.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 Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            WebBrowser2.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
    End Sub
    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        If TextBox4.Text = "" Or ComboBox2.Text = "" Or TextBox3.Text = "" Or TextBox8.Text = "" Then
            MsgBox("檔案名稱或資料不可空白")
        Else
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 圖片編號 FROM 鞋型圖片資料庫 " &
                   "WHERE 客戶 LIKE N'" & TextBox8.Text & "' AND 形體號 LIKE N'" & TextBox3.Text & "' AND 形體名 LIKE N'" & TextBox5.Text &
                   "' AND 顏色 LIKE N'" & TextBox6.Text & "' AND 類別 LIKE N'" & ComboBox2.Text & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            If dr.Read() Then
                MsgBox("資料重複，請從新輸入或檢查")
                conn.Close() : dr.Close()
            Else
                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 : TextBox1.Text = dr("圖片編號").ToString : Else : TextBox1.Text = "PN00000000" : End If
                conn.Close()
                Dim NUM1 As Integer
                NUM1 = Double.Parse(Strings.Right(TextBox1.Text, 8)) + 1
                If NUM1 < 10 Then : TextBox1.Text = "PN" & "0000000" & NUM1
                ElseIf NUM1 > 9 And NUM1 < 100 Then : TextBox1.Text = "PN" & "000000" & NUM1
                ElseIf NUM1 > 99 And NUM1 < 1000 Then : TextBox1.Text = "PN" & "00000" & NUM1
                ElseIf NUM1 > 999 And NUM1 < 10000 Then : TextBox1.Text = "PN" & "0000" & NUM1
                ElseIf NUM1 > 9999 And NUM1 < 100000 Then : TextBox1.Text = "PN" & "000" & NUM1
                ElseIf NUM1 > 99999 And NUM1 < 100000 Then : TextBox1.Text = "PN" & "00" & NUM1
                ElseIf NUM1 > 999999 And NUM1 < 1000000 Then : TextBox1.Text = "PN" & "0" & NUM1
                ElseIf NUM1 > 9999999 Then : TextBox1.Text = "PN" & NUM1
                End If
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "INSERT INTO 鞋型圖片資料庫 (客戶, 形體號, 形體名, 顏色, 類別, 入圖者, 圖片編號, 圖片) SELECT N'" & TextBox8.Text & "',N'" & TextBox3.Text & "',N'" & TextBox5.Text &
                       "',N'" & TextBox6.Text & "',N'" & ComboBox2.Text & "',N'" & gUserName & "',N'" & TextBox1.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()
                '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                TextBox6.Text = "" : TextBox4.Text = ""
                MsgBox("新增完成")
            End If
        End If
    End Sub
End Class