Imports System.Windows.Forms
Public Class 客戶資料表
    ReadOnly ds1 As New DataSet
    Dim 資料新增判斷 As Boolean : Dim 第一次開啟判斷 As Boolean = False : Dim N1 As Integer
    Dim EDR As Integer = 0 : Dim ESTR As String : Dim EDR1 As Integer = 0 : Dim ESTR1 As String
    Dim 首次開啟 As Boolean = False
    Private Sub Set_清單()
        客戶_dgv.DataSource = Nothing : ds1.Clear() : 客戶_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        客戶_dgv.ColumnHeadersHeight = 25
        客戶_dgv.AllowUserToAddRows = False
        客戶_dgv.RowTemplate.Height = 30
        SQL_客戶資料表_清單()
        da.Fill(ds1) : 客戶_dgv.DataSource = ds1.Tables(0) : conn.Close()

        客戶_dgv.Columns(0).Visible = False : 客戶_dgv.Columns(1).FillWeight = 50 : 客戶_dgv.Columns(2).FillWeight = 50 : 客戶_dgv.Columns(3).FillWeight = 100
        客戶_dgv.Columns(4).FillWeight = 80 : 客戶_dgv.Columns(5).FillWeight = 80 : 客戶_dgv.Columns(6).FillWeight = 150 : 客戶_dgv.Columns(7).FillWeight = 80
        客戶_dgv.Columns(8).FillWeight = 150

        資料筆數_tb.Text = 客戶_dgv.Rows.Count : N1 = 1
        For i As Integer = 0 To 客戶_dgv.Rows.Count - 1
            客戶_dgv.Rows(i).Cells("項次").Value = N1
            N1 = N1 + 1
        Next i
    End Sub
    Private Sub 客戶資料表_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        業務_cb.Text = 業務號碼
        If 客戶資料強制對話框模式 = False Then
            Me.MdiParent = YD_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
            帶入銷售_bt.Visible = False
        Else
            帶入銷售_bt.Visible = True : 查詢_tb.Text = PA30 : PA = PA30
            業務_cb.Text = 銷售單申請.業務_cb.Text
        End If

        流水號_tb.Visible = False : 編號_tb.Enabled = False : 第一次開啟判斷 = True '--------------------控件隱藏、操作許可設定與其他基本顯示設定
        狀態_lb.Text = "預覽
狀態"
        If Val(登入人級別) <= 2 Then '--------------------刪除資料權限
            刪除ToolStripMenuItem.Enabled = True : 刪除_bt.Enabled = True
        Else
            刪除ToolStripMenuItem.Enabled = False : 刪除_bt.Enabled = False
        End If
        首次開啟 = False : Set_清單() : 首次開啟 = True '--------------------資料讀取
    End Sub
    Private Sub 客戶資料表_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
        If 第一次開啟判斷 = True Then
            Set_清單()
        End If
    End Sub
    Private Sub 客戶資料表_Closed(sender As Object, e As EventArgs) Handles MyBase.Closed
        If 資料新增判斷 = True Then '--------------------如果再關閉介面時，資料新增判斷是開啟的，就無條件刪除控件上的那筆資料
            SQL_客戶資料表_刪除() '--------------------資料刪除
        End If
        第一次開啟判斷 = False
    End Sub
    Private Sub 客戶_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 客戶_dgv.CellEnter
        If 首次開啟 = True Then
            If e.RowIndex = -1 Then : Else
                流水號_tb.Text = 客戶_dgv("流水號", e.RowIndex).Value.ToString : 編號_tb.Text = 客戶_dgv("編號", e.RowIndex).Value.ToString
                電話1_tb.Text = 客戶_dgv("電話1", e.RowIndex).Value : 客戶_tb.Text = 客戶_dgv("客戶", e.RowIndex).Value.ToString
                電話2_tb.Text = 客戶_dgv("電話2", e.RowIndex).Value.ToString : 住址_tb.Text = 客戶_dgv("地址", e.RowIndex).Value.ToString
                公司_tb.Text = 客戶_dgv("公司名稱", e.RowIndex).Value.ToString : 統編_tb.Text = 客戶_dgv("統編", e.RowIndex).Value.ToString
            End If
        End If
    End Sub
    Private Sub 業務_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 業務_cb.SelectedIndexChanged
        Set_清單()
    End Sub
    Private Sub 查詢_tb_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles 查詢_tb.KeyPress
        If e.KeyChar = Chr(13) Then
            PA = 查詢_tb.Text : Set_清單()
        End If
    End Sub
    Private Sub 新增準備_bt_Click(sender As Object, e As EventArgs) Handles 新增準備_bt.Click
        If 業務_cb.Text = "" Then '--------------------設定條件讓業務控件不為空
            MsgBox("沒有選擇業務編號")
        Else
            流水號_tb.Text = "" : 編號_tb.Text = "" : 客戶_tb.Text = "" : 住址_tb.Text = "" : 電話1_tb.Text = "" : 電話2_tb.Text = "" '--------------------控件清空
            資料新增判斷 = True '--------------------目前的編輯狀態
            狀態_lb.Text = "編輯
