Public Class form_Material
    Dim m_Table As New System.Data.DataTable
    Dim m_Material As New CMaterialInOut
    Dim m_PartNumber As New CPartNumber
    Dim 库位表列表 As New ArrayList
    Dim 库位表名 As String = ""
    Dim m_InitOver As Boolean = False

    Dim m_RecentList_PN As New System.Windows.Forms.AutoCompleteStringCollection
    Dim m_RecentList_OrderList As New System.Windows.Forms.AutoCompleteStringCollection
    Dim m_RecentList_UseInfo As New System.Windows.Forms.AutoCompleteStringCollection

    Public Property OutStore() As Boolean
        Get
            Return m_Material.OutStore
        End Get
        Set(ByVal value As Boolean)
            m_Material.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()
        combo_StoreLoc.Items.Add("")
        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()
        CGirdInfo.SaveRecentList("Recent_PN", m_RecentList_PN)
        CGirdInfo.SaveRecentList("Recent_OL", m_RecentList_OrderList)
        CGirdInfo.SaveRecentList("Recent_UI", m_RecentList_UseInfo)
    End Sub

    Private Sub form_Material_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CGirdInfo.LoadRecentList("Recent_PN", m_RecentList_PN)
        CGirdInfo.LoadRecentList("Recent_OL", m_RecentList_OrderList)
        CGirdInfo.LoadRecentList("Recent_UI", m_RecentList_UseInfo)
        tb_PartNumber.AutoCompleteCustomSource = m_RecentList_PN
        tb_OderNumber.AutoCompleteCustomSource = m_RecentList_OrderList
        tb_UseInfo.AutoCompleteCustomSource = m_RecentList_UseInfo

        If CBool(Current_Rights(COL_RIGHTS.原料库存管理) And RIGHTS.WRITE) = False Then
            bt_Save.Enabled = False
        End If
        If CBool(Current_Rights(COL_RIGHTS.原料库存管理) And RIGHTS.READ) = False Then
            bt_Query.Enabled = False
        End If

        If CBool(Current_Rights(COL_RIGHTS.库位管理) And RIGHTS.WRITE) = False Then
            bt_AddStoreLoc.Enabled = False
        End If

        My.Settings.Reload()

        GetStoreList()

        Me.Left = My.Settings.FORM_MATERIAL_X
        Me.Top = My.Settings.FORM_MATERIAL_Y

        m_InitOver = True

        If Microsoft.VisualBasic.Right(Me.Text, 4) = "物料出库" Then
            bt_Save.ForeColor = Color.Red
            bt_Save.Text = "出库"
        ElseIf Microsoft.VisualBasic.Right(Me.Text, 4) = "物料入库" Then
            bt_Save.ForeColor = Color.Blue
            bt_Save.Text = "入库"
        End If
    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
        If 库位表名 Is Nothing OrElse 库位表名.Length = 0 Then
            MsgBox("未选择仓库")
            Return
        End If

        Dim pForm As New form_StoreLocManage(库位表名)
        pForm.MdiParent = MainForm
        AddHandler pForm.StoreLocUpdated, AddressOf StoreLocUpdated
        pForm.Show()
    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 tb_PartNumber.Text.Length > 0 Then
            m_RecentList_PN.Add(tb_PartNumber.Text)
        End If
        If tb_OderNumber.Text.Length > 0 Then
            m_RecentList_OrderList.Add(tb_OderNumber.Text)
        End If
        If tb_UseInfo.Text.Length > 0 Then
            m_RecentList_UseInfo.Add(tb_UseInfo.Text)
        End If

        If m_RecentList_PN.Count > 10 Then
            m_RecentList_PN.RemoveAt(0)
        End If
        If m_RecentList_OrderList.Count > 10 Then
            m_RecentList_OrderList.RemoveAt(0)
        End If
        If m_RecentList_UseInfo.Count > 10 Then
            m_RecentList_UseInfo.RemoveAt(0)
        End If

        lbl_QueryInfo.Text = ""

        '首先检查料号信息完整性
        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
        '=================================================================================================
        '检查物料的当前仓库库位的库存量是否出库后小于0

        Dim numCount As Integer = num_Count.Value
        Dim m_Sum As Integer = m_PartNumber.GetStockSum(combo_StoreName.Text, combo_StoreLoc.Text, isExist)
        If m_Material.OutStore Then
            If m_Sum - numCount < 0 Then
                MsgBox("当前库位库存量不足，出库失败！！")
                Return
            Else
                m_Sum -= numCount
            End If
        Else
            m_Sum += numCount
        End If
        '=================================================================================================

        '保存物料
        'Dim m_date As Date = Now
        'Dim result As ERROR_CODE = m_Material.AddItem(m_Material.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)

        '=================================================================================================
        '添加出入库内容至原料库存表
        Dim m_date As Date = Now
        Dim result As ERROR_CODE = m_Material.AddItem(m_Material.OutStore,
                                               tb_PartNumber.Text,
                                               tb_OderNumber.Text,
                                               m_date.ToShortDateString,
                                               m_date.ToShortTimeString,
                                                numCount,
                                               Current_UserName,
                                               combo_StoreName.Text,
                                               combo_StoreLoc.Text,
                                               tb_UseInfo.Text,
                                               m_Sum
                                               )
        '=================================================================================================
        If result = ERROR_CODE.SUCCESS Then
            Dim Count As Integer
            Dim minIndex As Integer
            Dim maxIndex As Integer
            result = m_Material.QueryInformation(Count, minIndex, maxIndex)

            If result = ERROR_CODE.SUCCESS Then
                lbl_QueryInfo.Text = "总条数: " & Count & ", 最小序号: " & minIndex & ", 最大序号: " & maxIndex
                AddLog("成功添加: " & maxIndex & "，" &
                       IIf(m_Material.OutStore, "出库", "入库") & "," &
                       tb_PartNumber.Text & "," &
                       tb_OderNumber.Text & "," &
                       num_Count.Value & "," &
                       combo_StoreName.Text & "," &
                       combo_StoreLoc.Text & "," &
                       tb_UseInfo.Text)
            Else
                MsgBox("保存完成,查询数据库信息失败!")
            End If
        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

    Private Sub bt_Query_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_Query.Click
        If tb_PartNumber.Text.Length >= 0 Then
            GetPartNumberInformation(tb_PartNumber.Text)
        End If
    End Sub

    Private Sub bt_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_Clear.Click
        tb_PartNumber.Text = ""
        tb_OderNumber.Text = ""
        num_Count.Value = 0
        combo_Unit.Text = ""
        combo_StoreLoc.Text = ""
        tb_UseInfo.Text = ""
        tb_PartNumberInfo.Text = ""
        PictureBox1.BackgroundImage = Nothing
    End Sub

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

End Class