Imports System.IO.Ports
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO



Public Class sequential
    Dim count1 As Integer = 0
    Public count2 As Integer = 0
    Dim complete As Boolean = False
    Dim ComPort As String = ""
    Private Sub sequential_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = mainForm
        Control.CheckForIllegalCrossThreadCalls = False

        dgPN.Rows.Clear()
        dgPN.DefaultCellStyle.Font = New Font("Arial", 18, FontStyle.Bold, GraphicsUnit.Point)
        txtDID.Focus()

        Dim style As DataGridViewCellStyle = New DataGridViewCellStyle()
        style.Font = New Font("Arial", 18, FontStyle.Bold, GraphicsUnit.Point)
        dgPN.Columns(0).HeaderCell.Style = style
        dgPN.Columns(1).HeaderCell.Style = style
        dgPN.Columns(2).HeaderCell.Style = style


        Timer1.Start()



    End Sub

    Private Sub cmbLine_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbLine.SelectedIndexChanged
        If cmbLine.Text <> Nothing Then
            dgPN.Rows.Clear()
            lblrev.Text = New Mounting().getSettings(cmbLine.SelectedItem.ToString)
            getSeq.Text = New Mounting().getKseq(cmbLine.SelectedItem.ToString)
            Dim arrayseq() = New Mounting().getSequencePN(cmbLine.SelectedItem.ToString)
            Try

                For x = 0 To arrayseq.Length - 1
                    If (arrayseq(x) <> "") Then
                        dgPN.Rows.Add(arrayseq(x), "", x)
                    End If
                Next
            Catch ex As Exception

            End Try
        End If

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

        Try
            conn.Open()

            Dim myQuery As String = "SELECT * FROM [dbo].[RelayCom] WHERE LineId ='" & cmbLine.Text & "'"
            mycommand = New SqlCommand(myQuery, conn)
            Dim reader As SqlDataReader = mycommand.ExecuteReader()
            reader.Close()

            reader = mycommand.ExecuteReader()
            If (reader.HasRows) Then
                reader.Read()
                ComPort = reader("ComPort")
                ComTextBox.Text = ComPort
            End If

            reader.Close()
            conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            conn.Close()
        End Try

        SerialPort1.Close()
        SerialPort1.PortName = ComTextBox.Text
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default


    End Sub

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        'Dim mydate As DateTime = Date.Now
        'Dim hour As Integer = mydate.Hour
        'Dim min As Integer = mydate.Minute
        'Dim sec As Integer = mydate.Second
        'Dim milisec As Integer = mydate.Millisecond



        Dim cnstr = mainForm.cnstr
        Dim conn As New SqlConnection(cnstr)
        Dim stime As String = ""
        Dim myQuery As String
        myQuery = "SELECT GETDATE() AS SERVERTIME "
        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)


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

        Dim mydate As DateTime = stime
        Dim hour As Integer = mydate.Hour
        Dim min As Integer = mydate.Minute
        Dim sec As Integer = mydate.Second

        If (min = 0 And sec = 0) Then

            If complete = True Then
                Timer1.Stop()
                mountingForm.Timer3.Start()
            Else
                UpdateComPort()
                SerialPort1.Open()
                SerialPort1.Write("B")
                SerialPort1.Close()
                failform.Show()
            End If

        End If
    End Sub

    Private Sub txtDID_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtDID.KeyPress

        If Asc(e.KeyChar) = 13 Then
            If Not txtDID.Text.StartsWith("LA") Then
                txtDID.Text = txtDID.Text.Substring(0, 10).ToUpper
            End If

            If (count1 = dgPN.Rows.Count - 1) Then
                btnComplete.PerformClick()
                complete = True

            Else

                Dim cnstr = mainForm.cnstr
                Dim conn As New SqlConnection(cnstr)
                Dim i As Integer = 0
                Dim temp As String = ""
                Dim did As String = ""
                Dim myQuery As String
                myQuery = "SELECT * FROM PartsLog WHERE MaterialID = '" & txtDID.Text.ToUpper & "'"
                Dim mycommand As SqlCommand
                mycommand = New SqlCommand(myQuery, conn)

                'Try
                conn.Open()
                Dim reader As SqlDataReader = mycommand.ExecuteReader()
                If (reader.HasRows) Then
                    reader.Read()
                    temp = reader("IonicsPN")
                    did = reader("MaterialID")
                    reader.Close()
                End If
                conn.Close()



                For i = 0 To dgPN.Rows.Count - 1
                    If (dgPN.Rows(i).Cells(0).Value = temp) Then
                        If (dgPN.Rows(i).Cells(1).Value = "") Then
                            If (dgPN.Rows(i).Cells(2).Value = "0") Then
                                If dgPN.Rows(i).Cells(1).Value <> "" Then
                                    ' MsgBox("ERROR")
                                    SerialPort1.Open()
                                    SerialPort1.Write("B")
                                    SerialPort1.Close()
                                    reader.Close()

                                    For y = 0 To dgPN.Rows.Count - 1
                                        If dgPN.Rows(y).Cells(1).Value = "" Then
                                            If (dgPN.Rows(y).Cells(2).Value = "0") Then
                                                conn.Open()
                                                myQuery = "INSERT INTO KSEQ_HISTORY(LineNum, ModelRev, MaterialID, IonicsPN, SequenceNo, Result, ScannedBy, DateScan) VALUES ('" & cmbLine.Text & "','" & lblrev.Text & "','" & txtDID.Text & "','" & dgPN.Rows(y).Cells(0).Value & "','" & dgPN.Rows(y).Cells(2).Value & "','" & "FAIL" & "','" & loginForm.username & "','" & Date.Now & "')"
                                                mycommand = New SqlCommand(myQuery, conn)
                                                mycommand.ExecuteNonQuery()
                                                conn.Close()

                                                Exit For

                                            Else
                                                conn.Open()
                                                myQuery = "INSERT INTO KSEQ_HISTORY(LineNum, ModelRev, MaterialID, IonicsPN, SequenceNo, Result, ScannedBy, DateScan) VALUES ('" & cmbLine.Text & "','" & lblrev.Text & "','" & txtDID.Text & "','" & dgPN.Rows(y).Cells(0).Value & "','" & dgPN.Rows(y).Cells(2).Value & "','" & "FAIL" & "','" & loginForm.username & "','" & Date.Now & "')"
                                                mycommand = New SqlCommand(myQuery, conn)
                                                mycommand.ExecuteNonQuery()
                                                conn.Close()

                                                Exit For

                                            End If
                                            txtDID.Text = ""
                                        End If

                                    Next

                                    NotificationForm.text = "WRONG SEQUENCE OF SCANNING!"
                                    NotificationForm.ShowDialog()
                                    failverification.Show()



                                Else

                                    dgPN.Rows(i).Cells(1).Value = did
                                    dgPN.Rows(i).Cells(0).Style.BackColor = Color.Green
                                    dgPN.Rows(i).Cells(1).Style.BackColor = Color.Green
                                    dgPN.Rows(i).Cells(2).Style.BackColor = Color.Green



                                    count1 = count1 + 1
                                    conn.Open()
                                    myQuery = "INSERT INTO KSEQ_HISTORY(LineNum, ModelRev, MaterialID, IonicsPN, SequenceNo, Result, ScannedBy, DateScan) VALUES ('" & cmbLine.Text & "','" & lblrev.Text & "','" & txtDID.Text & "','" & dgPN.Rows(i).Cells(0).Value & "','" & dgPN.Rows(i).Cells(2).Value & "','" & "PASS" & "','" & loginForm.username & "','" & Date.Now & "')"
                                    mycommand = New SqlCommand(myQuery, conn)
                                    mycommand.ExecuteNonQuery()
                                    conn.Close()
                                    txtDID.Text = ""
                                    Exit For

                                End If
                            Else
                                If (dgPN.Rows(i - 1).Cells(1).Value <> "") Then
                                    dgPN.Rows(i).Cells(1).Value = did
                                    dgPN.Rows(i).Cells(0).Style.BackColor = Color.Green
                                    dgPN.Rows(i).Cells(1).Style.BackColor = Color.Green
                                    dgPN.Rows(i).Cells(2).Style.BackColor = Color.Green
                                    count1 = count1 + 1
                                    conn.Open()
                                    myQuery = "INSERT INTO KSEQ_HISTORY(LineNum, ModelRev, MaterialID, IonicsPN, SequenceNo, Result, ScannedBy, DateScan) VALUES ('" & cmbLine.Text & "','" & lblrev.Text & "','" & txtDID.Text & "','" & dgPN.Rows(i).Cells(0).Value & "','" & dgPN.Rows(i).Cells(2).Value & "','" & "PASS" & "','" & loginForm.username & "','" & Date.Now & "')"
                                    mycommand = New SqlCommand(myQuery, conn)
                                    mycommand.ExecuteNonQuery()
                                    conn.Close()
                                    txtDID.Text = ""
                                    Exit For

                                Else
                                    ' MsgBox("ERROR")
                                    SerialPort1.Open()
                                    SerialPort1.Write("B")
                                    SerialPort1.Close()

                                    For y = 0 To dgPN.Rows.Count - 1
                                        If dgPN.Rows(y).Cells(1).Value = "" Then
                                            If (dgPN.Rows(y).Cells(2).Value = "0") Then
                                                conn.Open()
                                                myQuery = "INSERT INTO KSEQ_HISTORY(LineNum, ModelRev, MaterialID, IonicsPN, SequenceNo, Result, ScannedBy, DateScan) VALUES ('" & cmbLine.Text & "','" & lblrev.Text & "','" & txtDID.Text & "','" & dgPN.Rows(y).Cells(0).Value & "','" & dgPN.Rows(y).Cells(2).Value & "','" & "FAIL" & "','" & loginForm.username & "','" & Date.Now & "')"
                                                mycommand = New SqlCommand(myQuery, conn)
                                                mycommand.ExecuteNonQuery()
                                                conn.Close()
                                                Exit For

                                            Else
                                                conn.Open()
                                                myQuery = "INSERT INTO KSEQ_HISTORY(LineNum, ModelRev, MaterialID, IonicsPN, SequenceNo, Result, ScannedBy, DateScan) VALUES ('" & cmbLine.Text & "','" & lblrev.Text & "','" & txtDID.Text & "','" & dgPN.Rows(y).Cells(0).Value & "','" & dgPN.Rows(y).Cells(2).Value & "','" & "FAIL" & "','" & loginForm.username & "','" & Date.Now & "')"
                                                mycommand = New SqlCommand(myQuery, conn)
                                                mycommand.ExecuteNonQuery()
                                                conn.Close()
                                                Exit For

                                            End If
                                            txtDID.Text = ""
                                        End If
                                    Next

                                    NotificationForm.text = "WRONG SEQUENCE OF SCANNING!"
                                    NotificationForm.ShowDialog()
                                    failverification.Show()


                                End If
                            End If
                        Else
                            txtDID.Focus()
                        End If

                    End If

                Next


                'End While

            End If

            ' Catch ex As Exception
            'MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            'End Try
            ' txtDID.Text = ""


        End If
    End Sub

    Private Sub btnReload_Click(sender As System.Object, e As System.EventArgs) Handles btnReload.Click
        Me.Close()
    End Sub

    Private Sub btnComplete_Click(sender As System.Object, e As System.EventArgs) Handles btnComplete.Click
        Dim msg As String = ""
        Dim i As Integer = 0
        Dim t As Integer = 0
        Dim p As Integer = 0
        Dim f As Integer = 0

        If dgPN.Rows.Count > 0 Then
            While i < dgPN.Rows.Count - 1
                If dgPN.Rows(i).Cells(1).Value <> "" Then
                    p = p + 1
                    If dgPN.Rows(i).Cells(1).Value = "" Then
                        f = f + 1
                    End If
                    t = t + 1
                End If
                i = i + 1
            End While
        End If

        msg = vbCrLf + "RESULT :" + vbCrLf + t.ToString + "   of   " + i.ToString + "  checked" + vbCrLf + vbCrLf + "PASSED = " + p.ToString + vbCrLf + "FAILED = " + f.ToString
        myMessageBox.Show(msg)


        If p.ToString <> "0" Then
            If p.ToString = i.ToString Then
                completeform.Show()

            End If
        End If
    End Sub

    Private Sub dgPN_DoubleClick(sender As System.Object, e As System.EventArgs) Handles dgPN.DoubleClick
        dgPN.ReadOnly = True
    End Sub

    Private Sub UpdateComPort()
        SerialPort1.Close()
        SerialPort1.PortName = ComTextBox.Text
        'SerialPort1.PortName = "COM3"
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default
    End Sub

End Class