public ViewResult StudentDetails(int id)
        {
            var students = db.Students.Single(x => x.Client_Id == id);
            int stid = 0;
            if (students != null)
                stid = students.Student_Id;
            int caseId = db.Cases.Single(x => x.Student_Id == stid).Case_Id;
            string name = db.Clients.Single(x => x.Client_Id == id).GivenName + " " + db.Clients.Single(x => x.Client_Id == id).LastName;

            // Getting all the applications with completed = false
            //var applications = db.Applications.ToList().Where(c => c.Student_Id == stid && (c.Completed == false || c.Completed == null));

            // Getting all the applications
            var applications = db.Applications.ToList().Where(c => c.Student_Id == stid);

            ViewBag.clientId = id;
            ViewBag.casedocs = GetCaseItems(caseId);
            ViewBag.studentName = name;
            ViewBag.caseId = caseId;
            BindCurrency("-Select Currency-");

            List<DocumentIndexViewModel> documentsViewModel = new List<DocumentIndexViewModel>();

            foreach (var item in applications)
            {
                List<Template_Document> appDocTemplates = db.Template_Document.Where(c => c.Course_Id == item.Course_Id).ToList();
                List<Application_Document> app = (from a in db.Applications
                                                  from t in db.Application_Document
                                                  where a.Student_Id == stid && a.Course_Id == item.Course_Id && a.Case_Id == caseId && t.Application_Id == a.Application_Id
                                                  select t).ToList();
                DocumentIndexViewModel docVM = new DocumentIndexViewModel(item.Course_Id);

                docVM.appDoc = app;
                docVM.tempDoc = appDocTemplates;
                docVM.courseId = item.Course_Id;

                documentsViewModel.Add(docVM);
            }

            ViewBag.DocumentViewModel = documentsViewModel;

            ViewBag.casedocs = GetCaseItems(caseId);

            //ViewBag.casedocs = casedocs;
            return View(applications);
        }
        public ViewResult StudentDetailsCompleted(int id)
        {
            int stid = db.Students.Single(x => x.Client_Id == id).Student_Id;
            int caseId = db.Cases.Single(x => x.Student_Id == stid).Case_Id;
            string name = db.Clients.Single(x => x.Client_Id == id).GivenName + " " + db.Clients.Single(x => x.Client_Id == id).LastName;
            var applications = db.Applications.ToList().Where(c => c.Student_Id == stid && c.Completed == true);
            ViewBag.clientId = id;
            ViewBag.casedocs = GetCaseItems(caseId);
            ViewBag.studentName = name;
            ViewBag.caseId = caseId;

            List<DocumentIndexViewModel> documentsViewModel = new List<DocumentIndexViewModel>();

            foreach (var item in applications)
            {
                List<Template_Document> appDocTemplates = db.Template_Document.Where(c => c.Course_Id == item.Course_Id).ToList();
                List<Application_Document> app = (from a in db.Applications
                                                  from t in db.Application_Document
                                                  where a.Student_Id == stid && a.Course_Id == item.Course_Id && a.Case_Id == caseId && t.Application_Id == a.Application_Id
                                                  select t).ToList();
                DocumentIndexViewModel docVM = new DocumentIndexViewModel(item.Course_Id);

                docVM.appDoc = app;
                docVM.tempDoc = appDocTemplates;
                docVM.courseId = item.Course_Id;

                documentsViewModel.Add(docVM);
            }

            ViewBag.DocumentViewModel = documentsViewModel;

            ViewBag.casedocs = GetCaseItems(caseId);

            //ViewBag.casedocs = casedocs;
            return View(applications);
        }
        public ViewResult Index(int go = -1, bool faq = false)
        {
            BindCurrency("--Currency--");
            PopulatePayment(-1);
            PopulatePaymentDetails();
            // Determine which application to show on page load
            if (go >= 0)
                ViewBag.Go = go;

            // Show FAQ
            if (faq)
            {
                ViewBag.FAQ = true;
            }

            List<ApplicationDocumentViewModel> appViewModel = new List<ApplicationDocumentViewModel>();
            if ((string)TempData[SUCCESS_EDIT] == "true")
            {
                ViewBag.SuccessEdit = true;
            }

            if (CookieHelper.isStudent())
            {
                stid = Convert.ToInt32(CookieHelper.StudentId);
                //ViewBag.EditMessage = Request.QueryString["message"];

            }
            else
            {
                stid = -1;
                NotificationHandler.setNotification(NotificationHandler.NOTY_ERROR, "Invalid student!");
                RedirectToAction("Index", "Home");
            }

            var applications = db.Applications.ToList().Where(c => c.Student_Id == stid);

            List<CaseDocument> completedCaseDocs = new List<CaseDocument>();
            List<CaseDocument> pendingCaseDocs = new List<CaseDocument>();

            // Retrieve all Case Documents
            int caseId = db.Cases.Single(x => x.Student_Id == stid).Case_Id;
            var caseDocs = db.CaseDocuments.Where(x => x.Case_Id == caseId);
            foreach (CaseDocument cd in caseDocs)
            {
                if (cd.FileName == null)
                {
                    pendingCaseDocs.Add(cd);
                }
                else
                {
                    completedCaseDocs.Add(cd);
                }
            }

            // Retrieve all documents for this case
            foreach (var item in applications)
            {
                List<Application_Document> appDocs = item.Application_Document.ToList();

                ApplicationDocumentViewModel tempAppViewModel = new ApplicationDocumentViewModel();

                tempAppViewModel.app = item;

                foreach (Template_Document template in item.Course.Template_Document.ToList())
                {
                    Application_Document app = appDocs.SingleOrDefault(x => x.TemplateDoc_Id == template.TemplateDoc_Id);
                    // If file already uploaded for this template
                    if (appDocs.Where(x => x.TemplateDoc_Id == template.TemplateDoc_Id).Count() <= 0)
                    {
                        tempAppViewModel.notCompleted.Add(template);
                    }
                    else
                    {
                        tempAppViewModel.completed.Add(app);
                    }
                }

                tempAppViewModel.offer = item.Application_Result.SingleOrDefault(x => x.Type == OFFER_LETTER_TYPE);
                tempAppViewModel.coe = item.Application_Result.SingleOrDefault(x => x.Type == COE_TYPE);
                tempAppViewModel.acceptance = item.Application_Result.SingleOrDefault(x => x.Type == ACCEPTANCE_TYPE);
                tempAppViewModel.completedAcceptance = item.Application_Result.SingleOrDefault(x => x.Type == COMPLETED_ACCEPTANCE_TYPE);

                tempAppViewModel.notCompletedGeneralDocs = pendingCaseDocs;
                tempAppViewModel.completedGeneralDocs = completedCaseDocs;

                appViewModel.Add(tempAppViewModel);
            }

            List<DocumentIndexViewModel> documentsViewModel = new List<DocumentIndexViewModel>();

            foreach (var item in applications)
            {
                List<Template_Document> appDocTemplates = db.Template_Document.Where(c => c.Course_Id == item.Course_Id).ToList();
                List<Application_Document> app = (from a in db.Applications
                                                  from t in db.Application_Document
                                                  where a.Student_Id == stid && a.Course_Id == item.Course_Id && a.Case_Id == caseId && t.Application_Id == a.Application_Id
                                                  select t).ToList();
                DocumentIndexViewModel docVM = new DocumentIndexViewModel(item.Course_Id);

                docVM.appDoc = app;
                docVM.tempDoc = appDocTemplates;
                docVM.courseId = item.Course_Id;

                documentsViewModel.Add(docVM);

            }

            ViewBag.DocumentViewModel = documentsViewModel;

            ViewBag.casedocs = GetCaseItems(caseId);

            //ViewBag.casedocs = casedocs;
            return View(appViewModel);
        }