﻿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 BTClientLib
Imports System.IO
Imports System.Reflection
Imports Newtonsoft
Imports Microsoft.WindowsCE.Forms

Public Class frm1E80
    Inherits System.Windows.Forms.Form
    '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 WarpFrame1 As WarpControl.WarpFrame
    'Friend WithEvents WarpBox1 As WarpControl.WarpBox
    'Friend WithEvents wfName As WarpControl.WarpField
    'Friend WithEvents WarpHWBcr1 As WarpControl.WarpHWBcr
    'Friend WithEvents wfInput As WarpControl.WarpField
    '<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    '    Me.WarpFrame1 = New WarpControl.WarpFrame()
    '    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.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 = ""
    '
    '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 = ""
    'Me.WarpBox1.Location = New System.Drawing.Point(0, 36)
    'Me.WarpBox1.Name = "WarpBox1"
    'Me.WarpBox1.Size = New System.Drawing.Size(240, 96)
    'Me.WarpBox1.TabIndex = 0
    '
    'wfInput
    '
    '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.ForeColor = CType(15, Short)
    'Me.wfInput.Format = ""
    'Me.wfInput.InputMode.Secret = True
    'Me.wfInput.Length = CType(1, Short)
    'Me.wfInput.Location = New System.Drawing.Point(216, 145)
    '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(47, 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(1, Short)
    '
    'WarpHWBcr1
    '
    'Me.WarpHWBcr1.Decode.Code39 = 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)
    '
    'frm1E80
    '
    '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 = "frm1E80"
    'Me.Text = "Form1"
    'Me.WarpFrame1.ResumeLayout(False)
    'Me.ResumeLayout(False)

    'End Sub

#End Region
    Public Enum M1E80_STATUS As Integer
        INIT = 0
        MENUBACK = 1
        NOTA = 2
        DONE = 3
    End Enum

    Public mint1E80status As Integer

    Private Sub frm1E80_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
            mint1E80status = M1E80_STATUS.INIT

            '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_E1E81, WarpBox1, 1)
            'sSetImgFName(G_CONST_MID_E1E80, WarpBox1, 1)
