Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Public Class mountingForm

    Public mountStart As Boolean = False
    Public status As Boolean = False
    Public mountAfter As Boolean = False
    Public reload As Boolean = False
    Private sequence As New Sequence()

    Private Sub mountingForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = mainForm
        Control.CheckForIllegalCrossThreadCalls = False
        getSequence()
        Timer1.Interval = 500
        Timer1.Start()
        Timer1.Enabled = False
    End Sub

    Private Sub getSequence()
        Dim sequence As New Sequence
        Dim sequenceList As New List(Of String)
        Dim i As Integer = 0

        sequenceList = sequence.getValidSequence
        cmbSeq.Items.Clear()
        While i < sequenceList.Count
            cmbSeq.Items.Add(sequenceList(i))
            i = i + 1
        End While
    End Sub

    Private Sub getRevisions()
        Dim rev As New Sequence
        Dim revList As New List(Of String)
        Dim i As Integer = 0

        revList = rev.getRevisions(cmbSeq.SelectedItem)
        cmbRev.Items.Clear()
        While i < revList.Count
            cmbRev.Items.Add(revList(i))
            i = i + 1
        End While
    End Sub


    Private Sub txtStart_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtStart.KeyUp
        If e.KeyCode = Keys.Enter Then
            Dim fChar As String = String.Empty
            Dim entry As String = txtStart.Text
            Dim str() As String

            If mountAfter = True Then
                clear()
                mountAfter = False
                Timer1.Enabled = False
                lbl_Pass.Visible = False
                lbl_fail.Visible = False
            End If

            fChar = Mid(entry, 1, 1)
            If entry.Contains("*") Then
                txtFeeder.Text = entry.Replace("*", "")
                txtStart.Text = String.Empty
                txtStart.Focus()
            ElseIf fChar = "@" Then
                txtOperator.Text = Mid(entry, 2, Len(entry))
                txtStart.Text = String.Empty
                txtStart.Focus()
            ElseIf IsNumeric(entry) Then
                txtQuantity.Text = entry
                txtStart.Text = String.Empty
                txtStart.Focus()
            ElseIf IsPartNumber(entry) Then
                txtPartNo.Text = entry
                txtPartDesc.Text = getDesc(entry)
                txtStart.Text = String.Empty
                txtStart.Focus()
            Else
                txtLotNum.Text = entry
                txtStart.Text = String.Empty
                txtStart.Focus()
            End If

            'validate inputs
            If validateInput() Then
                str = Split(txtFeeder.Text, "-")
                Dim mounting As New Mounting

                mounting.mDate = DateTime.Now
                mounting.sequence = sequence.sequence
                mounting.revision = sequence.revision
                mounting.feeder = txtFeeder.Text
                mounting.partno = txtPartNo.Text
                mounting.quantity = CDbl(txtQuantity.Text)
                mounting.mOperator = txtOperator.Text
                mounting.threePL = 0

                If sequence.verify(txtPartNo.Text, str(0), str(1)) = True Then
                    lbl_Pass.Visible = True
                    lbl_fail.Visible = False
                    mounting.verify = 1
                    status = True
                    mountAfter = True
                    Timer1.Enabled = True
                Else
                    lbl_Pass.Visible = False
                    lbl_fail.Visible = True
                    mounting.verify = 0
                    status = False
                    mountAfter = True
                    Timer1.Enabled = True
                End If
                If loginForm.userType = "Operator" Then
                    mounting.add(reload)
                End If
            End If
        End If
    End Sub

    Public Shared Function IsNumeric(ByVal sText As String) As Boolean
        If Double.TryParse(sText, Globalization.NumberStyles.AllowDecimalPoint) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Shared Function IsPartNumber(ByVal sText As String) As Boolean
        Dim isPartNo As Boolean = False
        Dim cnstr = mainForm.cnstr
        Dim conn As New SqlConnection(cnstr)

        Dim myQuery As String
        myQuery = "SELECT * FROM KPARTS WHERE KPTPARTNUM = '" & sText & "'"

        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)

        Try
            conn.Open()
            Dim reader As SqlDataReader = mycommand.ExecuteReader()
            If reader.HasRows Then
                isPartNo = True
            End If
            conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            conn.Close()
        End Try
        Return isPartNo
    End Function

    Private Function getDesc(ByVal partNum As String) As String
        Dim desc As String = String.Empty

        Dim cnstr = mainForm.cnstr
        Dim conn As New SqlConnection(cnstr)

        Dim myQuery As String
        myQuery = "SELECT * FROM KPARTS WHERE KPTPARTNUM = '" & partNum & "'"

        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)

        Try
            conn.Open()
            Dim reader As SqlDataReader = mycommand.ExecuteReader()
            reader.Read()
            desc = reader("KPTDESCRIPTION")
            conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            conn.Close()
        End Try

        Return desc
    End Function

    Private Function validateInput() As Boolean
        Dim valid As Boolean = False

        If txtPartNo.Text <> "" And txtFeeder.Text <> "" And txtLotNum.Text <> "" And txtQuantity.Text <> "" And txtOperator.Text <> "" Then
            valid = True
        End If

        Return valid
    End Function

    Private Sub clear()
        txtFeeder.Text = String.Empty
        txtPartNo.Text = String.Empty
        txtPartDesc.Text = String.Empty
        txtQuantity.Text = String.Empty
        txtLotNum.Text = String.Empty
        txtOperator.Text = String.Empty
        txtStart.Text = String.Empty
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If Timer1.Enabled = True Then
            If Timer1.Tag = True Then
                If status = True Then
                    lbl_Pass.Visible = True
                Else
                    lbl_fail.Visible = True
                End If
                Timer1.Tag = False
            Else
                If status = True Then
                    lbl_Pass.Visible = False
                Else
                    lbl_fail.Visible = False
                End If
                Timer1.Tag = True
            End If
        End If
    End Sub

    Private Sub btn_setSequence_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_setSequence.Click
        If mountStart = False Then
            If cmbSeq.SelectedItem <> "" Then
                sequence.sequence = cmbSeq.SelectedItem
                sequence.revision = cmbRev.SelectedItem
                cmbSeq.Enabled = False
                cmbRev.Enabled = False
                btn_setSequence.Text = "Change Sequence"
                txtStart.Focus()
                mountStart = True
            Else
                MessageBox.Show("Please select the machine and revision to use.", "Set Machine", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            btn_setSequence.Text = "Set Sequence"
            cmbSeq.Enabled = True
            cmbRev.Enabled = True
            mountStart = False
        End If
    End Sub

    Private Sub cmbSeq_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbSeq.SelectedIndexChanged
        getRevisions()
    End Sub

    Private Sub btn_reload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_reload.Click
        If reload = False Then
            btn_reload.Text = "Set Normal Mode"
            reload = True
        Else
            btn_reload.Text = "Set Reload Mode"
            reload = False
        End If
    End Sub
End Class