Imports iMESExceptionManager

Public Class frmBinStateAdjust

    Inherits System.Windows.Forms.Form

    '//Public Variables
    Public LotNo As String = defString
    Public dvSysBin As DataView
    Public dtGoodBin As DataTable
    Public dtBinNew As DataTable
    Public PrivFunNo As String = defString

    '//資料表相關變數

    Dim drSel As DataRow()
    Dim drAdd As DataRow

    '//控制項物件

    Dim lblBinNo() As Label
    Dim txtOBin() As TextBox
    '  Dim txtBin_R() As TextBox   '剩餘
    Dim txtBin_S() As TextBox   'Split

    '//其他共用變數
    Dim aryRBin() As Long
    Dim intCount As Integer
    Dim i, j As Integer
    Friend WithEvents lblOnlyAllowOrgBinSub As Label

#Region " Windows Form 設計工具產生的程式碼 "

    Public Sub New()
        MyBase.New()

        '此呼叫為 Windows Form 設計工具的必要項。

        InitializeComponent()

        '在 InitializeComponent() 呼叫之後加入所有的初始設定

    End Sub

    'Form 覆寫 Dispose 以清除元件清單。

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    '為 Windows Form 設計工具的必要項
    Private components As System.ComponentModel.IContainer

    '注意: 以下為 Windows Form 設計工具所需的程序

    '您可以使用 Windows Form 設計工具進行修改。

    '請勿使用程式碼編輯器來修改這些程序。

    Friend WithEvents pnlBin As System.Windows.Forms.Panel
    Friend WithEvents LineFrame1 As iMESWizards.LineFrame
    Friend WithEvents lblOrgBinState As System.Windows.Forms.Label
    Friend WithEvents lblFinalBinState As System.Windows.Forms.Label
    Friend WithEvents btnConfirm As Infragistics.Win.Misc.UltraButton
    Friend WithEvents btnClose As Infragistics.Win.Misc.UltraButton
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.pnlBin = New System.Windows.Forms.Panel()
        Me.lblFinalBinState = New System.Windows.Forms.Label()
        Me.LineFrame1 = New iMESWizards.LineFrame()
        Me.lblOrgBinState = New System.Windows.Forms.Label()
        Me.btnConfirm = New Infragistics.Win.Misc.UltraButton()
        Me.btnClose = New Infragistics.Win.Misc.UltraButton()
        Me.lblOnlyAllowOrgBinSub = New System.Windows.Forms.Label()
        Me.pnlBin.SuspendLayout()
        Me.SuspendLayout()
        '
        'pnlBin
        '
        Me.pnlBin.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
            Or System.Windows.Forms.AnchorStyles.Left) _
            Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.pnlBin.AutoScroll = True
        Me.pnlBin.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.pnlBin.Controls.Add(Me.lblFinalBinState)
        Me.pnlBin.Controls.Add(Me.LineFrame1)
        Me.pnlBin.Controls.Add(Me.lblOrgBinState)
        Me.pnlBin.Location = New System.Drawing.Point(16, 41)
        Me.pnlBin.Name = "pnlBin"
        Me.pnlBin.Size = New System.Drawing.Size(696, 128)
        Me.pnlBin.TabIndex = 106
        '
        'lblFinalBinState
        '
        Me.lblFinalBinState.Font = New System.Drawing.Font("Times New Roman", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblFinalBinState.Location = New System.Drawing.Point(16, 53)
        Me.lblFinalBinState.Name = "lblFinalBinState"
        Me.lblFinalBinState.Size = New System.Drawing.Size(80, 15)
        Me.lblFinalBinState.TabIndex = 79
        Me.lblFinalBinState.Text = "FinalBinState"
        Me.lblFinalBinState.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'LineFrame1
        '
        Me.LineFrame1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
            Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.LineFrame1.Location = New System.Drawing.Point(0, 23)
        Me.LineFrame1.Name = "LineFrame1"
        Me.LineFrame1.Size = New System.Drawing.Size(692, 4)
        Me.LineFrame1.TabIndex = 78
        Me.LineFrame1.Text = "LineFrame1"
        '
        'lblOrgBinState
        '
        Me.lblOrgBinState.Font = New System.Drawing.Font("Times New Roman", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblOrgBinState.Location = New System.Drawing.Point(16, 30)
        Me.lblOrgBinState.Name = "lblOrgBinState"
        Me.lblOrgBinState.Size = New System.Drawing.Size(80, 15)
        Me.lblOrgBinState.TabIndex = 76
        Me.lblOrgBinState.Text = "OrgBinState"
        Me.lblOrgBinState.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'btnConfirm
        '
        Me.btnConfirm.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnConfirm.Location = New System.Drawing.Point(544, 186)
        Me.btnConfirm.Name = "btnConfirm"
        Me.btnConfirm.Size = New System.Drawing.Size(80, 32)
        Me.btnConfirm.TabIndex = 114
        Me.btnConfirm.Text = "Confirm"
        '
        'btnClose
        '
        Me.btnClose.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnClose.Location = New System.Drawing.Point(632, 186)
        Me.btnClose.Name = "btnClose"
        Me.btnClose.Size = New System.Drawing.Size(80, 32)
        Me.btnClose.TabIndex = 113
        Me.btnClose.Text = "Close"
        '
        'lblOnlyAllowOrgBinSub
        '
        Me.lblOnlyAllowOrgBinSub.Font = New System.Drawing.Font("Times New Roman", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblOnlyAllowOrgBinSub.ForeColor = System.Drawing.Color.Red
        Me.lblOnlyAllowOrgBinSub.Location = New System.Drawing.Point(15, 9)
        Me.lblOnlyAllowOrgBinSub.Name = "lblOnlyAllowOrgBinSub"
        Me.lblOnlyAllowOrgBinSub.Size = New System.Drawing.Size(231, 29)
        Me.lblOnlyAllowOrgBinSub.TabIndex = 115
        Me.lblOnlyAllowOrgBinSub.Text = "OrgBin sub Qty only"
        Me.lblOnlyAllowOrgBinSub.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'frmBinStateAdjust
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)
        Me.ClientSize = New System.Drawing.Size(728, 228)
        Me.ControlBox = False
        Me.Controls.Add(Me.lblOnlyAllowOrgBinSub)
        Me.Controls.Add(Me.btnConfirm)
        Me.Controls.Add(Me.btnClose)
        Me.Controls.Add(Me.pnlBin)
        Me.Font = New System.Drawing.Font("Times New Roman", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "frmBinStateAdjust"
        Me.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "BinStateAdjust "
        Me.pnlBin.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

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

        Cursor.Current = Cursors.WaitCursor

        If LotNo = defString Then
            iMESExceptionManager.ExceptionManager.iMESMsgBoxShowDialog(ChgTranslateName_Msg("0000-200001", "[%LotNo%]"), MsgBoxStyle.Exclamation, Me.Text)
            Exit Sub
        End If

        If dtGoodBin Is Nothing Then
            iMESExceptionManager.ExceptionManager.iMESMsgBoxShowDialog(ChgTranslateName_Msg("0000-200001", "GoodBin state"), MsgBoxStyle.Exclamation, Me.Text)
            Exit Sub
        End If

        Call funCreateBin()

        Call ExeChangeResource(Me, gLanguageMode)   '語系切換
        lblOnlyAllowOrgBinSub.ForeColor = Color.Red
        '群組權限控管
        'ExeGroupControlPriv(Me, PrivFunNo)

        Cursor.Current = Cursors.Default

    End Sub

    Private Sub frmLotSplit_BinAssign_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated

        If Not txtBin_S Is Nothing Then txtBin_S(0).Focus()

    End Sub


    '//Private Sub or Function-------------------------------------------------------------------------------------------------
    Private Function funCreateBin()

        If dvSysBin Is Nothing Then Exit Function
        dvSysBin.Sort = "BinOrder, BinNo"
        intCount = dvSysBin.Count

        '重新宣告物件陣列
        ReDim lblBinNo(intCount - 1)
        ReDim txtOBin(intCount - 1)
        'ReDim txtBin_R(intCount - 1)
        ReDim txtBin_S(intCount - 1)

        Dim fFont As New Font("Times New Roman", 9)
        Dim x, y As Long

        x = 0
        y = 0
        For i = 0 To intCount - 1
            'BinNo
            lblBinNo(i) = New Label
            lblBinNo(i).Location = New System.Drawing.Point(104 + x, 6)
            lblBinNo(i).Size = New System.Drawing.Size(40, 16)
            lblBinNo(i).ForeColor = System.Drawing.Color.Blue
            lblBinNo(i).Font = fFont
            lblBinNo(i).Text = "Bin" & Mid(dvSysBin.Item(i)("BinNo"), 4)
            lblBinNo(i).Tag = dvSysBin.Item(i)("BinNo") 'BinNo
            lblBinNo(i).TextAlign = ContentAlignment.TopCenter
            pnlBin.Controls.Add(lblBinNo(i))

            'GoodBin
            txtOBin(i) = New TextBox
            txtOBin(i).Location = New System.Drawing.Point(104 + x, 28)
            txtOBin(i).Size = New System.Drawing.Size(40, 21)
            txtOBin(i).Text = 0
            txtOBin(i).ReadOnly = True
            txtOBin(i).TextAlign = HorizontalAlignment.Right
            txtOBin(i).TabIndex = i

            pnlBin.Controls.Add(txtOBin(i))
            AddHandler txtOBin(i).KeyPress, AddressOf Me.txtValue_KeyPress
            AddHandler txtOBin(i).Enter, AddressOf Me.txtValue_Enter
            AddHandler txtOBin(i).TextChanged, AddressOf Me.txtValue_TextChanged

            '剩餘Bin
            'txtBin_R(i) = New TextBox
            'txtBin_R(i).Location = New System.Drawing.Point(104 + x, 50)
            'txtBin_R(i).Size = New System.Drawing.Size(40, 21)
            'txtBin_R(i).Text = 0
            'txtBin_R(i).ReadOnly = True
            'txtBin_R(i).TextAlign = HorizontalAlignment.Right
            'txtBin_R(i).TabIndex = i + intCount
            '
            'pnlBin.Controls.Add(txtBin_R(i))
            'AddHandler txtBin_R(i).KeyPress, AddressOf Me.txtValue_KeyPress
            'AddHandler txtBin_R(i).Enter, AddressOf Me.txtValue_Enter
            'AddHandler txtBin_R(i).TextChanged, AddressOf Me.txtValue_TextChanged

            'SplitBin
            txtBin_S(i) = New TextBox
            txtBin_S(i).Location = New System.Drawing.Point(104 + x, 53)
            txtBin_S(i).Size = New System.Drawing.Size(40, 21)
            txtBin_S(i).Text = 0
            txtBin_S(i).TextAlign = HorizontalAlignment.Right
            txtBin_S(i).TabIndex = i + (intCount * 2)

            pnlBin.Controls.Add(txtBin_S(i))
            AddHandler txtBin_S(i).KeyPress, AddressOf Me.txtValue_KeyPress
            AddHandler txtBin_S(i).Enter, AddressOf Me.txtValue_Enter
            AddHandler txtBin_S(i).TextChanged, AddressOf Me.txtValue_TextChanged

            x = x + 42
        Next
        ReDim aryRBin(intCount - 1)
        '將GoodBin資料放上
        If dtGoodBin.Rows.Count > 0 Then
            For i = 0 To intCount - 1
                txtOBin(i).Text = dtGoodBin.Rows(0)(lblBinNo(i).Tag)
                'txtBin_R(i).Text = txtOBin(i).Text
                aryRBin(i) = txtOBin(i).Text

                If (txtOBin(i).Text = "0") Then
                    txtBin_S(i).ReadOnly = True
                End If
            Next
        End If
        Dim bRE As Boolean = False
        '顯示已Assign的BinValue
        drSel = dtBinNew.Select("LotNo ='" & LotNo & "'")
        If drSel.Length <> 0 Then
            'SplitBin
            For i = 0 To intCount - 1
                If (txtBin_S(i).ReadOnly = True) Then
                    bRE = True
                    txtBin_S(i).ReadOnly = False
                Else
                    bRE = False
                End If
                txtBin_S(i).Text = drSel(0)(lblBinNo(i).Tag)

                If (bRE = True) Then txtBin_S(i).ReadOnly = True
            Next
        End If

        '剩餘Bin
        ' For i = 0 To dtBinNew.Rows.Count - 1
        '     If dtBinNew.Rows(i)("LotNo") <> LotNo Then
        '         For j = 0 To intCount - 1
        '             If dtBinNew.Rows(i)(lblBinNo(j).Tag) <> 0 Then
        '                 aryRBin(j) = aryRBin(j) - CLng(dtBinNew.Rows(i)(lblBinNo(j).Tag))
        '             End If
        '         Next
        '     End If
        ' Next
        ' For i = 0 To intCount - 1
        '     txtBin_R(i).Text = aryRBin(i)
        ' Next




    End Function

    Private Sub txtValue_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs)
        sender.SelectAll()
    End Sub
    Private Sub txtValue_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If e.KeyChar = Convert.ToChar(13) Then
            SendKeys.Send("{TAB}")
        End If
    End Sub
    Private Sub txtValue_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'btnSave.Enabled = False
    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub


    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        gReturnKeyValue = ""
        Me.Close()
    End Sub


    Private Sub btnConfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirm.Click

        Cursor.Current = Cursors.WaitCursor

        'Check
        For i = 0 To intCount - 1
            If Not IsNumeric(txtBin_S(i).Text) Then
                iMESExceptionManager.ExceptionManager.iMESMsgBoxShowDialog(ChgTranslateName_Msg("0000-200011", lblBinNo(i).Text & " [%MUST BE NUMERIC%]"), MsgBoxStyle.Exclamation, Me.Text)
                txtBin_S(i).Focus()
                Exit Sub
            End If

            If (txtBin_S(i).ReadOnly = False) Then
                If (CInt(txtBin_S(i).Text) < 0) Then
                    iMESExceptionManager.ExceptionManager.iMESMsgBoxShowDialog(ChgTranslateName_Msg("0000-200017", lblBinNo(i).Text), MsgBoxStyle.Exclamation, Me.Text)
                    txtBin_S(i).Focus()
                    Exit Sub
                End If
            End If

            '輸入值不可大於剩餘Bin
            If CLng(txtBin_S(i).Text) > CLng(txtOBin(i).Text) Then
                iMESExceptionManager.ExceptionManager.iMESMsgBoxShowDialog(ChgTranslateName_Msg("0000-200060", "[%FinalBin%]:" & lblBinNo(i).Text & " must less than total numbers of OrgBin:" & lblBinNo(i).Text), MsgBoxStyle.Exclamation, Me.Text)
                txtBin_S(i).Focus()
                Exit Sub
            End If
        Next i

        '加入SplitBin
        drSel = dtBinNew.Select("LotNo = '" & LotNo & "'")
        If drSel.Length = 0 Then    'Add
            drAdd = dtBinNew.NewRow
            drAdd("LotNo") = LotNo
            For i = 0 To intCount - 1
                drAdd(lblBinNo(i).Tag) = txtBin_S(i).Text
            Next
            dtBinNew.Rows.Add(drAdd)
        Else    'Update
            drSel(0).BeginEdit()
            For i = 0 To intCount - 1
                drSel(0)(lblBinNo(i).Tag) = txtBin_S(i).Text
            Next
            drSel(0).EndEdit()
        End If


        gReturnKeyValue = LotNo    '做為判斷是否變更的Flag

        Cursor.Current = Cursors.Default

        Me.Close()
    End Sub
End Class

