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

Public Class frm2000

    'Private dbClient As BtrieveDbClient
    Private tmpStr As List(Of Dictionary(Of String, String))
    Private affected_rows As Integer

    Private Sub frm2000(ByVal message As String)
        InitializeComponent()
    End Sub

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

    End Sub

    Private Sub frm2000_Load(ByVal sender As System.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 = 0

        Try
            'rtn = WarpFrame1.Open()
            If (rtn <> 0) Then
                Err.Raise(vbObjectError + 1, gAppName, "WarpError")
            End If

            Dim fileloc As String = "Setting.ini"

            Dim appPath As String = fgetAppPath() ' Assuming fgetAppPath() retrieves the application directory
            Dim filePath As String = Path.Combine(appPath, fileloc)

            Dim i As Integer = 0
            Dim strIP As String = "192.168.1.1"
            Dim intPort As Integer = 11433
            Dim strDatabase As String = "demodata"

            If File.Exists(filePath) Then

                Dim sr As StreamReader = New StreamReader(filePath)
                Dim s As String = sr.ReadToEnd()
                Dim words As String() = s.Split(New Char() {","c})

                Dim word As String

                For Each word In words

                    If i = 0 Then
                        strIP = word
                    ElseIf i = 1 Then

                        If IsNumeric(word) Then
                            intPort = Convert.ToInt32(word)
                        End If

                    ElseIf i = 2 Then
                        strDatabase = word
                    End If

                    i = i + 1

                Next

            End If


            'MessageBox.Show(strIP & " | " & intPort.ToString() & " | " & strDatabase)

            'dbClient = New BtrieveDbClient(strIP, intPort, strDatabase)

            rtn = fGetCountry()
            If (rtn <> 0) Then
                '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)
                gintStatus = ALL_STATUS.QUIT
                Me.Close()
                Exit Sub
            End If

            rtn = fGetTitle()
            If (rtn = -2) Then
                fFnc_WrpMsg("E", G_CONST_MID_E2E03)
                gintStatus = ALL_STATUS.QUIT
                Me.Close()
                Exit Sub
            ElseIf (rtn <> 0) Then
                '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)
                gintStatus = ALL_STATUS.QUIT
                Me.Close()
                Exit Sub
            End If

            If (Not gstrctKDHT.strUserNo Is Nothing) Then
                rtn = fGetEmplName(gstrctKDHT.strUserNo)
                If (rtn = 0) Then
                    '###(2017/01/26 T. SHIMOKUBO)### UPD_ST [—v–]‘Î‰ž]
                    'gintStatus = ALL_STATUS.LOCATION
                    gintStatus = ALL_STATUS.NAME
                    Me.Close()
                    '###(2017/01/26 T. SHIMOKUBO)### UPD_ED [—v–]‘Î‰ž]
                ElseIf (rtn = -1) Then
                    '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)
                    gintStatus = ALL_STATUS.QUIT
                    Me.Close()
                    Exit Sub
                ElseIf (rtn = -2) Then
                    gintStatus = ALL_STATUS.NAME
                    Me.Close()
                End If
            Else
                gintStatus = ALL_STATUS.NAME
                Me.Close()
            End If

#If Not Debug Then
            'sSetImgFName(G_CONST_MID_H0000, WarpBox1)
#End If

            ' ƒtƒH[ƒ€EƒƒCƒ“ ŽÀs
            'sMain()

            Exit Sub

        Catch ex As Exception
            gintStatus = ALL_STATUS.QUIT
            sErrLog(gAppName, "frm2000_Load", ex.ToString)
            Me.Close()
        End Try

    End Sub

    Public Function fgetAppPath() As String
        ' Get the directory of the currently executing assembly (the application)
        Return Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)
    End Function

    Public Sub sMain()

        'Dim WKey As UInteger
        'Dim buf As String = ""
        'Dim rtn As Integer = 0

        'Try

        '    Do While (True)

        '        'WKey = WarpFrame1.Input(WarpField1)

        '        'WKey = WarpField1.Text

        '        If WKey = WKey = Bt.LibDef.BT_KEY_ENT Then

        '            gintStatus = ALL_STATUS.NAME
        '            Exit Do

        '        ElseIf WKey = Bt.LibDef.BT_KEY_F1 Then


        '        ElseIf WKey = Bt.LibDef.BT_KEY_F2 Then

        '            gintStatus = ALL_STATUS.QUIT
        '            Exit Do

        '        ElseIf WKey = Bt.LibDef.BT_ERR Then
        '            'WarpCtrl1.MessageBox(G_CONST_EMT_WARP, G_CONST_EM_WARP, MWA_DB_WARNING, MWA_DB_OK)
        '            MessageBox.Show(G_CONST_EM_WARP, G_CONST_EMT_WARP, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)

        '        End If

        '    Loop

        '    Exit Sub

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

    End Sub
    '/*
    '|| Purpose: Retrieve country information.
    '|| Function: fGetCountry
    '|| Description: Retrieves country information based on specified criteria.
    '*/
    Private Function fGetCountry() As Integer

        '’[––IDŠm”F•‘º°ÄÞ“ü—Í
        Dim si As New MWA_STARTUP
        Dim wintCnt As Integer
        Dim wstrWhere As String
        Dim wstrCount As String
        Dim wstrUserNo As String
        Dim wstrKeyFlg As String
        Dim wstrKey(0) As String, wstrKeyValue(0) As String

        fGetCountry = -1

        Try

            'WarpCtrl1.GetStartupInfo(si)

            gstrctKDHT.strHdNo = si.term_dev      ' ’[––IDŠl“¾

            'gstrctKDHT.strHdNo = "HT002"

            'BtLib.Transaction.Begin()


            wstrWhere = ""
            wstrWhere = "HTNumber = '" & gstrctKDHT.strHdNo & "'"
            'wintCnt = gclsDB.fGetData(G_CONST_TBL_HTCTRL, G_CONST_KEY_HTCTRLKEY00, wstrWhere, "HTCTRL1")

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

            Dim values As Dictionary(Of String, String) = New Dictionary(Of String, String)

            If tmpStr.Count > 0 Then

                values = tmpStr(0)

                Dim result As String = Nothing
                If values.TryGetValue("Country", result) Then
                    Console.WriteLine("RESULT: {0}", result)
                    wstrCount = fstrZeroput(3, result)
                End If
                If values.TryGetValue("EmplCode", result) Then
                    Console.WriteLine("RESULT: {0}", result)
                    wstrUserNo = result
                End If
                If values.TryGetValue("SectionID", result) Then
                    Console.WriteLine("RESULT: {0}", result)
                    gstrctKDHT.strSectionID = result
                End If
                If values.TryGetValue("Section", result) Then
                    Console.WriteLine("RESULT: {0}", result)
                    gstrctKDHT.strSection = result
                End If
                If values.TryGetValue("Cstmid", result) Then
                    Console.WriteLine("RESULT: {0}", result)
                    gstrctKDHT.strCstmid = result
                End If
                If values.TryGetValue("CstmCode", result) Then
                    Console.WriteLine("RESULT: {0}", result)
                    gstrctKDHT.strCstmCode = result
                End If
                If values.TryGetValue("PackType", result) Then
                    Console.WriteLine("RESULT: {0}", result)
                    gstrctKDHT.strPackType = result
                End If

                If wstrCount <> "000" Then
                    gstrctKDHT.strCountry = wstrCount
                    gstrctKDHT.strUserNo = wstrUserNo
                    fGetCountry = 0
                    Exit Function
                ElseIf wstrCount = "000" Then
                    wstrCount = "001"
                    wstrKeyFlg = "0"
                End If
            ElseIf tmpStr.Count = 0 Then
                wstrCount = "001"
                wstrKeyFlg = "1"
            Else
                Exit Function
            End If

            gstrctKDHT.strCountry = "001"
            Select Case wstrKeyFlg

                Case "0"        'XVÃÞ°À

                    wstrKey(0) = "HTNumber"
                    wstrKeyValue(0) = gstrctKDHT.strHdNo

                    affected_rows = dbClient.Execute("UPDATE " + G_CONST_TBL_HTCTRL + " SET Country='" + gstrctKDHT.strCountry + "' WHERE " + wstrKey(0) + "=" + wstrKeyValue(0))

                    'gclsDB.Setcolumn = ("Country")          '“o˜^/XV‘ÎÛ—ñ–¼
                    'gclsDB.SetData = (gstrctKDHT.strCountry)      '“o˜^/XVƒf[ƒ^

                    'wstrKey(0) = "HTNumber"
                    'wstrKeyValue(0) = gstrctKDHT.strHdNo
                    'If gclsDB.fUpdRecords(G_CONST_TBL_HTCTRL, G_CONST_KEY_HTCTRLKEY00, wstrKey, wstrKeyValue) = -1 Then
                    '    BtLib.Transaction.Abort()

                    'End If

                Case "1"        'V‹KÃÞ°À

                    affected_rows = dbClient.Execute("INSERT INTO " + G_CONST_TBL_HTCTRL + " (HTNumber, Country) VALUES ('" + gstrctKDHT.strHdNo & "','" & gstrctKDHT.strCountry + "')")

                    'gclsDB.Setcolumn = ("HTNumber,Country")          '“o˜^/XV‘ÎÛ—ñ–¼
                    'gclsDB.SetData = (gstrctKDHT.strHdNo & "," & gstrctKDHT.strCountry)      '“o˜^/XVƒf[ƒ^

                    'If gclsDB.fInsRecord(G_CONST_TBL_HTCTRL) = -1 Then
                    '    BtLib.Transaction.Abort()

                    'End If

            End Select

            'BtLib.Transaction.End()

            fGetCountry = 0

            Exit Function

        Catch ex As Exception
            'BtLib.Transaction.Abort()
            sErrLog(gAppName, "fGetCountry", ex.ToString)
        End Try


            'If wintCnt > 0 Then             'ÃÞ°À‚ª‚ ‚ê‚Î‘º°ÄÞ‚ðŒŸõ

            '    With gclsDB.Extended("HTCTRL1")

            '        wstrCount = fstrZeroput(3, .Item("Country"))    '‘OZero–„‚ß
            '        wstrUserNo = .Item("EmplCode")
            '        gstrctKDHT.strSectionID = .Item("SectionID")
            '        gstrctKDHT.strSection = .Item("Section")
            '        gstrctKDHT.strCstmid = .Item("Cstmid")
            '        gstrctKDHT.strCstmCode = .Item("CstmCode")
            '        gstrctKDHT.strPackType = .Item("PackType")

            '        If wstrCount <> "000" Then        '‘º°ÄÞ‚ª“ü—Í‚³‚ê‚Ä‚¢‚é

            '            gstrctKDHT.strCountry = wstrCount
            '            gstrctKDHT.strUserNo = wstrUserNo
            '            gclsDB.fCloseRecord("HTCTRL1")
            '            fGetCountry = 0
            '            Exit Function

            '        ElseIf wstrCount = "000" Then     '‘º°ÄÞ‚ª“ü—Í‚³‚ê‚Ä‚¢‚È‚¯‚ê‚Î"001"‚ð“ü—Í‚·‚é

            '            wstrCount = "001"
            '            wstrKeyFlg = "0"

            '        End If
            '    End With

            'ElseIf wintCnt = 0 Then       'ÃÞ°À‚ª‚È‚¯‚ê‚Î’[––”Ô†E‘º°ÄÞ("001")‚ð’Ç‰Á‚·‚é

            '    With gclsDB.Extended("HTCTRL1")
            '        wstrCount = "001"
            '        wstrKeyFlg = "1"
            '    End With

            'ElseIf wintCnt = -1 Then

            '    gclsDB.fCloseRecord("HTCTRL1")
            '    Exit Function

            'End If

            'gclsDB.fCloseRecord("HTCTRL1")

            'XVÃÞ°ÀAV‹KÃÞ°À‚ðDB‚É“o˜^
        '    gstrctKDHT.strCountry = "001"
        '    Select Case wstrKeyFlg

        '        Case "0"        'XVÃÞ°À

        '            gclsDB.Setcolumn = ("Country")          '“o˜^/XV‘ÎÛ—ñ–¼
        '            gclsDB.SetData = (gstrctKDHT.strCountry)      '“o˜^/XVƒf[ƒ^

        '            wstrKey(0) = "HTNumber"
        '            wstrKeyValue(0) = gstrctKDHT.strHdNo
        '            If gclsDB.fUpdRecords(G_CONST_TBL_HTCTRL, G_CONST_KEY_HTCTRLKEY00, wstrKey, wstrKeyValue) = -1 Then
        '                BtLib.Transaction.Abort()

        '            End If

        '        Case "1"        'V‹KÃÞ°À
        '            gclsDB.Setcolumn = ("HTNumber,Country")          '“o˜^/XV‘ÎÛ—ñ–¼
        '            gclsDB.SetData = (gstrctKDHT.strHdNo & "," & gstrctKDHT.strCountry)      '“o˜^/XVƒf[ƒ^

        '            If gclsDB.fInsRecord(G_CONST_TBL_HTCTRL) = -1 Then
        '                BtLib.Transaction.Abort()

        '            End If

        '    End Select

        '    BtLib.Transaction.End()

        '    fGetCountry = 0

        '    Exit Function

        'Catch ex As Exception
        '    BtLib.Transaction.Abort()
        '    sErrLog(gAppName, "fGetCountry", ex.ToString)
        'End Try

    End Function

    '<StructLayout(LayoutKind.Sequential, pack:=4)> _
    Public Structure MWA_STARTUP
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=17)> _
        Public term_dev As String

        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=17)> _
        Public proc_dev As String

        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=4097)> _
        Public proc_path As String
    End Structure
    '*******************************************************************************
    '   Description: If it's ZERO, skip it.
    '   Parameters :
    '   pstrData   : Data to be processed
    '   pintNum    : Number to be processed
    '   Return     : Processed data (1 is 001)
    '*******************************************************************************
    Public Function fstrZeroput(ByVal pintNum As Integer, ByVal pstrData As String) As String
        Dim wstrValue As String = ""
        Dim wintData As String = ""
        Dim rut As Integer = 0

        rut = pstrData.Length

        If pintNum <> rut Then

            wstrValue = pstrData.PadLeft(pintNum, "0")

        ElseIf pintNum = rut Then

            wstrValue = pstrData

        End If

        fstrZeroput = wstrValue

    End Function
    Private Function fGetEmplName(ByVal pstrUserNo As String) As Integer

        Dim rtn As Integer
        Dim wstrWhere As String
        Dim wintCnt As Integer

        fGetEmplName = -1

        Try

            'ŒŸõ‚ðŠJŽn‚µ‚Ü‚·
            wstrWhere = ""
            wstrWhere = "EmplCode = '" & pstrUserNo & "'"

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

            Dim values As Dictionary(Of String, String) = New Dictionary(Of String, String)

            If tmpStr.Count > 0 Then

                values = tmpStr(0)

                Dim result As String = Nothing
                If values.TryGetValue("EmplName", result) Then
                    gstrctKDHT.strUserName = result
                    Console.WriteLine("RESULT: {0}", result)
                End If
            ElseIf tmpStr.Count = 0 Then
                fGetEmplName = -2
                Exit Function
            Else
                Exit Function
            End If

            fGetEmplName = 0

            Exit Function

        Catch ex As Exception
            'gintStatus = ALL_STATUS.QUIT
            sErrLog(gAppName, "fGetEmplName", ex.ToString)
        End Try


        'wintCnt = gclsDB.fGetData(G_CONST_TBL_PICSUSER, G_CONST_KEY_PICSUSERKEY00, wstrWhere, "USER1")
        'If wintCnt > 0 Then

        '    gstrctKDHT.strUserName = gclsDB.Extended("USER1").Item("EmplName")

        'ElseIf wintCnt = 0 Then

        '    fGetEmplName = -2
        '    gclsDB.fCloseRecord("USER1")
        '    Exit Function

        'ElseIf wintCnt = -1 Then

        '    gclsDB.fCloseRecord("USER1")
        '    Exit Function

        'End If

        'gclsDB.fCloseRecord("USER1")
        'fGetEmplName = 0

        'Exit Function

        'Catch ex As Exception
        '    'gintStatus = ALL_STATUS.QUIT
        '    sErrLog(gAppName, "fGetEmplName", ex.ToString)
        'End Try

    End Function
    Private Function fGetTitle() As Integer

        Dim wstrWhere As String = ""
        Dim wstrCount As String = ""
        Dim wintCnt As Integer
        Dim wstrKey(0) As String, wstrKeyValue(0) As String

        fGetTitle = -1

        Try

            'BtLib.Transaction.Begin()

            wstrWhere = ""
            wstrWhere = "Country = '" & gstrctKDHT.strCountry
            wstrWhere &= "' and PrgID = '" & gstrctKDHT.strPrgID & "'"

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

            Dim values As Dictionary(Of String, String) = New Dictionary(Of String, String)

            If tmpStr.Count > 0 Then

                values = tmpStr(0)

                Dim result As String = Nothing
                If values.TryGetValue("Title", result) Then
                    gstrctKDHT.strTitle = result
                    Console.WriteLine("RESULT: {0}", result)
                End If
            ElseIf tmpStr.Count = 0 Then
                fGetTitle = -2
                Exit Function
            Else
                Exit Function
            End If


            wstrKey(0) = "HTNumber"
            'wstrKey(1) = "Country"
            wstrKeyValue(0) = gstrctKDHT.strHdNo
            'wstrKeyValue(1) = gstrctKDHT.strCountry

            affected_rows = dbClient.Execute("UPDATE " + G_CONST_TBL_HTCTRL + " SET ExeName='" + gstrctKDHT.strPrgID + "' WHERE " + wstrKey(0) + "=" + wstrKeyValue(0))

            fGetTitle = 0

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



        'wintCnt = gclsDB.fGetData(G_CONST_TBL_HTTITLE, G_CONST_KEY_HTTITLEKEY00, wstrWhere, "TITLE1")

        'If wintCnt > 0 Then

        '    gstrctKDHT.strTitle = gclsDB.Extended("TITLE1").Item("Title")


        'ElseIf wintCnt = 0 Then
        '    gclsDB.fCloseRecord("TITLE1")
        '    fGetTitle = -2
        '    Exit Function

        'ElseIf wintCnt = -1 Then
        '    gclsDB.fCloseRecord("TITLE1")
        '    Exit Function

        'End If

        'gclsDB.fCloseRecord("TITLE1")

        'gclsDB.Setcolumn = ("ExeName")              '“o˜^/XV‘ÎÛ—ñ–¼
        'gclsDB.SetData = (gstrctKDHT.strPrgID)     '“o˜^/XVƒf[ƒ^

        'wstrKey(0) = "HTNumber"
        ''wstrKey(1) = "Country"
        'wstrKeyValue(0) = gstrctKDHT.strHdNo
        ''wstrKeyValue(1) = gstrctKDHT.strCountry

        'If gclsDB.fUpdRecords(G_CONST_TBL_HTCTRL, G_CONST_KEY_HTCTRLKEY00, wstrKey, wstrKeyValue) = -1 Then
        '    BtLib.Transaction.Abort()

        'End If

        'BtLib.Transaction.End()

        'fGetTitle = 0

        'Catch ex As Exception
        '    BtLib.Transaction.Abort()
        '    sErrLog(gAppName, "fGetTitle", ex.ToString)
        'End Try

    End Function

    Private Sub frm2000_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown


        Select Case e.KeyCode.ToString()
            Case "Return"
                gintStatus = ALL_STATUS.NAME
                Me.Close()
                Exit Sub
            Case "F1"
            Case "F2"
                gintStatus = ALL_STATUS.QUIT
                Me.Close()
                Exit Sub
        End Select
    End Sub
End Class