Public Class Form1
    Public QuitReson As QUIT_RESON = QUIT_RESON.close

    Public Enum FORM_TYPE
        none
        料号管理
        料号查询
        物料入库
        物料出库
        物料查询
        物料入出库记录
        BOM管理
        供应商管理
        用户管理
        系统记录
        财务明细录入_开发
        财务明细查询_开发
        财务明细录入_销售
        财务明细查询_销售
        合并用料查询
        备份管理
        max
    End Enum

    Dim g_Form_Track(FORM_TYPE.max - 1) As Form


    Dim m_AccountManage As New CAccountManage

    Dim m_LastError As String = ""

    Private Function GetLastError() As String
        Return m_LastError
    End Function

    'Private Function LoginCheck() As Boolean
    '    Dim pLoginForm As New LoginForm1

    '    pLoginForm.ShowDialog()
    '    Return pLoginForm.LoginResult
    'End Function

    '根据权限设置相应菜单的禁用状态
    Private Sub SetAccessState()
        '开启最低权限
        Current_Rights(COL_RIGHTS.仓库管理) = Current_Rights(COL_RIGHTS.仓库管理) Or RIGHTS.READ
        Current_Rights(COL_RIGHTS.库位管理) = Current_Rights(COL_RIGHTS.库位管理) Or RIGHTS.READ


        '设置相应权限
        CAccountManage.AccessRight = Current_Rights(COL_RIGHTS.用户管理)
        CBOM.AccessRight = Current_Rights(COL_RIGHTS.BOM管理)
        'CCompanyManage.AccessRight = Current_Rights(COL_RIGHTS.
        'CDepartmentManage.AccessRight = Current_Rights(
        CLogManage.AccessRight = Current_Rights(COL_RIGHTS.系统记录)
        CMaterialInOut.AccessRight = Current_Rights(COL_RIGHTS.原料库存管理)
        CPartNumber.AccessRight = Current_Rights(COL_RIGHTS.料号管理)
        CProductsManage.AccessRight = Current_Rights(COL_RIGHTS.成品库存管理)
        CStore.AccessRight = Current_Rights(COL_RIGHTS.仓库管理)
        CStoreLocation.AccessRight = Current_Rights(COL_RIGHTS.库位管理)
        CSupplier.AccessRight = Current_Rights(COL_RIGHTS.供应商管理)

        料号管理ToolStripMenuItem.Enabled = CBool(Current_Rights(COL_RIGHTS.料号管理))
        查询ToolStripMenuItem3.Enabled = CBool(Current_Rights(COL_RIGHTS.料号管理) And RIGHTS.READ)
        录入ToolStripMenuItem2.Enabled = CBool(Current_Rights(COL_RIGHTS.料号管理) And RIGHTS.WRITE)

        原料库存管理ToolStripMenuItem.Enabled = CBool(Current_Rights(COL_RIGHTS.原料库存管理)) And 料号管理ToolStripMenuItem.Enabled
        录入ToolStripMenuItem.Enabled = CBool(Current_Rights(COL_RIGHTS.原料库存管理) And RIGHTS.WRITE) And 查询ToolStripMenuItem3.Enabled
        删除ToolStripMenuItem.Enabled = CBool(Current_Rights(COL_RIGHTS.原料库存管理) And RIGHTS.WRITE) And 查询ToolStripMenuItem3.Enabled
        查询ToolStripMenuItem.Enabled = CBool(Current_Rights(COL_RIGHTS.原料库存管理) And RIGHTS.READ) And 查询ToolStripMenuItem3.Enabled

        '成品库存管理ToolStripMenuItem.Enabled = CBool(Current_Rights(COL_RIGHTS.成品库存管理))
        '录入ToolStripMenuItem1.Enabled = CBool(Current_Rights(COL_RIGHTS.成品库存管理) And RIGHTS.WRITE)
        '删除ToolStripMenuItem1.Enabled = CBool(Current_Rights(COL_RIGHTS.成品库存管理) And RIGHTS.WRITE)
        '查询ToolStripMenuItem1.Enabled = CBool(Current_Rights(COL_RIGHTS.成品库存管理) And RIGHTS.READ)

        'BOM管理ToolStripMenuItem.Enabled = CBool(Current_Rights(COL_RIGHTS.BOM管理))
        供应商管理ToolStripMenuItem.Enabled = CBool(Current_Rights(COL_RIGHTS.供应商管理) And RIGHTS.WRITE)
        用户管理ToolStripMenuItem.Enabled = CBool(Current_Rights(COL_RIGHTS.用户管理) And RIGHTS.WRITE)
        系统记录ToolStripMenuItem.Enabled = CBool(Current_Rights(COL_RIGHTS.系统记录) And RIGHTS.READ)

        财务管理ToolStripMenuItem.Enabled = CBool(Current_Rights(COL_RIGHTS.财务管理) And RIGHTS.WRITE)
        录入ToolStripMenuItem3.Enabled = CBool(Current_Rights(COL_RIGHTS.财务管理) And RIGHTS.WRITE)
        查询ToolStripMenuItem2.Enabled = CBool(Current_Rights(COL_RIGHTS.财务管理) And RIGHTS.READ)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text &= " (Build:" & My.Application.Info.Version.ToString & ") " & "用户:" & Current_UserName

        MainForm = Me

        '根据权限设置相应菜单的禁用状态
        SetAccessState()
    End Sub

    Private Sub 关于ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 关于ToolStripMenuItem.Click
        Dim pAbout As New AboutBox1
        pAbout.ShowDialog()
    End Sub

    Private Sub 用户管理ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 用户管理ToolStripMenuItem.Click
        If g_Form_Track(FORM_TYPE.用户管理) Is Nothing OrElse g_Form_Track(FORM_TYPE.用户管理).IsDisposed Then
            g_Form_Track(FORM_TYPE.用户管理) = New form_UserManage
            g_Form_Track(FORM_TYPE.用户管理).Text = "Form" & FORM_TYPE.用户管理 & "-" & g_Form_Track(FORM_TYPE.用户管理).Text
        End If
        g_Form_Track(FORM_TYPE.用户管理).MdiParent = Me
        g_Form_Track(FORM_TYPE.用户管理).Show()
        g_Form_Track(FORM_TYPE.用户管理).BringToFront()
    End Sub

    Private Sub 新建ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'Dim m_BomOper As New CBOM
        'If m_BomOper.CreateNew("333") = ERROR_CODE.SUCCESS Then
        '    MsgBox("Create OK")
        'Else
        '    MsgBox("Create Fail")
        'End If
    End Sub

    Private Sub 注销ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 注销ToolStripMenuItem.Click
        QuitReson = QUIT_RESON.logout
        Me.Close()
    End Sub

    Private Sub 退出ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出ToolStripMenuItem.Click
        QuitReson = QUIT_RESON.close
        Me.Close()
    End Sub

    Private Sub 系统记录ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 系统记录ToolStripMenuItem.Click
        If CBool(Current_Rights(COL_RIGHTS.系统记录) & RIGHTS.READ) = False Then
            MsgBox("权限不足，请联系管理员!")
            Return
        End If

        If g_Form_Track(FORM_TYPE.系统记录) Is Nothing OrElse g_Form_Track(FORM_TYPE.系统记录).IsDisposed Then
            g_Form_Track(FORM_TYPE.系统记录) = New Form_SysLog
            g_Form_Track(FORM_TYPE.系统记录).Text = "Form" & FORM_TYPE.系统记录 & "-" & g_Form_Track(FORM_TYPE.系统记录).Text
        End If
        g_Form_Track(FORM_TYPE.系统记录).MdiParent = Me
        g_Form_Track(FORM_TYPE.系统记录).Show()
        g_Form_Track(FORM_TYPE.系统记录).BringToFront()
    End Sub

    Private Function StoreSelect(ByRef storeName As String, ByRef 库位表名 As String) As Boolean
        Dim pdlg As New dlg_StoreSelect
        If dlg_StoreSelect.ShowDialog() <> DialogResult.OK Then Return False

        storeName = dlg_StoreSelect.combo_Store.Text
        库位表名 = dlg_StoreSelect.m_SelPosName
        Return True
    End Function

    Private Sub 录入ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 录入ToolStripMenuItem.Click
        If CBool(Current_Rights(COL_RIGHTS.原料库存管理) & RIGHTS.WRITE) = False Then
            MsgBox("权限不足，请联系管理员!")
            Return
        End If

        Dim StoreName As String = ""
        Dim PosName As String = ""
        If g_Form_Track(FORM_TYPE.物料入库) Is Nothing OrElse g_Form_Track(FORM_TYPE.物料入库).IsDisposed Then
            g_Form_Track(FORM_TYPE.物料入库) = New form_Material
        End If
        CType(g_Form_Track(FORM_TYPE.物料入库), form_Material).OutStore = False          '指定为入库操作
        g_Form_Track(FORM_TYPE.物料入库).Text = "物料入库"
        g_Form_Track(FORM_TYPE.物料入库).Text = "Form" & FORM_TYPE.物料入库 & "-" & g_Form_Track(FORM_TYPE.物料入库).Text
        g_Form_Track(FORM_TYPE.物料入库).MdiParent = Me
        g_Form_Track(FORM_TYPE.物料入库).Show()
        g_Form_Track(FORM_TYPE.物料入库).BringToFront()
    End Sub

    Private Sub 删除ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除ToolStripMenuItem.Click
        If CBool(Current_Rights(COL_RIGHTS.原料库存管理) & RIGHTS.WRITE) = False Then
            MsgBox("权限不足，请联系管理员!")
            Return
        End If

        Dim StoreName As String = ""
        Dim PosName As String = ""
        If g_Form_Track(FORM_TYPE.物料出库) Is Nothing OrElse g_Form_Track(FORM_TYPE.物料出库).IsDisposed Then
            g_Form_Track(FORM_TYPE.物料出库) = New form_Material
        End If
        CType(g_Form_Track(FORM_TYPE.物料出库), form_Material).OutStore = True          '指定为出库操作
        g_Form_Track(FORM_TYPE.物料出库).Text = "物料出库"
        g_Form_Track(FORM_TYPE.物料出库).Text = "Form" & FORM_TYPE.物料出库 & "-" & g_Form_Track(FORM_TYPE.物料出库).Text
        g_Form_Track(FORM_TYPE.物料出库).MdiParent = Me
        g_Form_Track(FORM_TYPE.物料出库).Show()
        g_Form_Track(FORM_TYPE.物料出库).BringToFront()
    End Sub

    Private Sub 供应商管理ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 供应商管理ToolStripMenuItem.Click
        If g_Form_Track(FORM_TYPE.供应商管理) Is Nothing OrElse g_Form_Track(FORM_TYPE.供应商管理).IsDisposed Then
            g_Form_Track(FORM_TYPE.供应商管理) = New form_SupplierManage
            g_Form_Track(FORM_TYPE.供应商管理).Text = "Form" & FORM_TYPE.供应商管理 & "-" & g_Form_Track(FORM_TYPE.供应商管理).Text
        End If
        g_Form_Track(FORM_TYPE.供应商管理).MdiParent = Me
        g_Form_Track(FORM_TYPE.供应商管理).Show()
        g_Form_Track(FORM_TYPE.供应商管理).BringToFront()
    End Sub

    'Private Sub 录入ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 录入ToolStripMenuItem1.Click
    '    If CBool(Current_Rights(COL_RIGHTS.成品库存管理) & RIGHTS.WRITE) = False Then
    '        MsgBox("权限不足，请联系管理员!")
    '        Return
    '    End If

    '    Dim StoreName As String = ""
    '    Dim PosName As String = ""
    '    Dim pForm As New form_Product
    '    pForm.OutStore = False          '指定为入库操作
    '    pForm.Text = "成品入库"
    '    pForm.MdiParent = Me
    '    pForm.Show()
    'End Sub

    'Private Sub 删除ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除ToolStripMenuItem1.Click
    '    If CBool(Current_Rights(COL_RIGHTS.成品库存管理) & RIGHTS.WRITE) = False Then
    '        MsgBox("权限不足，请联系管理员!")
    '        Return
    '    End If

    '    Dim StoreName As String = ""
    '    Dim PosName As String = ""
    '    Dim pForm As New form_Product
    '    pForm.OutStore = True          '指定为出库操作
    '    pForm.Text = "成品出库"
    '    pForm.MdiParent = Me
    '    pForm.Show()
    'End Sub

    Private Sub 查询ToolStripMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 查询ToolStripMenuItem3.Click
        If g_Form_Track(FORM_TYPE.料号查询) Is Nothing OrElse g_Form_Track(FORM_TYPE.料号查询).IsDisposed Then
            g_Form_Track(FORM_TYPE.料号查询) = New form_QuerySim
            g_Form_Track(FORM_TYPE.料号查询).Text = "Form" & FORM_TYPE.料号查询 & "-" & g_Form_Track(FORM_TYPE.料号查询).Text
        End If
        g_Form_Track(FORM_TYPE.料号查询).MdiParent = Me
        g_Form_Track(FORM_TYPE.料号查询).Show()
        g_Form_Track(FORM_TYPE.料号查询).BringToFront()
    End Sub

    Private Sub 录入ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 录入ToolStripMenuItem2.Click
        If g_Form_Track(FORM_TYPE.料号管理) Is Nothing OrElse g_Form_Track(FORM_TYPE.料号管理).IsDisposed Then
            g_Form_Track(FORM_TYPE.料号管理) = New form_PartNumber
            g_Form_Track(FORM_TYPE.料号管理).Text = "Form" & FORM_TYPE.料号管理 & "-" & g_Form_Track(FORM_TYPE.料号管理).Text
        End If
        g_Form_Track(FORM_TYPE.料号管理).MdiParent = Me
        g_Form_Track(FORM_TYPE.料号管理).Show()
        g_Form_Track(FORM_TYPE.料号管理).BringToFront()
    End Sub

    Private Sub 查询ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 查询ToolStripMenuItem.Click
        If g_Form_Track(FORM_TYPE.物料查询) Is Nothing OrElse g_Form_Track(FORM_TYPE.物料查询).IsDisposed Then
            g_Form_Track(FORM_TYPE.物料查询) = New Form_QueryStorage
            g_Form_Track(FORM_TYPE.物料查询).Text = "Form" & FORM_TYPE.物料查询 & "-" & g_Form_Track(FORM_TYPE.物料查询).Text
        End If

        g_Form_Track(FORM_TYPE.物料查询).MdiParent = Me
        g_Form_Track(FORM_TYPE.物料查询).Show()
        g_Form_Track(FORM_TYPE.物料查询).BringToFront()
    End Sub

    Private Sub 出入库记录ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 出入库记录ToolStripMenuItem.Click
        If g_Form_Track(FORM_TYPE.物料入出库记录) Is Nothing OrElse g_Form_Track(FORM_TYPE.物料入出库记录).IsDisposed Then
            g_Form_Track(FORM_TYPE.物料入出库记录) = New Form_QueryStorageRecord
            g_Form_Track(FORM_TYPE.物料入出库记录).Text = "Form" & FORM_TYPE.物料入出库记录 & "-" & g_Form_Track(FORM_TYPE.物料入出库记录).Text
        End If

        g_Form_Track(FORM_TYPE.物料入出库记录).MdiParent = Me
        g_Form_Track(FORM_TYPE.物料入出库记录).Show()
        g_Form_Track(FORM_TYPE.物料入出库记录).BringToFront()
    End Sub

    Private Sub BOM管理ToolStripMenuItem_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub 录入ToolStripMenuItem3_Click(sender As Object, e As EventArgs) Handles 录入ToolStripMenuItem3.Click
        If g_Form_Track(FORM_TYPE.财务明细录入_开发) Is Nothing OrElse g_Form_Track(FORM_TYPE.财务明细录入_开发).IsDisposed Then
            g_Form_Track(FORM_TYPE.财务明细录入_开发) = New Form_FinancialRecords_Development
            g_Form_Track(FORM_TYPE.财务明细录入_开发).Text = "Form" & FORM_TYPE.财务明细录入_开发 & "-" & g_Form_Track(FORM_TYPE.财务明细录入_开发).Text
        End If

        g_Form_Track(FORM_TYPE.财务明细录入_开发).MdiParent = Me
        g_Form_Track(FORM_TYPE.财务明细录入_开发).Show()
        g_Form_Track(FORM_TYPE.财务明细录入_开发).BringToFront()
    End Sub

    Private Sub 查询ToolStripMenuItem2_Click(sender As Object, e As EventArgs) Handles 查询ToolStripMenuItem2.Click
        If g_Form_Track(FORM_TYPE.财务明细查询_开发) Is Nothing OrElse g_Form_Track(FORM_TYPE.财务明细查询_开发).IsDisposed Then
            g_Form_Track(FORM_TYPE.财务明细查询_开发) = New Form_FinancialQuery_Development
            g_Form_Track(FORM_TYPE.财务明细查询_开发).Text = "Form" & FORM_TYPE.财务明细查询_开发 & "-" & g_Form_Track(FORM_TYPE.财务明细查询_开发).Text
        End If

        g_Form_Track(FORM_TYPE.财务明细查询_开发).MdiParent = Me
        g_Form_Track(FORM_TYPE.财务明细查询_开发).Show()
        g_Form_Track(FORM_TYPE.财务明细查询_开发).BringToFront()
    End Sub

    Private Sub tsmi_录入_销售部_Click(sender As Object, e As EventArgs) Handles tsmi_录入_销售部.Click
        If g_Form_Track(FORM_TYPE.财务明细录入_销售) Is Nothing OrElse g_Form_Track(FORM_TYPE.财务明细录入_销售).IsDisposed Then
            g_Form_Track(FORM_TYPE.财务明细录入_销售) = New Form_FinancialRecords_Sales
            g_Form_Track(FORM_TYPE.财务明细录入_销售).Text = "Form" & FORM_TYPE.财务明细录入_销售 & "-" & g_Form_Track(FORM_TYPE.财务明细录入_销售).Text
        End If

        g_Form_Track(FORM_TYPE.财务明细录入_销售).MdiParent = Me
        g_Form_Track(FORM_TYPE.财务明细录入_销售).Show()
        g_Form_Track(FORM_TYPE.财务明细录入_销售).BringToFront()
    End Sub

    Private Sub tsmi_查询_销售部_Click(sender As Object, e As EventArgs) Handles tsmi_查询_销售部.Click
        If g_Form_Track(FORM_TYPE.财务明细查询_销售) Is Nothing OrElse g_Form_Track(FORM_TYPE.财务明细查询_销售).IsDisposed Then
            g_Form_Track(FORM_TYPE.财务明细查询_销售) = New Form_FinancialQuery_Sales
            g_Form_Track(FORM_TYPE.财务明细查询_销售).Text = "Form" & FORM_TYPE.财务明细查询_销售 & "-" & g_Form_Track(FORM_TYPE.财务明细查询_销售).Text
        End If

        g_Form_Track(FORM_TYPE.财务明细查询_销售).MdiParent = Me
        g_Form_Track(FORM_TYPE.财务明细查询_销售).Show()
        g_Form_Track(FORM_TYPE.财务明细查询_销售).BringToFront()
    End Sub

    Private Sub BOMToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BOMToolStripMenuItem.Click
        If g_Form_Track(FORM_TYPE.BOM管理) Is Nothing OrElse g_Form_Track(FORM_TYPE.BOM管理).IsDisposed Then
            g_Form_Track(FORM_TYPE.BOM管理) = New form_BomManage
            g_Form_Track(FORM_TYPE.BOM管理).Text = "Form" & FORM_TYPE.BOM管理 & "-" & g_Form_Track(FORM_TYPE.BOM管理).Text
        End If

        g_Form_Track(FORM_TYPE.BOM管理).MdiParent = Me
        g_Form_Track(FORM_TYPE.BOM管理).Show()
        g_Form_Track(FORM_TYPE.BOM管理).BringToFront()
    End Sub

    Private Sub 合并备料查询ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 合并备料查询ToolStripMenuItem.Click
        If g_Form_Track(FORM_TYPE.合并用料查询) Is Nothing OrElse g_Form_Track(FORM_TYPE.合并用料查询).IsDisposed Then
            g_Form_Track(FORM_TYPE.合并用料查询) = New Form_合并备料查询
            g_Form_Track(FORM_TYPE.合并用料查询).Text = "Form" & FORM_TYPE.合并用料查询 & "-" & g_Form_Track(FORM_TYPE.合并用料查询).Text
        End If

        g_Form_Track(FORM_TYPE.合并用料查询).MdiParent = Me
        g_Form_Track(FORM_TYPE.合并用料查询).Show()
        g_Form_Track(FORM_TYPE.合并用料查询).BringToFront()
    End Sub

    Private Sub 备份管理ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles 备份管理ToolStripMenuItem1.Click
        If g_Form_Track(FORM_TYPE.备份管理) Is Nothing OrElse g_Form_Track(FORM_TYPE.备份管理).IsDisposed Then
            g_Form_Track(FORM_TYPE.备份管理) = New Form_备份管理
            g_Form_Track(FORM_TYPE.备份管理).Text = "Form" & FORM_TYPE.备份管理 & "-" & g_Form_Track(FORM_TYPE.备份管理).Text
        End If

        g_Form_Track(FORM_TYPE.备份管理).MdiParent = Me
        g_Form_Track(FORM_TYPE.备份管理).Show()
        g_Form_Track(FORM_TYPE.备份管理).BringToFront()
    End Sub
End Class


