// GET: PCPSurvey
        public ActionResult Index(string surveyCode)
        {
            if (!string.IsNullOrEmpty(surveyCode))
            {
                var surveyDto = _serviceSurvey.GetBySurveyCode(surveyCode);

                if (surveyDto == null )
                {
                    ViewBag.Message = Messages.GetMessage(Messages.MessageType.NotExist);
                    return View("../Message/Message");
                }

                if (surveyDto != null && surveyDto.SurveyType != Core.Common.SurveyType.PCP)
                {
                    return RedirectToAction("Index", "Survey", new { surveyCode = surveyCode });
                }

                if (surveyDto != null && surveyDto.IsComplete)
                {
                    ViewBag.Message = Messages.GetMessage(Messages.MessageType.Completed, surveyDto.ProviderLastName, surveyDto.FacilityName); 
                    return View("../Message/Message");
                }

                if (surveyDto != null && surveyDto.IsExpired)
                {
                    ViewBag.Message = Messages.GetMessage(Messages.MessageType.Expired, surveyDto.ProviderLastName, surveyDto.FacilityName); 
                    return View("../Message/Message");
                }

                if (surveyDto != null)
                {
                    var questionsDto = _serviceQuestion.GetBySurveyType(surveyDto.SurveyType);                    
                    PCPSurvey pcpSurvey = new PCPSurvey(surveyDto);

                    var props = pcpSurvey.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();
                    props.ForEach(prop =>
                    {
                        if (prop.PropertyType == typeof(Question))
                        {
                            var attr = prop.GetCustomAttribute<QuestionKeyAttribute>();                            
                            var question = new Question();
                            if(questionsDto != null && questionsDto.Count > 0)
                            {
                                var questionDto = questionsDto.First(d => d.Key.Equals(attr.QuestionKey));
                                if(questionDto != null)
                                {                                   
                                   question.QuestionText = questionDto.QuestionText;
                                }
                            }
                            prop.SetValue(pcpSurvey, question);
                        }
                    });

                    return View(pcpSurvey);
                }
            }
            return null;// View("../Message/Message");
        }
        public ActionResult Index(PCPSurvey pcpSurvey)
        {
            if (ModelState.IsValid)
            {
                var surveyDto = _serviceSurvey.GetBySurveyCode(pcpSurvey.Survey.SurveyCode);
                bool isEscalationReport = false;
                if (surveyDto != null && !surveyDto.IsComplete && !surveyDto.IsExpired)
                {  
                    List<AnswerDto> answerList = new List<AnswerDto>();
                    var questionsDto = _serviceQuestion.GetBySurveyType(surveyDto.SurveyType);
                    var props = pcpSurvey.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();
                    var surveyAnswerDto = new SurveyAnswerDto()
                    {
                        SurveyId = pcpSurvey.Survey.Id,
                        RespondDate = DateTime.Today
                    };
                    surveyAnswerDto = _serviceSurveyAnswer.Insert(surveyAnswerDto);
                    props.ForEach(prop =>
                    {
                        if (prop.PropertyType == typeof(Question))
                        {
                            var attr = prop.GetCustomAttribute<QuestionKeyAttribute>();                            
                            var question = questionsDto.First(d => d.Key.Equals(attr.QuestionKey));
                            var answer = new AnswerDto()
                            {
                                SurveyAnswerId = surveyAnswerDto.Id,
                                QuestionId = question.Id,
                                ResponseText = ((Question)prop.GetValue(pcpSurvey, null)).Response,
                                Note = ((Question)prop.GetValue(pcpSurvey, null)).Note

                            };
                            answerList.Add(answer);

                            //To know if Disagre or Strongly Disagree was selected as an answer, for send an email
                            if ((answer.ResponseText != null) && QuestionKey.PCPSurvey.ContainsKey(question.Key))
                            {
                                if (answer.ResponseText.ToLower().Equals("disagree") || answer.ResponseText.ToLower().Equals("strongly disagree"))
                                    isEscalationReport = true;
                            }
                        }
                    });

                    //Add answer
                    _serviceAnswer.Insert(answerList);
                    _serviceSurvey.UpdateComplete(pcpSurvey.Survey.Id);
                    ViewBag.Message = Messages.GetMessage(Messages.MessageType.Succefully);

                    //Send Email To Performance Management
                    if (isEscalationReport)
                    {
                        HospitalDetailsDto hospitalDetails = _serviceSurvey.GetHospitalDetailsByName(pcpSurvey.Survey.FacilityName);
                        byte[] fileToSend = GetDataToExportPCPSurvey(pcpSurvey, hospitalDetails);
                        _emailSender.SendEmail(fileToSend, ConfigurationManager.AppSettings["PerformanceManagementEmail"].ToString());
                    }

                    return View("../Message/Message");
                }

                if (surveyDto != null && surveyDto.IsComplete)
                {
                    ViewBag.Message = Messages.GetMessage(Messages.MessageType.Completed, surveyDto.ProviderLastName, surveyDto.FacilityName);
                    return View("../Message/Message");
                }

            }
            return View(pcpSurvey);
        }