﻿Imports System
Imports System.Linq
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports Bt
Imports Bt.ScanLib
'Imports KDPP6000.BtScanLib
Imports BTClientLib
Imports System.IO
Imports System.Reflection
Imports BtScan2

Public Class frm6010
    Inherits System.Windows.Forms.Form
    Private btScanLib As BtScanLib
    Private scanHandler As Boolean = False
    'Private dbClient As BtrieveDbClient
    Private tmpStr As List(Of Dictionary(Of String, String))
    Private affected_rows As Integer


#Region " Windows tH[€ fUCiลถฌณ๊ฝR[h "

    Public Sub New()
        MyBase.New()

        ' ฑฬฤัoตอ Windows tH[€ fUCiล•K—vลทB
        InitializeComponent()

        ' InitializeComponent() ฤัoตฬใษ๚ป๐’วมตทB

    End Sub

    ' Form อAR“|[l“g๊——ษใ—๐ภsท้ฝ฿ษ dispose ๐I[o[ChตทB
    '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 tH[€ fUCiล•K—vลทB
    'Private components As System.ComponentModel.IContainer

    '  : ศบฬvV[WอAWindows tH[€ fUCiล•K—vลทB
    'Windows tH[€ fUCi๐gมฤ•ฯXตฤญพณขB  
    ' R[h GfB^๐gมฤ•ฯXตศขลญพณขB
    'Friend WithEvents WarpCtrl1 As WarpControl.WarpCtrl
    'Friend WithEvents WarpFrame1 As WarpControl.WarpFrame
    'Friend WithEvents WarpHWBcr1 As WarpControl.WarpHWBcr
    'Friend WithEvents wfName As WarpControl.WarpField
    'Friend WithEvents wfInput As WarpControl.WarpField
    'Friend WithEvents WarpBox1 As WarpControl.WarpBox
    'Friend WithEvents wfCarton As WarpControl.WarpField
    'Friend WithEvents WarpField10 As WarpControl.WarpField

    '<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    '    Me.WarpFrame1 = New WarpControl.WarpFrame()
    '    Me.WarpField10 = New WarpControl.WarpField()
    '    Me.wfCarton = New WarpControl.WarpField()
    '    Me.WarpBox1 = New WarpControl.WarpBox()
    '    Me.wfInput = New WarpControl.WarpField()
    '    Me.wfName = New WarpControl.WarpField()
    '    Me.WarpHWBcr1 = New WarpControl.WarpHWBcr()
    '    Me.WarpFrame1.SuspendLayout()
    '    Me.SuspendLayout()
    '    '
    '    'WarpFrame1
    '    '
    '    Me.WarpFrame1.Controls.Add(Me.WarpField10)
    '    Me.WarpFrame1.Controls.Add(Me.wfCarton)
    '    Me.WarpFrame1.Controls.Add(Me.WarpBox1)
    '    Me.WarpFrame1.Controls.Add(Me.wfInput)
    '    Me.WarpFrame1.Controls.Add(Me.wfName)
    '    Me.WarpFrame1.Font = New System.Drawing.Font("MS UI Gothic", 9.0!)
    '    Me.WarpFrame1.Location = New System.Drawing.Point(0, 0)
    '    Me.WarpFrame1.Name = "WarpFrame1"
    '    Me.WarpFrame1.Size = New System.Drawing.Size(240, 296)
    '    Me.WarpFrame1.TabIndex = 1
    '    Me.WarpFrame1.Text = "WarpFrame1"
    '    Me.WarpFrame1.UseMacro = False
    '    Me.WarpFrame1.UseMacroFileName = ""
    '    '
    '    'WarpField10
    '    '
    '    Me.WarpField10.DecimalPointPosition = CType(0, Short)
    '    Me.WarpField10.Font = New System.Drawing.Font("lr SVbN", 28.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel)
    '    Me.WarpField10.FontSize = WarpControl.FontSizes.Small
    '    Me.WarpField10.Format = ""
    '    Me.WarpField10.Length = CType(6, Short)
    '    Me.WarpField10.Location = New System.Drawing.Point(12, 24)
    '    Me.WarpField10.MacroBcr = ""
    '    Me.WarpField10.MacroIn = ""
    '    Me.WarpField10.MacroKey = ""
    '    Me.WarpField10.MacroOut = ""
    '    Me.WarpField10.Name = "WarpField10"
    '    Me.WarpField10.Size = New System.Drawing.Size(90, 30)
    '    Me.WarpField10.TabIndex = CType(14, Short)
    '    Me.WarpField10.Text = "Carton"
    '    '
    '    'wfCarton
    '    '
    '    Me.wfCarton.DecimalPointPosition = CType(0, Short)
    '    Me.wfCarton.Font = New System.Drawing.Font("lr SVbN", 28.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel)
    '    Me.wfCarton.FontSize = WarpControl.FontSizes.Small
    '    Me.wfCarton.Format = ""
    '    Me.wfCarton.Length = CType(16, Short)
    '    Me.wfCarton.Location = New System.Drawing.Point(0, 60)
    '    Me.wfCarton.MacroBcr = ""
    '    Me.wfCarton.MacroIn = ""
    '    Me.wfCarton.MacroKey = ""
    '    Me.wfCarton.MacroOut = ""
    '    Me.wfCarton.Name = "wfCarton"
    '    Me.wfCarton.Size = New System.Drawing.Size(240, 30)
    '    Me.wfCarton.TabIndex = CType(6, Short)
    '    '
    '    'WarpBox1
    '    '
    '    Me.WarpBox1.BoxType = WarpControl.BoxTypes.Box
    '    Me.WarpBox1.DrawMode = WarpControl.DrawModes.Paint
    '    Me.WarpBox1.Font = New System.Drawing.Font("lr SVbN", 33.5!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel)
    '    Me.WarpBox1.ForeColor = CType(15, Short)
    '    Me.WarpBox1.Image = "C:\H081-0070.jpg"
    '    Me.WarpBox1.Location = New System.Drawing.Point(0, 180)
    '    Me.WarpBox1.Name = "WarpBox1"
    '    Me.WarpBox1.Size = New System.Drawing.Size(240, 24)
    '    Me.WarpBox1.TabIndex = 5
    '    Me.WarpBox1.Text = "WarpBox1"
    '    '
    '    'wfInput
    '    '
    '    Me.wfInput.BcrFlag = True
    '    Me.wfInput.DecimalPointPosition = CType(0, Short)
    '    Me.wfInput.Font = New System.Drawing.Font("lr SVbN", 33.5!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel)
    '    Me.wfInput.Format = ""
    '    Me.wfInput.InputMode.Secret = True
    '    Me.wfInput.Length = CType(1, Short)
    '    Me.wfInput.Location = New System.Drawing.Point(210, 112)
    '    Me.wfInput.MacroBcr = ""
    '    Me.wfInput.MacroIn = ""
    '    Me.wfInput.MacroKey = ""
    '    Me.wfInput.MacroOut = ""
    '    Me.wfInput.Name = "wfInput"
    '    Me.wfInput.Size = New System.Drawing.Size(18, 36)
    '    Me.wfInput.TabIndex = CType(4, Short)
    '    '
    '    'wfName
    '    '
    '    Me.wfName.DecimalPointPosition = CType(0, Short)
    '    Me.wfName.Font = New System.Drawing.Font("lr SVbN", 22.5!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel)
    '    Me.wfName.FontSize = WarpControl.FontSizes.Small2
    '    Me.wfName.Format = ""
    '    Me.wfName.Length = CType(20, Short)
    '    Me.wfName.Location = New System.Drawing.Point(0, 216)
    '    Me.wfName.MacroBcr = ""
    '    Me.wfName.MacroIn = ""
    '    Me.wfName.MacroKey = ""
    '    Me.wfName.MacroOut = ""
    '    Me.wfName.Name = "wfName"
    '    Me.wfName.Size = New System.Drawing.Size(240, 24)
    '    Me.wfName.TabIndex = CType(2, Short)
    '    '
    '    'WarpHWBcr1
    '    '
    '    Me.WarpHWBcr1.Decode.Code39 = True
    '    Me.WarpHWBcr1.Decode.UPC_EAN_JAN = True
    '    Me.WarpHWBcr1.DiscreteFirstLen = CType(0, Short)
    '    Me.WarpHWBcr1.DiscreteSecondLen = CType(0, Short)
    '    Me.WarpHWBcr1.InterleavedFirstLen = CType(0, Short)
    '    Me.WarpHWBcr1.InterleavedSecondLen = CType(0, Short)
    '    '
    '    'frm6010
    '    '
    '    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
    '    Me.ClientSize = New System.Drawing.Size(240, 296)
    '    Me.Controls.Add(Me.WarpFrame1)
    '    Me.Font = New System.Drawing.Font("MS UI Gothic", 9.0!)
    '    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
    '    Me.Name = "frm6010"
    '    Me.Text = "Form1"
    '    Me.WarpFrame1.ResumeLayout(False)
    '    Me.ResumeLayout(False)

    'End Sub

