Public Class 適應症選擇
    Private Sub Set_適應症設定()
        Dim ds2 As New DataSet
        適應症_dgv.DataSource = Nothing : ds2.Clear()
        適應症_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        適應症_dgv.ColumnHeadersHeight = 25 : 適應症_dgv.AllowUserToAddRows = False
        SQL_適應症設定()
        da.Fill(ds2) : 適應症_dgv.DataSource = ds2.Tables(0) : conn.Close()
        適應症選擇()
        For i As Integer = 0 To 適應症_dgv.Rows.Count - 1 : 適應症_dgv("備註", i).Value = "" : Next

        Dim ds3 As New DataSet
        保養品_dgv.DataSource = Nothing : ds3.Clear()
        保養品_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        保養品_dgv.ColumnHeadersHeight = 25 : 保養品_dgv.AllowUserToAddRows = False
        SQL_保養品設定()
        da.Fill(ds3) : 保養品_dgv.DataSource = ds3.Tables(0) : conn.Close()
        保養品選擇()
        For i As Integer = 0 To 保養品_dgv.Rows.Count - 1 : 保養品_dgv("備註", i).Value = "" : Next
    End Sub
    Private Sub SQL_下拉清單載入1()
        SQL_適應症主清單1() : 病症_cb.Items.Clear() : While (dr.Read()) : 病症_cb.Items.Add(dr("適應症")) : End While : conn.Close()
    End Sub
    Private Sub SQL_下拉清單載入2()
        SQL_適應症主清單2() : 療程_cb.Items.Clear() : While (dr.Read()) : 療程_cb.Items.Add(dr("治療建議")) : End While : conn.Close()
    End Sub
    Private Sub SQL_下拉清單載入3()
        SQL_保養品主清單1() : 品牌_cb.Items.Clear() : While (dr.Read()) : 品牌_cb.Items.Add(dr("品牌")) : End While : conn.Close()
    End Sub
    Private Sub SQL_下拉清單載入4()
        SQL_保養品主清單2() : 保養品_cb.Items.Clear() : While (dr.Read()) : 保養品_cb.Items.Add(dr("產品")) : End While : conn.Close()
    End Sub
    Private Sub 適應症選擇_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        適應症_dgv.SendToBack() : 保養品_dgv.SendToBack()
        Set_適應症設定() : SQL_下拉清單載入1() : SQL_下拉清單載入3()
    End Sub
    Private Sub 適應症選擇_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        醫生_tb.Text = PA
    End Sub
    Private Sub 適應症選擇_Closed(sender As Object, e As EventArgs) Handles MyBase.Closed
        LoginForm1.Close()
    End Sub
    Private Sub 適應症選擇()
        TreeView1.Nodes.Clear() : Dim 主項數量 As Integer = -1 : Dim 症狀 As String = ""
        For I As Integer = 0 To 適應症_dgv.Rows.Count - 1
            If 症狀 <> 適應症_dgv("適應症", I).Value.ToString Then
                TreeView1.Nodes.Add(適應症_dgv("適應症", I).Value.ToString) : 症狀 = 適應症_dgv("適應症", I).Value.ToString : 主項數量 += 1
            End If
            If 症狀 = 適應症_dgv("適應症", I).Value.ToString Then
                TreeView1.Nodes(主項數量).Nodes.Add(適應症_dgv("治療建議", I).Value.ToString)
            End If
        Next
    End Sub
    Private Sub 保養品選擇()
        TreeView2.Nodes.Clear() : Dim 主項數量 As Integer = -1 : Dim 症狀 As String = ""
        For I As Integer = 0 To 保養品_dgv.Rows.Count - 1
            If 症狀 <> 保養品_dgv("品牌", I).Value.ToString Then
                TreeView2.Nodes.Add(保養品_dgv("品牌", I).Value.ToString) : 症狀 = 保養品_dgv("品牌", I).Value.ToString : 主項數量 += 1
            End If
            If 症狀 = 保養品_dgv("品牌", I).Value.ToString Then
                TreeView2.Nodes(主項數量).Nodes.Add(保養品_dgv("產品", I).Value.ToString)
            End If
        Next
    End Sub
    Private Sub TreeView1_NodeMouseClick(sender As Object, e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick  ' 判斷是否點擊在 CheckBox 的範圍內
        If e.Node.Bounds.Contains(e.Location) Then                                                                             ' 判斷是否為第二層節點
            If e.Node.Parent IsNot Nothing Then
                Dim CV As String = e.Node.Text
                Dim index As Integer = CV.IndexOf(" => ") : Dim resultS As String = "" : Dim 有備註 As Boolean
                If index <> -1 Then
                    Dim result As String = CV.Substring(0, index) : resultS = result : 有備註 = True
                Else
                    resultS = e.Node.Text : 有備註 = False
                End If
                病症_cb.Text = e.Node.Parent.Text : 症狀_lb.Text = "* " & e.Node.Parent.Text : 治療建議_lb.Text = "--> " & resultS
                If 症狀_lb.Text = "* " Or 治療建議_lb.Text = "--> " Then : Else
                    For i As Integer = 0 To 適應症_dgv.Rows.Count - 1
                        If Strings.Mid(症狀_lb.Text, 3, 100) = 適應症_dgv("適應症", i).Value.ToString And Strings.Mid(治療建議_lb.Text, 5, 100) = 適應症_dgv("治療建議", i).Value.ToString Then
                            備註_tb.Text = 適應症_dgv("備註", i).Value.ToString
                            If 有備註 = True Then
                                e.Node.Text = resultS & " => " & 備註_tb.Text
                            End If : Exit For
                        End If
                    Next
                End If
                If e.Node.Checked = True Then : Else : e.Node.Checked = Not e.Node.Checked : End If
            Else : 症狀_lb.Text = "* " : 治療建議_lb.Text = "--> " : 備註_tb.Text = "" : 病症_cb.Text = "" : End If                  ' 切換 CheckBox 的勾選狀態
        Else                                                                                                                   ' 如果是第一層節點，判斷是否已經展開
            If e.Node.IsExpanded Then : 病症_cb.Text = e.Node.Text : 療程_cb.Text = "" : e.Node.Expand()                       ' 如果已經展開，則收合
            Else : e.Node.Collapse() : End If                                                                                  ' 如果未展開，則展開
        End If
    End Sub
    Dim XC As Integer
    Private Sub CheckChildNodes1(parentNode As TreeNode)
        For Each childNode As TreeNode In parentNode.Nodes
            If childNode.Checked Then
                Dim CV As String = childNode.Text
                Dim index As Integer = CV.IndexOf(" => ") : Dim resultS As String = ""
                If index <> -1 Then
                    Dim result As String = CV.Substring(0, index) : resultS = result
                Else
                    resultS = childNode.Text
                End If
                PA1 = resultS : PA2 = 適應症_dgv("適應症", XC).Value.ToString : PA3 = 適應症_dgv("備註", XC).Value.ToString : SQL_銷售明細新增() : XC += 1
            Else : XC += 1 : End If : If childNode.Nodes.Count > 0 Then : CheckChildNodes1(childNode) : End If
        Next
    End Sub

    Private Sub TreeView2_NodeMouseClick(sender As Object, e As TreeNodeMouseClickEventArgs) Handles TreeView2.NodeMouseClick  ' 判斷是否點擊在 CheckBox 的範圍內
        If e.Node.Bounds.Contains(e.Location) Then                                                                             ' 判斷是否為第二層節點
            If e.Node.Parent IsNot Nothing Then
                Dim CV As String = e.Node.Text
                Dim index As Integer = CV.IndexOf(" => ") : Dim resultS As String = "" : Dim 有備註 As Boolean
                If index <> -1 Then
                    Dim result As String = CV.Substring(0, index) : resultS = result : 有備註 = True
                Else
                    resultS = e.Node.Text : 有備註 = False
                End If
                品牌_cb.Text = e.Node.Parent.Text : 品牌_lb.Text = "* " & e.Node.Parent.Text : 保養品_lb.Text = "--> " & resultS
                If 品牌_lb.Text = "* " Or 保養品_lb.Text = "--> " Then : Else
                    For i As Integer = 0 To 保養品_dgv.Rows.Count - 1
                        If Strings.Mid(品牌_lb.Text, 3, 100) = 保養品_dgv("品牌", i).Value.ToString And Strings.Mid(保養品_lb.Text, 5, 100) = 保養品_dgv("產品", i).Value.ToString Then
                            備註1_tb.Text = 保養品_dgv("備註", i).Value.ToString
                            If 有備註 = True Then
                                e.Node.Text = resultS & " => " & 備註1_tb.Text
                            End If : Exit For
                        End If
                    Next
                End If
                If e.Node.Checked = True Then : Else : e.Node.Checked = Not e.Node.Checked : End If
            Else : 品牌_lb.Text = "* " : 保養品_lb.Text = "--> " : 備註1_tb.Text = "" : 品牌_cb.Text = "" : End If                ' 切換 CheckBox 的勾選狀態
        Else                                                                                                                   ' 如果是第一層節點，判斷是否已經展開
            If e.Node.IsExpanded Then : 品牌_cb.Text = e.Node.Text : 療程_cb.Text = "" : e.Node.Expand()                       ' 如果已經展開，則收合
            Else : e.Node.Collapse() : End If                                                                                  ' 如果未展開，則展開
        End If
    End Sub
    Dim XD As Integer
    Private Sub CheckChildNodes2(parentNode As TreeNode)
        For Each childNode As TreeNode In parentNode.Nodes
            If childNode.Checked Then
                Dim CV As String = childNode.Text
                Dim index As Integer = CV.IndexOf(" => ") : Dim resultS As String = ""
                If index <> -1 Then
                    Dim result As String = CV.Substring(0, index) : resultS = result
                Else
                    resultS = childNode.Text
                End If
                PA1 = resultS : PA2 = 保養品_dgv("品牌", XD).Value.ToString : PA3 = 保養品_dgv("備註", XD).Value.ToString : SQL_銷售保養品明細新增() : XD += 1
            Else : XD += 1 : End If : If childNode.Nodes.Count > 0 Then : CheckChildNodes2(childNode) : End If
        Next
    End Sub
    Private Sub 病症_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 病症_cb.SelectedIndexChanged
        療程_cb.Text = "" : SQL_下拉清單載入2()
    End Sub
    Private Sub 品牌_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 品牌_cb.SelectedIndexChanged
        保養品_cb.Text = "" : SQL_下拉清單載入4()
    End Sub
    Private Sub 備註_tb_TextChanged(sender As Object, e As EventArgs) Handles 備註_tb.TextChanged
        If 症狀_lb.Text = "* " Or 治療建議_lb.Text = "--> " Then : Else
            For i As Integer = 0 To 適應症_dgv.Rows.Count - 1
                If Strings.Mid(症狀_lb.Text, 3, 100) = 適應症_dgv("適應症", i).Value.ToString And Strings.Mid(治療建議_lb.Text, 5, 100) = 適應症_dgv("治療建議", i).Value.ToString Then
                    適應症_dgv("備註", i).Value = 備註_tb.Text : Exit For
                End If
            Next
            For i As Integer = 0 To 適應症_dgv.Rows.Count - 1
                If 適應症_dgv("備註", i).Value.ToString() = "" Then : Else
                    Dim symptom As String = 適應症_dgv("適應症", i).Value.ToString() : Dim treatment As String = 適應症_dgv("治療建議", i).Value.ToString()
                    Dim note As String = 適應症_dgv("備註", i).Value.ToString() : Dim CV As String = treatment : Dim index As Integer = CV.IndexOf(" => ") : Dim resultS As String = ""
                    If index <> -1 Then : Dim result As String = CV.Substring(0, index) : resultS = result : Else : resultS = treatment : End If
                    For Each node As TreeNode In TreeView1.Nodes
                        If node.Text = symptom Then
                            For Each childNode As TreeNode In node.Nodes
                                If childNode.Text.StartsWith(resultS) Then
                                    If note = "" Then : childNode.Text = resultS : Else : childNode.Text = resultS & " => " & note : Exit For : End If
                                End If
                            Next
                        End If
                    Next
                End If
            Next
        End If
    End Sub
    Private Sub 備註1_tb_TextChanged(sender As Object, e As EventArgs) Handles 備註1_tb.TextChanged
        If 品牌_lb.Text = "* " Or 保養品_lb.Text = "--> " Then : Else
            For i As Integer = 0 To 保養品_dgv.Rows.Count - 1
                If Strings.Mid(品牌_lb.Text, 3, 100) = 保養品_dgv("品牌", i).Value.ToString And Strings.Mid(保養品_lb.Text, 5, 100) = 保養品_dgv("產品", i).Value.ToString Then
                    保養品_dgv("備註", i).Value = 備註1_tb.Text : Exit For
                End If
            Next
            For i As Integer = 0 To 保養品_dgv.Rows.Count - 1
                If 保養品_dgv("備註", i).Value.ToString() = "" Then : Else
                    Dim symptom As String = 保養品_dgv("品牌", i).Value.ToString() : Dim treatment As String = 保養品_dgv("產品", i).Value.ToString()
                    Dim note As String = 保養品_dgv("備註", i).Value.ToString() : Dim CV As String = treatment : Dim index As Integer = CV.IndexOf(" => ") : Dim resultS As String = ""
                    If index <> -1 Then : Dim result As String = CV.Substring(0, index) : resultS = result : Else : resultS = treatment : End If
                    For Each node As TreeNode In TreeView2.Nodes
                        If node.Text = symptom Then
                            For Each childNode As TreeNode In node.Nodes
                                If childNode.Text.StartsWith(resultS) Then
                                    If note = "" Then : childNode.Text = resultS : Else : childNode.Text = resultS & " => " & note : Exit For : End If
                                End If
                            Next
                        End If
                    Next
                End If
            Next
        End If
    End Sub
    Private Sub 確認_bt_Click(sender As Object, e As EventArgs) Handles 確認_bt.Click
        If 病歷號_tb.Text = "" Then : MsgBox("病歷號 沒有填寫!!") : Else
            Dim 流水號 As String = "" : Dim 流水 As Integer : SQL_查詢銷售()
            If dr.Read Then : PA = dr("流水號").ToString : SQL_銷售明細刪除() : XC = 0
                For Each parentNode As TreeNode In TreeView1.Nodes : CheckChildNodes1(parentNode) : Next
            Else
                SQL_銷售流水號()
                If dr.Read() Then : 流水 = Double.Parse(Strings.Right(dr("流水號").ToString, 8)) : Else : 流水 = 0 : End If : conn.Close() : 流水 += 1
                If 流水 < 10 Then : 流水號 = "PO" & "0000000" & 流水
                ElseIf 流水 > 9 And 流水 < 100 Then : 流水號 = "PO" & "000000" & 流水
                ElseIf 流水 > 99 And 流水 < 1000 Then : 流水號 = "PO" & "00000" & 流水
                ElseIf 流水 > 999 And 流水 < 10000 Then : 流水號 = "PO" & "0000" & 流水
                ElseIf 流水 > 9999 And 流水 < 100000 Then : 流水號 = "PO" & "000" & 流水
                ElseIf 流水 > 99999 And 流水 < 1000000 Then : 流水號 = "PO" & "00" & 流水
                ElseIf 流水 > 999999 And 流水 < 10000000 Then : 流水號 = "PO" & "0" & 流水
                ElseIf 流水 > 9999999 Then : 流水號 = "PO" & 流水 : End If : PA = 流水號
                SQL_銷售控制新增() : SQL_銷售明細刪除() : XC = 0
                For Each parentNode As TreeNode In TreeView1.Nodes : CheckChildNodes1(parentNode) : Next
            End If

            流水號 = "" : 流水 = 0 : SQL_查詢銷售()
            If dr.Read Then : PA = dr("流水號").ToString : SQL_銷售保養品明細刪除() : XD = 0
                For Each parentNode As TreeNode In TreeView2.Nodes : CheckChildNodes2(parentNode) : Next
            Else
                SQL_銷售流水號()
                If dr.Read() Then : 流水 = Double.Parse(Strings.Right(dr("流水號").ToString, 8)) : Else : 流水 = 0 : End If : conn.Close() : 流水 += 1
                If 流水 < 10 Then : 流水號 = "PO" & "0000000" & 流水
                ElseIf 流水 > 9 And 流水 < 100 Then : 流水號 = "PO" & "000000" & 流水
                ElseIf 流水 > 99 And 流水 < 1000 Then : 流水號 = "PO" & "00000" & 流水
                ElseIf 流水 > 999 And 流水 < 10000 Then : 流水號 = "PO" & "0000" & 流水
                ElseIf 流水 > 9999 And 流水 < 100000 Then : 流水號 = "PO" & "000" & 流水
                ElseIf 流水 > 99999 And 流水 < 1000000 Then : 流水號 = "PO" & "00" & 流水
                ElseIf 流水 > 999999 And 流水 < 10000000 Then : 流水號 = "PO" & "0" & 流水
                ElseIf 流水 > 9999999 Then : 流水號 = "PO" & 流水 : End If : PA = 流水號
                SQL_銷售控制新增() : SQL_銷售保養品明細刪除() : XD = 0
                For Each parentNode As TreeNode In TreeView2.Nodes : CheckChildNodes2(parentNode) : Next
            End If
            MsgBox("資料傳遞 至 業務系統 完成!!")
            For i As Integer = 0 To 適應症_dgv.Rows.Count - 1 : 適應症_dgv("備註", i).Value = "" : Next
            For i As Integer = 0 To 保養品_dgv.Rows.Count - 1 : 保養品_dgv("備註", i).Value = "" : Next
            病歷號_tb.Text = "" : 適應症選擇() : 保養品選擇()
        End If
    End Sub
    Private Sub 存檔_bt_Click(sender As Object, e As EventArgs) Handles 存檔_bt.Click
        If 病症_cb.Text = "" Or 療程_cb.Text = "" Then : MsgBox("資料空白無法執行!!") : Else
            SQL_適應症流水()
            If dr.Read Then : MsgBox("資料已存在不需要重複新增!!") : Else
                Dim 流水號 As String = "" : Dim 流水 As Integer
                SQL_適應症流水1()
                If dr.Read() Then : 流水 = Double.Parse(Strings.Right(dr("流水號").ToString, 8)) : Else : 流水 = 0 : End If : conn.Close() : 流水 += 1
                If 流水 < 10 Then : 流水號 = "IN" & "0000000" & 流水
                ElseIf 流水 > 9 And 流水 < 100 Then : 流水號 = "IN" & "000000" & 流水
                ElseIf 流水 > 99 And 流水 < 1000 Then : 流水號 = "IN" & "00000" & 流水
                ElseIf 流水 > 999 And 流水 < 10000 Then : 流水號 = "IN" & "0000" & 流水
                ElseIf 流水 > 9999 And 流水 < 100000 Then : 流水號 = "IN" & "000" & 流水
                ElseIf 流水 > 99999 And 流水 < 1000000 Then : 流水號 = "IN" & "00" & 流水
                ElseIf 流水 > 999999 And 流水 < 10000000 Then : 流水號 = "IN" & "0" & 流水
                ElseIf 流水 > 9999999 Then : 流水號 = "IN" & 流水 : End If : PA = 流水號
                SQL_適應症新增1() : Set_適應症設定()
            End If
        End If
    End Sub
    Private Sub 刪除_bt_Click(sender As Object, e As EventArgs) Handles 刪除_bt.Click
        Dim cc As MsgBoxResult = MsgBox("確定要刪除這筆適應症資料嗎??", MsgBoxStyle.OkCancel)
        If cc = MsgBoxResult.Ok Then : SQL_適應症刪除1() : Set_適應症設定() : End If
    End Sub
    Private Sub 存檔1_bt_Click(sender As Object, e As EventArgs) Handles 存檔1_bt.Click
        If 品牌_cb.Text = "" Or 保養品_cb.Text = "" Then : MsgBox("資料空白無法執行!!") : Else
            SQL_保養品流水()
            If dr.Read Then : MsgBox("資料已存在不需要重複新增!!") : Else
                Dim 流水號 As String = "" : Dim 流水 As Integer
                SQL_保養品流水1()
                If dr.Read() Then : 流水 = Double.Parse(Strings.Right(dr("流水號").ToString, 8)) : Else : 流水 = 0 : End If : conn.Close() : 流水 += 1
                If 流水 < 10 Then : 流水號 = "IN" & "0000000" & 流水
                ElseIf 流水 > 9 And 流水 < 100 Then : 流水號 = "IN" & "000000" & 流水
                ElseIf 流水 > 99 And 流水 < 1000 Then : 流水號 = "IN" & "00000" & 流水
                ElseIf 流水 > 999 And 流水 < 10000 Then : 流水號 = "IN" & "0000" & 流水
                ElseIf 流水 > 9999 And 流水 < 100000 Then : 流水號 = "IN" & "000" & 流水
                ElseIf 流水 > 99999 And 流水 < 1000000 Then : 流水號 = "IN" & "00" & 流水
                ElseIf 流水 > 999999 And 流水 < 10000000 Then : 流水號 = "IN" & "0" & 流水
                ElseIf 流水 > 9999999 Then : 流水號 = "IN" & 流水 : End If : PA = 流水號
                SQL_保養品新增1() : Set_適應症設定()
            End If
        End If
    End Sub
    Private Sub 刪除1_bt_Click(sender As Object, e As EventArgs) Handles 刪除1_bt.Click
        Dim cc As MsgBoxResult = MsgBox("確定要刪除這筆適應症資料嗎??", MsgBoxStyle.OkCancel)
        If cc = MsgBoxResult.Ok Then : SQL_適應症刪除2() : Set_適應症設定() : End If
    End Sub
    Private Sub 醫生_tb_Click(sender As Object, e As EventArgs) Handles 醫生_tb.Click
        非第一次 = True : 醫師.ShowDialog() : 醫生_tb.Text = PA
    End Sub
End Class