void CreateReport(PatientReport report)
        {
            BackgroundWorker bw = new BackgroundWorker();

            bw.DoWork += (sender, e) =>
            {
                string path     = Path.Combine(Program.BaseDir(), "Uploads");
                string dir      = Path.Combine(Program.BaseDir(), "Uploads", PatientEntity.UniqueID.ToString(), report.UniqueID.ToString());
                string filePDF  = Path.Combine(dir, "report.pdf");
                string fileHTML = Path.Combine(dir, "report.html");
                if (!File.Exists(filePDF))
                {
                    string text = Common.Helper.ReadAllTextReportFile();

                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    //else if (File.Exists(filePDF))
                    //    File.Delete(filePDF);

                    string            posteriorHTML = "<tr>";
                    string            anteriorHTML = "<tr>";
                    string            prediction = string.Empty;
                    int               posteriorCount = 0, anteriorCount = 0;
                    List <ReportData> posteriorDatas = new Patient().GetPosteriorReportData(report.Id);
                    List <ReportData> anteriorDatas  = new Patient().GetAnteriorReportData(report.Id);

                    if (posteriorDatas != null)
                    {
                        bool docReview = posteriorDatas.Any(x => x.Prediction == "Doctor review recommended");
                        bool noDR      = posteriorDatas.Any(x => x.Prediction == "No DR detected");
                        bool badImg    = posteriorDatas.Any(x => x.Prediction == "Bad Image");

                        if (docReview)
                        {
                            prediction = "EXAMINATION RESULT: Diabetic Retinopathy Suspected - Doctor Review Recommended                    * KINDLY CORRELATE CLINICALLY *";
                        }
                        else if (noDR)
                        {
                            prediction = "EXAMINATION RESULT: No Abnormlities detected                    * KINDLY CORRELATE CLINICALLY *";
                        }
                        else if (badImg)
                        {
                            prediction = "EXAMINATION RESULT: Bad Image                    * KINDLY CORRELATE CLINICALLY *";
                        }
                        else
                        {
                            prediction = "EXAMINATION RESULT:                     * KINDLY CORRELATE CLINICALLY *";
                        }

                        foreach (var item in posteriorDatas)
                        {
                            item.ImageUrl = Path.Combine(path, item.Img);
                            item.FileName = Path.GetFileName(item.ImageUrl);
                            string result = item.Prediction + " | " + (item.Eye == "OS" ? "Right Eye" : "Left Eye");
                            if (posteriorCount != 0 && (posteriorCount % 2 == 0))
                            {
                                posteriorHTML += "<tr><td style='width:33.33%; vertical-align:top;padding-bottom:15px;'><table style='width:100%;height:200px;margin-bottom:10px;'><tr><td><img style='max-width:200px;max-height:200px;' src='" + item.FileName + "' /></td></tr></table><h3 style='font-size: 14px;color:#333;font-weight:400;margin:0;'>" + result + "</h3></td>";
                            }
                            else
                            {
                                posteriorHTML += "<td style='width:33.33%; vertical-align:top;padding-bottom:15px;'><table style='width:100%;height:200px;margin-bottom:10px;'><tr><td><img style='max-width:200px;max-height:200px;' src='" + item.FileName + "' /></td></tr></table><h3 style='font-size: 14px;color:#333;font-weight:400;margin:0;'>" + result + "</h3></td>" + (posteriorCount != 0 ? "</tr>" : "");
                            }
                            posteriorCount++;
                        }
                    }

                    foreach (var item in anteriorDatas)
                    {
                        item.ImageUrl = Path.Combine(path, item.Img);
                        item.FileName = Path.GetFileName(item.ImageUrl);
                        string result = item.Prediction + " | " + item.Eye == "OS" ? "Right Eye" : "Left Eye";
                        if (posteriorCount != 0 && (posteriorCount % 2 == 0))
                        {
                            anteriorHTML += "<tr><td style='width:33.33%; vertical-align:top;padding-bottom:15px;'><table style='width:100%;height:200px;margin-bottom:10px;'><tr><td><img style='max-width:200px;max-height:200px;' src=" + item.FileName + " /></td></tr></table><h3 style='font-size: 14px;color:#333;font-weight:400;margin:0;'>" + result + "</h3></td>";
                        }
                        else
                        {
                            anteriorHTML += "<td style='width:33.33%; vertical-align:top;padding-bottom:15px;'><table style='width:100%;height:200px;margin-bottom:10px;'><tr><td><img style='max-width:200px;max-height:200px;' src=" + item.FileName + " /></td></tr></table><h3 style='font-size: 14px;color:#333;font-weight:400;margin:0;'>" + result + "</h3></td>" + (posteriorCount != 0 ? "</tr>" : "");
                        }
                        anteriorCount++;
                    }
                    text = string.Format(text, DateTime.Now.ToShortDateString(), PatientEntity.Nm, PatientEntity.DocNm, PatientEntity.HospitalNm, PatientEntity.Id.ToString(), PatientEntity.HospitalID, PatientEntity.HospitalScreening, PatientEntity.Mob, PatientEntity.Age, PatientEntity.Sex, PatientEntity.Hypertension, PatientEntity.Cataract, PatientEntity.LaserTreatment, PatientEntity.AllergyDrugs, PatientEntity.CurrentMedications, PatientEntity.Info, posteriorHTML, PatientEntity.OtherOption, PatientEntity.OthersID, anteriorHTML, predictionResult, PatientEntity.PatientId);
                    System.IO.File.WriteAllText(fileHTML, text);


                    HtmlToPdf   converter = new HtmlToPdf();
                    PdfDocument doc       = converter.ConvertUrl(fileHTML);
                    //doc.Margins = new PdfMargins(5);

                    doc.Save(filePDF);
                    doc.Close();
                }
            };

            bw.RunWorkerCompleted += (sender, e) =>
            {
                if (e.Error != null)
                {
                    //ModernDialog.ShowMessage(e.Error.InnerException.Message, "Error Alert", MessageBoxButton.OK);
                    ModernDialog.ShowMessage("Internal Server Error. Please contact your administrator", "Error Alert", MessageBoxButton.OK);
                }
            };
            bw.RunWorkerAsync();
        }
        public ReportViewModel(PatientEntity model, PatientReport obj)
        {
            ShowImageCommand = new DelegateCommand(OnShowImageCommand);
            BackCommand      = new DelegateCommand(OnBackCommand);
            LogOffCommand    = new DelegateCommand(OnLogOffCommand);
            PrintPDFCommand  = new DelegateCommand(OnPrintPDFCommand);
            string path = Path.Combine(Program.BaseDir(), "Uploads");

            ReportDatas   = new ObservableCollection <ReportData>();
            PatientEntity = model;
            if (PatientEntity.Sex == "m")
            {
                PatientEntity.Sex = "Male";
            }
            else
            {
                PatientEntity.Sex = "Female";
            }

            if (PatientEntity.MaritalStatus == "No")
            {
                PatientEntity.MaritalStatus = "Single";
            }
            else
            {
                PatientEntity.MaritalStatus = "Married";
            }

            if (PatientEntity.IfResidentOfM == "Yes")
            {
                PatientEntity.OtherOption = "IC Number:";
                PatientEntity.OthersID    = PatientEntity.IcNumber;
            }
            else
            {
                PatientEntity.OtherOption = PatientEntity.OtherOption + ":";
            }

            if (obj != null)
            {
                PatientReport = obj;
            }
            else
            {
                PatientReport = new Patient().GetLastestReport(model.Id);
            }

            if (PatientReport != null)
            {
                var  osResult  = new Patient().GetPosteriorOSReportData(PatientReport.Id, false);
                var  odResult  = new Patient().GetPosteriorODReportData(PatientReport.Id, false);
                bool docReview = osResult.Any(x => x.Prediction == "Doctor review recommended");
                if (!docReview)
                {
                    docReview = odResult.Any(x => x.Prediction == "Doctor review recommended");
                }

                bool noDR = osResult.Any(x => x.Prediction == "No DR detected");
                if (!noDR)
                {
                    noDR = odResult.Any(x => x.Prediction == "No DR detected");
                }

                bool badImg = osResult.Any(x => x.Prediction == "Bad Image");
                if (!badImg)
                {
                    badImg = odResult.Any(x => x.Prediction == "Bad Image");
                }

                if (docReview)
                {
                    PredictionResult = "EXAMINATION RESULT: Diabetic Retinopathy Suspected - Doctor Review Recommended                    * KINDLY CORRELATE CLINICALLY *";
                }
                else if (noDR)
                {
                    PredictionResult = "EXAMINATION RESULT: No Abnormlities detected                    * KINDLY CORRELATE CLINICALLY *";
                }
                else if (badImg)
                {
                    PredictionResult = "EXAMINATION RESULT: Bad Image                    * KINDLY CORRELATE CLINICALLY *";
                }
                else
                {
                    PredictionResult = "EXAMINATION RESULT:                     * KINDLY CORRELATE CLINICALLY *";
                }

                foreach (var data in osResult)
                {
                    data.ImageUrl = Path.Combine(path, data.Img);
                    data.FileName = Path.GetFileName(data.ImageUrl);
                    OSReportDatas.Add(data);
                }
                foreach (var data in odResult)
                {
                    data.ImageUrl = Path.Combine(path, data.Img);
                    data.FileName = Path.GetFileName(data.ImageUrl);
                    ODReportDatas.Add(data);
                }
            }

            var report = new Patient().GetAllReport(model.Id);

            foreach (var item in report)
            {
                var result = new Patient().GetAllReportData(item.Id);
                item.ReportDatas = new ObservableCollection <ReportData>();
                foreach (var data in result)
                {
                    data.ImageUrl = Path.Combine(path, data.Img);
                    item.ReportDatas.Add(data);
                }
                PatientReports.Add(item);
            }

            TakeReportCommand     = new DelegateCommand(OnTakeReportCommand);
            PreviousReportCommand = new DelegateCommand(OnPreviousReportCommand);
            FtpTransferCommand    = new DelegateCommand(OnFtpTransferCommand);
            ViewReportDataCommand = new DelegateCommand(OnViewReportDataCommand);
            SaveNextCommand       = new DelegateCommand(OnSaveNextCommand);
            SaveExitCommand       = new DelegateCommand(OnSaveExitCommand);
            SendMailCommand       = new DelegateCommand(OnSendMailCommand);
            ViewPDFCommand        = new DelegateCommand(OnViewPDFCommand);
            if (PatientReport != null)
            {
                CreateReport(PatientReport);
            }
        }