Public Class frmEr
 
 

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Dim CHK As Integer
            If CHKPHILHEALTH.CheckState = CheckState.Checked Then
                CHK = 1
            Else
                CHK = 0
            End If

            If CHECKERRORMSGLBL(lblmsg, GroupBox1) = False Then




                '=====Check if the patient exist=========
                sql = "SELECT * FROM `tblpatient` WHERE `CASENUM`=" & txtpatientCasenum.Text
                result = RETRIEVESINGLE(sql)

                If result = False Then
                    sql = "INSERT INTO `tblpatient` (`CASENUM`, `FNAME`, `LNAME`, `MNAME`, `PADD`, `GENDER`, `DBIRTH`, `CIVILSTATUS`,`PHILHEALTH`)" & _
                                    " VALUES('" & txtpatientCasenum.Text & "','" & txtFname.Text & "','" & txtlname.Text & "','" & txtMname.Text & "','" & _
                                    txtAddress.Text & "','" & INSERTGENDER(rdofmale, rdomale) & "','" & INSERTMYSQLDATE(dtpDateOfBirth, "yyyy-MM-dd") & "','" & cbocivilstats.Text & "'," & CHK & ")"
                    result = CUD(sql)
                    If result = False Then
                        MsgBox("qUERY CANNOT BE PROCESS, PLEASE CONTACT ADMINISRATOR!", MsgBoxStyle.Exclamation)
                    Else
                        updateautonumbercateg("casenum")
                    End If
               
                End If
                '=====================================end=====================




                '==============for admitting=====================
                Dim max As Integer = dtglistRoom.Rows.Count
              
                If Not txtAttendingPhysician.Text = "Select" Or txtAttendingPhysician.Text = "" And max = 0 Then

                    sql = "INSERT INTO `tbladmitting` (`ADMITTINGID`,`CASENUM`,`ROOMID`,`BEDID`,`DATEADMIT`,`DOCID`,`ADMITTINGSTATUS`) " & _
                    " VALUES (" & lbladmitid.Text & "," & txtpatientCasenum.Text & ",'" & dtglistRoom.CurrentRow.Cells(0).Value & "','" & dtglistRoom.CurrentRow.Cells(1).Value & _
                    "','" & Format(Now(), "yyyy-MM-dd h:i:s") & "'," & txtAttendingPhysician.SelectedValue & ",1)"
                    CUD(sql)


                    Dim maxrow As Integer = dtgDiagnosis.Rows.Count - 1
                    If maxrow > 0 Then
                        For i = 0 To dtgDiagnosis.Rows.Count - 2
                            With dtgDiagnosis.Rows(i)
                                sql = "INSERT INTO `tblmedication` (`ADMITTINGID`,`CASENUM`, `MEDICATIONDATE`, `MEDECATION`,`TREATMENT`, `DOCID`) " & _
                                      " VALUES(" & lbladmitid.Text & "," & txtpatientCasenum.Text & ",'" & Format(Now(), "yyyy-MM-dd h:i:s") & "','" & .Cells(0).FormattedValue & "','" & .Cells(1).FormattedValue & "'," & txtAttendingPhysician.SelectedValue & ")"
                                CUD(sql)
                            End With

                        Next

                        sql = "UPDATE `tblpatient` SET `ADMITTED`=1 WHERE `CASENUM` =" & txtpatientCasenum.Text
                        result = CUD(sql)
                        If result = False Then
                            MsgBox("Query FAILED tblpatient", MsgBoxStyle.Exclamation)
                        End If

                        sql = "UPDATE `tblbed` SET `STATUS`='NotAvailable' WHERE `BEDID`=" & dtglistRoom.CurrentRow.Cells(1).Value
                        result = CUD(sql)
                        If result = False Then
                            MsgBox("Query FAILED tblbed", MsgBoxStyle.Exclamation)
                        End If

                        updateautonumbercateg("admitid")

                        MsgBox("New patient has been ademitted.")



                        loadautonumbercateg("admitid", lbladmitid)
                        cleartext(GroupBox1)
                        clear()
                        rdomale.Checked = True
                        dtpDateOfBirth.Value = Now()
                        'txtAdmittingDiagnosis.Text = "Select"
                        dtgDiagnosis.Rows.Clear()
                        CBOADDITEMS(cbocivilstats)
                        CHKPHILHEALTH.Checked = False
                        Call frmEr_Load(sender, e)

                    Else
                        MsgBox("Medication is required!", MsgBoxStyle.Exclamation)
                    End If



                Else
                    MsgBox("New patient has been saved.")
                End If

            Else

            End If
            '==================end===========================================




        Catch ex As Exception
            MsgBox(ex.Message & "btnSave_Click")
        End Try


    End Sub

    
    Private Sub clear()
        txtFname.Text = ""
        txtlname.Text = ""
        txtMname.Text = ""
        txtAddress.Text = ""
        rdomale.Checked = True
        cbocivilstats.Text = "Select"
        txtAttendingPhysician.Text = ""
        dtpDateOfBirth.Value = Now()
        CHKPHILHEALTH.Checked = False
        dtgHistory.Columns.Clear()
        CHKPHILHEALTH.Enabled = True
        dtglistRoom.Columns.Clear()
        dtgDiagnosis.Rows.Clear()
        'txtAdmittingDiagnosis.Text = "Select"
    End Sub

    Private Sub TabControl1_Selected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected
        Try 
            If Not txtpatientCasenum.Text = "" Then

                sql = "SELECT `ADMITTINGDIAGNOSIS` ,`OTHERDIAGNOSIS`, `FINALDIAGNOSIS` " & _
                " FROM `tbldischargesummary` d ,`tbladmitting` a " & _
                " WHERE  d.`ADMITTINGID`=a.`ADMITTINGID` AND a.`CASENUM` =" & txtpatientCasenum.Text
                RETRIEVEDTG(dtgHistory, sql)
               
            End If

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

     
    Private Sub frmEr_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lblmsg.Text = ""
        Try

            loadautonumbercateg("admitid", lbladmitid)

            loadautonumbercateg("casenum", txtpatientCasenum)

            sql = "SELECT `DOCID`,CONCAT(`DOCNAME`,'    --[',`DOCFIELDS`,']') AS 'DOC' FROM `tbldoctor` WHERE NOT DOCFIELDS LIKE '%OB-GYNE%'"
            fillcbo(sql, txtAttendingPhysician, "DOC", "DOCID")

            'sql = "SELECT   `DOCTOR`,`DIAGNOSIS` FROM `tblroom` "
            'fillcbo(sql, txtAdmittingDiagnosis, "DIAGNOSIS", "DOCTOR")
        Catch ex As Exception

        End Try
    End Sub

    

    Private Sub txtpatientCasenum_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtpatientCasenum.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

                ElseIf txtpatientCasenum.Text.Contains(".") Then
                    MessageBox.Show("Only one decimal point allowed")
                Else
                    e.Handled = False
                End If
            End If
            e.Handled = True
        Catch ex As Exception
            'logs(ex.Message & " at TXTTENDERED_KeyPress")
            MsgBox(ex.Message & " at txtpatientCasenum_KeyPress")
        End Try
    End Sub

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



            If txtpatientCasenum.Text <> "" Then
                sql = "SELECT `FNAME`, `LNAME`, `MNAME`, `PADD`,  `DBIRTH`,`GENDER`, `CIVILSTATUS`,`PHILHEALTH`  FROM `tblpatient` WHERE ADMITTED=0 AND `CASENUM` = " & txtpatientCasenum.Text
                result = RETRIEVESINGLE(sql)
                If result = True Then

                    'MsgBox(DISPLAYDATE(dt.Rows(0).Item(4), "dd/MM/yyyy"))


                    With dt.Rows(0)
                        txtFname.Text = .Item(0)
                        txtlname.Text = .Item(1)
                        txtMname.Text = .Item(2)
                        txtAddress.Text = .Item(3)
                        dtpDateOfBirth.Text = DISPLAYDATE(.Item(4), "MM/dd/yyyy")
                        If .Item(5) = "Male" Then
                            rdomale.Checked = True
                        Else
                            rdofmale.Checked = True
                        End If
                        cbocivilstats.Text = .Item(6)
                        CHKPHILHEALTH.Checked = .Item(7)

                        
                    End With

                    DISABLEOBJECT(txtpatientCasenum, GroupBox1)
                    txtAttendingPhysician.Enabled = True
                    CHKPHILHEALTH.Enabled = False
                Else
                    clear()
                    ENABLEOBJECT(GroupBox1)
                End If
            Else
                ENABLEOBJECT(GroupBox1)
                clear()
            End If

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

    End Sub

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

        End Try
    End Sub

  
    Private Sub txtAttendingPhysician_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        e.Handled = True
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage1.Click

    End Sub

    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        Try
            With frmListPatient
                .Show()
                .Focus()

            End With
        Catch ex As Exception

        End Try
    End Sub

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

        End Try
    End Sub

     

  

  
    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Try
            clear()
            txtpatientCasenum.Clear()

            loadautonumbercateg("admitid", lbladmitid)

            loadautonumbercateg("casenum", txtpatientCasenum)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
        Try
            With frmListPatient
                .Show()
                .Focus()
            End With
        Catch ex As Exception

        End Try
    End Sub

    Private Sub rdomale_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdomale.CheckedChanged

        Try
            sql = "SELECT `DOCID`,CONCAT(`DOCNAME`,'    --[',`DOCFIELDS`,']') AS 'DOC' FROM `tbldoctor` WHERE NOT DOCFIELDS LIKE '%OB-GYNE%'"
            fillcbo(sql, txtAttendingPhysician, "DOC", "DOCID")

        Catch ex As Exception

        End Try
    End Sub

    Private Sub rdofmale_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdofmale.CheckedChanged
        Try
            sql = "SELECT `DOCID`,CONCAT(`DOCNAME`,'    --[',`DOCFIELDS`,']') AS 'DOC' FROM `tbldoctor`"
            fillcbo(sql, txtAttendingPhysician, "DOC", "DOCID")

        Catch ex As Exception

        End Try
    End Sub

  

    Private Sub cboRoomType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboRoomType.SelectedIndexChanged
        Try
            Dim max As Integer = dtgDiagnosis.Rows.Count - 1

            If Not txtAttendingPhysician.Text = "Select" And max > 0 Then
                Dim docid As Integer
                docid = txtAttendingPhysician.SelectedValue
                sql = "SELECT DOCFIELDS FROM tbldoctor WHERE DOCID =" & docid
                result = RETRIEVESINGLE(sql)

                If result = True Then
                    If cboRoomType.Text = "Private" Then
                        sql = "SELECT r.`ROOMID`,  BEDID,  `RNAME` AS 'Room',`BEDNUM` AS 'Bed' " & _
                         " FROM `tblroom` r, `tblbed` b " & _
                         " WHERE r.`ROOMID`=b.`ROOMID` AND " & _
                         " `STATUS`='Available' AND `ROOMTYPE`='Private'"
                    ElseIf cboRoomType.Text = "Ward" Then
                        sql = "SELECT r.`ROOMID`, BEDID,  CONCAT(DOCTOR,' - ', `RNAME`) AS 'Room',DIAGNOSIS as 'Description',`BEDNUM` AS 'Bed' " & _
                         " FROM `tblroom` r, `tblbed` b " & _
                         " WHERE r.`ROOMID`=b.`ROOMID` AND " & _
                         " `STATUS`='Available' AND DOCTOR LIKE '%" & dt.Rows(0).Item(0) & "%' "

                    End If

                    RETRIEVEDTG(dtglistRoom, sql)
                    dtglistRoom.Columns(0).Visible = False
                    dtglistRoom.Columns(1).Visible = False
                End If
            Else
                MsgBox("Select the curresponding attending physician. The medication fields cannot be empty!", MsgBoxStyle.Exclamation)
            End If


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

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

        End Try
    End Sub
End Class