//Agent Team CreateManage
        public ActionResult CreateTeam()
        {
            var model = new CallCenterTeamEditModel
            {
                AgentsMasterList = _organizationRoleUserRepository.GetIdNamePairofUsersByRole(Roles.CallCenterRep).Sort(OrderByDirection.Ascending, x => x.SecondValue)
            };

            return(View(model));
        }
        public ActionResult EditTeam(long teamId)
        {
            var model = _callCenterTeamService.GetCallCenterTeamEditModel(teamId);

            if (model == null)
            {
                IoC.Resolve <ILogManager>().GetLogger <Global>().Error(string.Format("Error occurred while fetching team details. No Data Present for TeamId:{0} ", teamId));
                model = new CallCenterTeamEditModel();
                model.FeedbackMessage             = new FeedbackMessageModel();
                model.FeedbackMessage.MessageType = UserInterfaceMessageType.Error;
                model.FeedbackMessage.Message     = "Invalid Team Id provided";
                return(View(model));
            }
            return(View(model));
        }
        public ActionResult EditTeam(CallCenterTeamEditModel model)
        {
            try
            {
                model.AgentsMasterList = _organizationRoleUserRepository.GetIdNamePairofUsersByRole(Roles.CallCenterRep).Sort(OrderByDirection.Ascending, x => x.SecondValue);
                if (!model.Assignments.IsNullOrEmpty())
                {
                    model.AgentsMasterList = model.AgentsMasterList.Where(x => !model.Assignments.Select(y => y.FirstValue).Contains(x.FirstValue)).Select(x => x);
                }

                #region Validations
                model.Name                        = model.Name.Trim();
                model.FeedbackMessage             = new FeedbackMessageModel();
                model.FeedbackMessage.MessageType = UserInterfaceMessageType.Error;

                if (string.IsNullOrEmpty(model.Name))
                {
                    model.FeedbackMessage.MessageType = UserInterfaceMessageType.Warning;
                    model.FeedbackMessage.Message     = "Team name cannot be empty.";
                    return(View(model));
                }
                if (model.Name.Trim().Length < 5)
                {
                    model.FeedbackMessage.MessageType = UserInterfaceMessageType.Warning;
                    model.FeedbackMessage.Message     = "Team name must be at least 5 characters long.";
                    return(View(model));
                }

                var domain = _callCenterTeamRepository.GetById(model.Id);
                if (domain == null)
                {
                    model.FeedbackMessage.Message = "No such team present in our records";
                    return(View(model));
                }


                if (!string.Equals(domain.Name.ToLower(), model.Name.ToLower()))
                {
                    if (!_callCenterTeamRepository.IsTeamNameUnique(model.Name))
                    {
                        model.FeedbackMessage.Message = "Team name already in use";
                        return(View(model));
                    }
                }

                if (model.Assignments == null || (model.Assignments != null && !model.Assignments.Any()))
                {
                    model.FeedbackMessage.Message = "Add some agents to be in team";
                    return(View(model));
                }

                if (model.TypeId <= 0)
                {
                    model.FeedbackMessage.Message = "Invalid team type";
                    return(View(model));
                }
                #endregion

                #region Save Team Data
                domain.Name        = model.Name;
                domain.Description = model.Description;
                domain.TypeId      = model.TypeId;
                domain.DataRecorderMetaData.DataRecorderModifier = new OrganizationRoleUser(_sessionContext.UserSession.CurrentOrganizationRole.OrganizationRoleUserId);
                domain.DataRecorderMetaData.DateModified         = DateTime.Now;
                _callCenterTeamRepository.Save(domain);
                #endregion

                #region Updating CallCenterAgentTeamLog
                var previousAgentsAssigned = _organizationRoleUserRepository.GetNameIdPairofUsers(_callCenterTeamRepository.GetTeamAgents(model.Id).ToArray());
                if (previousAgentsAssigned != null)
                {
                    var removedAgents = previousAgentsAssigned.Select(x => x.FirstValue).Except(model.Assignments.Select(x => x.FirstValue));
                    var newAgents     = model.Assignments.Select(x => x.FirstValue).Except(previousAgentsAssigned.Select(x => x.FirstValue));

                    if (removedAgents.Any())
                    {
                        var removedAgentslog = _callCenterAgentTeamLogRepository.GetAgentTeamLogForTeam(model.Id, removedAgents.ToArray());
                        foreach (var agent in removedAgentslog)
                        {
                            agent.DateRemoved            = DateTime.Now;
                            agent.RemovedByOrgRoleUserId = _sessionContext.UserSession.CurrentOrganizationRole.OrganizationRoleUserId;
                            agent.IsActive = false;
                        }
                        _callCenterAgentTeamLogRepository.SaveAll(removedAgentslog);
                    }

                    if (newAgents.Any())
                    {
                        var callCenterAgentTeamLogList = newAgents.Select(newAgent => new CallCenterAgentTeamLog
                        {
                            TeamId  = domain.Id,
                            AgentId = newAgent,
                            AssignedByOrgRoleUserId = _sessionContext.UserSession.CurrentOrganizationRole.OrganizationRoleUserId,
                            DateAssigned            = DateTime.Now,
                            DateRemoved             = null,
                            IsActive = true,
                            RemovedByOrgRoleUserId = null
                        }).ToList();
                        _callCenterAgentTeamLogRepository.SaveAll(callCenterAgentTeamLogList);
                    }
                }
                #endregion

                //Save current team and agent mapping
                _callCenterTeamRepository.SaveAgentTeamAssignment(domain.Id, model.Assignments.Select(x => x.FirstValue).ToList());

                model.FeedbackMessage.MessageType = UserInterfaceMessageType.Success;
                model.FeedbackMessage.Message     = "Team updated successfully";
                return(View(model));
            }
            catch (Exception ex)
            {
                IoC.Resolve <ILogManager>().GetLogger <Global>().Error(ex.Message);
                IoC.Resolve <ILogManager>().GetLogger <Global>().Error("Call Center Team EditTeam[Post] Stack Trace :" + ex.StackTrace);
                model.FeedbackMessage             = new FeedbackMessageModel();
                model.FeedbackMessage.MessageType = UserInterfaceMessageType.Error;
                model.FeedbackMessage.Message     = ex.Message;
                return(View(model));
            }
        }
        public ActionResult CreateTeam(CallCenterTeamEditModel model)
        {
            try
            {
                model.AgentsMasterList = _organizationRoleUserRepository.GetIdNamePairofUsersByRole(Roles.CallCenterRep).Sort(OrderByDirection.Ascending, x => x.SecondValue);
                if (!model.Assignments.IsNullOrEmpty())
                {
                    model.AgentsMasterList = model.AgentsMasterList.Where(x => !model.Assignments.Select(y => y.FirstValue).Contains(x.FirstValue)).Select(x => x);
                }


                #region Validations
                model.FeedbackMessage             = new FeedbackMessageModel();
                model.FeedbackMessage.MessageType = UserInterfaceMessageType.Error;

                model.Name = model.Name.Trim();
                if (string.IsNullOrEmpty(model.Name))
                {
                    model.FeedbackMessage.MessageType = UserInterfaceMessageType.Warning;
                    model.FeedbackMessage.Message     = "Team name cannot be empty.";
                    return(View(model));
                }
                if (model.Name.Trim().Length < 5)
                {
                    model.FeedbackMessage.MessageType = UserInterfaceMessageType.Warning;
                    model.FeedbackMessage.Message     = "Team name must be at least 5 characters long.";
                    return(View(model));
                }

                if (!_callCenterTeamRepository.IsTeamNameUnique(model.Name))
                {
                    model.FeedbackMessage.Message = "Team name already in use.";
                    return(View(model));
                }

                if (model.Assignments == null || (model.Assignments != null && !model.Assignments.Any()))
                {
                    model.FeedbackMessage.Message = "Select some agents to be in team.";
                    return(View(model));
                }

                if (model.TypeId <= 0)
                {
                    model.FeedbackMessage.Message = "Select valid team type.";
                    return(View(model));
                }
                #endregion

                var domain = _callCenterTeamRepository.Save(new CallCenterTeam
                {
                    Name                 = model.Name,
                    Description          = model.Description,
                    TypeId               = model.TypeId,
                    DataRecorderMetaData = new DataRecorderMetaData
                    {
                        DataRecorderCreator  = new OrganizationRoleUser(_sessionContext.UserSession.CurrentOrganizationRole.OrganizationRoleUserId),
                        DataRecorderModifier = null,
                        DateCreated          = DateTime.Now,
                        DateModified         = null
                    },
                    IsActive = true
                });

                _callCenterTeamRepository.SaveAgentTeamAssignment(domain.Id, model.Assignments.Select(x => x.FirstValue).ToList());

                var callCenterAgentTeamLogList = model.Assignments.Select(assignment => new CallCenterAgentTeamLog
                {
                    TeamId  = domain.Id,
                    AgentId = assignment.FirstValue,
                    AssignedByOrgRoleUserId = domain.DataRecorderMetaData.DataRecorderCreator.Id,
                    DateAssigned            = DateTime.Now,
                    DateRemoved             = null,
                    IsActive = true,
                    RemovedByOrgRoleUserId = null
                }).ToList();
                _callCenterAgentTeamLogRepository.SaveAll(callCenterAgentTeamLogList);

                model.FeedbackMessage.MessageType = UserInterfaceMessageType.Success;
                model.FeedbackMessage.Message     = "Team Created Successfully";

                return(View(model));
            }
            catch (Exception ex)
            {
                IoC.Resolve <ILogManager>().GetLogger <Global>().Error(ex.Message);
                IoC.Resolve <ILogManager>().GetLogger <Global>().Error("Call Center Team CreateTeam[Post] Stack Trace :" + ex.StackTrace);
                model.FeedbackMessage             = new FeedbackMessageModel();
                model.FeedbackMessage.MessageType = UserInterfaceMessageType.Error;
                model.FeedbackMessage.Message     = ex.Message;

                return(View(model));
            }
        }