Public Class frmRoom

    Private Sub BTNSAVE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNSAVE.Click
        Try
            If CBOTYPE.Text = "Private" Then
                txtDrExperties.Text = ""
            End If
            sql = "SELECT * FROM `tblroom` WHERE  `ROOMID`=" & TXTROOMID.Text
            result = RETRIEVESINGLE(sql)

            If result = False Then
                sql = "INSERT INTO `tblroom` (ROOMID,`RNAME`, `RBEDNUM`, `DIAGNOSIS`, `STATION`,`DOCTOR`, `ROOMTYPE`,`RPRICE`) " _
                & " VALUES (" & TXTROOMID.Text & ",'" & txtROOM.Text & "'," & TXTBED.Text & ",'" & TXTDIAGNOSIS.Text & "','" & TXTSTATION.Text & "','" & txtDrExperties.Text & "','" & CBOTYPE.Text & "'," & txtPrice.Text & ")"
                result = CUD(sql)

                If result = True Then

                    For i As Integer = 1 To TXTBED.Text
                        sql = "INSERT INTO `tblbed` (`BEDNUM`, `STATUS`, `ROOMID`) VALUES (" & i & ",'Available'," & TXTROOMID.Text & ")"
                        CUD(sql)
                    Next

                    updateautonumber(2)
                    MsgBox("New room has been saved into the database.")

                End If

            Else

                sql = "DELETE FROM `tblbed` WHERE ROOMID=" & TXTROOMID.Text
                CUD(sql)

                For i As Integer = 1 To TXTBED.Text
                    sql = "INSERT INTO `tblbed` (`BEDNUM`, `STATUS`, `ROOMID`) VALUES (" & i & ",'Available'," & TXTROOMID.Text & ")"
                    CUD(sql)
                Next

                sql = "UPDATE `tblroom` SET`RNAME`='" & txtROOM.Text & "', `RBEDNUM`='" & TXTBED.Text & _
                "', `DIAGNOSIS`='" & TXTDIAGNOSIS.Text & "', `STATION`='" & TXTSTATION.Text & _
                "', `ROOMTYPE`='" & CBOTYPE.Text & "',`RPRICE`=" & txtPrice.Text & " WHERE  `ROOMID`=" & TXTROOMID.Text
                result = CUD(sql)
                If result = True Then
                    MsgBox("Room has been updates into the database.")

                End If
            End If
            clear()
            loadautonumber(2, TXTROOMID)
            sql = "SELECT DISTINCT(DOCFIELDS) FROM tbldoctor"
            fillcbo(sql, txtDrExperties, "DOCFIELDS", "DOCFIELDS")
            'End If

        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub frmRoom_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Try
          
            sql = "SELECT  `ROOMID` as 'Room Id', concat(`RNAME`, ' - ', `DOCTOR`) as 'Room', `RBEDNUM` as 'Number of Beds', `DIAGNOSIS` as 'Description', `STATION` as 'Station', `ROOMTYPE` as 'Type' FROM `tblroom`"
            result = RETRIEVEDTG(frmListRoom.dtglist, sql)
        Catch ex As Exception

        End Try

    End Sub

    Private Sub frmRoom_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtROOM.Focus()

        'If LBLTITLE.Text = "Add New Room / Ward" Then
        loadautonumber(2, TXTROOMID)
        sql = "SELECT DISTINCT(DOCFIELDS) FROM tbldoctor"
        fillcbo(sql, txtDrExperties, "DOCFIELDS", "DOCFIELDS")
        'End If

     

    End Sub

    Private Sub TXTBED_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TXTBED.KeyPress
        Try

            If e.KeyChar <> ChrW(Keys.Back) Then
                e.Handled = True
                If e.KeyChar <> "" Then
                    If IsNumeric(e.KeyChar) Then
                        e.Handled = False

                    End If
                Else
                    e.Handled = False
                End If
            End If

        Catch ex As Exception
            'logs(ex.Message & " at TXTTENDERED_KeyPress")
            MsgBox(ex.Message & " at txtPateintId_KeyPress")
        End Try
    End Sub

    Private Sub BTNCLOSE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNCLOSE.Click
        Try
            With frmListRoom
                .Focus()
                sql = "SELECT  `ROOMID` as 'Room Id', concat(`RNAME`, ' - ', `DOCTOR`) as 'Room', `RBEDNUM` as 'Number of Beds', `DIAGNOSIS` as 'Diagnosis', `STATION` as 'Station', `ROOMTYPE` as 'Type' FROM `tblroom`"
                result = RETRIEVEDTG(.dtglist, sql)
                Me.Close()
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


    End Sub

    Private Sub TXTROOMID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TXTROOMID.TextChanged
        Try

            Dim id As Integer = TXTROOMID.Text

            sql = "SELECT  `RNAME`, `RBEDNUM`, `DIAGNOSIS`, `STATION`, `ROOMTYPE`,`RPRICE`,`DOCTOR` FROM `tblroom` WHERE `ROOMID`= " & id
            result = RETRIEVESINGLE(sql)
            If result = True Then

    

                With dt.Rows(0)
                    txtROOM.Text = .Item(0)
                    TXTBED.Text = .Item(1)
                    TXTDIAGNOSIS.Text = .Item(2)
                    TXTSTATION.Text = .Item(3)
                    CBOTYPE.Text = .Item(4)
                    txtPrice.Text = .Item(5)
                    txtDrExperties.Text = .Item(6)
                End With
            Else

                ' cleartext(Me)

            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnlist_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlist.Click
        Try
            With frmListRoom
                .Show()
                .Focus()
            End With
        Catch ex As Exception

        End Try
    End Sub
    Private Sub clear()
        Try
            txtROOM.Text = ""
            TXTBED.Text = ""
            TXTDIAGNOSIS.Text = ""
            TXTSTATION.Text = ""
            txtDrExperties.Text = "Select"
            txtPrice.Clear()
            CBOTYPE.Text = "----Select---"
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub txtPrice_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPrice.KeyPress
        CURENCY_ONLY(e, txtPrice)
    End Sub
 
    'Private Sub txtDrExperties_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDrExperties.DropDown
    '    Try
    '        sql = "SELECT DISTINCT(DOCTOR) FROM tblroom"
    '        fillcbo(sql, txtDrExperties, "DOCTOR", "DOCTOR")
    '    Catch ex As Exception

    '    End Try
    'End Sub
 
    Private Sub txtDrExperties_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDrExperties.KeyPress
        Try
            e.Handled = True
        Catch ex As Exception

        End Try
    End Sub

    Private Sub CBOTYPE_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CBOTYPE.KeyPress
        Try
            e.Handled = True
        Catch ex As Exception

        End Try
    End Sub

    Private Sub CBOTYPE_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBOTYPE.SelectedIndexChanged
   
    End Sub

    Private Sub CBOTYPE_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBOTYPE.SelectedValueChanged
        Try

            Select Case CBOTYPE.Text
                Case "Ward"
                    txtdoctoexpert.Visible = True
                    txtDrExperties.Visible = True

                Case "Private"
                    txtdoctoexpert.Visible = False
                    txtDrExperties.Visible = False
            End Select

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub CBOTYPE_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBOTYPE.TextChanged
     
    End Sub
End Class