public JsonResult CreateOrEdit(BELibrary.Entity.Doctor input, bool isEdit)
        {
            try
            {
                if (isEdit)
                {
                    using (var workScope = new UnitOfWork(new PatientManagementDbContext()))
                    {
                        var elm = workScope.Doctors.Get(input.Id);

                        if (elm != null) //update
                        {
                            elm = input;

                            workScope.Doctors.Put(elm, elm.Id);
                            workScope.Complete();

                            return(Json(new { status = true, mess = "Cập nhập thành công " }));
                        }
                        else
                        {
                            return(Json(new { status = false, mess = "Không tồn tại " + KeyElement }));
                        }
                    }
                }
                else
                {
                    using (var workScope = new UnitOfWork(new PatientManagementDbContext()))
                    {
                        input.Id = Guid.NewGuid();
                        workScope.Doctors.Add(input);
                        workScope.Complete();
                        return(Json(new { status = true, mess = "Thêm thành công " + KeyElement }));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Json(new { status = false, mess = "Có lỗi xảy ra: " + ex.Message }));
            }
        }
        public JsonResult Register(AccountRegiter usreg, string rePassword)
        {
            if (usreg.Password != rePassword)
            {
                return(Json(new { status = false, mess = "Mật khẩu không khớp" }));
            }
            using (var workScope = new UnitOfWork(new PatientManagementDbContext()))
            {
                var account = workScope.Accounts.FirstOrDefault(x => x.UserName.ToLower() == usreg.UserName.ToLower());
                if (account == null)
                {
                    try
                    {
                        var doctors = workScope.Doctors.GetAll().ToList();
                        BELibrary.Entity.Doctor doctor = doctors[0];
                        //add record
                        Record record   = new Record();
                        var    recordid = Guid.NewGuid();
                        record.Id           = recordid;
                        record.CreatedDate  = DateTime.Today;
                        record.CreatedBy    = "Quản trị";
                        record.ModifiedBy   = "Quản trị";
                        record.ModifiedDate = DateTime.Today;
                        record.StatusRecord = 1;
                        record.DoctorId     = doctor.Id;
                        workScope.Records.Add(record);
                        workScope.Complete();

                        //add patient
                        Patient pati  = new Patient();
                        var     paiid = Guid.NewGuid();
                        pati.Id       = paiid;
                        pati.FullName = usreg.FullName;
                        pati.Gender   = usreg.Gender;
                        pati.IndentificationCardDate = usreg.IndentificationCardDate;
                        pati.IndentificationCardId   = usreg.IndentificationCardId;
                        pati.Phone       = usreg.Phone;
                        pati.Email       = usreg.Email;
                        pati.Address     = usreg.Address;
                        pati.DateOfBirth = usreg.DateOfBirth;
                        int code;
                        // Create patient code

                        var patient = workScope.Patients.GetAll().OrderByDescending(x => x.PatientCode.Length).
                                      ThenByDescending(x => x.PatientCode).FirstOrDefault();
                        if (patient != null)
                        {
                            code = Int32.Parse(patient.PatientCode) + 1;
                        }
                        else
                        {
                            code = 1;
                        }

                        pati.PatientCode = code.ToString();

                        pati.JoinDate = DateTime.Now;
                        pati.Status   = true;

                        pati.RecordId = recordid;

                        workScope.Patients.Add(pati);

                        workScope.Complete();

                        var passwordFactory = usreg.Password + VariableExtensions.KeyCrypto;
                        var passwordCrypto  = CryptorEngine.Encrypt(passwordFactory, true);

                        Account ac = new Account();
                        ac.FullName   = pati.FullName;
                        ac.Gender     = pati.Gender;
                        ac.Phone      = pati.Phone;
                        ac.UserName   = usreg.UserName;
                        ac.Password   = passwordCrypto;
                        ac.Role       = RoleKey.Patient;
                        ac.LinkAvatar = "/Content/images/team/2.png";
                        ac.Id         = Guid.NewGuid();
                        ac.PatientId  = paiid;

                        workScope.Accounts.Add(ac);
                        workScope.Complete();

                        //Login luon
                        if (HttpContext.Request.Url != null)
                        {
                            var host = HttpContext.Request.Url.Authority;

                            var cookieClient       = usreg.UserName + "|" + host.ToLower() + "|" + ac.Id;
                            var decodeCookieClient = CryptorEngine.Encrypt(cookieClient, true);
                            var userCookie         = new HttpCookie(CookiesKey.Client)
                            {
                                Value   = decodeCookieClient,
                                Expires = DateTime.Now.AddDays(30)
                            };
                            HttpContext.Response.Cookies.Add(userCookie);
                            return(Json(new { status = true, mess = "Đăng ký thành công" }));
                        }
                        else
                        {
                            return(Json(new { status = false, mess = "Thêm không thành công" }));
                        }
                    }
                    catch (Exception ex)
                    {
                        return(Json(new { status = false, mess = "Thêm không thành công" }));
                    }
                }
                else
                {
                    return(Json(new { status = false, mess = "Username không khả dụng" }));
                }
            }
        }