Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
Public Class machineForm

    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("Machines")
    Private i, limit As Integer
    Private oldMchCode As String = String.Empty
    Private addMode, editMode As Integer

    Private Sub machineForm_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

        fillDataSet()
        getplants()
        disableControls()
        moveFirst()
        addMode = 0
        editMode = 0
        btn_find.Visible = False
    End Sub

    Private Sub enableControls()
        txtMchCode.Enabled = True
        txtMchName.Enabled = True
        txtMchModel.Enabled = True
        cmbPlant.Enabled = True
        txtLine.Enabled = True
        cbActive.Enabled = True
    End Sub

    Private Sub disableControls()
        txtMchCode.Enabled = False
        txtMchName.Enabled = False
        txtMchModel.Enabled = False
        cmbPlant.Enabled = False
        txtLine.Enabled = False
        cbActive.Enabled = False
    End Sub

    Private Sub clear()
        txtMchCode.Text = String.Empty
        txtMchName.Text = String.Empty
        txtMchModel.Text = String.Empty
        txtLine.Text = String.Empty
        cbActive.Checked = False
    End Sub

    Public Sub toNormalMode()
        ts_new.Enabled = True
        ts_edit.Enabled = True
        ts_delete.Enabled = True
        ts_find.Enabled = True
        ts_revert.Enabled = False
        ts_save.Enabled = False
        disableControls()
    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
        enableControls()
    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
        enableControls()
    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 fillDataSet()
        Dim conn As New SqlConnection(cnstr)
        myQuery = "SELECT * FROM KMACHINE ORDER BY KMCHCODE"

        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 trace(ByVal code As String)
        Dim j As Integer = 0

        For j = 0 To ds.Tables(0).Rows.Count - 1
            If ds.Tables(0).Rows(j).Item("KMCHCODE") = code Then
                txtMchCode.Text = ds.Tables(0).Rows(j).Item("KMCHCODE")
                txtMchName.Text = ds.Tables(0).Rows(j).Item("KMCHNAME")
                txtMchModel.Text = ds.Tables(0).Rows(j).Item("KMCHMODEL")
                If ds.Tables(0).Rows(j).Item("KACTIVE") = 1 Then
                    cbActive.Checked = True
                Else
                    cbActive.Checked = False
                End If
                cmbPlant.SelectedItem = ds.Tables(0).Rows(j).Item("KMCHPLANT")
                txtLine.Text = ds.Tables(0).Rows(j).Item("KMCHLINE")
                i = j
                Exit For
            End If
        Next
    End Sub

    Public Sub moveFirst()
        If ds.Tables(0).Rows.Count <> 0 And limit > -1 Then
            i = 0
            txtMchCode.Text = ds.Tables(0).Rows(i).Item("KMCHCODE")
            oldMchCode = ds.Tables(0).Rows(i).Item("KMCHCODE")
            txtMchName.Text = ds.Tables(0).Rows(i).Item("KMCHNAME")
            txtMchModel.Text = ds.Tables(0).Rows(i).Item("KMCHMODEL")
            cmbPlant.SelectedItem = ds.Tables(0).Rows(i).Item("KMCHPLANT")
            txtLine.Text = ds.Tables(0).Rows(i).Item("KMCHLINE")
            If ds.Tables(0).Rows(i).Item("KACTIVE") = 1 Then
                cbActive.Checked = True
            Else
                cbActive.Checked = False
            End If
        Else
            clear()
        End If
    End Sub

    Public Sub moveNext()
        If i < limit And limit > -1 Then
            i = i + 1
            txtMchCode.Text = ds.Tables(0).Rows(i).Item("KMCHCODE")
            oldMchCode = ds.Tables(0).Rows(i).Item("KMCHCODE")
            txtMchName.Text = ds.Tables(0).Rows(i).Item("KMCHNAME")
            txtMchModel.Text = ds.Tables(0).Rows(i).Item("KMCHMODEL")
            cmbPlant.SelectedItem = ds.Tables(0).Rows(i).Item("KMCHPLANT")
            txtLine.Text = ds.Tables(0).Rows(i).Item("KMCHLINE")
            If ds.Tables(0).Rows(i).Item("KACTIVE") = 1 Then
                cbActive.Checked = True
            Else
                cbActive.Checked = False
            End If
        End If
    End Sub

    Public Sub movePrevious()
        If i <> 0 And limit > -1 Then
            i = i - 1
            txtMchCode.Text = ds.Tables(0).Rows(i).Item("KMCHCODE")
            oldMchCode = ds.Tables(0).Rows(i).Item("KMCHCODE")
            txtMchName.Text = ds.Tables(0).Rows(i).Item("KMCHNAME")
            txtMchModel.Text = ds.Tables(0).Rows(i).Item("KMCHMODEL")
            cmbPlant.SelectedItem = ds.Tables(0).Rows(i).Item("KMCHPLANT")
            txtLine.Text = ds.Tables(0).Rows(i).Item("KMCHLINE")
            If ds.Tables(0).Rows(i).Item("KACTIVE") = 1 Then
                cbActive.Checked = True
            Else
                cbActive.Checked = False
            End If
        End If
    End Sub

    Public Sub moveLast()
        If limit > -1 Then
            i = limit
            txtMchCode.Text = ds.Tables(0).Rows(i).Item("KMCHCODE")
            oldMchCode = ds.Tables(0).Rows(i).Item("KMCHCODE")
            txtMchName.Text = ds.Tables(0).Rows(i).Item("KMCHNAME")
            txtMchModel.Text = ds.Tables(0).Rows(i).Item("KMCHMODEL")
            cmbPlant.SelectedItem = ds.Tables(0).Rows(i).Item("KMCHPLANT")
            txtLine.Text = ds.Tables(0).Rows(i).Item("KMCHLINE")
            If ds.Tables(0).Rows(i).Item("KACTIVE") = 1 Then
                cbActive.Checked = True
            Else
                cbActive.Checked = False
            End If
        Else
            clear()
        End If
    End Sub

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

    Public Sub edit()
        oldMchCode = txtMchCode.Text

        editMode = 1
        addMode = 0

        toEditMode()
    End Sub

    Public Sub save()
        Dim machine As New Machine()
        If addMode Or editMode Then
            machine.code = txtMchCode.Text
            machine.name = txtMchName.Text
            machine.model = txtMchModel.Text
            machine.plant = cmbPlant.SelectedItem
            machine.line = txtLine.Text
            If addMode Then
                machine.add()
            ElseIf editMode Then
                machine.update()
            End If
            fillDataSet()
            If ds.Tables(0).Rows.Count <> 0 Then
                oldMchCode = txtMchCode.Text
                trace(oldMchCode)
            End If
            disableControls()
            addMode = 0
            editMode = 0
            toNormalMode()
        End If
    End Sub

    Public Sub delete()
        If txtMchCode.Text <> "" Then
            If MessageBox.Show("Are you sure you want to delete?", "Delete Machine", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                Dim machine As New Machine()
                machine.code = txtMchCode.Text
                machine.delete()
                fillDataSet()
                moveFirst()
            End If
        End If
    End Sub

    Public Sub revert()
        If (editMode Or addMode) Then
            disableControls()
            clear()
            If oldMchCode <> "" Then
                retrieve(oldMchCode)
            End If

            toNormalMode()
        End If
    End Sub

    Public Sub find()
        txtMchCode.Enabled = True
        txtMchCode.Text = String.Empty
        txtMchName.Text = String.Empty
        txtLine.Text = String.Empty
        cbActive.Visible = False
        btn_find.Visible = True

        toSearchMode()
    End Sub

    Private Sub getplants()
        Dim user As New User
        Dim plantlist As New List(Of String)
        Dim i As Integer = 0

        cmbPlant.Items.Clear()
        plantlist = user.getPlants
        While i < plantlist.Count
            cmbPlant.Items.Add(plantlist(i))
            i = i + 1
        End While
    End Sub

    Private Sub retrieve(ByVal code As String)
        fillDataSet()
        trace(code)
        txtMchCode.Enabled = False
        cbActive.Visible = True
        btn_find.Visible = False

        toNormalMode()
    End Sub

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

    Private Sub ts_edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_edit.Click
        edit()
    End Sub

    Private Sub ts_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_delete.Click
        delete()
    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 ts_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_save.Click
        save()
    End Sub

    Private Sub ts_revert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_revert.Click
        revert()
    End Sub

    Private Sub btn_find_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_find.Click
        oldMchCode = txtMchCode.Text
        retrieve(oldMchCode)
    End Sub

    Private Sub txtUsername_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMchCode.KeyUp
        If e.KeyCode = Keys.Enter Then
            oldMchCode = txtMchCode.Text
            retrieve(oldMchCode)
        End If
    End Sub

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