#End Region


    Public Enum M6010_STATUS As Integer
        INIT = 0
        MENUBACK = 1
        SHUTDOWN = 2
        NOTCODE = 3
        NOTEXIST = 4
        NOTCHECK = 5
        RETRY1 = 6
        RETRY2 = 7
        DONE = 8
    End Enum

    Public mint6010status As Integer

    Private Sub frm6010_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Select Case e.KeyCode.ToString()
            Case "F1"
            Case "F2"
                gintStatus = RETRY_STATUS.QUIT
                Me.Close()
                Exit Sub
        End Select
    End Sub

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

        '// Case QVGA -> resolution to 1/2
        If System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width = 240 Then
            Call resolution.VGAtoQVGA(Me)
        End If

        Dim wstrImage As String
        Dim rtn As Integer

        Try

            ' t[€ฬI[v“
            'rtn = WarpFrame1.Open()
            'If (rtn <> 0) Then
            '    Err.Raise(vbObjectError + 1, gAppName, "WarpError")
            'End If
#If Not Debug Then
            'sSetImgFName(G_CONST_MID_H6010, WarpBox1)
#End If
            sDisp6010()

            ' tH[€EC“ ภs
            sMain()

            ' t[€ฬN[Y
            'rtn = WarpFrame1.Close()
            'If (rtn <> 0) Then
            '    Err.Raise(vbObjectError + 1, gAppName, "WarpError")
            'End If

            ' tH[€EN[Y
            'Me.Close()

            Exit Sub

        Catch ex As Exception
            gintStatus = RETRY_STATUS.QUIT
            sErrLog(gAppName, "frm6010_Load", ex.ToString)
            Me.Close()
            Exit Sub
        End Try

    End Sub

    Private Sub btScanLib_OnScan(ByVal result As String)
        Dim buf As String = result
        Dim rtn As Integer

        If (Not fCheckCode(buf)) Then
            sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
