Imports System.IO
Public Class uploadMcSeq

    Private Sub uploadMcSeq_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = mainForm
        Control.CheckForIllegalCrossThreadCalls = False
        Me.Top = 5
        Me.Left = 5
    End Sub

    Private Function browseFile() As String
        Dim stFileName As String = String.Empty
        Dim openFileDialog1 As New OpenFileDialog

        openFileDialog1.InitialDirectory = "C:\"
        openFileDialog1.Title = "Open Machine Sequence Text File"
        openFileDialog1.Filter = "Text files (*.txt)|*.txt"
        openFileDialog1.FilterIndex = 1
        openFileDialog1.RestoreDirectory = True

        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            stFileName = openFileDialog1.FileName
        End If
        Return stFileName
    End Function

    Private Sub btn_browse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_browse.Click
        txtFName.Text = browseFile()
    End Sub

    Private Sub btn_cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_cancel.Click
        Me.Close()
    End Sub

    Private Sub btn_upload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_upload.Click
        getfilecontents(txtFName.Text)
    End Sub

    Private Sub getfilecontents(ByVal fullpath As String)
        Dim strcontent As String = String.Empty
        Dim line() As String
        Dim objreader As StreamReader
        Dim seq As New Sequence
        Dim addHeader As Boolean = False

        Try
            objreader = New StreamReader(fullpath)
            strcontent = objreader.ReadLine 'disregard header
            strcontent = objreader.ReadLine
            While strcontent <> ""
                line = Split(strcontent, vbTab)
                seq.sequence = line(0)
                seq.revision = line(1)
                seq.machine = line(2)
                seq.model = line(3)
                seq.pcbside = line(4)
                seq.partno = line(5)
                seq.modules = line(6)
                seq.feeder = line(7)
                seq.feederType = line(8)
                seq.topsdloc = line(9)
                seq.fsbsqty = line(10)
                seq.desc1 = line(11)
                seq.desc2 = line(12)
                seq.topmark = line(13)
                seq.remarks = line(14)
                seq.prepby = loginForm.username
                seq.checkby = String.Empty
                seq.chkdate = String.Empty
                seq.updtime = DateTime.Now
                If addHeader = False Then
                    If seq.addHeader() Then
                        addHeader = True
                    Else
                        MessageBox.Show("An error was encountered.", "Upload Sequence", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        Exit Sub
                    End If
                End If
                If Not seq.addDetail() Then
                    MessageBox.Show("An error was encountered.", "Upload Sequence", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Exit Sub
                End If
                strcontent = objreader.ReadLine
            End While
            
            objreader.Close()
            MessageBox.Show("Successfully uploaded!")
            txtFName.Text = String.Empty
            mcSeqForm.getSequence()
        Catch ex As Exception

        End Try
    End Sub

End Class