Public Class frmDischargeSum
    Private Sub clear()
        txtPatient.Clear()
        txtStation.Clear()
        txtRoom.Clear()
        txtBed.Clear()
        txtPatient.Clear()
        txtFinalDiagnosis.Text = "Select"
        txtCheifComplaint.Clear()
        txtAdmittingDate.Clear()
        txtadmittingDiagnosis.Clear()
        LBLADMITTINGID.Text = ""
        dtpDischargeDate.Text = Now()
    End Sub
    Private Sub txtCaseNum_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCaseNum.TextChanged
        Try
            If txtCaseNum.Text <> "" Then
                'Dim days As Integer
                'sql = "SELECT " & _
                '" a.`CASENUM`,  " & _
                '    " `ADMITTINGID`, " & _
                '    " `HOSPITALNO`, " & _
                '    " `PHYSICIAN`, " & _
                '    " `ADMITTINGCLERK`, " & _
                '    " `TYPEOFADMISSION`," & _
                '    " `REFERREDBY`, " & _
                '    " `SOCIALSERVICECLASS`,  " & _
                '    " `ALERGICTO`,  " & _
                '    " `HOSPITALIZATIONPLAN`, " & _
                '    " `HEALTHINSURANCE`,  " & _
                '    " `PHIC`,  " & _
                '    " `DATAFURNISHBY`,  " & _
                '    " `ADDOFINFORMANT`,  " & _
                '    " `OTHERDIAGNOSIS`, " & _
                '    " `FINALDIAGNOSIS`,  " & _
                '    " `ICD_RUVCODE`, " & _
                '    " `DISPOSISTION`,  " & _
                '    " `RESULT`,  " & _
                '    " `CHIEFCOMPLAINT`, " & _
                '    " `DISCHARGEDDATE` " & _
                '    " FROM `tblpatient` p, `tbladmitting` a,`tblroom` r,tblbed b " & _
                '    " WHERE  p.`CASENUM`=a.`CASENUM` AND a.ROOMID=r.ROOMID AND a.BEDID=b.BEDID  " & _
                '    " AND ADMITTED=1  and p.CASENUM=" & txtCaseNum.Text

                sql = "SELECT  * " & _
                   " FROM `tblpatient` p, `tbladmitting` a,`tblroom` r,tblbed b " & _
                   " WHERE  p.`CASENUM`=a.`CASENUM` AND a.ROOMID=r.ROOMID AND a.BEDID=b.BEDID  " & _
                   " AND ADMITTED=1  and p.CASENUM=" & txtCaseNum.Text
                result = RETRIEVESINGLE(sql)

                If result = True Then
                    With dt.Rows(0)
                        txtPatient.Text = .Item("FNAME") & " " & .Item("LNAME")
                        txtAddress.Text = .Item("PADD")
                        txtStation.Text = .Item("STATION")
                        txtRoom.Text = .Item("RNAME")
                        txtBed.Text = .Item("BEDNUM")

                        txtAdmittingDate.Text = DISPLAYDATE(.Item("DATEADMIT"), "MM/dd/yyyy")

                        txtNumays.Text = DISNUMBERDAY(.Item("DATEADMIT"), dtpDischargeDate.Value)

                      
                        'MsgBox(DISNUMBERDAY(.Item("DATEADMIT"), Now()))
                        txtadmittingDiagnosis.Text = .Item("DIAGNOSIS")
                        LBLADMITTINGID.Text = .Item("ADMITTINGID")


                        sql = "SELECT * " & _
                        " FROM `tbldoctor` "
                        result = RETRIEVESINGLE(sql)
                        fillcbo(sql, txtPhysician, "DOCNAME", "DOCID")



                        sql = "SELECT * FROM `tbldiagnosis`"
                        fillcbo(sql, txtFinalDiagnosis, "DIAGNOSIS", "DIAGNOSISID")
                    End With
                Else
                    clear()
                End If
            Else
                clear()
            End If

        Catch ex As Exception

        End Try
    End Sub

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

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            'MsgBox(DISPLAYDATE(dtpDischargeDate.Value, "yyyy-M-d"))

            If txtCaseNum.Text = "" _
            Or txtOtherDiagnosis.Text = "" _
            Or txtFinalDiagnosis.Text = "" _
            Or txtPhysician.Text = "" _
            Or txtFinalDiagnosis.Text = "" _
            Or txtCheifComplaint.Text = "" _
            Or txtAdmittingClerk.Text = "" _
            Or txtOtherDiagnosis.Text = "" _
            Or txtReferredBy.Text = "" _
            Or txtICDRUVCODE.Text = "" Then
                MsgBox("All fields are required!", MsgBoxStyle.Exclamation)

            Else
                Dim rdoResult As String = ""

                If rdoRecovered.Checked = True Then
                    rdoResult = "Recovered"
                ElseIf rdoImprove.Checked = True Then
                    rdoResult = "Improve"
                ElseIf rdoUnImprove.Checked = True Then
                    rdoResult = "UnImprove"
                ElseIf rdoDied.Checked = True Then
                    rdoResult = "Died"
                ElseIf rdoN48h.Checked = True Then
                    rdoResult = "-48h"
                ElseIf rdoP48H.Checked = True Then
                    rdoResult = "+48H"
                ElseIf rdoAutopsy.Checked = True Then
                    rdoResult = "Autopsy"
                ElseIf rdpNo.Checked = True Then
                    rdoResult = "No"
                End If


                Dim positioning As String = ""

                If rdoHAMA.Checked = True Then
                    positioning = "HAMA"
                ElseIf rdoTransfered.Checked = True Then
                    positioning = "Transferred"
                ElseIf rdoAbsconded.Checked = True Then
                    positioning = "Absconded"
                ElseIf rdoDischarge.Checked = True Then
                    positioning = "Discharge"
                End If


                sql = "UPDATE `tblpatient` SET `ADMITTED`=0 WHERE `CASENUM` =" & txtCaseNum.Text
                CUD(sql)

                sql = "INSERT INTO  `tbldischargesummary` (`CASENUM`, `ADMITTINGID`, `HOSPITALNO`, `PHYSICIAN`, " & _
                "  `FINALDIAGNOSIS`, `CHIEFCOMPLAINT`, `DISCHARGEDDATE`,ADMITTINGCLERK,OTHERDIAGNOSIS, " & _
                " REFERREDBY, ICD_RUVCODE,DISPOSISTION,  RESULT,`ALERGICTO`, `DATAFURNISHBY`,`ADDOFINFORMANT` ,`RELATIONTOPATIEN` ) " & _
                " VALUES (" & txtCaseNum.Text & "," & LBLADMITTINGID.Text & "," & txtCaseNum.Text & ",'" & txtPhysician.Text & _
                "','" & txtFinalDiagnosis.Text & "','" & txtCheifComplaint.Text & "','" & DISPLAYDATE(dtpDischargeDate.Value, "yyyy-M-d") & _
                "','" & txtAdmittingClerk.Text & "','" & txtOtherDiagnosis.Text & "','" & txtReferredBy.Text & "','" & txtICDRUVCODE.Text & _
                "','" & positioning & "','" & rdoResult & "','" & txtallergicto.Text & "','" & txtdatafurnishby.Text & _
                "','" & txtaddressofinformant.Text & "','" & txtrelationtoPatient.Text & "')"
                result = CUD(sql)

                If result = True Then
                    MsgBox("Patient already discharged.")
                    cleartext(Me)
                End If
            End If
            '            sql = "`CASENUM`='" & txtCaseNum.Text & "',"
            '`ADMITTINGID`='" &  TXTADMITTINGID.TEXT &"',
            '`HOSPITALNO`='" &  TXT.TEXT &"',
            '`PHYSICIAN`='" &  TXTCASENUM.TEXT &"',
            '`ADMITTINGCLERK`='" &  TXTADMITTINGCLERK.TEXT &"', 
            '`TYPEOFADMISSION`='" &  TXTTYPEOFADMISSION.TEXT &"',
            '`REFERREDBY`='" &  TXTREFERREDBY.TEXT &"',
            '`SOCIALSERVICECLASS`='" &  TXTSOCIALSERVICECLASS.TEXT &"', 
            '`ALERGICTO`='" &  TXTALERGICTO.TEXT &"',
            '`DATAFURNISHBY`='" &  TXTDATAFURNISHBY.TEXT &"',
            '`ADDOFINFORMANT`='" &  TXTADDOFINFORMANT.TEXT &"', 
            '`RELATIONTOPATIEN`='" &  TXTRELATIONTOPATIEN.TEXT &"',
            '`OTHERDIAGNOSIS`='" &  TXTOTHERDIAGNOSIS.TEXT &"',
            '`FINALDIAGNOSIS`='" &  TXTFINALDIAGNOSIS.TEXT &"', 
            '`ICD_RUVCODE`='" &  TXTICD_RUVCODE.TEXT &"', 
            '`DISPOSISTION`='" &  TXTDISPOSISTION.TEXT &"',
            '`USERID`='" &  TXT.TEXT &"',
            '`PRINCIPALOPERATION`='" &  TXTPRINCIPALOPERATION.TEXT &"',
            '`OTHEROPERATION`='" &  TXTOTHEROPERATION.TEXT &"'
        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub

 


    Private Sub txtCaseNum_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtHostpitalNum.KeyPress, txtCaseNum.KeyPress
        Numbers_Only(e)
        
    End Sub

    

    Private Sub dtpDischargeDate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpDischargeDate.ValueChanged
        Try
            Dim days As Integer
            sql = "SELECT  *  FROM  `tbladmitting`  " & _
                 " WHERE  CASENUM=" & txtCaseNum.Text
            result = RETRIEVESINGLE(sql)

            If result = True Then
                With dt.Rows(0)
                    days = DISNUMBERDAY(.Item("DATEADMIT"), dtpDischargeDate.Value)
                    If days < 0 Then
                        MsgBox("Choosen date is not valid", MsgBoxStyle.Exclamation)
                        txtNumays.Text = 1
                        dtpDischargeDate.Focus()
                    ElseIf days = 0 Then
                        days = 1
                    Else
                        txtNumays.Text = days
                    End If
                    txtNumays.Text = days
                End With
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub frmDischargeSum_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        rdoDischarge.Checked = True
        rdoRecovered.Checked = True
    End Sub

 
  
    Private Sub txtPatient_KeyPress_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtStation.KeyPress, txtRoom.KeyPress, txtPatient.KeyPress, txtNumays.KeyPress, txtBed.KeyPress, txtAdmittingDate.KeyPress, txtAddress.KeyPress
        Try
            e.Handled = True
        Catch ex As Exception

        End Try
    End Sub

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

        End Try
    End Sub

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

        End Try
    End Sub
End Class