Public Class form_Product
    Dim m_Table As New System.Data.DataTable
    Dim m_Product As New CProductsManage
    Dim m_PartNumber As New CPartNumber
    Dim 库位表列表 As New ArrayList
    Dim 库位表名 As String = ""

    Public Property OutStore() As Boolean
        Get
            Return m_Product.OutStore
        End Get
        Set(ByVal value As Boolean)
            m_Product.OutStore = value
        End Set
    End Property

    Private Function GetStoreList() As Boolean
        Dim m_Store As New CStore

        combo_StoreName.Items.Clear()
        库位表列表.Clear()

        m_Table.Rows.Clear()
        m_Table.Columns.Clear()
        If m_Store.QueryAll(m_Table) = ERROR_CODE.SUCCESS Then
            For i As Integer = 0 To m_Table.Rows.Count - 1
                combo_StoreName.Items.Add(m_Table.Rows(i).Item(CStore.COLS.仓库名称))
                库位表列表.Add(m_Table.Rows(i).Item(CStore.COLS.仓库库位表))
            Next
            Return True
        End If

        Return False
    End Function

    Private Function GetStoreLocationList() As Boolean
        Dim m_StoreLocation As New CStoreLocation()
        combo_StoreLoc.Items.Clear()

        m_StoreLocation.TABLE_NAME = 库位表名
        m_Table.Rows.Clear()
        m_Table.Columns.Clear()
        If m_StoreLocation.QueryAll(m_Table) = ERROR_CODE.SUCCESS Then
            For i As Integer = 0 To m_Table.Rows.Count - 1
                combo_StoreLoc.Items.Add(m_Table.Rows(i).Item(CStoreLocation.COLS.库位))
            Next

            Return True
        End If

        Return False
    End Function

    '获取料号信息
    Private Function GetPartNumberInformation(ByVal strPartNumber As String) As Boolean
        Dim 物料名称 As String = ""
        Dim 规格描述 As String = ""
        Dim 单位 As String = ""
        Dim 单重 As Double = 0
        Dim 图片(0) As Byte
        Dim 供应商ID As String = ""

        Dim strInfo As String = ""
        tb_PartNumberInfo.Text = ""
        Dim result As Boolean = False
        If chk_Preview.Checked = True Then
            If m_PartNumber.QueryItem(tb_PartNumber.Text, 物料名称, 规格描述, 单位, 单重, 图片, 供应商ID) = ERROR_CODE.SUCCESS Then
                strInfo = "物料名称: " & 物料名称 & vbNewLine
                strInfo &= "规格描述: " & 规格描述 & vbNewLine
                strInfo &= "单位: " & 单位 & vbNewLine
                strInfo &= "单重: " & 单重 & vbNewLine
                strInfo &= "供应商ID: " & 供应商ID & vbNewLine

                PictureBox1.BackgroundImage = GetImageFromBytes(图片)
                tb_PartNumberInfo.Text = strInfo
                result = True
            End If
        Else
            If m_PartNumber.QueryItem(tb_PartNumber.Text, 物料名称, 规格描述, 单位, 单重, 供应商ID) = ERROR_CODE.SUCCESS Then
                strInfo = "物料名称: " & 物料名称 & vbNewLine
                strInfo &= "规格描述: " & 规格描述 & vbNewLine
                strInfo &= "单位: " & 单位 & vbNewLine
                strInfo &= "单重: " & 单重 & vbNewLine
                strInfo &= "供应商ID: " & 供应商ID & vbNewLine

                PictureBox1.BackgroundImage = Nothing
                tb_PartNumberInfo.Text = strInfo
                result = True
            End If
        End If

        If result = True Then
            tb_PartNumber.BackColor = Color.White
        Else
            tb_PartNumber.BackColor = Color.Red
        End If

        Return result
    End Function

    Private Sub AddLog(ByVal text As String)
        Dim index As Integer = lstb_Log.Items.Add(Now.ToString & ":" & text)
        lstb_Log.SelectedIndex = index
    End Sub

    Private Sub form_Material_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        My.Settings.Save()
    End Sub

    Private Sub form_Material_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        My.Settings.Reload()

        GetStoreList()
    End Sub

    Private Sub StoreLocUpdated()
        '重新载入库位列表
        If GetStoreLocationList() = False Then
            MsgBox("载入库位列表失败!")
        End If
    End Sub

    Private Sub bt_AddStoreLoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_AddStoreLoc.Click
        Dim pForm As New form_StoreLocManage(库位表名)
        pForm.MdiParent = MainForm
        AddHandler pForm.StoreLocUpdated, AddressOf StoreLocUpdated
        pForm.Show()
    End Sub

    Private Sub tb_PartNumber_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tb_PartNumber.TextChanged
        If tb_PartNumber.Text.Length >= 8 Then
            GetPartNumberInformation(tb_PartNumber.Text)
        End If
    End Sub

    Private Sub tb_PartNumber_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tb_PartNumber.Leave
        If tb_PartNumber.Text.Length > 0 Then
            GetPartNumberInformation(tb_PartNumber.Text)
        End If
    End Sub

    Private Sub chk_Preview_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chk_Preview.CheckedChanged
        If tb_PartNumber.Text.Length > 0 Then
            GetPartNumberInformation(tb_PartNumber.Text)
        End If
    End Sub

    Private Function CheckInputValid() As Boolean
        Dim valid As Boolean = True

        '料号
        If tb_PartNumber.Text.Length <= 0 Then
            valid = False
        End If
        If tb_PartNumber.Text.Length > SEC_LENGTH.料号PN Then
            valid = False
        End If

        If valid = True Then
            tb_PartNumber.BackColor = Color.White
        Else
            tb_PartNumber.BackColor = Color.Red
        End If

        '订单号

        '数量
        If num_Count.Value > 0 Then
            num_Count.BackColor = Color.White
        Else
            num_Count.BackColor = Color.Red
            valid = False
        End If

        '仓库
        If combo_StoreName.Text.Length > 0 Then
            combo_StoreName.BackColor = Color.White
        Else
            combo_StoreName.BackColor = Color.Red
            valid = False
        End If

        '库位
        If combo_StoreLoc.Text.Length > 0 Then
            combo_StoreLoc.BackColor = Color.White
        Else
            combo_StoreLoc.BackColor = Color.Red
            valid = False
        End If

        Return valid
    End Function

    Private Sub bt_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_Save.Click
        '首先检查料号信息完整性
        If CheckInputValid() = False Then
            MsgBox("物料属性存在错误！请见红色背景提示")
            Return
        End If

        '检测料号唯一性
        Dim isExist As Boolean = False
        If m_PartNumber.CheckPartNumber(tb_PartNumber.Text, isExist) = ERROR_CODE.SUCCESS Then
            If isExist = False Then
                tb_PartNumber.BackColor = Color.Red
                MsgBox("当前料号不存在: " & tb_PartNumber.Text)
                Return
            End If
        Else
            MsgBox("访问数据库失败: 请检查网络连接或联系管理员!")
            Return
        End If


        '保存物料
        Dim m_date As Date = Now
        Dim result As ERROR_CODE = m_Product.AddItem(m_Product.OutStore,
                                                       tb_PartNumber.Text,
                                                       tb_OderNumber.Text,
                                                       m_date.ToShortDateString,
                                                       m_date.ToShortTimeString,
                                                       num_Count.Value,
                                                       Current_UserName,
                                                       combo_StoreName.Text,
                                                       combo_StoreLoc.Text,
                                                       tb_UseInfo.Text)
        If result = ERROR_CODE.SUCCESS Then
            AddLog("成功添加: " &
                   IIf(m_Product.OutStore, "出库", "入库") & "," &
                   tb_PartNumber.Text & "," &
                   tb_OderNumber.Text & "," &
                   num_Count.Value & "," &
                   combo_StoreName.Text & "," &
                   combo_StoreLoc.Text & "," &
                   tb_UseInfo.Text)
        ElseIf result = ERROR_CODE.NORIGHT Then
            MsgBox("保存失败： ""无访问权限!""")
        Else
            MsgBox("保存失败: 请检查网络连接或联系管理员!")
        End If
    End Sub

    Private Sub combo_StoreName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles combo_StoreName.SelectedIndexChanged
        If combo_StoreName.SelectedIndex >= 0 Then
            库位表名 = 库位表列表.Item(combo_StoreName.SelectedIndex)
        Else
            库位表名 = ""
        End If

        StoreLocUpdated()
    End Sub
End Class