Imports System.Windows.Forms

Public Class Frm_Filter
    Public ColumList() As String  '//필드네임
    Public ColumTitle() As String '//필드제목줄
    Public Colorlist As String
    Public Numfield As String '//숫자필드일경우
    Public Default_Filter As String   '//기본값   code <> '0'

    Private Sub Frm_Filter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Init_combo()

    End Sub

    Private Sub Init_combo()   '//콤보박스목록을 생성
        Dim CmbJ As String() = {"AND", "OR"}
        Dim CmbCond As String() = {"LIKE", "=", "<>", "<=", ">="}


        '//clear
        Me.cmb_j1.Items.Clear()
        Me.cmb_j2.Items.Clear()
        Me.cmb_j3.Items.Clear()
        'Me.cmb_j4.Items.Clear()

        Me.cmb_cond1.Items.Clear()
        Me.cmb_cond2.Items.Clear()
        Me.cmb_cond3.Items.Clear()
        Me.cmb_cond4.Items.Clear()

        Me.cmb_field1.Items.Clear()
        Me.cmb_field2.Items.Clear()
        Me.cmb_field3.Items.Clear()
        Me.cmb_field4.Items.Clear()
        Me.cmb_fieldc.Items.Clear()

        '/set
        Me.cmb_j1.Items.AddRange(CmbJ)
        Me.cmb_j2.Items.AddRange(CmbJ)
        Me.cmb_j3.Items.AddRange(CmbJ)


        Me.cmb_cond1.Items.AddRange(CmbCond)
        Me.cmb_cond2.Items.AddRange(CmbCond)
        Me.cmb_cond3.Items.AddRange(CmbCond)
        Me.cmb_cond4.Items.AddRange(CmbCond)


        Try    '//필드선택 콤보박승 필드명 넣기
            Me.cmb_field1.Items.AddRange(ColumTitle)
            Me.cmb_field2.Items.AddRange(ColumTitle)
            Me.cmb_field3.Items.AddRange(ColumTitle)
            Me.cmb_field4.Items.AddRange(ColumTitle)
            Me.cmb_fieldc.Items.AddRange(ColumTitle)
        Catch ex As Exception
            MsgBox("필드목록이 없거나 매개변수값이 없습니다", MsgBoxStyle.Critical, "확인")
        End Try

        'Me.cmb_j1.SelectedIndex = 0
        'Me.cmb_j2.SelectedIndex = 0
        'Me.cmb_j3.SelectedIndex = 0


        'Me.cmb_cond1.SelectedIndex = 0
        'Me.cmb_cond2.SelectedIndex = 0
        'Me.cmb_cond3.SelectedIndex = 0
        'Me.cmb_cond4.SelectedIndex = 0

        AddHandler Me.cmb_j1.SelectedIndexChanged, AddressOf event_comboselectindex
        AddHandler Me.cmb_j2.SelectedIndexChanged, AddressOf event_comboselectindex
        AddHandler Me.cmb_j3.SelectedIndexChanged, AddressOf event_comboselectindex

        AddHandler Me.cmb_cond1.SelectedIndexChanged, AddressOf event_comboselectindex
        AddHandler Me.cmb_cond2.SelectedIndexChanged, AddressOf event_comboselectindex
        AddHandler Me.cmb_cond3.SelectedIndexChanged, AddressOf event_comboselectindex
        AddHandler Me.cmb_cond4.SelectedIndexChanged, AddressOf event_comboselectindex

        AddHandler Me.cmb_field1.SelectedIndexChanged, AddressOf event_comboselectindex
        AddHandler Me.cmb_field2.SelectedIndexChanged, AddressOf event_comboselectindex
        AddHandler Me.cmb_field3.SelectedIndexChanged, AddressOf event_comboselectindex
        AddHandler Me.cmb_field4.SelectedIndexChanged, AddressOf event_comboselectindex

        AddHandler Me.tb_s1.TextChanged, AddressOf event_textboxchange
        AddHandler Me.tb_s2.TextChanged, AddressOf event_textboxchange
        AddHandler Me.tb_s3.TextChanged, AddressOf event_textboxchange
        AddHandler Me.tb_s4.TextChanged, AddressOf event_textboxchange


    End Sub

    Private Function IsnumField(ByVal v_Fname As String) As Boolean
        '//숫자필드목록에서 해당 필드가 잇는지 확인한다.
        If Me.Numfield.IndexOf(v_Fname) = -1 Then   '//해당필드가 숫자목록에 잇으면 True
            Return False
        Else
            Return True
        End If
    End Function
    Private Sub MakeWhreState()
        '//콤보박스값을 가지고 Textbox1 을 생성
        Dim NewStat As New System.Text.StringBuilder()
        If Me.cmb_field1.SelectedIndex <> -1 And Me.tb_s1.Text <> "" Then
            If cmb_cond1.Text.ToUpper = "LIKE" Then
                NewStat.Append(Space(1) + Me.ColumList(Me.cmb_field1.SelectedIndex).ToString + Space(1) + cmb_cond1.Text + Space(1) + "'%" + tb_s1.Text + "%'")
            Else
                '//숫자필드이면
                If IsnumField(Me.cmb_field1.Text) Then
                    NewStat.Append(Space(1) + "convert(isnull(" & Me.ColumList(Me.cmb_field1.SelectedIndex).ToString & ",'0'),'System.Single')" + Space(1) + cmb_cond1.Text + Space(1) + tb_s1.Text)
                Else
                    NewStat.Append(Space(1) + Me.ColumList(Me.cmb_field1.SelectedIndex).ToString + Space(1) + cmb_cond1.Text + Space(1) + "'" + tb_s1.Text + "'")
                End If

            End If

        End If
        If Me.cmb_field2.SelectedIndex <> -1 And Me.tb_s2.Text <> "" Then
            If cmb_cond2.Text.ToUpper = "LIKE" Then
                NewStat.Append(Space(1) + Me.cmb_j1.Text + Space(1) + Me.ColumList(Me.cmb_field2.SelectedIndex).ToString + Space(1) + cmb_cond2.Text + Space(1) + "'%" + tb_s2.Text + "%'")
            Else
                If IsnumField(Me.cmb_field1.Text) Then
                    NewStat.Append(Space(1) + Me.cmb_j1.Text + Space(1) + "convert(isnull(" & Me.ColumList(Me.cmb_field2.SelectedIndex).ToString & ",'0'),'System.Single')" + Space(1) + cmb_cond2.Text + Space(1) + tb_s2.Text)
                Else
                    NewStat.Append(Space(1) + Me.cmb_j1.Text + Space(1) + Me.ColumList(Me.cmb_field2.SelectedIndex).ToString + Space(1) + cmb_cond2.Text + Space(1) + "'" + tb_s2.Text + "'")
                End If

            End If

        End If
        If Me.cmb_field3.SelectedIndex <> -1 And Me.tb_s3.Text <> "" Then
            If cmb_cond3.Text.ToUpper = "LIKE" Then
                NewStat.Append(Space(1) + Me.cmb_j2.Text + Space(1) + Me.ColumList(Me.cmb_field3.SelectedIndex).ToString + Space(1) + cmb_cond3.Text + Space(1) + "'%" + tb_s3.Text + "%'")
            Else
                If IsnumField(Me.cmb_field1.Text) Then
                    NewStat.Append(Space(1) + Me.cmb_j2.Text + Space(1) + "convert(isnull(" & Me.ColumList(Me.cmb_field3.SelectedIndex).ToString & ",'0'),'System.Single')" + Space(1) + cmb_cond3.Text + Space(1) + tb_s3.Text)
                Else
                    NewStat.Append(Space(1) + Me.cmb_j2.Text + Space(1) + Me.ColumList(Me.cmb_field3.SelectedIndex).ToString + Space(1) + cmb_cond3.Text + Space(1) + "'" + tb_s3.Text + "'")
                End If

            End If

        End If
        If Me.cmb_field4.SelectedIndex <> -1 And Me.tb_s4.Text <> "" Then
            If cmb_cond3.Text.ToUpper = "LIKE" Then
                NewStat.Append(Space(1) + Me.cmb_j3.Text + Space(1) + Me.ColumList(Me.cmb_field4.SelectedIndex).ToString + Space(1) + cmb_cond4.Text + Space(1) + "'%" + tb_s4.Text + "%'")
            Else
                If IsnumField(Me.cmb_field1.Text) Then
                    NewStat.Append(Space(1) + Me.cmb_j3.Text + Space(1) + "convert(isnull(" & Me.ColumList(Me.cmb_field4.SelectedIndex).ToString & ",'0'),'System.Single')" + Space(1) + cmb_cond4.Text + Space(1) + tb_s4.Text)
                Else
                    NewStat.Append(Space(1) + Me.cmb_j3.Text + Space(1) + Me.ColumList(Me.cmb_field4.SelectedIndex).ToString + Space(1) + cmb_cond4.Text + Space(1) + "'" + tb_s4.Text + "'")
                End If

            End If

        End If
        If NewStat.ToString.Trim = "" Then
            Me.tb_filter.Text = Default_Filter
        Else
            Me.tb_filter.Text = Default_Filter & " and " + NewStat.ToString
        End If


    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        If Me.tb_filter.Text = "" Then Me.tb_filter.Text = Default_Filter
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub


    Private Sub event_textboxchange(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.MakeWhreState()
    End Sub

    Private Sub event_comboselectindex(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.MakeWhreState()
    End Sub

    Private Sub cmb_field1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_field1.SelectedIndexChanged

        Dim Find As Boolean = False
        For Each C As String In Me.Colorlist.Split(",")
            If C.ToUpper = Me.ColumList(Me.cmb_field1.SelectedIndex).ToUpper Then ' Me.cmb_field1.Text.ToUpper Then
                Find = True
                Exit For
            End If
        Next

        If Find Then

            Me.tb_s1.BackColor = Color.LightGreen
        Else
            Me.tb_s1.BackColor = Color.White
        End If
    End Sub

    Private Sub cmb_field2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_field2.SelectedIndexChanged

        Dim Find As Boolean = False
        For Each C As String In Me.Colorlist.Split(",")
            If C.ToUpper = Me.ColumList(Me.cmb_field2.SelectedIndex).ToUpper Then
                Find = True
                Exit For
            End If
        Next

        If Find Then
            Me.tb_s2.BackColor = Color.LightGreen
        Else
            Me.tb_s2.BackColor = Color.White
        End If
    End Sub

    Private Sub cmb_field3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_field3.SelectedIndexChanged
        Dim Find As Boolean = False
        For Each C As String In Me.Colorlist.Split(",")
            If C.ToUpper = Me.ColumList(Me.cmb_field3.SelectedIndex).ToUpper Then
                Find = True
                Exit For
            End If
        Next
        If Find Then
            Me.tb_s3.BackColor = Color.LightGreen
        Else
            Me.tb_s3.BackColor = Color.White
        End If
    End Sub

    Private Sub cmb_field4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_field4.SelectedIndexChanged
        Dim Find As Boolean = False
        For Each C As String In Me.Colorlist.Split(",")
            If C.ToUpper = Me.ColumList(Me.cmb_field4.SelectedIndex).ToUpper Then
                Find = True
                Exit For
            End If
        Next
        If Find Then
            Me.tb_s4.BackColor = Color.LightGreen
        Else
            Me.tb_s4.BackColor = Color.White
        End If
    End Sub

    Private Sub cmb_cond1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_cond1.SelectedIndexChanged

    End Sub
End Class