#If Not Debug Then
                        fFnc_WrpMsg("E", G_CONST_MID_E2E25)
#End If
            Exit Sub
        End If

        '“—อ—
        rtn = fProcInput(RTrim(buf))

        If rtn = 0 Then
            Select Case mint6010status
                Case M6010_STATUS.INIT
                    'gintStatus = AGAIN_STATUS.CARTON


                Case M6010_STATUS.MENUBACK          'MENUBACK

                    RemoveHandler btScanLib.OnScan, AddressOf btScanLib_OnScan
                    btScanLib.Disable()

                    gintStatus = RETRY_STATUS.NAME
                    Me.Close()
                    Exit Sub

                Case M6010_STATUS.SHUTDOWN          'HTCTRL.ExeName๐มตฤI—น
                    sFinishApp()
                    gintStatus = RETRY_STATUS.QUIT
                    Me.Close()
                    Exit Sub

                Case M6010_STATUS.NOTCODE              'GศOศฬลดืฐาฏพฐผ
                    sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
                    sOutputLog(gstrctPicsIni.strOutput_KDPP, "KDPP6E15", G_CONST_BC_USER, buf)
#If Not Debug Then
                                fFnc_WrpMsg("E", G_CONST_MID_E6E15)
#End If
                    Exit Sub
                Case M6010_STATUS.NOTEXIST          'Qฦรฐภษ–ณขฬลดืฐาฏพฐผ
                    sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
#If Not Debug Then
                                fFnc_WrpMsg("E", G_CONST_MID_E6E20)

#End If
                    Exit Sub
                Case M6010_STATUS.NOTCHECK          'ฝรฐภฝช"7"ศOฬฝ฿มชฏธ•sย
                    sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