#End If
            sDisp1E80()
            ' 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

        End Try

    End Sub

    '/*
    '|| ึ”–ผFsDisp1E80
    '|| T—v  F1E80ๆ–ส•\ฆ
    '*/
    Private Sub sDisp1E80()

        'wfName.Puts(gstrctKDHT.strUserName)

        wfName.Text = gstrctKDHT.strUserName

    End Sub
    ' t[€EC“
    Public Sub sMain()

        'Dim WKey As Short
        'Dim buf As String
        'Dim rtn As Integer

        'Try

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

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

        '        If WKey = WKY_ENT Then
        '            'ทฌพู—
        '            If fCancelPallete() = 1 Then
        '                'pbg”ธZ
        '                rtn = fUpdPaletteQty(0)
        '                If rtn = -1 Then
        '                    BtLib.Transaction.Abort()
        '                    f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, fEditErrorInfo(RTrim(gclsDB.BtriveStatus), RTrim(gclsDB.ErrFile)), MWA_DB_WARNING, MWA_DB_OK)
        '                    Exit Sub
        '                End If
        '                gintStatus = PL_STATUS.PALETTESCN
        '                rtn = fClearSKey2()
        '                If rtn = -1 Then
        '                    f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, fEditErrorInfo(RTrim(gclsDB.BtriveStatus), RTrim(gclsDB.ErrFile)), MWA_DB_WARNING, MWA_DB_OK)
        '                End If
        '                fMakeCommandFile(G_CONST_PROCESS_PAL_DEL, G_CONST_SPEECH_CCLPALT)
        '                Exit Do
        '            Else
        '                f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, fEditErrorInfo(RTrim(gclsDB.BtriveStatus), RTrim(gclsDB.ErrFile)), MWA_DB_WARNING, MWA_DB_OK)
        '            End If
        '        ElseIf WKey = WKY_BCR Then


        '        ElseIf WKey = WKY_F1 Then

        '            gintStatus = PL_STATUS.PCSCAN
        '            Exit Do
        '        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

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

    End Sub

    '*******************************************************************************
    '* @”\     : oษทฌพู—
    '* ๘”     :   
    '* –฿่’l   : 1:OK -1:ERROR
    '* ”๕l     : ศต
    '*******************************************************************************
    Private Function fCancelPallete() As Integer
        Dim rtn As Integer
        Dim wstrctSPM As STRCT_KEY_SPM2
        Dim wstrKey(6) As String
        Dim wstrKeyValue(6) As String
        Dim wstrWhere As String
        Const KEY1 = "DEL01"
        Const KEY2 = "DEL02"
        fCancelPallete = -1
        wstrKey(0) = "PaletteID"
        wstrKey(1) = "ItemGr"
        wstrKey(2) = "ItemID"
        wstrKey(3) = "ItemCode"
        wstrKey(4) = "ItemInID"
        wstrKey(5) = "ItemInCode"
        wstrKey(6) = "TempSerial"

        With gstrctAicCase
            wstrKeyValue(0) = "A"
            wstrKeyValue(1) = .AicCaseNo & .YYYYMMDD & Space(2)
            wstrKeyValue(2) = Space(1)
            wstrKeyValue(3) = Space(30)
            wstrKeyValue(4) = Space(1)
            wstrKeyValue(5) = Space(30)
            wstrKeyValue(6) = Space(6)

        End With

        wstrWhere = ""
        wstrWhere = wstrKey(0) & " = '" & wstrKeyValue(0) & "'"
        wstrWhere &= " and " & wstrKey(1) & " = '" & wstrKeyValue(1) & "'"
        wstrWhere &= " and " & wstrKey(2) & " = '" & wstrKeyValue(2) & "'"
        wstrWhere &= " and " & wstrKey(3) & " = '" & wstrKeyValue(3) & "'"
        wstrWhere &= " and " & wstrKey(4) & " = '" & wstrKeyValue(4) & "'"
        wstrWhere &= " and " & wstrKey(5) & " = '" & wstrKeyValue(5) & "'"
        wstrWhere &= " and " & wstrKey(6) & " = '" & wstrKeyValue(6) & "'"

        tmpStr = dbClient.Query("SELECT * FROM " + G_CONST_TBL_PACKAP + " WHERE " + wstrWhere)

        Dim values As Dictionary(Of String, String) = New Dictionary(Of String, String)
        Dim sItemGr As String = ""
        Dim i As Integer = 0

        If tmpStr.Count > 0 Then
            Try
                Do
                    values = tmpStr(i)

                    Dim result As String = Nothing
                    If values.TryGetValue("ItemGr", result) Then
                        sItemGr = RTrim(result)
                    End If
                   
                    If RTrim(wstrKeyValue(1)) = RTrim(sItemGr) Then
                        rtn = fDeletePackAP(KEY1)
                    Else
                        Exit Do
                    End If



                    i = i + 1

                    If tmpStr.Count = i Then
                        Exit Do
                    End If

                Loop



            Catch ex As Exception

            End Try

        Else

            sErrLog(gAppName, "fCancelPallete", "Error")
            Exit Function

        End If


        tmpStr = dbClient.Query("SELECT * FROM " + G_CONST_TBL_PACKAJ + " WHERE " + wstrWhere)

        Dim values1 As Dictionary(Of String, String) = New Dictionary(Of String, String)
        Dim sItemGr1 As String = ""
        Dim i1 As Integer = 0

        If tmpStr.Count > 0 Then

            Try
                Do

                    values1 = tmpStr(i)

                    Dim result1 As String = Nothing
                    If values1.TryGetValue("ItemGr", result1) Then
                        sItemGr1 = RTrim(result1)
                    End If

                    If RTrim(wstrKeyValue(1)) = RTrim(sItemGr1) Then
                        rtn = fDeletePackAJ(KEY2)
                    Else
                        Exit Do
                    End If

                    i1 = i1 + 1

                    If tmpStr.Count = i1 Then
                        Exit Do
                    End If

                Loop



            Catch ex As Exception

            End Try



        End If

        'PlanXV
        rtn = fSetPlan("0", "0000")
        If rtn <> 1 Then
            'BtLib.Transaction.Abort()
            sErrLog(gAppName, "fCancelPallete", "Error")
            Exit Function
        End If
        'BtLib.Transaction.End()

        fCancelPallete = 1



        'BtLib.Transaction.BeginConCurrent()
        'rtn = gclsDB.fOpenRecord(G_CONST_TBL_PACKAP, G_CONST_KEY_PACKAPKEY00, KEY1, wstrKey, wstrKeyValue, BtLib.Operation.GetGreaterOrEqual)
        'If rtn = -1 Then
        '    sErrLog(gAppName, "fCancelPallete", gclsDB.ErrMsg)
        '    Exit Function
        'End If
        'If rtn = 0 Then
        '    Do
        '        If RTrim(wstrKeyValue(1)) = RTrim(gclsDB.Records(KEY1).Item("ItemGr")) Then
        '            rtn = fDeletePackAP(KEY1)
        '        Else
        '            Exit Do
        '        End If
        '        If gclsDB.Records(KEY1).Read(BtLib.Operation.GetNext) <> 0 Then
        '            'R[hชศญศ๊ฮI—นท้
        '            Exit Do
        '        End If
        '    Loop
        'End If
        'gclsDB.fCloseRecord(KEY1)
        'rtn = gclsDB.fOpenRecord(G_CONST_TBL_PACKAJ, G_CONST_KEY_PACKAJKEY00, KEY2, wstrKey, wstrKeyValue, BtLib.Operation.GetGreaterOrEqual)
        'If rtn = -1 Then
        '    'BtLib.Transaction.Abort()
        '    sErrLog(gAppName, "fCancelPallete", gclsDB.ErrMsg)
        '    Exit Function
        'End If
        'If rtn = 0 Then
        '    Do
        '        If RTrim(wstrKeyValue(1)) = RTrim(gclsDB.Records(KEY2).Item("ItemGr")) Then
        '            rtn = fDeletePackAJ(KEY2)
        '        Else
        '            Exit Do
        '        End If
        '        If gclsDB.Records(KEY2).Read(BtLib.Operation.GetNext) <> 0 Then
        '            'R[hชศญศ๊ฮI—นท้
        '            Exit Do
        '        End If
        '    Loop
        'End If
        'gclsDB.fCloseRecord(KEY2)

        ''PlanXV
        'rtn = fSetPlan("0", "0000")
        'If rtn <> 1 Then
        '    'BtLib.Transaction.Abort()
        '    sErrLog(gAppName, "fCancelPallete", gclsDB.ErrMsg)
        '    Exit Function
        'End If
        ''BtLib.Transaction.End()

        'fCancelPallete = 1
    End Function

    Private Sub frm1E80_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Select Case e.KeyCode.ToString()
            Case "Return"
                If fCancelPallete() = 1 Then
                    Dim rtn As Integer = 0
                    'pbg”ธZ
                    rtn = fUpdPaletteQty(0)
                    If rtn = -1 Then
                        'BtLib.Transaction.Abort()
                        'f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, fEditErrorInfo(RTrim(gclsDB.BtriveStatus), RTrim(gclsDB.ErrFile)), MWA_DB_WARNING, MWA_DB_OK)
                        MessageBox.Show(G_CONST_EM_DB, G_CONST_EMT_DB, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)

                        Exit Sub
                    End If

                    gintStatus = PL_STATUS.PALETTESCN
                    rtn = fClearSKey2()
                    If rtn = -1 Then
                        'f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, fEditErrorInfo(RTrim(gclsDB.BtriveStatus), RTrim(gclsDB.ErrFile)), MWA_DB_WARNING, MWA_DB_OK)
                        MessageBox.Show(G_CONST_EM_DB, G_CONST_EMT_DB, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)

                    End If
                    fMakeCommandFile(G_CONST_PROCESS_PAL_DEL, G_CONST_SPEECH_CCLPALT)
                    'Exit Do
                    Me.Close()
                Else
                    'f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, fEditErrorInfo(RTrim(gclsDB.BtriveStatus), RTrim(gclsDB.ErrFile)), MWA_DB_WARNING, MWA_DB_OK)
                    MessageBox.Show(G_CONST_EM_DB, G_CONST_EMT_DB, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)

                End If
                Me.Close()
                Exit Sub
            Case "F1"
                gintStatus = PL_STATUS.PCSCAN
                Me.Close()
                Exit Sub
            Case "F2"
                gintStatus = PL_STATUS.QUIT
                Me.Close()
                Exit Sub
        End Select
    End Sub
End Class
