public string UpdateServiceTimes(Patient aPatient)
        {
            int value = patientGateway.UpdateServiceTimes(aPatient);

            if (value > 0)
            {
                return "Patient Has Been Saved";
            }
            else return "Failed";
        }
        public string SavePatient(Patient aPatient)
        {
            int value =patientGateway.SavePatient(aPatient);

            if (value > 0)
            {
                return "Patient has been saved!";
            }
            else return "Failed";
        }
        public int GetPatientId(Patient aPatient)
        {
            int patientId = 0;
            string query = "SELECT * FROM PatientTBL WHERE VoterId='" + aPatient.VoterId + "'";
            SqlConnection connection = new SqlConnection(connectionString);

            SqlCommand command = new SqlCommand(query, connection);
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                patientId =int.Parse( reader["Id"].ToString());
                ServiceTime = int.Parse(reader["ServiceTimes"].ToString());
            }
            reader.Close();
            connection.Close();
            return patientId;
        }
        public int SaveObservation(Treatment aTreatment, Patient aPatient, int centerId)
        {
            int observationId = 0;
            string query = "INSERT INTO ObservationTBL VALUES('" + aTreatment.Observation + "','" + aTreatment.Date + "','" + aPatient.Id + "','" + centerId + "','" + aTreatment.DoctorId + "')";
            SqlConnection connection = new SqlConnection(connectionString);

            SqlCommand command = new SqlCommand(query, connection);
            connection.Open();
            command.ExecuteNonQuery();
            query = "SELECT * FROM ObservationTBL";
            command = new SqlCommand(query, connection);
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read()) {
                observationId = int.Parse(reader["Id"].ToString());
            }
            reader.Close();
            connection.Close();
            return observationId;
        }
        public bool IfPatientExists(Patient aPatient)
        {
            string query = "SELECT * FROM PatientTBL WHERE VoterId='" + aPatient.VoterId + "' ";
            SqlConnection connection = new SqlConnection(connectionString);

            SqlCommand command = new SqlCommand(query, connection);
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
               if(reader.Read())
               {
               reader.Close();
               connection.Close();
               return true;
               }
            else
               {
               reader.Close();
               connection.Close();
            return false;
             }
        }
 public List<Treatment> GetObservationList(Patient aPatient)
 {
     List<Treatment> observationList = new List<Treatment>();
     string query = "SELECT * FROM ObservationTBL WHERE PatientId='"+aPatient.Id+"'";
     SqlConnection  connection = new SqlConnection(connectionString);
     SqlCommand  command = new SqlCommand(query, connection);
     connection.Open();
     SqlDataReader reader = command.ExecuteReader();
     while (reader.Read())
     {
         Treatment aTreatment = new Treatment();
         aTreatment.ObservationId = int.Parse(reader["Id"].ToString());
         aTreatment.Observation = reader["Observation"].ToString();
         aTreatment.Date = reader["Date"].ToString();
         aTreatment.CenterId = int.Parse(reader["CenterId"].ToString());
         aTreatment.DoctorId = int.Parse(reader["DoctorId"].ToString());
         observationList.Add(aTreatment);
     }
     reader.Close();
     connection.Close();
     return observationList;
 }
        protected void saveButton_Click(object sender, EventArgs e)
        {
            int districtId = int.Parse(Session["DistrictId"].ToString());
            Patient aPatient = new Patient();
            aPatient.VoterId = voterIdTextBox.Text;
            int count = int.Parse(serviceGivenTextBox.Text);
            aPatient.ServiceTimes = count + 1;
            if (patientManager.IfPatientExists(aPatient))
            {

                megLabel.Text = patientManager.UpdateServiceTimes(aPatient);
                AllClear();
            }
            else
            {
                megLabel.Text = patientManager.SavePatient(aPatient);
                AllClear();
            }

            aPatient.Id = patientManager.GetPatientId(aPatient);
            int centerId = int.Parse(Session["CenterId"].ToString());
            patientManager.PatientCenterTblValue(aPatient.Id, centerId, districtId);

            Treatment aTreatment = new Treatment();
            aTreatment.Observation = observationTextBox.Text;
            aTreatment.DoctorId = int.Parse(doctorDropDownList.SelectedValue);
            string date = Request.Form["bday"];
            int year = int.Parse(date.Substring(0, 4));
            int month = int.Parse(date.Substring(5, 2));
            int day = int.Parse(date.Substring(8, 2));
            aTreatment.Date = year + "-" + month + "-" + day;

            int observationId=treatmentManager.SaveObservation(aTreatment, aPatient, centerId);

            foreach (var treatment in TreatmentList)
            {
                Treatment newTreatment = new Treatment();
                newTreatment.DiseaseId = treatment.DiseaseId;
                newTreatment.MedicineId = treatment.MedicineId;
                newTreatment.Dose = treatment.Dose;
                newTreatment.Quantity = treatment.Quantity;
                newTreatment.Note = treatment.Note;
                newTreatment.TakenTime = treatment.TakenTime;
                treatmentManager.SaveTreatment(newTreatment,observationId);
            }
        }
        public int UpdateServiceTimes(Patient aPatient)
        {
            string query = "UPDATE PatientTBL SET ServiceTimes='"+aPatient.ServiceTimes+"' WHERE VoterId='"+aPatient.VoterId +"'";
            SqlConnection connection = new SqlConnection(connectionString);

            SqlCommand command = new SqlCommand(query, connection);
            connection.Open();
            int rowAffected = command.ExecuteNonQuery();
            connection.Close();
            return rowAffected;
        }
        public int SavePatient(Patient aPatient)
        {
            string query = "INSERT INTO PatientTBL VALUES('" + aPatient.VoterId + "','" + aPatient.ServiceTimes + "')";
            SqlConnection connection = new SqlConnection(connectionString);

            SqlCommand command = new SqlCommand(query, connection);
            connection.Open();
            int rowAffected = command.ExecuteNonQuery();
            connection.Close();
            return rowAffected;
        }
        protected void showButton_Click(object sender, EventArgs e)
        {
            Patient aPatient = new Patient();
            aPatient.VoterId = nationalIdTextBox.Text;
            GetPatientInformation(aPatient.VoterId);
            aPatient.Id = patientManager.GetPatientId(aPatient);
            aPatient.ServiceTimes = patientManager.GetServiceTimes(aPatient);
            if (aPatient.ServiceTimes < 1)
            {
                megLabel.Text = "Didn't take any treatment!";
                pdfButton.Visible =false;
            }
            else {
                int count = 0;
                List<Treatment> ObservationList = treatmentManager.GetObservationList(aPatient);
                foreach (var observation in ObservationList) {
                    count++;
                    string centerName = centerManager.GetCenterName(observation.CenterId);
                    string Date = observation.Date;
                    string DoctorName = doctorManager.GetDoctorName(observation.DoctorId);
                    string Observation = observation.Observation;
                    List<Treatment> treatmentList = treatmentManager.GetTreatmentList(observation.ObservationId);
                    List<Treatment> aTreatmentList= new List<Treatment>();
                    foreach (var treatment in treatmentList)
                    {
                       string diseaseName= diseaseManager.GetDiseaseName(treatment.DiseaseId);
                       string medicineName = medicineManager.GetMedicineName(treatment.MedicineId);
                        Treatment aTreatment = new Treatment();
                        aTreatment.NameOfDisease = diseaseName;
                        aTreatment.NameOfMedicine = medicineName;
                        aTreatment.Dose = treatment.Dose;
                        aTreatment.TakenTime = treatment.TakenTime;
                        aTreatment.Quantity = treatment.Quantity;
                        aTreatment.Note = treatment.Note;

                        aTreatmentList.Add(aTreatment);
                    }
                    ShowAllTreatment(centerName, Date, DoctorName, Observation, count, aTreatmentList);
                }
                pdfButton.Visible = true;
            }
        }
        protected void pdfButton_Click(object sender, EventArgs e)
        {
            Document doc = new Document(PageSize.A4, 36, 72, 108, 180);
            string path = @"D:\";
            PdfWriter.GetInstance(doc, new FileStream(path + "/Treatment.pdf", FileMode.Create));
            doc.Open();
            Voter aVoter = new Voter();
            aVoter.Id=nationalIdTextBox.Text;
            aVoter.Name = nameTextBox.Text;
            aVoter.Address = addressTextBox.Text;
                Paragraph paragraph = new Paragraph();
                paragraph.Add("National Id : " + aVoter.Id);
                paragraph.Add(Environment.NewLine);
                paragraph.Add("Name : " + aVoter.Name);
                paragraph.Add(Environment.NewLine);
                paragraph.Add("Address :" + aVoter.Address);
                paragraph.Add(Environment.NewLine);
                paragraph.Add(Environment.NewLine);
                doc.Add(paragraph);

                Patient aPatient = new Patient();
                aPatient.VoterId = nationalIdTextBox.Text;
                GetPatientInformation(aPatient.VoterId);
                aPatient.Id = patientManager.GetPatientId(aPatient);

             int count = 0;
                List<Treatment> ObservationList = treatmentManager.GetObservationList(aPatient);
                foreach (var observation in ObservationList)
                {
                    count++;
                    PdfPTable table = new PdfPTable(1);
                    Paragraph aParagraph = new Paragraph();
                    Chunk chunk = new Chunk("Treatment-" + count, FontFactory.GetFont("dax-black"));
                    chunk.SetUnderline(0.5f, -1.5f);
                    doc.Add(chunk);

                    string centerName = centerManager.GetCenterName(observation.CenterId);
                    aParagraph.Add("Center Name : " + centerName);
                    aParagraph.Add(Environment.NewLine);
                    string Date = observation.Date;
                    aParagraph.Add("Date : " + Date);
                    aParagraph.Add(Environment.NewLine);
                    string DoctorName = doctorManager.GetDoctorName(observation.DoctorId);
                    aParagraph.Add("Doctor Name : " + DoctorName);
                    aParagraph.Add(Environment.NewLine);
                    string Observation = observation.Observation;
                    aParagraph.Add("Observation : " + Observation);
                    aParagraph.Add(Environment.NewLine);
                    table.AddCell(aParagraph);
                    doc.Add(table);
                    List<Treatment> treatmentList = treatmentManager.GetTreatmentList(observation.ObservationId);
                    List<Treatment> aTreatmentList = new List<Treatment>();
                    foreach (var treatment in treatmentList)
                    {
                        string diseaseName = diseaseManager.GetDiseaseName(treatment.DiseaseId);
                        string medicineName = medicineManager.GetMedicineName(treatment.MedicineId);
                        Treatment aTreatment = new Treatment();
                        aTreatment.NameOfDisease = diseaseName;
                        aTreatment.NameOfMedicine = medicineName;
                        aTreatment.Dose = treatment.Dose;
                        aTreatment.TakenTime = treatment.TakenTime;
                        aTreatment.Quantity = treatment.Quantity;
                        aTreatment.Note = treatment.Note;

                        aTreatmentList.Add(aTreatment);
                    }
                    ShowAllTreatment(centerName, Date, DoctorName, Observation, count, aTreatmentList);
                    PdfPTable aTable = new PdfPTable(6);
                    aTable.AddCell("Disease");
                    aTable.AddCell("Medicine");
                    aTable.AddCell("Dose");
                    aTable.AddCell("Before/After meal");
                    aTable.AddCell("Quantity");
                    aTable.AddCell("Note");
                    foreach (var eachTreatment in aTreatmentList) {
                        aTable.AddCell(eachTreatment.NameOfDisease);
                        aTable.AddCell(eachTreatment.NameOfMedicine);
                        aTable.AddCell(eachTreatment.Dose);
                        aTable.AddCell(eachTreatment.TakenTime);
                        aTable.AddCell(eachTreatment.Quantity.ToString());
                        aTable.AddCell(eachTreatment.Note);
                    }
                    doc.Add(aTable);
                }
            doc.Close();
            Response.Redirect("OpenPdfUI.aspx");
            megLabel.Text = "PDF Creation Successful!";
        }
 public bool IfPatientExists(Patient aPatient)
 {
     return patientGateway.IfPatientExists(aPatient);
 }
 public int GetServiceTimes(Patient aPatient)
 {
     patientGateway.GetPatientId(aPatient);
     return patientGateway.ServiceTime;
 }
 public int GetPatientId(Patient aPatient)
 {
     return patientGateway.GetPatientId(aPatient);
 }
 public int SaveObservation(Treatment aTreatment, Patient aPatient, int centerId)
 {
     return  treatmentGateway.SaveObservation(aTreatment, aPatient,centerId);
 }
 public List<Treatment> GetObservationList(Patient aPatient)
 {
     return treatmentGateway.GetObservationList(aPatient);
 }