/*private Boolean retrieveReport(String reportNo)
        {
            database.connectToDatabase();
            if (database.hasEntry(reportNo, "reportNo"))
            {
                Record tempRecord = database.getReport(reportNo);
                database.closeConnection();
                fillFields(tempRecord);
                return true;
            }
            else
            {
                database.closeConnection();
                return false;
            }
        }*/
        private void fillFields(Record record)
        {
            clear();
            textReportNo.Text = record.Reference_No;
            textPatientName.Text = record.Name;
            textWardNo.Text = record.Ward;
            textBhtNo.Text = record.BHT;
            comboBoxTitle.SelectedIndex = -1;
            comboBoxTitle.Text = record.title;
            if (comboBoxTitle.Text == "Baby")
            {
                textMonth.Visibility = System.Windows.Visibility.Visible;
                labelMonths.Visibility = System.Windows.Visibility.Visible;
                textMonth.Text = record.months.ToString();
            }
            textPatientName.Text = record.Name;
            textAge.Text = record.years.ToString();
            comboBoxGender.Text = record.Gender;
            textSpecimen.Text = record.specimenArray[0];
            comboBoxSeverity.Text = record.severity;        //**
            textClinicalDetails.Text = record.clinicalDetails;  //**
            for (int i = 1; i < record.specimenArray.Length; i++)
            {
                methodForButtonAddSpecimen_Click();
                speciTexts.Last().Text =record.specimenArray[i];
            }

            textMacroscopy.Text = record.macroscopy;
            textMicroscopy.Text = record.microscopy;
            textConclusion.Text = record.conclusion;

            String[] dateformat2 = record.TestDate.Split('/');                                                                                  //**
            datePicker2.SelectedDate = new DateTime(Int32.Parse(dateformat2[0]), Int32.Parse(dateformat2[1]), Int32.Parse(dateformat2[2]));     //**
            String[] dateformat1 = record.requestDate.Split('/');                                                                                  //**
            datePicker1.SelectedDate = new DateTime(Int32.Parse(dateformat1[0]), Int32.Parse(dateformat1[1]), Int32.Parse(dateformat1[2]));     //**
        }
        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            Stream FileStream = null;

            grid1.IsEnabled = false;
            if (save())
            {
                labelError.Foreground = Brushes.CadetBlue;
                labelError.Content = "Report saved....";
                labelError.Visibility = System.Windows.Visibility.Visible;
                timer1.Start();

                if (draftWorkingOn != null)
                {
                    ((List<Record>)dataGridDraftsList.DataContext).Remove(draftWorkingOn);
                    draftWorkingOn = null;
                    DraftList list = new DraftList();
                    list.list = (List<Record>)dataGridDraftsList.DataContext;
                    FileStream = File.Create(draftsPath);
                    BinaryFormatter serializer = new BinaryFormatter();
                    serializer.Serialize(FileStream, list);
                    FileStream.Close();
                }

            }
            grid1.IsEnabled = true;
        }
 private void dataGridDraftsList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
 {
     draftWorkingOn=(Record)dataGridDraftsList.SelectedItem;
     fillFields(draftWorkingOn);
     gridDraftsList.Visibility = System.Windows.Visibility.Hidden;
     menu.IsEnabled = false;
 }
        private void buttonSaveDraft_Click(object sender, RoutedEventArgs e)
        {
            if (textPatientName.Text != "")
            {
                DraftList list;
                Stream FileStream = null;
                BinaryFormatter deserializer;
                if (dataGridDraftsList.DataContext == null)
                {
                    try
                    {
                        FileStream = File.OpenRead(draftsPath);
                        deserializer = new BinaryFormatter();
                        list = (DraftList)deserializer.Deserialize(FileStream);
                        FileStream.Close();
                    }
                    catch
                    {
                        if (FileStream != null)
                        {
                            FileStream.Close();
                        }
                        list = new DraftList();
                    }
                }
                else
                {
                    list = new DraftList();
                    list.list = (List<Record>)dataGridDraftsList.DataContext;
                }

                int months = 0;
                if (comboBoxTitle.Text == "Baby")
                    Int32.TryParse(textMonth.Text, out months);
                int years = 0;
                Int32.TryParse(textAge.Text, out years);

                list.list.Add(new Record(textReportNo.Text, textWardNo.Text, textBhtNo.Text, comboBoxTitle.Text, textPatientName.Text, years, months, comboBoxGender.Text, textBoxListToStringArray(speciTexts), textMacroscopy.Text, textMicroscopy.Text, textConclusion.Text, dateToString(datePicker2.SelectedDate.Value), dateToString(datePicker1.SelectedDate.Value), comboBoxSeverity.Text, textClinicalDetails.Text));
                if (draftWorkingOn != null)
                {
                    list.list.Remove(draftWorkingOn);
                    draftWorkingOn = null;
                }
                FileStream = File.Create(draftsPath);
                BinaryFormatter serializer = new BinaryFormatter();
                serializer.Serialize(FileStream, list);
                FileStream.Close();

                labelError.Foreground = Brushes.CadetBlue;
                labelError.Content = "Draft Saved successfully";
                labelError.Visibility = System.Windows.Visibility.Visible;
                timer1.Start();
            }
            else
                MessageBox.Show("Can't save a draft without a name.", "Draft Not Saved", MessageBoxButton.OK);
        }
        private void buttonPrint_Click(object sender, RoutedEventArgs e)
        {
            Stream FileStream = null;
            if (grid3.Visibility == System.Windows.Visibility.Visible)
            {
                if ((Record)dataGrid1.SelectedItem == null)
                {
                    MessageBox.Show("Please select a report first.", "", MessageBoxButton.OK, MessageBoxImage.Stop);
                    return;
                }
            }

            if (grid3.Visibility == System.Windows.Visibility.Hidden)
            {
                labelError.Foreground = Brushes.CadetBlue;
                labelError.Content = "Saving report in the database....";
                labelError.Visibility = System.Windows.Visibility.Visible;
                if (!save())
                {
                    return;
                }
            }

            if (draftWorkingOn != null)
            {
                ((List<Record>)dataGridDraftsList.DataContext).Remove(draftWorkingOn);
                draftWorkingOn = null;
                DraftList list = new DraftList();
                list.list = (List<Record>)dataGridDraftsList.DataContext;
                FileStream = File.Create(draftsPath);
                BinaryFormatter serializer = new BinaryFormatter();
                serializer.Serialize(FileStream, list);
                FileStream.Close();
            }

            if (grid3.Visibility == System.Windows.Visibility.Hidden)
            {
                labelError.Foreground = Brushes.CadetBlue;
                labelError.Visibility = System.Windows.Visibility.Visible;
                labelError.Content = "Report Saved...";
            }

            Window1 printC = new Window1();
            printC.Owner = this;
            printC.Left = this.Left;
            printC.Top = this.Top;
            printC.ShowDialog();

            if (!canceled)
            {
                labelError.Foreground = Brushes.CadetBlue;
                labelError.Visibility = System.Windows.Visibility.Visible;
                labelError.Content = "Transferring report to the printer....";

                print();

                labelError.Content = "Report has been transferred to the printer....";
            }

            timer1.Start();
        }
        public void store(Record record)
        {
            int dateid = 0;
            if (hasEntry(record.TestDate, "testdate"))
            {
                string StrCmd = "SELECT top 1 dateid FROM Table1 WHERE testdate = '" + record.TestDate + "' order by testdate desc";
                OleDbCommand Cmd1 = new OleDbCommand(StrCmd, MyConn);                            //**
                OleDbDataReader ObjReader = Cmd1.ExecuteReader();
                ObjReader.Read();
                dateid = Int32.Parse(ObjReader["dateid"].ToString()) + 1;
            }
            OleDbCommand Cmd = new OleDbCommand("INSERT INTO Table1 ( reportNo, ward,bht,title, patientName, age, gender, specimen,macroscopy, microscopy, conclusion,testDate,requestDate, severity, clinicalDetails, dateid, months ) VALUES ('" + record.Reference_No + "'," + "'" + record.Ward + "'," + "'" + record.BHT + "'," + "'" + record.title + "'," + "'" + record.Name + "',"
                + "'" + record.years + "'," + "'" + record.Gender + "'," + "'" + Record.ArrayToString(record.specimenArray) + "'," + "'" + record.macroscopy + "'," + "'" + record.microscopy + "'," + "'" + record.conclusion + "'," + "'" + record.TestDate + "'," + "'" + record.requestDate + "'," + "'" + record.severity + "'," + "'" + record.clinicalDetails + "'," + "'" + dateid + "'," + "'" + record.months + "')", MyConn); ;
            //OleDbCommand Cmd = new OleDbCommand("INSERT INTO Table1 ( name) VALUES ('" +record.microscopy + "')", MyConn);

            Cmd.ExecuteNonQuery();
        }
        /*
        public List<Record> getRecordListByPartOfName(String partOfValue,String column, int option)
        {
            List<Record> refNoSet = new List<Record>();
            string StrCmd;
            switch (option)
            {
                case (0):
                    {
                        StrCmd = "SELECT top " + (MainWindow.listsize + 1) + " reportNo, patientName,gender, ward, bht, age, specimen,testDate,severity FROM Table1 where "+column+" LIKE '%"+partOfValue+"%' AND ID > " + MainWindow.topid + " order by ID ASC";
                        break;
                    }
                case (1):
                default:
                    {
                        if (MainWindow.bottomid != 0)
                            StrCmd = "SELECT top " + (MainWindow.listsize + 1) + " reportNo, patientName,gender, ward, bht, age, specimen,testDate,severity FROM Table1 where " + column + " LIKE '%" + partOfValue + "%' AND ID <= " + MainWindow.bottomid + " order by ID DESC";
                        else
                            StrCmd = "SELECT top " + (MainWindow.listsize + 1) + " reportNo, patientName,gender, ward, bht, age, specimen,testDate,severity FROM Table1 where " + column + " LIKE '%" + partOfValue + "%' order by ID DESC";
                        break;
                    }
            }
            OleDbCommand Cmd = new OleDbCommand(StrCmd, MyConn);
            OleDbDataReader ObjReader = Cmd.ExecuteReader();
            String name, gender, date, reportNo, bht, ward, severity;
            String[] specimen;
            int age;

            while (ObjReader.Read())
            {
                name = ObjReader["patientName"].ToString();
                gender = ObjReader["gender"].ToString();
                ward = ObjReader["ward"].ToString();
                bht = ObjReader["bht"].ToString();
                date = ObjReader["testDate"].ToString();
                reportNo = ObjReader["reportNo"].ToString();
                severity = ObjReader["severity"].ToString();        //**
                age = Int32.Parse(ObjReader["age"].ToString());
                specimen = Record.StringToArray(ObjReader["specimen"].ToString());
                refNoSet.Add(new Record(reportNo, ward, bht, "", name, age, gender, specimen, "", "", "", date, "", severity, "",0));
            }
            if (refNoSet.Count > 0)
            {
                int temp = MainWindow.topid;
                string StrCmd2;
                if (option == 1)
                    StrCmd2 = "SELECT * FROM Table1 WHERE reportNo = '" + refNoSet.Last().Reference_No + "'";
                else
                    StrCmd2 = "SELECT * FROM Table1 WHERE reportNo = '" + refNoSet[(MainWindow.listsize - 1)].Reference_No + "'";
                string StrCmd3 = "SELECT * FROM Table1 WHERE reportNo = '" + refNoSet.First().Reference_No + "'";
                OleDbCommand Cmd2 = new OleDbCommand(StrCmd2, MyConn);
                OleDbDataReader ObjReader2 = Cmd2.ExecuteReader();
                if (ObjReader2 == null)
                {
                    MainWindow.hasmore = false;
                    Console.WriteLine();////////////////////////////////////error in connecting to the database
                    return null;
                }
                else
                {
                    ObjReader2.Read();
                    MainWindow.bottomid = Int32.Parse(ObjReader2["ID"].ToString());
                    OleDbCommand Cmd3 = new OleDbCommand(StrCmd3, MyConn);
                    OleDbDataReader ObjReader3 = Cmd3.ExecuteReader();
                    if (ObjReader3 == null)
                    {
                        MainWindow.hasmore = false;
                        Console.WriteLine();////////////////////////////////////error in connecting to the database
                        return null;
                    }
                    else
                    {
                        ObjReader3.Read();
                        MainWindow.topid = Int32.Parse(ObjReader3["ID"].ToString());
                    }
                    if (refNoSet.Count == (MainWindow.listsize + 1))
                    {
                        MainWindow.hasmore = true;
                        refNoSet.Remove(refNoSet.Last());
                    }
                    else
                        MainWindow.hasmore = false;
                    if (option == 0)
                    {
                        MainWindow.topid = MainWindow.bottomid;
                        MainWindow.bottomid = temp;
                        refNoSet.Reverse();
                    }
                    return refNoSet;
                }
            }
            else
            {
                MainWindow.hasmore = false;
                return null;
            }
        }

        public List<Record> getReportbyfullVariable(String value, String column,int option)
        {
            List<Record> refNoSet = new List<Record>();
            string StrCmd;
            switch (option)
            {
                case (0):
                    {
                        StrCmd = "SELECT top " + (MainWindow.listsize + 1) + " reportNo, patientName,gender, ward, bht, age, specimen,testDate,severity FROM Table1 WHERE " + column + " = '" + value + "' AND ID > " + MainWindow.topid + " order by ID ASC";
                        break;
                    }
                case (1):
                default:
                    {
                        if (MainWindow.bottomid != 0)
                            StrCmd = "SELECT top " + (MainWindow.listsize + 1) + " reportNo, patientName,gender, ward, bht, age, specimen,testDate,severity FROM Table1 WHERE " + column + " = '" + value + "' AND ID <= " + MainWindow.bottomid + " order by ID DESC";
                        else
                            StrCmd = "SELECT top " + (MainWindow.listsize + 1) + " reportNo, patientName,gender, ward, bht, age, specimen,testDate,severity FROM Table1 WHERE " + column + " = '" + value + "' order by ID DESC";
                        break;
                    }
            }
            OleDbCommand Cmd = new OleDbCommand(StrCmd, MyConn);                                    //**
            OleDbDataReader ObjReader = Cmd.ExecuteReader();
            String name, gender, date, reportNo, bht, ward, severity;
            String[] specimen;
            int age,months;

            while (ObjReader.Read())
            {
                    name = ObjReader["patientName"].ToString();
                    gender = ObjReader["gender"].ToString();
                    ward = ObjReader["ward"].ToString();
                    bht = ObjReader["bht"].ToString();
                    date = ObjReader["testDate"].ToString();
                    reportNo = ObjReader["reportNo"].ToString();
                    severity = ObjReader["severity"].ToString();        //**
                    age = Int32.Parse(ObjReader["age"].ToString());
                    months = Int32.Parse(ObjReader["months"].ToString());
                    specimen = Record.StringToArray(ObjReader["specimen"].ToString());
                    refNoSet.Add(new Record(reportNo, ward, bht, "", name, age, gender, specimen, "", "", "", date,"",severity,"",0));
            }
            if (refNoSet.Count > 0)
            {
                int temp = MainWindow.topid;
                string StrCmd2;
                if (option == 1)
                    StrCmd2 = "SELECT * FROM Table1 WHERE reportNo = '" + refNoSet.Last().Reference_No + "'";
                else
                    StrCmd2 = "SELECT * FROM Table1 WHERE reportNo = '" + refNoSet[(MainWindow.listsize - 1)].Reference_No + "'";
                string StrCmd3 = "SELECT * FROM Table1 WHERE reportNo = '" + refNoSet.First().Reference_No + "'";
                OleDbCommand Cmd2 = new OleDbCommand(StrCmd2, MyConn);
                OleDbDataReader ObjReader2 = Cmd2.ExecuteReader();
                if (ObjReader2 == null)
                {
                    MainWindow.hasmore = false;
                    Console.WriteLine();////////////////////////////////////error in connecting to the database
                    return null;
                }
                else
                {
                    ObjReader2.Read();
                    MainWindow.bottomid = Int32.Parse(ObjReader2["ID"].ToString());
                    OleDbCommand Cmd3 = new OleDbCommand(StrCmd3, MyConn);
                    OleDbDataReader ObjReader3 = Cmd3.ExecuteReader();
                    if (ObjReader3 == null)
                    {
                        MainWindow.hasmore = false;
                        Console.WriteLine();////////////////////////////////////error in connecting to the database
                        return null;
                    }
                    else
                    {
                        ObjReader3.Read();
                        MainWindow.topid = Int32.Parse(ObjReader3["ID"].ToString());
                    }
                    if (refNoSet.Count == (MainWindow.listsize + 1))
                    {
                        MainWindow.hasmore = true;
                        refNoSet.Remove(refNoSet.Last());
                    }
                    else
                        MainWindow.hasmore = false;
                    if (option == 0)
                    {
                        MainWindow.topid = MainWindow.bottomid;
                        MainWindow.bottomid = temp;
                        refNoSet.Reverse();
                    }
                    return refNoSet;
                }
            }
            else
            {
                MainWindow.hasmore = false;
                return null;
            }
        }
        */
        public void getTheRest(Record record)
        {
            string StrCmd = "SELECT  title, macroscopy, microscopy, conclusion, requestDate, clinicalDetails FROM Table1  WHERE reportNo = '" + record.Reference_No + "'";
            OleDbCommand Cmd = new OleDbCommand(StrCmd, MyConn);                            //**
            OleDbDataReader ObjReader = Cmd.ExecuteReader();
            ObjReader.Read();
            record.title = ObjReader["title"].ToString();
            record.macroscopy = ObjReader["macroscopy"].ToString();
            record.microscopy = ObjReader["microscopy"].ToString();
            record.conclusion = ObjReader["conclusion"].ToString();
            record.requestDate = ObjReader["requestDate"].ToString();   //**
            record.clinicalDetails = ObjReader["clinicalDetails"].ToString();//**
            record.complete = true;
        }
 private void buttonClear_Click(object sender, RoutedEventArgs e)
 {
     clear();
     draftWorkingOn = null;
 }