// GET: F8YLProject
        //public ActionResult ProjectList()
        //{
        //    var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();
        //    var currentUserId = Session["CurrentUserID"] == null ? "-1" : Session["CurrentUserID"].ToString();
        //    UserRequest user = new UserRequest();

        //    UserProfileResponse userFile = user.profile(token, currentUserId);

        //    ProjectListResponse entData = new ProjectListResponse();
        //    if (userFile.data != null)
        //    {
        //        entData = projectRequest.list(token, userFile.data.hospitalid, 0, currentUserId);
        //    }

        //    ViewBag.UserFile = userFile.data;
        //    ViewBag.ProjectList = entData.data.data;
        //    return View();
        //}
        #endregion

        public ActionResult ProjectList()
        {
            var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();
            ViewBag.Token = token;
            var currentUserId = Session["CurrentUserID"] == null ? "-1" : Session["CurrentUserID"].ToString();

            ViewBag.UserID = currentUserId;
            ViewBag.HospitalID = Session["hospitalid"] == null ? "-1" : Session["hospitalid"].ToString();

            UserRequest user = new UserRequest();

            UserProfileResponse userFile = user.profile(token, currentUserId);
            if (userFile.data.role == "90")
            {
                return RedirectToAction("ManageIndex", "F8YLManage");
            }


            ProjectJoined entData = new ProjectJoined();
            if (userFile.data != null)
            {
                entData = projectRequest.Joined(token,"0");
            }

            ViewBag.UserFile = userFile.data;
            ViewBag.ProjectList = entData.data;
            return View();
        }
        public string Login(string mobile, string password)
        {
            string bllRsponse = string.Empty;
            AuthRequest auth = new AuthRequest();
            UserRequest userRequest = new UserRequest();

            bllRsponse = auth.Login(mobile, password);
            //bllRsponse = auth.Login("13012345676", "654321");

            // 将接口返回的信息记录在服务端Sesson
            LoginResponse bllRsponseModel = new LoginResponse();
            bllRsponseModel = JsonHelper.DeserializeJsonToObject<LoginResponse>(bllRsponse);

            if (bllRsponseModel.code == 0)
            {
                Session["Phone"] = mobile;
                Session["token"] = bllRsponseModel.data.token;
                Session["role"] = bllRsponseModel.data.role;
                
                UserProfileResponse userProfileResponse = userRequest.profile(bllRsponseModel.data.token, "");
                Session["username"] = userProfileResponse.data.username;
                Session["hospitalid"] = userProfileResponse.data.hospitalid;
                Session["CurrentUserID"] = userProfileResponse.data.id;
                Session["CurrentUserHospitalID"] = userProfileResponse.data.hospitalid;
                Session["password"] = password;
            }

            ////返回接口对象
            return bllRsponse;
        }
        // GET: F8YLManage       
        public ActionResult ManageIndex()
        {
            try
            {
                var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();
                ViewBag.Token = token;
                if (token == "XXXXXXX")//未登录返回首页
                {
                    return RedirectToAction("Index", "F8YLHome");
                }

                UserRequest user = new UserRequest();
                HospitalSearchResponse hsr = new HospitalSearchResponse();
                HospitalSearchResponse hsr80 = new HospitalSearchResponse();
                UserProfileResponse userFile = user.profile(token, "");
                //Session["ManageUserRole"] = userFile.data.role;
                if (Convert.ToInt32(userFile.data.role) < 80)//只有医院管理员(80)和后台管理员(90)账号才能访问管理页面
                {
                    return Content("<script>alert('此用户无权限访问,请使用医院或者后台管理员账号。');window.location.href='/F8YLHome/Index';</script>");
                }


                if (userFile != null)
                {
                    hsr = hospitalRequest.Search(token, 1, "");
                }
                if (userFile.data.role == "80")
                {
                    hsr80.code = hsr.code;
                    hsr.message = hsr.message;
                    hsr80.data = new HospitalSearchEntity();
                    hsr80.data.data = new List<HospitalEntity>();
                    hsr80.data.data.Add(hsr.data.data.Find(x => x.id == userFile.data.hospitalid));
                    hsr = hsr80;
                }
                ViewBag.LoginUserRole = userFile.data.role;
                ViewBag.UserFile = userFile.data;
                ViewBag.HospitalList = hsr;
            }
            catch (Exception ex)
            {
                AppLog.Instance.Write("ManageIndex", AppLog.LogMessageType.Error, ex);
            }
            return View();
        }
        public string HostpitalSearch()
        {
            string strJson = string.Empty;
            try
            {
                var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();
                UserRequest user = new UserRequest();
                HospitalSearchResponse hsr = new HospitalSearchResponse();
                HospitalSearchResponse hsr80 = new HospitalSearchResponse();
                UserProfileResponse userFile = user.profile(token, "");
                if (userFile != null)
                {
                    hsr = hospitalRequest.Search(token, 1, "");
                }
                if (userFile.data.role == "80")
                {
                    hsr80.code = hsr.code;
                    hsr.message = hsr.message;
                    hsr80.data = new HospitalSearchEntity();
                    hsr80.data.data = new List<HospitalEntity>();
                    hsr80.data.data.Add(hsr.data.data.Find(x => x.id == userFile.data.hospitalid));
                    hsr = hsr80;

                }
                if (hsr != null)
                {
                    strJson = JsonHelper.SerializeObject(hsr);
                }

            }
            catch (Exception ex)
            {
                AppLog.Instance.Write("HostpitalSearch", AppLog.LogMessageType.Error, ex);
            }
            return strJson;
        }
        public string ImportUser(string filexls)
        {
            string jsonDropUser = string.Empty;
            try
            {
                UserRequest ur = new UserRequest();
                var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();

                jsonDropUser = ur.ImportUser(token, filexls);

            }
            catch (Exception ex)
            {

                AppLog.Instance.Write("ImportUser", AppLog.LogMessageType.Error, ex);
            }
            return jsonDropUser;
        }
        public string UserStatusDisable(string id)
        {
            string jsonDropUser = string.Empty;
            try
            {
                UserRequest ur = new UserRequest();
                var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();

                jsonDropUser = ur.UserStatusDisable(token, id);

            }
            catch (Exception ex)
            {

                AppLog.Instance.Write("UserStatusDisable", AppLog.LogMessageType.Error, ex);
            }
            return jsonDropUser;
        }
        public string UpdateUser(string userid, string username, string deptname, string duty, string tel, string email)
        {
            string strJson = string.Empty;
            try
            {
                UserRequest ur = new UserRequest();
                var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();

                strJson = ur.UpdateUser(token, userid, username, deptname, duty, tel, email);

            }
            catch (Exception ex)
            {
                AppLog.Instance.Write("UpdateUser", AppLog.LogMessageType.Error, ex);
            }
            return strJson;
        }
        public string AddUser(string id, string idcard, string role, string username, string mobile, string email, string deptname, string duty, string tel)
        {
            string jsonAddUser = string.Empty;
            try
            {
                UserRequest ur = new UserRequest();
                var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();

                UserAddResponseDataEntity ua = new UserAddResponseDataEntity
                {
                    idcard = idcard,
                    username = username,
                    role = role,
                    mobile = mobile,
                    hospitalid = id,
                    email = email,

                    deptname = deptname,
                    duty = duty,
                    tel = tel,

                };

                jsonAddUser = ur.AddUser(token, ua);

            }
            catch (Exception ex)
            {

                AppLog.Instance.Write("AddUser", AppLog.LogMessageType.Error, ex);
            }
            return jsonAddUser;
        }
        public string GetUserProfileByHospitalId(string hospitalId)
        {
            string jsonUser = string.Empty;

            try
            {
                var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();
                HospitalUserProfileResponse hupr = new HospitalUserProfileResponse();

                UserRequest ur = new UserRequest();
                hupr = ur.SearchUsers(token, hospitalId, "");
                if (hupr.data != null)
                {
                    hupr.data.data.Sort((left, right) => Convert.ToInt32(right.role) - Convert.ToInt32(left.role));//按角色由大到小排序

                    jsonUser = JsonHelper.SerializeObject(hupr);
                }
                else if (hupr.code == 0 && hupr.data == null)
                {
                    APIResponseBaseExtend res = new APIResponseBaseExtend
                    {
                        code = 0,
                        message = "无用户。",
                        data = null

                    };
                    jsonUser = JsonHelper.SerializeObject(res);
                }

            }
            catch (Exception ex)
            {

                AppLog.Instance.Write("GetUserProfileByHospitalId", AppLog.LogMessageType.Error, ex);
            }
            return jsonUser;
        }
        public void DownloadTemplate(string templateName)
        {
            var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();

            UserRequest user = new UserRequest();
            string fileData = user.Download(templateName, token);
        }
        public string GetUserInfo(string userid)
        {
            var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();
            UserRequest user = new UserRequest();

            return user.getProfile(token, userid);
        }
        //ProjectDetail
        public ActionResult ProjectDetail(string id)
        {
            var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();

            ViewBag.role = Session["role"] == null ? "XXXXXXX" : Session["role"].ToString();
            ViewBag.username = Session["username"] == null ? "XXXXXXX" : Session["username"].ToString();

            ViewBag.sessionHospitalid= Session["hospitalid"] == null ? "XXXXXXX" : Session["hospitalid"].ToString();


            //实体
            ProjectDetailResponse projectDetailResponse = new ProjectDetailResponse();

            HospitalRequest hospitalrequest = new HospitalRequest();

            HospitalSearchResponse response = new HospitalSearchResponse();//医院列表

            HospitalUserProfileResponse hospitalprofile = new HospitalUserProfileResponse();//医院pi

            ProjectTplListResponse tplListResponse = new ProjectTplListResponse();//模板列表

            TplDetailResponse tpldetailresponse = new TplDetailResponse();//模板详情

            //方法
            UserRequest userrequest = new UserRequest();
            ViewBag.UserFile = userrequest.profile(token, "").data;

            ProjectRequest projecttplrequest = new ProjectRequest();

            //TplRequest tplrequest = new TplRequest();

            List<HospitalUserProfileResponse> listhospitalfrofile = new List<HospitalUserProfileResponse>();

            string apiResponse = string.Empty;
            try
            {
                projectDetailResponse = projectRequest.detail(token, id);

                if (projectDetailResponse.data != null)
                {
                    if (projectDetailResponse.data.tpl == null)
                    {
                        projectDetailResponse.data.tpl = new List<ProjectDetailTplInfo>();
                    }

                    //模板列表
                    tplListResponse = projecttplrequest.ProjectTplList(token, projectDetailResponse.data.id, "", 1);

                    if (tplListResponse.data != null)
                    {
                        //列表detail
                        tpldetailresponse = tplrequest.TplDetail(token, tplListResponse.data.data[0].tplid);
                    }

                    //医院列表
                    response = hospitalrequest.Search(token, 1, "");

                    //医院pi
                    if (response.data != null)
                    {
                        foreach (HospitalEntity hs in response.data.data)
                        {
                            hospitalprofile = userrequest.SearchHospitalPi(token, hs.id, "");
                            if (hospitalprofile.data != null)
                            {
                                listhospitalfrofile.Add(hospitalprofile);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            ViewBag.TplList = tplListResponse;//tpllist
            ViewBag.hospitalSearch = response;//hospitallist
            ViewBag.hopital = listhospitalfrofile;//pi
            ViewBag.TplDetail = tpldetailresponse;//tpldetail
            ViewBag.Token = token;
            return View(projectDetailResponse);
        }
        //获取病人列表getSearchHospitalPatient
        public string getPatientlisttest(string hospitalId)
        {
            UserRequest user = new UserRequest();
            string apiResponse = string.Empty;

            var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();
            try
            {
                apiResponse = user.getSearchHospitalPatient(token, hospitalId, null);
            }
            catch (Exception ex)
            {
                throw;
            }

            return apiResponse;
        }
        //保存资料
        public string saveInforamtion(string username,  string email,  string avatar,  string sex, string ethnic,string height,string weight,string address,string doctorid,string userid,string mobile,string telphone,string num_il)
        {
            UserRequest user = new UserRequest();
            string apiResponse = string.Empty;

            var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();
            try
            {
                apiResponse = user.getProfiles(token, username, email, "", avatar, sex, ethnic,height,weight,address,doctorid, userid, mobile, telphone, num_il);
            }
            catch (Exception ex)
            {
                throw;
            }

            return apiResponse;
        }
        //修改密码
        public string updatePassword(string pass_old, string pass_new1, string pass_new2)
        {
            string apiResponse = string.Empty;
            UserRequest user = new UserRequest();

            var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();
            try
            {
                apiResponse = user.getUCenterUserPasswd(pass_old, pass_new1, pass_new2, token);
            }
            catch (Exception ex)
            {
                throw;
            }

            return apiResponse;
        }
        public void UploadFile()
        {
            string[] upfiles = Request.Files.AllKeys;

            UserRequest user = new UserRequest();
            string apiResponse = string.Empty;

            var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();
            try
            {
                var x = Request.Files[upfiles[0]];
                // BufferedReader reader = new BufferedReader(Request.Files[upfiles[0]].InputStream);
                byte[] bt = new byte[x.ContentLength];
                int test = x.InputStream.ReadByte();

                StringBuilder strB = new StringBuilder();
                //System.Text.Encoding.ASCII.GetString(bt);
                foreach (byte b in bt)
                {
                    strB.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
                    //strB.Append(System.Text.Encoding.ASCII.GetString(b));
                }
                //}

                var lala = user.UploadWeb(upfiles[0], token);

            }
            catch (Exception ex)
            {
                throw;
            }

            // return apiResponse;
        }
        //保存资料
        public string saveInforamtion(string username, string mobile, string email, string idcard, string avatar, string userId, string sex, string ethnic, string height, string weight, string address, string doctorid)
        {
            UserRequest user = new UserRequest();
            string apiResponse = string.Empty;

            var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();
            try
            {
                apiResponse = user.getProfile(token, username, email, "", avatar, sex, ethnic, height, weight, address, doctorid);
            }
            catch (Exception ex)
            {
                AppLog.Instance.Write("saveInforamtion", AppLog.LogMessageType.Error, ex);
            }

            return apiResponse;
        }
        //查看医生患者资料
        public string getuserinformation(string userId)
        {
            UserRequest user = new UserRequest();
            string apiResponse = string.Empty;

            var token = Session["token"] == null ? "XXXXXXX" : Session["token"].ToString();
            try
            {
                apiResponse = user.getProfile(token, userId);
            }
            catch (Exception ex)
            {
                throw;
            }

            return apiResponse;
        }