public ActionResult Update(DoctorProfile model, IList <AddressDetail> addr)
        {
            if (addr != null)
            {
                model.Addresses = addr; // Assign AddressDetail list to PatientProfile model
            }
            else
            {
                model.Addresses = null;
            }

            if (ModelState.IsValid)
            {
                int             userid = User.Identity.GetUserId <int>();
                ApplicationUser user   = UserManager.FindById(userid);
                if (user == null)
                {
                    ViewBag.Errors = "Không thấy bệnh nhân này";
                    return(RedirectToAction("Detail", "DoctorProfile", new { Message = ProfileMessageId.Error }));
                }

                // Update account data
                user.Email       = model.Email;
                user.DateOfBirth = model.DoB;
                user.FullName    = model.FullName;
                user.PhoneNumber = model.PhoneNumber;
                user.Gender      = model.Gender;
                var updateacc = UserManager.Update(user);
                if (!updateacc.Succeeded)
                {
                    ViewBag.Errors = "Không cập nhật được account";
                    return(View("Detail", model));
                }

                // Update profile data
                TM_Doctor doctor = new DoctorDao().FindByUserId(userid);
                if (doctor == null)
                {
                    doctor              = new TM_Doctor();
                    doctor.UserId       = userid;
                    doctor.Major        = model.Major;
                    doctor.IdentityCard = model.IdentityCard;
                    if (new DoctorDao().Insert(doctor) < 1)
                    {
                        ViewBag.Errors = "Không thêm được profile";
                        return(View("Detail", model));
                    }
                }
                else
                {
                    doctor.Major        = model.Major;
                    doctor.IdentityCard = model.IdentityCard;
                    if (!new DoctorDao().Update(doctor))
                    {
                        ViewBag.Errors = "Không cập nhật được profile";
                        return(View("Detail", model));
                    }
                }
                return(RedirectToAction("Detail", new { message = ProfileMessageId.ChangeAccountSuccess }));
            }
            return(View("Detail", model));
        }
        public ActionResult Create(OrderViewModel model)
        {
            // List all paraclinical params
            ViewBag.SymptomList = new MeasureParamDao().ListAll(2);
            if (ModelState.IsValid)
            {
                int curUserId = User.Identity.GetUserId <int>(); //Get current user Id
                if (curUserId <= 0)
                {
                    curUserId = 1021;
                }

                TM_Order order = new TM_Order();
                order.OrderDate = DateTime.Now;
                order.PatientId = curUserId;
                order.Status    = true;
                order.Title     = model.Title;
                order.Notes     = model.Notes;
                // Create an order
                long orderId = new OrderDao().Create(order);
                // add symptoms to order
                for (int i = 0; i < model.Symptoms.Length; i++)
                {
                    //
                    if (!(new OrderDao().AddSymptom(order.Id, model.Symptoms[i])))
                    {
                        ModelState.AddModelError("", "Không thêm được triệu chứng thứ" + (i + 1));
                        return(View(model));
                    }
                }
                // add clinical params to order
                // add Height param
                if (!new OrderDao().AddParamByName(order.Id, "Height", model.Height, DateTime.Now))
                {
                    ModelState.AddModelError("", "Không thêm được chiều cao");
                    return(View(model));
                }
                // add Weight param
                if (!new OrderDao().AddParamByName(order.Id, "Weight", model.Weight, DateTime.Now))
                {
                    ModelState.AddModelError("", "Không thêm được cân nặng");
                    return(View(model));
                }
                // add Weight param
                if (!new OrderDao().AddParamByName(order.Id, "LowPressure", model.LowPressure, model.LowPressureDate))
                {
                    ModelState.AddModelError("", "Không thêm được huyết áp tâm thu");
                    return(View(model));
                }
                // add Weight param
                if (!new OrderDao().AddParamByName(order.Id, "HighPressure", model.HighPressure, model.HighPressureDate))
                {
                    ModelState.AddModelError("", "Không thêm được huyết áp tâm trương");
                    return(Json(new { loi = "HighPressure" }));
                    //return View(model);
                }
                // add Weight param
                if (!new OrderDao().AddParamByName(order.Id, "HeartBeat", model.HeartBeat, model.HeartBeatDate))
                {
                    ModelState.AddModelError("", "Không thêm được nhịp tim");
                    return(Json(new { loi = "HeartBeat" }));
                    //return View(model);
                }

                // Add paraclinical params to order
                for (int j = 0; j < model.ParamIds.Length; j++)
                {
                    if (!new OrderDao().AddParamById(orderId, (short)model.ParamIds[j], model.ParaclinicalParams[j], model.MeasuredDates[j]))
                    {
                        ModelState.AddModelError("", "Không thêm được thông số cận lâm sàng");
                        return(Json(new { loi = "paraclinical" }));
                        //return View(model);
                    }
                }

                // Find a doctor for this order
                int doctorId = new DoctorDao().FindDoctorForPatient(curUserId);
                if (doctorId <= 0)
                {
                    ModelState.AddModelError("", "Không tìm thấy bác sĩ nào trên hệ thống");
                    return(Json(new { loi = "doctor not found" }));
                    //return View(model);
                }
                // Assign doctor to order
                if (!new OrderDao().AssignDoctorToOrder(doctorId, orderId, curUserId))
                {
                    ModelState.AddModelError("", "Không thể gửi yêu cầu này cho bác sĩ");
                    return(Json(new { loi = "asign doctor for an order", doctor = doctorId, patient = curUserId }));
                    //return View(model);
                }

                // Notify to doctor
                TM_Notification noty = new TM_Notification();
                noty.Title       = "Yêu cầu chẩn đoán từ " + User.Identity.GetFullName();
                noty.Link        = (int)order.Id;
                noty.CreatedDate = DateTime.Now;
                noty.Contents    = "Bệnh nhân gửi yêu cầu chẩn đoán cho bác sĩ";
                noty.Type        = 1; // yêu cầu chẩn đoán
                noty.ReceiverId  = doctorId;
                noty.Status      = false;
                var notiResult = new NotificationDao().Create(noty);
                if (notiResult < 1)
                {
                    ModelState.AddModelError("", "Không tìm thấy bác sĩ nào trên hệ thống");
                    return(Json(new { loi = "Không thêm được thông báo" }));
                }
                TempData["Message"] = "Tạo bệnh án thành công";
                return(RedirectToAction("Index", "PatientOrder", new { Area = "Patient" }));
            }

            return(View(model));
        }