/// <summary>
        /// Binds the manger dashboard.
        /// </summary>
        /// <param name="masterId">The master identifier.</param>
        /// <returns></returns>
        private async Task<ManRequestViewModel> BindMangerDashboard(int masterId)
        {
            FARMasterDto master = await MasterRepository.SingleAsync(masterId);

            if (master == null)
                return null;

            var request = await BindRequestDashboard();
            var reason = await ReasonPriorityRepository.GetAllAsync();

            var farRequest = new ManRequestViewModel();
            farRequest.Id = masterId;
            farRequest.Analyst = master.Analyst;
            farRequest.BUId = master.BUId;
            farRequest.FailureDesc = master.FailureDesc;
            farRequest.FailureOriginId = master.FailureOriginId;
            farRequest.FailureRate = master.FailureRate;
            farRequest.FailureTypeId = master.FailureTypeId;
            farRequest.FARNumber = master.Number;
            farRequest.FinalReportTargetDate = master.FinalReportTargetDate;
            farRequest.InitialReportTargetDate = master.InitialReportTargetDate;
            farRequest.OriginId = master.OriginId;
            farRequest.PriorityId = master.PriorityId;
            farRequest.Product = master.Product;
            farRequest.RefNo = master.RefNo;
            farRequest.RequestDate = master.RequestDate;
            farRequest.Requestor = master.Requestor;
            farRequest.SamplesArriveDate = master.SamplesArriveDate;
            farRequest.StatusId = master.StatusId;
            farRequest.Submitted = master.Submitted;
            farRequest.Origins = request.Origins;
            farRequest.Status = request.Status;
            farRequest.BUs = request.BUs;
            farRequest.FailureTypes = request.FailureTypes;
            farRequest.FailureOrigins = request.FailureOrigins;
            farRequest.Priorities = request.Priorities;
            farRequest.Site = request.Site;
            farRequest.Reasons = reason;
            farRequest.InitialReason = await ReasonChangingINITargetRepository.GetAllAsync(); //reason;
            farRequest.FinalReason = await ReasonChangingFINTargetRepository.GetAllAsync();// reason;

            var userRequestor = UserRepository.CheckExistEmail(request.Requestor);
            if (userRequestor != null)
                farRequest.Site = userRequestor.Sites.Name;
            var userAnalyst = UserRepository.CheckExistEmail(master.Analyst);
            if (userAnalyst != null)
                farRequest.UserId = userAnalyst.Id;

            //Get user with the roles is Analyst or manager and not locked;
            farRequest.Users = (await UserRepository.GetAllAsync()).Where(w => w.RoleId == (int)RoleType.ANALYST || w.RoleId == (int)RoleType.MANAGER).Where(w => w.IsLocked == false);
            farRequest.Status = await StatusRepository.GetAllAsync();

            //Get all files in folder master with id
            string folderMaster = System.IO.Path.Combine(Server.MapPath("~/Upload"), masterId.ToString().PadLeft(10, '0'));
            if (System.IO.Directory.Exists(folderMaster))
            {
                string[] files = System.IO.Directory.GetFiles(folderMaster);
                string json = string.Empty;
                foreach (string file in files)
                {
                    json += "<a href=" + Url.Action("DownloadTemp", "Upload", new { id = masterId, file = System.IO.Path.GetFileName(file) }) + ">" + System.IO.Path.GetFileName(file) + "</a>" + "<br/>";
                }
                farRequest.Reports = json;
            }
            if (farRequest.Reports == null)
                farRequest.Reports = String.Empty;

            return farRequest;
        }
        public async Task<JsonResult> ManEditFA(int id, ManRequestViewModel request)
        {
            var master = await MasterRepository.SingleAsync(id);
            string oldAnalyst = master.Analyst;

            if (master != null)
            {
                string currentName = this.CurrentName;
                if (master.PriorityId != request.PriorityId)
                {
                    FARPriorityLogDto logPriority = new FARPriorityLogDto
                    {
                        MasterId = id,
                        PriorityIdOld = master.PriorityId,
                        PriorityIdNew = request.PriorityId,
                        ReasonId = request.ReasonId,
                        LastUpdatedBy = currentName,
                        IsDeleted = false,
                    };
                    //Log Priority
                    PriorityLogRepository.Add(logPriority);
                }
                var user = UserRepository.Single(request.UserId);
                if (user != null)
                {
                    master.Analyst = user.Email;
                }
                master.PriorityId = request.PriorityId;
                master.LastUpdatedBy = currentName;
                master.StatusId = (int)request.StatusId;
                master.SamplesArriveDate = request.SamplesArriveDate;

                if (request.InitialReportTargetDate != master.InitialReportTargetDate)
                {
                    //Write log
                    FARInitialTargetLogDto logIni = new FARInitialTargetLogDto
                    {
                        MasterId = id,
                        ReasonId = request.InitialReasonId,
                        TargetDate = request.InitialReportTargetDate,
                        LastUpdatedBy = this.CurrentName,
                    };
                    InitialTargetLogRepository.Add(logIni);
                }

                if (request.FinalReportTargetDate != master.FinalReportTargetDate)
                {
                    //Write log
                    FARFinalTargetLogDto logFinal = new FARFinalTargetLogDto
                    {
                        MasterId = id,
                        ReasonId = request.FinalReasonId,
                        TargetDate = request.FinalReportTargetDate,
                        LastUpdatedBy = this.CurrentName,
                    };
                    FinalTargetLogRepository.Add(logFinal);
                }
                if (request.SamplesArriveDate.HasValue)
                {
                    var reportType = ReportTypesRepository.GetAll();
                    if (request.InitialReportTargetDate == null)
                        master.InitialReportTargetDate = request.SamplesArriveDate.Value.AddDays(reportType.Single(x => x.Id == (int)ReportType.INITIAL).DaysAfter);
                    else
                        master.InitialReportTargetDate = request.InitialReportTargetDate;
                    if (request.FinalReportTargetDate == null)
                        master.FinalReportTargetDate = request.SamplesArriveDate.Value.AddDays(reportType.Single(x => x.Id == (int)ReportType.FINAL).DaysAfter);
                    else
                        master.FinalReportTargetDate = request.FinalReportTargetDate;
                }

                var result = await MasterRepository.UpdateAsync(master);
                if (result == Model.SaveResult.SUCCESS)
                {
                    Response.StatusCode = (int)HttpStatusCode.OK;
                    if (oldAnalyst != master.Analyst)
                    {
                        //Add Analyst Reassign Log
                        FARAnalystReassignLogDto analystReassign = new FARAnalystReassignLogDto
                        {
                            MasterId = id,
                            AnalystFrom = oldAnalyst,
                            AnalystTo = master.Analyst,
                            LastUpdatedBy = currentName,
                        };

                        //Log Analyst reassign
                        AnalystReassignLogRepository.Add(analystReassign);
                        //SendMail
                        string emailSubject = "FA Request " + request.FARNumber + " has been assigned to you";
                        string ebody = "You have been assigned by " + currentName + " to oversee FA Number: " + request.FARNumber + ". Please login to FA DB to update its details.";
                        string emailBody = string.Format("{0}<br><br>{1}", ebody, DateTime.Now.ToShortDateString());
                        try
                        {
                            Mail.Send(master.Analyst, emailSubject, ebody);
                        }
                        catch (Exception ex)
                        {
                            LogService.Error(ex.Message, ex);
                        }
                    }

                    return new JsonResult
                    {
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                        Data = new
                        {
                            code = "ED01",
                            initialDate = master.InitialReportTargetDate.HasValue ? master.InitialReportTargetDate.Value.ToString("MM/dd/yyyy") : "",
                            finalDate = master.FinalReportTargetDate.HasValue ? master.FinalReportTargetDate.Value.ToString("MM/dd/yyyy") : ""
                        }
                    };
                }
            }
            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return new JsonResult
            {
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                Data = new { code = "ED02" }
            };
        }