Public Class 業務銷售控制表
    Private Sub 讀取人員資料表()
        Dim ds As New DataSet
        人員_dgv.DataSource = Nothing : ds.Clear()
        人員_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        人員_dgv.ColumnHeadersHeight = 25 : 人員_dgv.AllowUserToAddRows = False
        SQL_考勤明細表_人員()
        da.Fill(ds) : 人員_dgv.DataSource = ds.Tables(0) : conn.Close()
        人員_dgv.Columns(0).FillWeight = 70 : 人員_dgv.Columns(1).FillWeight = 80 : 人員_dgv.Columns(2).FillWeight = 100 : 人員_dgv.Columns(3).Visible = False
        人員_dgv.Columns(4).Visible = False : 人員_dgv.Columns(5).Visible = False : 人員_dgv.Columns(6).Visible = False : 人員_dgv.Columns(7).Visible = False
        人員_dgv.Columns(8).Visible = False : 人員_dgv.Columns(9).Visible = False : 人員_dgv.Columns("到職日").Visible = False
        For i As Integer = 0 To 人員_dgv.Rows.Count - 1
            人員_dgv.Rows(i).Cells("No.").Value = i + 1
        Next
    End Sub
    Private Sub Set_業務()
        Dim ds2 As New DataSet
        業務_dgv.DataSource = Nothing : ds2.Clear()
        業務_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        業務_dgv.ColumnHeadersHeight = 25 : 業務_dgv.AllowUserToAddRows = False
        If 未指定_ch.Checked = True And 未聯繫_ch.Checked = False And 成功_ch.Checked = False And 失誤_ch.Checked = False Then
            SQL2 = " WHERE (銷售人員 IS NULL OR 銷售人員 LIKE '') AND (日期 NOT LIKE N'" & Strings.Format(Today(), "yyyy/MM/dd") & "') "
        ElseIf 未指定_ch.Checked = False And 未聯繫_ch.Checked = True And 成功_ch.Checked = False And 失誤_ch.Checked = False Then
            SQL2 = " WHERE (銷售狀態 IS NULL OR 銷售狀態 LIKE '') "
        ElseIf 未指定_ch.Checked = False And 未聯繫_ch.Checked = False And 成功_ch.Checked = True And 失誤_ch.Checked = False Then
            If gUserName = "*系統管理者" Or gUserName = "黃院長" Or gUserName = "呂總監" Or gUserName = "蔡紋婷" Or CC(28) = True Then
                SQL2 = " WHERE (銷售狀態 LIKE N'成功') "
            Else
                SQL2 = " WHERE (銷售狀態 LIKE N'成功') AND (銷售人員 LIKE N'" & gUserName & "') "
            End If
        ElseIf 未指定_ch.Checked = False And 未聯繫_ch.Checked = False And 成功_ch.Checked = False And 失誤_ch.Checked = True Then
            If gUserName = "*系統管理者" Or gUserName = "黃院長" Or gUserName = "呂總監" Or gUserName = "蔡紋婷" Or CC(28) = True Then
                SQL2 = " WHERE (銷售狀態 LIKE N'失敗') "
            Else
                SQL2 = " WHERE (銷售狀態 LIKE N'失敗') AND (銷售人員 LIKE N'" & gUserName & "') "
            End If
        End If
        SQL_業務控制()
        da.Fill(ds2) : 業務_dgv.DataSource = ds2.Tables(0) : conn.Close() : SQL2 = ""
        業務_dgv.Columns(0).Visible = False : 業務_dgv.Columns(2).FillWeight = 80 : 業務_dgv.Columns(4).FillWeight = 80 : 業務_dgv.Columns("備註").FillWeight = 160
        '適應症選擇()
    End Sub
    Private Sub 治療()
        Dim ds As New DataSet
        治療建議_dgv.DataSource = Nothing : ds.Clear()
        治療建議_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        治療建議_dgv.ColumnHeadersHeight = 25 : 治療建議_dgv.AllowUserToAddRows = False : 治療建議_dgv.RowTemplate.Height = 50
        SQL_業務明細()
        da.Fill(ds) : 治療建議_dgv.DataSource = ds.Tables(0) : conn.Close()
        治療建議_dgv.Columns("症狀").Visible = False : 治療建議_dgv.Columns("備註").Visible = False
        For i As Integer = 0 To 治療建議_dgv.Rows.Count - 1
            If 治療建議_dgv("症狀", i).Value.ToString = "" And 治療建議_dgv("備註", i).Value.ToString = "" Then
            ElseIf 治療建議_dgv("症狀", i).Value.ToString <> "" And 治療建議_dgv("備註", i).Value.ToString = "" Then
                治療建議_dgv("建議治療", i).Value = 治療建議_dgv("症狀", i).Value.ToString & " / " & 治療建議_dgv("建議治療", i).Value.ToString
            ElseIf 治療建議_dgv("症狀", i).Value.ToString = "" And 治療建議_dgv("備註", i).Value.ToString <> "" Then
                治療建議_dgv("建議治療", i).Value = 治療建議_dgv("建議治療", i).Value.ToString & " => " & 治療建議_dgv("備註", i).Value.ToString
            ElseIf 治療建議_dgv("症狀", i).Value.ToString <> "" And 治療建議_dgv("備註", i).Value.ToString <> "" Then
                治療建議_dgv("建議治療", i).Value = 治療建議_dgv("症狀", i).Value.ToString & " / " & 治療建議_dgv("建議治療", i).Value.ToString & " => " & 治療建議_dgv("備註", i).Value.ToString
            End If
        Next

        Dim ds1 As New DataSet
        保養品_dgv.DataSource = Nothing : ds1.Clear()
        保養品_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        保養品_dgv.ColumnHeadersHeight = 25 : 保養品_dgv.AllowUserToAddRows = False : 保養品_dgv.RowTemplate.Height = 50
        SQL_保養品明細()
        da.Fill(ds1) : 保養品_dgv.DataSource = ds1.Tables(0) : conn.Close()
        保養品_dgv.Columns("品牌").Visible = False : 保養品_dgv.Columns("備註").Visible = False
        For i As Integer = 0 To 保養品_dgv.Rows.Count - 1
            If 保養品_dgv("品牌", i).Value.ToString = "" And 保養品_dgv("備註", i).Value.ToString = "" Then
            ElseIf 保養品_dgv("品牌", i).Value.ToString <> "" And 保養品_dgv("備註", i).Value.ToString = "" Then
                保養品_dgv("建議產品", i).Value = 保養品_dgv("建議產品", i).Value.ToString
            ElseIf 保養品_dgv("品牌", i).Value.ToString = "" And 保養品_dgv("備註", i).Value.ToString <> "" Then
                保養品_dgv("建議產品", i).Value = 保養品_dgv("建議產品", i).Value.ToString & " => " & 保養品_dgv("備註", i).Value.ToString
            ElseIf 保養品_dgv("品牌", i).Value.ToString <> "" And 保養品_dgv("備註", i).Value.ToString <> "" Then
                保養品_dgv("建議產品", i).Value = 保養品_dgv("建議產品", i).Value.ToString & " => " & 保養品_dgv("備註", i).Value.ToString
            End If
        Next
    End Sub
    Private Sub 業務銷售控制表_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If gUserName = "*系統管理者" Or gUserName = "黃院長" Or gUserName = "呂總監" Or gUserName = "蔡紋婷" Then
            未指定_ch.Visible = True : 未聯繫_ch.Visible = True : 成功_ch.Visible = True : 失誤_ch.Visible = True
        ElseIf CC(28) = True Then
            未指定_ch.Visible = True : 未聯繫_ch.Visible = False : 成功_ch.Visible = True : 失誤_ch.Visible = True
            未指定_ch.Checked = True : 未聯繫_ch.Checked = False : 成功_ch.Checked = False : 失誤_ch.Checked = False
        ElseIf CC(28) = False Then
            未指定_ch.Visible = False : 未聯繫_ch.Visible = False : 成功_ch.Visible = True : 失誤_ch.Visible = True
            未指定_ch.Checked = False : 未聯繫_ch.Checked = False : 成功_ch.Checked = True : 失誤_ch.Checked = False
        End If
    End Sub
    Private Sub 業務銷售控制表_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        讀取人員資料表() : Set_業務()
    End Sub
    Private Sub 人員_dgv_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles 人員_dgv.CellClick
        If e.RowIndex = -1 Then : Else : 銷售人員_tb.Text = 人員_dgv("姓名", e.RowIndex).Value.ToString : End If
    End Sub
    Private Sub 業務_dgv_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles 業務_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            看診日期_dtp.Text = 業務_dgv("日期", 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
            備註_tb.Text = 業務_dgv("備註", e.RowIndex).Value.ToString : 流水號_bt.Text = 業務_dgv("流水號", e.RowIndex).Value.ToString
            治療()
        End If
    End Sub
    Private Sub 業務_dgv_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 業務_dgv.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = 業務_dgv.Rows.Count - 1 Then : Exit Sub : Else
            If Strings.Left(業務_dgv("日期", e.RowIndex).Value.ToString, 10) <> Strings.Left(業務_dgv("日期", e.RowIndex + 1).Value.ToString, 10) Then
                Dim startX As Integer = IIf(業務_dgv.RowHeadersVisible, 業務_dgv.RowHeadersWidth, 0) : Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + 業務_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 業務_dgv.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY) : Exit Sub
            End If
        End If
    End Sub
    Private Sub 未指定_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 未指定_ch.Click
        未指定_ch.Checked = True : 未聯繫_ch.Checked = False : 成功_ch.Checked = False : 失誤_ch.Checked = False : Set_業務()
    End Sub
    Private Sub 未聯繫_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 未聯繫_ch.Click
        未指定_ch.Checked = False : 未聯繫_ch.Checked = True : 成功_ch.Checked = False : 失誤_ch.Checked = False : Set_業務()
    End Sub
    Private Sub 成功_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 成功_ch.Click
        未指定_ch.Checked = False : 未聯繫_ch.Checked = False : 成功_ch.Checked = True : 失誤_ch.Checked = False : Set_業務()
    End Sub
    Private Sub 失誤_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 失誤_ch.Click
        未指定_ch.Checked = False : 未聯繫_ch.Checked = False : 成功_ch.Checked = False : 失誤_ch.Checked = True : Set_業務()
    End Sub
    Private Sub 指定銷售_bt_Click(sender As Object, e As EventArgs) Handles 指定銷售_bt.Click
        If 病歷號_tb.Text = "" Then : MsgBox("請先選擇銷售資料!!") : Else
            If 銷售人員_tb.Text = "" Then : MsgBox("請先選擇銷售人員!!") : Else
                SQL_業務控制指定銷售() : Set_業務() : MsgBox("指定業務人員完成!!")
            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 銷售人員_tb.Text = "" Then : MsgBox("請先選擇銷售人員!!") : Else
                PA1 = "成功" : PA2 = Strings.Format(Today(), "yyyy/MM/dd") : SQL_業務控制備註存檔() : SQL_業務控制指定銷售完成() : Set_業務() : MsgBox("單據記錄成功!!")
            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 銷售人員_tb.Text = "" Then : MsgBox("請先選擇銷售人員!!") : Else
                PA1 = "失敗" : PA2 = "" : SQL_業務控制指定銷售完成() : SQL_業務控制備註存檔() : Set_業務() : MsgBox("單據記錄成功!!")
            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 銷售人員_tb.Text = "" Then : MsgBox("請先選擇銷售人員!!") : Else
                SQL_業務控制備註存檔() : Set_業務() : MsgBox("單據記錄成功!!")
            End If
        End If
    End Sub
End Class