Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text

Public Class mcSeqForm
    Private cnstr = mainForm.cnstr
    Private conn As New SqlConnection(cnstr)
    Private myQuery As String = String.Empty
    Private myCommand As SqlCommand
    Private adapter As SqlDataAdapter = New SqlDataAdapter()
    Private ds As New DataSet("MachineSequence")
    Private i, limit As Integer
    Private oldMchName As String = String.Empty
    Private addMode, editMode As Integer

    Private Sub mcSeqForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = mainForm
        Control.CheckForIllegalCrossThreadCalls = False
        Me.Top = 0
        Me.Left = 0
        toNormalMode()
        getSequence()
        initializeDG()
    End Sub

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

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

    Public 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 fillDataSet(ByVal seqNumber As String, ByVal rev As String)
        Dim conn As New SqlConnection(cnstr)

        myQuery = "SELECT * FROM KSEQUENCE_DETAIL " & _
                  "WHERE KSEQUENCE = '" & seqNumber & "' " & _
                  "AND KSQREVISION = '" & rev & "' " & _
                  "ORDER BY KSQMODULE,KSQFEEDERNO"

        myCommand = New SqlCommand(myQuery, conn)

        Try
            conn.Open()
            adapter.SelectCommand = myCommand
            ds.Clear()
            adapter.Fill(ds)
            i = 0
            limit = ds.Tables(0).Rows.Count - 1
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Sub

    Private Sub fillDataGridView()
        Dim i As Integer = 0

        dg_mtSeq.Rows.Clear()
        For i = 0 To ds.Tables(0).Rows.Count - 1
            dg_mtSeq.Rows.Add()
            dg_mtSeq.Rows(i).Cells(0).Value = Trim(ds.Tables(0).Rows(i).Item("KSQPARTNO"))
            dg_mtSeq.Rows(i).Cells(1).Value = Trim(ds.Tables(0).Rows(i).Item("KSQMODULE"))
            dg_mtSeq.Rows(i).Cells(2).Value = Trim(ds.Tables(0).Rows(i).Item("KSQFEEDERNO"))
            dg_mtSeq.Rows(i).Cells(3).Value = Trim(ds.Tables(0).Rows(i).Item("KSQFEEDERTYP"))
            dg_mtSeq.Rows(i).Cells(4).Value = Trim(ds.Tables(0).Rows(i).Item("KSQTOPSDLOC"))
            dg_mtSeq.Rows(i).Cells(5).Value = Trim(ds.Tables(0).Rows(i).Item("KSQFSBSQTY"))
            dg_mtSeq.Rows(i).Cells(6).Value = Trim(ds.Tables(0).Rows(i).Item("KSQDESC1"))
            dg_mtSeq.Rows(i).Cells(7).Value = Trim(ds.Tables(0).Rows(i).Item("KSQDESC2"))
            dg_mtSeq.Rows(i).Cells(8).Value = Trim(ds.Tables(0).Rows(i).Item("KSQTOPMARK"))
            dg_mtSeq.Rows(i).Cells(9).Value = Trim(ds.Tables(0).Rows(i).Item("KSQREMARKS"))
        Next
    End Sub

    Private Sub initializeDG()
        Dim sequence As New DataGridViewTextBoxColumn
        Dim partNo As New DataGridViewTextBoxColumn
        Dim topSDLoc As New DataGridViewTextBoxColumn
        Dim fsbsQty As New DataGridViewTextBoxColumn
        Dim desc1 As New DataGridViewTextBoxColumn
        Dim desc2 As New DataGridViewTextBoxColumn
        Dim topMark As New DataGridViewTextBoxColumn
        Dim feeder As New DataGridViewTextBoxColumn
        Dim feederType As New DataGridViewTextBoxColumn
        Dim sqModule As New DataGridViewTextBoxColumn
        Dim remarks As New DataGridViewTextBoxColumn

        'sequence.HeaderText = "Sequence"
        partNo.HeaderText = "Part Number"
        sqModule.HeaderText = "Module"
        feeder.HeaderText = "Feeder"
        feederType.HeaderText = "Feeder Type"
        topSDLoc.HeaderText = "Top Side Location"
        fsbsQty.HeaderText = "Quantity"
        desc1.HeaderText = "Description 1"
        desc2.HeaderText = "Description 2"
        topMark.HeaderText = "Top Mark"
        remarks.HeaderText = "Remarks"

        dg_mtSeq.Columns.Add(partNo)
        dg_mtSeq.Columns.Add(sqModule)
        dg_mtSeq.Columns.Add(feeder)
        dg_mtSeq.Columns.Add(feederType)
        dg_mtSeq.Columns.Add(topSDLoc)
        dg_mtSeq.Columns.Add(fsbsQty)
        dg_mtSeq.Columns.Add(desc1)
        dg_mtSeq.Columns.Add(desc2)
        dg_mtSeq.Columns.Add(topMark)
        dg_mtSeq.Columns.Add(remarks)

        dg_mtSeq.Columns(0).ReadOnly = True
        dg_mtSeq.Columns(1).ReadOnly = True
        dg_mtSeq.Columns(2).ReadOnly = True
        dg_mtSeq.Columns(3).ReadOnly = True
        dg_mtSeq.Columns(4).ReadOnly = True
        dg_mtSeq.Columns(5).ReadOnly = True
        dg_mtSeq.Columns(6).ReadOnly = True
        dg_mtSeq.Columns(7).ReadOnly = True
        dg_mtSeq.Columns(8).ReadOnly = True
        dg_mtSeq.Columns(9).ReadOnly = True

        dg_mtSeq.Columns(0).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_mtSeq.Columns(1).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_mtSeq.Columns(2).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_mtSeq.Columns(3).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_mtSeq.Columns(4).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_mtSeq.Columns(5).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_mtSeq.Columns(6).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_mtSeq.Columns(7).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_mtSeq.Columns(8).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_mtSeq.Columns(9).SortMode = DataGridViewColumnSortMode.Programmatic
        
        dg_mtSeq.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        'dg_mtSeq.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader

        dg_mtSeq.RowsDefaultCellStyle.BackColor = Color.AntiqueWhite
        dg_mtSeq.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
    End Sub

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

    Private Sub ts_find_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_find.Click
        find()
    End Sub

    Private Sub find()
        Dim sequence As New Sequence
        Dim verDet As List(Of String)

        Try
            fillDataSet(cmbSeq.SelectedItem, cmbRev.SelectedItem)
            fillDataGridView()
            If sequence.isVerified(cmbSeq.SelectedItem, cmbRev.SelectedItem) Then
                btn_verify.Enabled = False
                verDet = sequence.verificationDetails(cmbSeq.SelectedItem, cmbRev.SelectedItem)
                txtVerBy.Text = verDet(0)
                txtVerDate.Text = CDate(verDet(1))
            Else
                If loginForm.userType = "Quality Control" Or loginForm.userType = "Administrator" Then
                    btn_verify.Enabled = True
                End If
                txtVerBy.Text = String.Empty
                txtVerDate.Text = String.Empty
            End If

            If loginForm.userType = "Engineer" Or loginForm.userType = "Administrator" Then
                ts_edit.Enabled = True
                ts_delete.Enabled = True
            End If
        Catch ex As Exception
            toNormalMode()
        End Try
    End Sub

    Private Sub ts_new_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_new.Click
        uploadMcSeq.Show()
    End Sub

    Private Sub ts_edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_edit.Click
        'dg_mtSeq.Columns(0).ReadOnly = False
        'dg_mtSeq.Columns(1).ReadOnly = False
        'dg_mtSeq.Columns(2).ReadOnly = False
        dg_mtSeq.Columns(3).ReadOnly = False
        dg_mtSeq.Columns(4).ReadOnly = False
        dg_mtSeq.Columns(5).ReadOnly = False
        dg_mtSeq.Columns(6).ReadOnly = False
        dg_mtSeq.Columns(7).ReadOnly = False
        dg_mtSeq.Columns(8).ReadOnly = False
        dg_mtSeq.Columns(9).ReadOnly = False
        toEditMode()
    End Sub

    Private Sub ts_close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Public Sub add()
        addMode = 1
        editMode = 0
        toAddMode()
    End Sub

    Public Sub edit()

        editMode = 1
        addMode = 0

        toEditMode()
    End Sub

    Public Sub toNormalMode()
        If loginForm.userType = "Engineer" Or loginForm.userType = "Administrator" Then
            ts_new.Enabled = True
        End If
        ts_edit.Enabled = False
        ts_delete.Enabled = False
        ts_find.Enabled = True
        ts_revert.Enabled = False
        ts_save.Enabled = False
    End Sub

    Public Sub toEditMode()
        ts_new.Enabled = False
        ts_edit.Enabled = False
        ts_delete.Enabled = False
        ts_find.Enabled = False
        ts_revert.Enabled = True
        ts_save.Enabled = True
        disableControls()
    End Sub

    Public Sub toAddMode()
        ts_new.Enabled = False
        ts_edit.Enabled = False
        ts_delete.Enabled = False
        ts_find.Enabled = False
        ts_revert.Enabled = True
        ts_save.Enabled = True
    End Sub

    Public Sub toSearchMode()
        ts_new.Enabled = False
        ts_edit.Enabled = False
        ts_delete.Enabled = False
        ts_find.Enabled = True
        ts_revert.Enabled = False
        ts_save.Enabled = False
    End Sub

    Private Sub enableControls()
        cmbRev.Enabled = True
        cmbSeq.Enabled = True
    End Sub

    Private Sub disableControls()
        cmbRev.Enabled = False
        cmbSeq.Enabled = False
    End Sub

    Private Sub clear()
        txtVerBy.Text = String.Empty
        txtVerDate.Text = String.Empty
    End Sub

    Private Sub btn_verify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_verify.Click
        If MessageBox.Show("Are you sure you want to approve Sequence Number: " & cmbSeq.SelectedItem & " with Revision Number: " & cmbRev.SelectedItem & " ?", "Approve Sequence Number", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Dim sequence As New Sequence
            sequence.sequence = cmbSeq.SelectedItem
            sequence.revision = cmbRev.SelectedItem
            sequence.checkby = loginForm.username
            sequence.chkdate = DateTime.Now
            If sequence.approve() Then
                MessageBox.Show("Successfully Approved.", "Approve Sequence Number", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtVerBy.Text = loginForm.username
                txtVerDate.Text = Date.Now
                btn_verify.Enabled = False
            End If
        End If
    End Sub

    Private Sub ts_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_delete.Click
        If MessageBox.Show("Are you sure you want to delete this Sequence Number: " & cmbSeq.SelectedItem & " with Revision Number: " & cmbRev.SelectedItem & "?", "Delete Sequence Number", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Dim sequence As New Sequence

            sequence.sequence = cmbSeq.SelectedItem
            sequence.revision = cmbRev.SelectedItem
            sequence.deleteDetail()
            sequence.deleteHeader()
            MessageBox.Show("Successfully Deleted.", "Delete Sequence Number", MessageBoxButtons.OK, MessageBoxIcon.Information)

            getSequence()
            cmbRev.Items.Clear()
            dg_mtSeq.Rows.Clear()
            clear()
            toNormalMode()
        End If
    End Sub

    Private Sub ts_revert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_revert.Click
        fillDataSet(cmbSeq.SelectedItem, cmbRev.SelectedItem)
        fillDataGridView()
        dg_mtSeq.Columns(0).ReadOnly = True
        dg_mtSeq.Columns(1).ReadOnly = True
        dg_mtSeq.Columns(2).ReadOnly = True
        dg_mtSeq.Columns(3).ReadOnly = True
        dg_mtSeq.Columns(4).ReadOnly = True
        dg_mtSeq.Columns(5).ReadOnly = True
        dg_mtSeq.Columns(6).ReadOnly = True
        dg_mtSeq.Columns(7).ReadOnly = True
        dg_mtSeq.Columns(8).ReadOnly = True
        dg_mtSeq.Columns(9).ReadOnly = True
        toNormalMode()
        enableControls()
    End Sub

    Private Sub save()
        Dim seq As New Sequence()
        Dim i As Integer = 0

        seq.sequence = cmbSeq.SelectedItem
        seq.revision = cmbRev.SelectedItem

        seq.checkby = String.Empty
        seq.chkdate = Nothing
        If seq.disApprove() Then
            seq.deleteDetail()

            While i < dg_mtSeq.Rows.Count
                seq.partno = dg_mtSeq.Rows(i).Cells(0).Value
                seq.modules = dg_mtSeq.Rows(i).Cells(1).Value
                seq.feeder = dg_mtSeq.Rows(i).Cells(2).Value
                seq.feederType = dg_mtSeq.Rows(i).Cells(3).Value
                seq.topsdloc = dg_mtSeq.Rows(i).Cells(4).Value
                seq.fsbsqty = dg_mtSeq.Rows(i).Cells(5).Value
                seq.desc1 = dg_mtSeq.Rows(i).Cells(6).Value
                seq.desc2 = dg_mtSeq.Rows(i).Cells(7).Value
                seq.topmark = dg_mtSeq.Rows(i).Cells(8).Value
                seq.remarks = dg_mtSeq.Rows(i).Cells(9).Value
                seq.addDetail()
                i = i + 1
            End While
        End If
    End Sub

    Private Sub ts_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_save.Click
        If MessageBox.Show("Are you sure you want to save the updates to this Sequence Number: " & cmbSeq.SelectedItem & " with Revision Number: " & cmbRev.SelectedItem & "?", "Delete Sequence Number", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            save()
            MessageBox.Show("Successfully Saved.", "Save Sequence Number", MessageBoxButtons.OK, MessageBoxIcon.Information)
            toNormalMode()
            find()
        End If
    End Sub

    Private Sub cmbRev_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbRev.SelectedIndexChanged
        If cmbSeq.SelectedItem <> "" And cmbRev.SelectedItem <> "" Then
            find()
        End If
    End Sub
End Class