Public Class form_QuerySim
    Dim m_Table As New System.Data.DataTable
    Dim m_QuickMode As Boolean = True
    Dim m_TotalCount As Integer = 0
    Dim m_MaxIndex As Integer = 0
    Dim m_MinIndex As Integer = 0
    Dim m_CurSelItem_Col As Integer = 0
    Dim m_CurSelItem_Row As Integer = 0
    Dim m_InitOver As Boolean = False

    Dim m_Cell_Enable_Edit As Boolean = False

    Dim m_PartNumber As New CPartNumber


    Public Function GetLikeString(ByVal likestr As String) As String
        Dim rString As String = ""
        Dim ch As Char
        For i As Integer = 1 To likestr.Length
            ch = Strings.GetChar(likestr, i)
            If ch = "%" OrElse ch = "_" OrElse ch = "/" Then
                rString &= "/"
            End If
            rString &= ch
        Next

        Return rString
    End Function

    Private Sub QueryData()
        Dim strSQL As String = "SELECT `" &
                                CPartNumber.m_COLS_NAME(CPartNumber.COLS.料号PN) & "`,`" &
                                CPartNumber.m_COLS_NAME(CPartNumber.COLS.物料名称) & "`,`" &
                                CPartNumber.m_COLS_NAME(CPartNumber.COLS.规格描述) & "`,`" &
                                CPartNumber.m_COLS_NAME(CPartNumber.COLS.单位) & "`,`" &
                                CPartNumber.m_COLS_NAME(CPartNumber.COLS.单重) & "`,`" &
                                CPartNumber.m_COLS_NAME(CPartNumber.COLS.供应商ID) & "`"
        strSQL &= " FROM `" & CPartNumber.cst_TABLE_NAME & "`"

        Dim oper_idx As Integer = combo_Operate.SelectedIndex
        If combo_Operate.Text.Length > 0 AndAlso
            combo_Section.Text.Length > 0 AndAlso
            tb_Value.Text.Length > 0 Then

            Dim oper_str As String = rel_Operator(oper_idx, 1)
            Dim isescape As Boolean = CBool(rel_Operator(oper_idx, 3))
            Dim str_Value As String = tb_Value.Text
            If isescape = True Then
                str_Value = GetLikeString(tb_Value.Text)
            End If

            strSQL &= " WHERE `" & combo_Section.Text & "` " &
                        " COLLATE gbk_chinese_ci " &
                        rel_Operator(oper_idx, 1) &
                        str_Value &
                        rel_Operator(oper_idx, 2)

            If isescape = True Then
                strSQL &= " escape '/'"
            End If

        End If

        SQL_Query(COL_RIGHTS.料号管理, strSQL, m_Table)
    End Sub

    Private Sub SetDataBinding()
        If Not m_Table Is Nothing Then
            m_Table.Dispose()
        End If
        m_Table = New DataTable

        If m_QuickMode = True Then
            QueryData_Quick()
        Else
            QueryData()
        End If

        Grid1.DisplayRowNumber = True
        Grid1.AllowUserSort = True
        Grid1.DisplayFocusRect = False
        Grid1.ExtendLastCol = True
        Grid1.DisplayRowArrow = True
        Grid1.BoldFixedCell = False
        Grid1.SortIndicatorStyle = FlexCell.SortIndicatorStyleEnum.Light3D

        Grid1.SetDataBinding(m_Table, "", True, 1)

        CGirdInfo.LoadGridInfo("料号查询", Grid1)
        Grid1.AllowUserSort = True

        Grid1.Locked = True
    End Sub

    Private Sub bt_Query_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_Query.Click
        m_QuickMode = False
        SetDataBinding()
    End Sub

    Private Sub form_QuerySim_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Width = My.Settings.FORM_QUERYSIM_WIDHT
        Me.Height = My.Settings.FORM_QUERYSIM_HEIGHT
        Me.WindowState = My.Settings.FORM_QUERYSIM_WINSTATE
        Me.Left = My.Settings.FORM_QUERYSIM_X
        Me.Top = My.Settings.FORM_QUERYSIM_Y

        lbl_ImageReading.Visible = False
        lbl_NoImage.Visible = False

        bt_Del.Enabled = False

        If CBool(Current_Rights(COL_RIGHTS.料号管理) And RIGHTS.READ) = False Then
            bt_Query.Enabled = False
            bt_QueryQuick.Enabled = False
        End If

        If CBool(Current_Rights(COL_RIGHTS.料号管理) And RIGHTS.MANAGE) = True Then
            bt_Del.Enabled = True
        End If

        combo_Operate.Items.Clear()

        For i As Integer = 0 To (rel_Operator.Length / 4 - 1)
            combo_Operate.Items.Add(rel_Operator(i, 0))
        Next

        m_InitOver = True
    End Sub

    Private Sub QueryData_Quick()
        Dim strSQL As String = "SELECT `" &
                                CPartNumber.m_COLS_NAME(CPartNumber.COLS.料号PN) & "`,`" &
                                CPartNumber.m_COLS_NAME(CPartNumber.COLS.物料名称) & "`,`" &
                                CPartNumber.m_COLS_NAME(CPartNumber.COLS.规格描述) & "`,`" &
                                CPartNumber.m_COLS_NAME(CPartNumber.COLS.单位) & "`,`" &
                                CPartNumber.m_COLS_NAME(CPartNumber.COLS.单重) & "`,`" &
                                CPartNumber.m_COLS_NAME(CPartNumber.COLS.供应商ID) & "`, `" &
                                CPartNumber.m_COLS_NAME(CPartNumber.COLS.序号) & "`"
        strSQL &= " FROM `" & CPartNumber.cst_TABLE_NAME & "`"

        If tb_PartNumber.Text.Length > 0 OrElse
            tb_Name.Text.Length > 0 OrElse
            tb_Desc.Text.Length > 0 OrElse
            ckb_Pn_Inhaos.Checked = True OrElse
            ckb_PnJohao.Checked = True Then

            strSQL &= " WHERE "

            Dim strSplit() As String
            Dim firstLike As Boolean = True
            If tb_PartNumber.Text.Length > 0 Then
                strSplit = Split(tb_PartNumber.Text, ",")
                For i As Integer = 0 To strSplit.Length - 1
                    If firstLike = True Then
                        firstLike = False
                    Else
                        strSQL &= " AND "
                    End If
                    strSQL &= "`" & CPartNumber.m_COLS_NAME(CPartNumber.COLS.料号PN) & "`"
                    strSQL &= " COLLATE gbk_chinese_ci "
                    strSQL &= " LIKE '%"
                    strSQL &= GetLikeString(strSplit(i)) & "%'"
                    strSQL &= " escape '/'"
                Next
            End If

            If tb_Name.Text.Length > 0 Then
                strSplit = Split(tb_Name.Text, ",")
                For i As Integer = 0 To strSplit.Length - 1
                    If firstLike = True Then
                        firstLike = False
                    Else
                        strSQL &= " AND "
                    End If
                    strSQL &= "`" & CPartNumber.m_COLS_NAME(CPartNumber.COLS.物料名称) & "`"
                    strSQL &= " COLLATE gbk_chinese_ci "
                    strSQL &= " LIKE '%"
                    strSQL &= GetLikeString(strSplit(i)) & "%'"
                    strSQL &= " escape '/'"
                Next
            End If

            If tb_Desc.Text.Length > 0 Then
                strSplit = Split(tb_Desc.Text, ",")
                For i As Integer = 0 To strSplit.Length - 1
                    If firstLike = True Then
                        firstLike = False
                    Else
                        strSQL &= " AND "
                    End If
                    strSQL &= "`" & CPartNumber.m_COLS_NAME(CPartNumber.COLS.规格描述) & "`"
                    strSQL &= " COLLATE gbk_chinese_ci "
                    strSQL &= " LIKE '%"
                    strSQL &= GetLikeString(strSplit(i)) & "%'"
                    strSQL &= " escape '/'"
                Next
            End If

            Dim customStr As String = ""
            If ckb_Pn_Inhaos.Checked Then
                customStr &= "`" & CPartNumber.m_COLS_NAME(CPartNumber.COLS.供应商ID) & "` = '02'"
            End If

            If ckb_PnJohao.Checked Then
                If String.IsNullOrEmpty(customStr) = False Then
                    customStr &= " OR "
                    customStr &= "`" & CPartNumber.m_COLS_NAME(CPartNumber.COLS.供应商ID) & "` ='01'"

                    customStr = $"({customStr})"
                Else
                    customStr &= "`" & CPartNumber.m_COLS_NAME(CPartNumber.COLS.供应商ID) & "` ='01'"
                End If
            End If

            If firstLike = True Then
                firstLike = False
            Else
                strSQL &= " AND "
            End If
            strSQL &= customStr

            'If ckb_Pn_Inhaos.Checked Then
            '    If firstLike = True Then
            '        firstLike = False
            '    Else
            '        strSQL &= " AND "
            '    End If
            '    strSQL &= "`" & CPartNumber.m_COLS_NAME(CPartNumber.COLS.供应商ID) & "` = '02'"
            'End If

            'If ckb_PnJohao.Checked Then
            '    If firstLike = True Then
            '        firstLike = False
            '    Else
            '        If ckb_Pn_Inhaos.Checked Then
            '            strSQL &= " or "
            '        Else
            '            strSQL &= " AND "
            '        End If
            '    End If
            '    strSQL &= "`" & CPartNumber.m_COLS_NAME(CPartNumber.COLS.供应商ID) & "` ='01'"
            'End If

        End If

        SQL_Query(COL_RIGHTS.料号管理, strSQL, m_Table)
    End Sub

    Private Function GetTableInformation() As Boolean
        Dim m_PartNumber As New CPartNumber
        lbl_Information.Text = ""
        If m_PartNumber.QueryInformation(m_TotalCount, m_MinIndex, m_MaxIndex) = ERROR_CODE.SUCCESS Then
            Return True
        Else
            m_TotalCount = 0
            m_MinIndex = 0
            m_MaxIndex = 0
        End If
        Return False
    End Function

    Private Sub ShowCurrSelectInformation(ByVal index As Integer)
        lbl_Information.Text = "查询条数: " & m_Table.Rows.Count
        lbl_Information.Text &= ", 总条数: " & m_TotalCount
        lbl_Information.Text &= ", 最小序号: " & m_MinIndex
        lbl_Information.Text &= ", 最大序号:" & m_MaxIndex
        If index >= 0 Then
            lbl_Select.Text = "当前条: " & index
            lbl_Select.Text &= ", 当前序号: " & Grid1.Cell(index, 7).Text
        Else
            lbl_Select.Text = ""
        End If
    End Sub

    Private Sub bt_QueryQuick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_QueryQuick.Click
        Dim tempRowIndex As Integer = 0

        Grid1.AutoRedraw = False

        m_QuickMode = True
        SetDataBinding()
        If GetTableInformation() Then
            ShowCurrSelectInformation(-1)
        End If

        For tempRowIndex = 1 To Grid1.Rows - 1
            Grid1.Cell(tempRowIndex, 0).ForeColor = Color.Black
            Grid1.Cell(tempRowIndex, 1).ForeColor = Color.Black
            Grid1.Cell(tempRowIndex, 1).FontBold = False
        Next

        Grid1.Refresh()
        Grid1.AutoRedraw = True
    End Sub

    Private Sub Grid1_CellChange(Sender As Object, e As FlexCell.Grid.CellChangeEventArgs) Handles Grid1.CellChange
        'If m_Cell_Enable_Edit Then MessageBox.Show(e.Col & "," & e.Row)
        'Grid1.Row(e.Row). = Color.Red
        If m_Cell_Enable_Edit Then
            Grid1.Cell(e.Row, e.Col).ForeColor = Color.Red
            Grid1.Cell(e.Row, 1).ForeColor = Color.Red
            Grid1.Cell(e.Row, 1).FontBold = True
        End If

        ' Grid1.Selection.ForeColor = Color.Red
    End Sub

    Private Sub Grid1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Grid1.MouseDoubleClick
        Try
            If e.Button = MouseButtons.Left Then
                Dim isHeaderClicked As Boolean = False
                Dim row As Integer

                '判断是否双击列HEADER
                Dim X_Start As Integer = 0
                Dim X_End As Integer = X_Start + Grid1.Column(0).Width
                If e.X >= X_Start AndAlso e.X <= X_End Then
                    If e.Y < Grid1.Height Then
                        row = e.Y \ Grid1.Row(0).Height

                        If row >= Grid1.Rows Then
                            Return
                        End If

                        If row >= 0 Then
                            isHeaderClicked = True
                        End If
                    End If
                End If

                If isHeaderClicked = True Then
                    Dim strCopy As String = ""
                    For i As Integer = 0 To Grid1.Cols - 1
                        strCopy &= Grid1.Cell(row, i).Text & vbTab
                    Next
                    Clipboard.SetText(strCopy)
                Else
                    row = Grid1.Selection.FirstRow
                    Dim col As Integer = Grid1.Selection.FirstCol
                    Grid1.Selection.ClearAll()
                    If row >= 1 AndAlso col >= 1 Then
                        Clipboard.SetText(Grid1.Cell(row, col).Text)
                    End If
                End If
            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub Grid1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Grid1.MouseClick
        m_CurSelItem_Col = -1
        m_CurSelItem_Row = -1

        If e.Button = MouseButtons.Left Then
            Dim isHeaderClicked As Boolean = False
            Dim row As Integer

            '判断是否双击列HEADER
            Dim X_Start As Integer = 0
            Dim X_End As Integer = X_Start + Grid1.Column(0).Width
            If e.X >= X_Start AndAlso e.X <= X_End Then
                If e.Y < Grid1.Height Then
                    row = e.Y \ Grid1.Row(0).Height

                    If row >= Grid1.Rows Then
                        Return
                    End If

                    If row >= 0 Then
                        isHeaderClicked = True
                    End If
                End If
            End If

            If isHeaderClicked = True Then
            Else
                row = Grid1.Selection.FirstRow
                Dim col As Integer = Grid1.Selection.FirstCol
                Grid1.Selection.ClearAll()
                If row >= 1 AndAlso col >= 1 Then
                    m_CurSelItem_Col = col
                    m_CurSelItem_Row = row

                    If chkShowImage.Checked = True Then
                        PictureBox1.BackgroundImage = Nothing
                        lbl_ImageReading.Visible = True
                        lbl_NoImage.Visible = False
                        Application.DoEvents()
                        If GetItemImage(Grid1.Cell(row, 1).Text) = ERROR_CODE.SUCCESS Then
                            If PictureBox1.BackgroundImage Is Nothing Then
                                lbl_NoImage.Visible = True
                            End If
                        End If
                        lbl_ImageReading.Visible = False
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub Grid1_SelChange(ByVal Sender As System.Object, ByVal e As FlexCell.Grid.SelChangeEventArgs) Handles Grid1.SelChange
        If e.FirstRow >= 1 Then
            ShowCurrSelectInformation(e.FirstRow)
        End If
    End Sub

    Private Sub Grid1_ColWidthChange(ByVal Sender As System.Object, ByVal e As FlexCell.Grid.ColWidthChangeEventArgs) Handles Grid1.ColWidthChange
        CGirdInfo.SaveGirdInfo("料号查询", Grid1)
    End Sub

    Private Sub form_QuerySim_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        If m_InitOver = True Then
            My.Settings.FORM_QUERYSIM_WIDHT = Me.Width
            My.Settings.FORM_QUERYSIM_HEIGHT = Me.Height
            My.Settings.FORM_QUERYSIM_WINSTATE = Me.WindowState
            My.Settings.Save()
        End If
    End Sub

    Private Sub form_QuerySim_LocationChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LocationChanged
        If m_InitOver = True Then
            My.Settings.FORM_QUERYSIM_X = Me.Left
            My.Settings.FORM_QUERYSIM_Y = Me.Top
            My.Settings.Save()
        End If
    End Sub

    Private Function GetItemImage(ByVal strPN As String) As ERROR_CODE
        tb_Name.Text = ""
        PictureBox1.BackgroundImage = Nothing

        Dim strSQL As String = "SELECT 图片 FROM " & CPartNumber.cst_TABLE_NAME & " WHERE "
        strSQL &= CPartNumber.m_COLS_NAME(CPartNumber.COLS.料号PN) & " = '" & strPN & "'"

        Dim 图片(0) As Byte
        Dim rValueTable(0) As Object
        If SQL_DataReader(COL_RIGHTS.料号管理, strSQL, rValueTable) = True Then
            图片 = rValueTable(0)

            PictureBox1.BackgroundImage = GetImageFromBytes(图片)

            Return ERROR_CODE.SUCCESS
        End If

        Return ERROR_CODE.ACCESS
    End Function

    Private Function DeleteItem(ByVal strPN As String) As ERROR_CODE
        tb_Name.Text = ""
        PictureBox1.BackgroundImage = Nothing
        'DELETE FROM 料号表 WHERE 料号PN = 'U01-C8051F321-MLP28'
        Dim strSQL As String = "DELETE FROM " & CPartNumber.cst_TABLE_NAME & " WHERE "
        strSQL &= CPartNumber.m_COLS_NAME(CPartNumber.COLS.料号PN) & " = '" & strPN & "'"

        If SQL_ExeCommand(COL_RIGHTS.料号管理, strSQL) = True Then
            Return ERROR_CODE.SUCCESS
        End If

        Return ERROR_CODE.ACCESS
    End Function


    Private Sub chkShowImage_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkShowImage.CheckedChanged
        If chkShowImage.Checked = False Then
            PictureBox1.BackgroundImage = Nothing
        End If
    End Sub

    Private Sub bt_Del_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_Del.Click
        If m_CurSelItem_Col >= 1 AndAlso m_CurSelItem_Row >= 1 Then '如果选择有效则删除
            Dim strPN As String = Grid1.Cell(m_CurSelItem_Row, 1).Text
            If MessageBox.Show("请确认删除料号: '" & strPN & "'", "料号删除", MessageBoxButtons.OKCancel) = DialogResult.OK Then
                If DeleteItem(strPN) = ERROR_CODE.SUCCESS Then
                    MsgBox("删除料号: '" & strPN & "' 成功!")
                Else
                    MsgBox("删除料号: '" & strPN & "' 失败!")
                End If

                bt_QueryQuick.PerformClick()
            End If
        End If
    End Sub

    Private Sub btn_Avtive_Click(sender As Object, e As EventArgs) Handles btn_Avtive.Click
        If Grid1.Locked = True Then
            Grid1.Locked = False
            btn_Avtive.Text = "锁定编辑"
            btn_Avtive.BackColor = Color.Green
            m_Cell_Enable_Edit = True
            Grid1.Column(1).Locked = True '锁定料号列
            Grid1.Column(7).Locked = True '锁定序列号列
        Else
            Grid1.Locked = True
            btn_Avtive.Text = "激活编辑"
            btn_Avtive.BackColor = Color.Red
            m_Cell_Enable_Edit = False
        End If
    End Sub
    Private Sub btn_SaveChange_Click(sender As Object, e As EventArgs) Handles btn_SaveChange.Click
        Dim tempRowIndex As Integer = 0
        Dim tb_PartNumber_Text As String = ""
        Dim tb_Name_Text As String = ""
        Dim tb_Descr_Text As String = ""
        Dim combo_Unit_Text As String = ""
        Dim num_Weight_Value As String = ""
        Dim strSupplierID As String = ""
        Dim SavedCount As Integer = 0
        Dim ChangeCount As Integer = 0

        Dim result As ERROR_CODE

        For tempRowIndex = 0 To Grid1.Rows - 1
            If Grid1.Cell(tempRowIndex, 1).ForeColor = Color.Red Then '判断数据修改过
                'MessageBox.Show(tempRowIndex)
                ChangeCount = ChangeCount + 1
                '保存料号
                tb_PartNumber_Text = Grid1.Cell(tempRowIndex, 1).Text
                tb_Name_Text = Grid1.Cell(tempRowIndex, 2).Text
                tb_Descr_Text = Grid1.Cell(tempRowIndex, 3).Text
                combo_Unit_Text = Grid1.Cell(tempRowIndex, 4).Text
                num_Weight_Value = Grid1.Cell(tempRowIndex, 5).Text
                strSupplierID = Grid1.Cell(tempRowIndex, 6).Text

                result = m_PartNumber.UpdateItem(tb_PartNumber_Text, tb_Name_Text, tb_Descr_Text, combo_Unit_Text, num_Weight_Value, Nothing, strSupplierID)

                If result = ERROR_CODE.SUCCESS Then
                    If chk_MsgForSaveItembyItem.Checked Then MsgBox("第 " & tempRowIndex & " 行保存成功！")
                    Grid1.Cell(tempRowIndex, 1).ForeColor = Color.Green
                    SavedCount = SavedCount + 1
                ElseIf result = ERROR_CODE.NORIGHT Then
                    MsgBox("保存失败： ""无访问权限!""")
                Else
                    MsgBox("保存失败: 请检查网络连接或联系管理员!")
                End If
            End If
        Next
        MsgBox("保存完成，共有 " & ChangeCount & " 项修改！共有 " & SavedCount & " 项保存成功！")

    End Sub



End Class