狀態"
            客戶_dgv.Enabled = False : 查詢_tb.Enabled = False '--------------------關閉DGV與關鍵字查詢控件的點選控制功能

            SQL_客戶資料表_流水號_最後一筆資料() '--------------------自動取得新流水號
            If dr.Read() Then : EDR = Double.Parse(Strings.Right(dr("流水號").ToString, 9)) : Else : EDR = 0 : End If
            conn.Close() : EDR += 1
            If EDR < 10 Then : ESTR = "CL" & "00000000" & EDR
            ElseIf EDR > 9 And EDR < 100 Then : ESTR = "CL" & "0000000" & EDR
            ElseIf EDR > 99 And EDR < 1000 Then : ESTR = "CL" & "000000" & EDR
            ElseIf EDR > 999 And EDR < 10000 Then : ESTR = "CL" & "00000" & EDR
            ElseIf EDR > 9999 And EDR < 100000 Then : ESTR = "CL" & "0000" & EDR
            ElseIf EDR > 99999 And EDR < 100000 Then : ESTR = "CL" & "000" & EDR
            ElseIf EDR > 999999 And EDR < 1000000 Then : ESTR = "CL" & "00" & EDR
            ElseIf EDR > 9999999 And EDR < 10000000 Then : ESTR = "CL" & "0" & EDR
            ElseIf EDR > 99999999 Then : ESTR = "CL" & EDR
            End If : 流水號_tb.Text = ESTR : PA16 = ESTR

            PA18 = 業務_cb.Text
            SQL_客戶資料表_編號_最後一筆資料()  '--------------------自動取得新編號
            If dr.Read() Then : EDR1 = Double.Parse(Strings.Right(dr("舊編").ToString, 4)) : Else : EDR1 = 0 : End If
            conn.Close() : EDR1 += 1
            If EDR1 < 10 Then : ESTR1 = 業務_cb.Text & "000" & EDR1
            ElseIf EDR1 > 9 And EDR1 < 100 Then : ESTR1 = 業務_cb.Text & "00" & EDR1
            ElseIf EDR1 > 99 And EDR1 < 1000 Then : ESTR1 = 業務_cb.Text & "0" & EDR1
            ElseIf EDR1 > 999 And EDR1 < 10000 Then : ESTR1 = 業務_cb.Text & EDR1
            End If : 編號_tb.Text = ESTR1 : PA17 = ESTR1

            SQL_客戶資料表_新增() '--------------------人事資料表中寫入一筆資料

            If 帶入銷售_bt.Visible = True Then
                客戶_tb.Text = PA30 : 住址_tb.Text = PA29 : 電話1_tb.Text = PA28 : 電話2_tb.Text = PA27 : 公司_tb.Text = PA26 : 統編_tb.Text = PA25
            End If
        End If
    End Sub
    Private Sub 存檔_bt_Click(sender As Object, e As EventArgs) Handles 存檔_bt.Click
        If 客戶_tb.Text = "" Then '--------------------設定條件讓客戶控件不為空
            MsgBox("是客戶就一定有名稱，請填入資料。")
        Else
            If 電話1_tb.Text = "" Then '--------------------設定條件讓電話1控件不為空
                MsgBox("最少要留下一支電話，不然怎麼追貨款。")
            Else
                If 住址_tb.Text = "" Then '--------------------設定條件讓住址控件不為空
                    MsgBox("沒有留地址是無法送貨的喔。")
                Else
                    資料新增判斷 = False '--------------------目前的編輯狀態
                    狀態_lb.Text = "預覽
狀態"
                    客戶_dgv.Enabled = True : 查詢_tb.Enabled = True '--------------------開啟DGV與關鍵字查詢控件的點選控制功能

                    PA14 = 客戶_tb.Text : PA13 = 電話1_tb.Text : PA12 = 電話2_tb.Text : PA11 = 住址_tb.Text
                    PA10 = 公司_tb.Text : PA9 = 統編_tb.Text : PA8 = 流水號_tb.Text
                    SQL_客戶資料表_修改() '--------------------資料修改
                    Set_清單() '--------------------資料修改後重新讀取清單
                End If
            End If
        End If
    End Sub
    Private Sub 刪除_bt_Click(sender As Object, e As EventArgs) Handles 刪除_bt.Click
        If 流水號_tb.Text = "" Then '--------------------設定條件讓流水號控件不為空
            MsgBox("沒有選擇要刪除的資料")
        Else
            Dim aa1 As MsgBoxResult = MsgBox("確定要執行刪除指令嗎?", MsgBoxStyle.OkCancel)
            If aa1 = MsgBoxResult.Ok Then
                SQL_客戶資料表_刪除() '--------------------資料刪除
                Set_清單() '--------------------資料修改後重新讀取清單
            End If
        End If
    End Sub
    Private Sub 帶入銷售_bt_Click(sender As Object, e As EventArgs) Handles 帶入銷售_bt.Click
        PA15 = 編號_tb.Text
        Me.Close()
    End Sub
    '-------------------滑鼠右鍵-------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Private Sub 重新整理ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 重新整理ToolStripMenuItem.Click
        Set_清單()
    End Sub
    Private Sub 新增準備ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 新增準備ToolStripMenuItem.Click
        新增準備_bt.PerformClick()
    End Sub
    Private Sub 存檔ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 存檔ToolStripMenuItem.Click
        存檔_bt.PerformClick()
    End Sub
    Private Sub 刪除ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 刪除ToolStripMenuItem.Click
        刪除_bt.PerformClick()
    End Sub
    Private Sub 字體_NUD_ValueChanged(sender As Object, e As EventArgs) Handles 字體_NUD.ValueChanged
        客戶_dgv.RowsDefaultCellStyle.Font = New Font("微軟正黑體", 字體_NUD.Value)
    End Sub
End Class