Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text

Public Class Machine
    Private _mCode As String
    Private _mName As String
    Private _mModel As String
    Private _plant As String
    Private _line As String
    Private _isActive As Boolean
    Private cnstr As String

#Region "Properties"
    Public Property code() As String
        Get
            Return _mCode
        End Get
        Set(ByVal value As String)
            _mCode = value
        End Set
    End Property

    Public Property name() As String
        Get
            Return _mName
        End Get
        Set(ByVal value As String)
            _mName = value
        End Set
    End Property

    Public Property model() As String
        Get
            Return _mModel
        End Get
        Set(ByVal value As String)
            _mModel = value
        End Set
    End Property

    Public Property plant() As String
        Get
            Return _plant
        End Get
        Set(ByVal value As String)
            _plant = value
        End Set
    End Property

    Public Property line() As String
        Get
            Return _line
        End Get
        Set(ByVal value As String)
            _line = value
        End Set
    End Property

    Public Property active() As Boolean
        Get
            Return _isActive
        End Get
        Set(ByVal value As Boolean)
            _isActive = value
        End Set
    End Property
#End Region

#Region "Methods"
    Public Sub New()
        cnstr = mainForm.cnstr
    End Sub

    Public Sub add()
        Dim conn As New SqlConnection(cnstr)
        Dim active As Integer = 0

        If _isActive = True Then
            active = 1
        Else
            active = 0
        End If

        Dim myQuery As String
        myQuery = "INSERT INTO KMACHINE VALUES(" & _
                  "'" & _mCode & "'," & _
                  "'" & _mName & "'," & _
                  "'" & _mModel & "'," & _
                  "'" & _plant & "'," & _
                  "'" & _line & "'," & _
                  "'" & DateTime.Now & "'," & _
                  active & ")"

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

        Try
            conn.Open()
            mycommand.ExecuteNonQuery()
            MessageBox.Show("Successfully Saved.", "Add Machine", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Sub

    Public Sub update()
        Dim conn As New SqlConnection(cnstr)
        Dim active As Integer = 0

        If _isActive = True Then
            active = 1
        Else
            active = 0
        End If

        Dim myQuery As String
        myQuery = "UPDATE KMACHINE SET" & _
                  " KMCHNAME = " & "'" & _mName & "'," & _
                  "KMCHMODEL = " & "'" & _mModel & "'," & _
                  "KMCHPLANT = " & "'" & _plant & "'," & _
                  "KMCHLINE = " & "'" & _line & "'," & _
                  "KUPDTTIME = " & "'" & DateTime.Now & "'," & _
                  "KACTIVE = " & active & _
                  " WHERE KMCHCODE = '" & _mCode & "'"

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

        Try
            conn.Open()
            mycommand.ExecuteNonQuery()
            MessageBox.Show("Successfully Saved.", "Update Machine", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Sub

    Public Sub delete()
        Dim conn As New SqlConnection(cnstr)

        Dim myQuery As String
        myQuery = "DELETE FROM KMACHINE WHERE KMCHCODE = '" & _mCode & "'"

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

        Try
            conn.Open()
            mycommand.ExecuteNonQuery()
            MessageBox.Show("Successfully Deleted.", "Delete Machine", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Sub

    Public Function getMachines(ByVal plant As String) As List(Of String)
        Dim machineList As New List(Of String)
        Dim conn As New SqlConnection(cnstr)

        Dim myQuery As String
        myQuery = "SELECT * FROM KMACHINE" & _
                  " WHERE KMCHPLANT = '" & plant & "'" & _
                  " ORDER BY KMCHCODE"

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

        Try
            conn.Open()
            Dim reader As SqlDataReader = mycommand.ExecuteReader()
            While reader.Read
                machineList.Add(reader("KMCHNAME"))
            End While
            conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            conn.Close()
        End Try
        Return machineList
    End Function
#End Region
End Class