#If Not Debug Then
                                fFnc_WrpMsg("E", G_CONST_MID_E6E25)

#End If
                    Exit Sub
                Case M6010_STATUS.RETRY1

                    RemoveHandler btScanLib.OnScan, AddressOf btScanLib_OnScan
                    btScanLib.Disable()

                    gintStatus = RETRY_STATUS.RETRY1
                    Me.Close()
                    Exit Sub

                Case M6010_STATUS.RETRY2

                    RemoveHandler btScanLib.OnScan, AddressOf btScanLib_OnScan
                    btScanLib.Disable()

                    gintStatus = RETRY_STATUS.RETRY2
                    Me.Close()
                    Exit Sub

                Case M6010_STATUS.DONE              'ณํษQฦลซฝ


            End Select

        Else

            'f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, G_CONST_EM_DB, MWA_DB_WARNING, MWA_DB_OK)
            MessageBox.Show(G_CONST_EM_DB, G_CONST_EMT_DB, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)


        End If


    End Sub


    ' t[€EC“
    Public Sub sMain()

        '        Dim WKey As Short
        '        Dim buf As String
        '        Dim rtn, rtn1 As Integer
        '        Dim wintCnt As Integer

        '        Try

        '            ' t[€ฬ—๐ฑฑษLqตทB
        '            Do While (True)

        '                ' R[h“—อ
        '                WKey = WarpFrame1.Input(wfInput)

        '                If WKey = WKY_ENT Then


        '                ElseIf WKey = WKY_BCR Then


        '                    'สฐบฐฤ“—อฬๆ“พ
        '                    rtn = WarpHWBcr1.Get(buf)
        '                    sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.OK)
        '                    If (rtn <> 0) Then
        '                        Err.Raise(vbObjectError + 1, gAppName, "WarpError")
        '                    End If

        '                    'มชฏธรจผฏฤ
        '                    If (Not fCheckCode(buf)) Then
        '                        sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
        '#If Not Debug Then
        '                        fFnc_WrpMsg("E", G_CONST_MID_E2E25)
        '#End If
        '                        Exit Do
        '                    End If

        '                    '“—อ—
        '                    rtn = fProcInput(RTrim(buf))

        '                    If rtn = 0 Then
        '                        Select Case mint6010status
        '                            Case M6010_STATUS.INIT
        '                                'gintStatus = AGAIN_STATUS.CARTON


        '                            Case M6010_STATUS.MENUBACK          'MENUBACK
        '                                gintStatus = RETRY_STATUS.NAME
        '                                Exit Do

        '                            Case M6010_STATUS.SHUTDOWN          'HTCTRL.ExeName๐มตฤI—น
        '                                sFinishApp()
        '                                gintStatus = RETRY_STATUS.QUIT

        '                            Case M6010_STATUS.NOTCODE              'GศOศฬลดืฐาฏพฐผ
        '                                sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
        '                                sOutputLog(gstrctPicsIni.strOutput_KDPP, "KDPP6E15", G_CONST_BC_USER, buf)
        '#If Not Debug Then
        '                                fFnc_WrpMsg("E", G_CONST_MID_E6E15)
        '#End If
        '                            Case M6010_STATUS.NOTEXIST          'Qฦรฐภษ–ณขฬลดืฐาฏพฐผ
        '                                sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
        '#If Not Debug Then
        '                                fFnc_WrpMsg("E", G_CONST_MID_E6E20)

        '#End If
        '                            Case M6010_STATUS.NOTCHECK          'ฝรฐภฝช"7"ศOฬฝ฿มชฏธ•sย
        '                                sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
        '#If Not Debug Then
        '                                fFnc_WrpMsg("E", G_CONST_MID_E6E25)

        '#End If
        '                            Case M6010_STATUS.RETRY1
        '                                gintStatus = RETRY_STATUS.RETRY1
        '                                Exit Do

        '                            Case M6010_STATUS.RETRY2
        '                                gintStatus = RETRY_STATUS.RETRY2
        '                                Exit Do

        '                            Case M6010_STATUS.DONE              'ณํษQฦลซฝ


        '                        End Select

        '                    Else

        '                        f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, G_CONST_EM_DB, MWA_DB_WARNING, MWA_DB_OK)

        '                    End If
        '                    Exit Do

        '                ElseIf WKey = WKY_F1 Then

        '                ElseIf WKey = WKY_F2 Then

        '                ElseIf WKey = WKY_ERROR Then

        '                    f0.WarpCtrl1.MessageBox(G_CONST_EMT_WARP, G_CONST_EM_WARP, MWA_DB_WARNING, MWA_DB_OK)

        '                End If

        '            Loop

        '            Exit Sub

        '        Catch ex As Exception
        '            sErrLog(gAppName, "sMain", ex.ToString)
        '        End Try

    End Sub

    '/*
    '|| ึ”–ผFsDisp2035
    '|| T—v  F2035ๆ–ส•\ฆ
    '*/
    Private Sub sDisp6010()


        'wfName.Puts(gstrctKDHT.strUserName)

        wfName.Text = gstrctKDHT.strUserName

    End Sub

    '/*
    '|| ึ”–ผFfProcInput
    '|| T—v  F“—อ—
    '*/
    Private Function fProcInput(ByVal pstrInput As String) As Integer

        Dim rtn As Integer
        Dim wstrEmplCode As String
        Dim wstrSecKey(2) As String
        Dim wstrSecKeyValue(2) As String
        Dim wstrCenterCode As String
        Dim wintCnt As Integer
        Dim wintCnt2 As Integer
        Dim wstrWhere As String
        Dim wstrKey(2) As String
        Dim wstrKeyValue(2) As String

        fProcInput = -1

        Try

            If pstrInput = "OCMENUBACK" Then             'OCMENUBACKศ็ฮ

                mint6010status = M6010_STATUS.MENUBACK
                fProcInput = 0
                Exit Function

            ElseIf pstrInput = "OCSHUTDOWN" Then             'OCSHUTDOWNศ็ฮ

                mint6010status = M6010_STATUS.SHUTDOWN
                fProcInput = 0
                Exit Function

            ElseIf pstrInput.StartsWith("C") Then        'ๆ“ชชCศ็ฮ

                gstrctKDPP.strItemGr = Mid(pstrInput, 2)
                With gstrctPPDisp
                    .strAic = Mid(pstrInput, 2, 4)
                    .strMsc = Mid(pstrInput, 6, 3)
                    .strPNT = Mid(pstrInput, 9, 3)
                    .strTRM = Mid(pstrInput, 12, 3)
                    .strCaseNo = Mid(pstrInput, 15, 7)
                    .strCrtnNo = Mid(pstrInput, 22, 5)
                    .strCrtnFrom = Mid(pstrInput, 22, 5)
                End With

                rtn = fDemotePackCJ(wintCnt)
                If rtn = -1 Then
                    Exit Function
                ElseIf rtn = -2 Then
                    mint6010status = M6010_STATUS.NOTEXIST
                    fProcInput = 0
                    Exit Function
                End If

                gstrctPPDisp.intNumOfPkg = wintCnt
                gstrctPPDisp.intNumOfScanned = 0
                gstrctPPDisp.sResetQty()
                gstrctPPDisp.sResetPkg()
                If gstrctPPDisp.intNumOfPkg < 6 Then
                    'If 1 < 0 Then
                    rtn = fProcData4SRetry2("NOUPDATE", wintCnt)
                    If rtn = -1 Then
                        Exit Function
                    End If
                    mint6010status = M6010_STATUS.RETRY2

                Else
                    gstrctPPDispPart.bDisp = False
                    mint6010status = M6010_STATUS.RETRY1

                End If
            Else

                mint6010status = M6010_STATUS.NOTCODE
                fProcInput = 0
                Exit Function

            End If

            fProcInput = 0
            Exit Function

        Catch ex As Exception
            sErrLog(gAppName, "fProcInput", ex.ToString)
        End Try
    End Function

    Private Sub frm6010_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        ' Enable BTScanLib
        If Not scanHandler Then
            btScanLib = btScanLib.getInstance()
            AddHandler btScanLib.OnScan, AddressOf btScanLib_OnScan
            scanHandler = True
            btScanLib.Debug = False
            btScanLib.Enable()
        End If
    End Sub
End Class
