public ActionResult AssignedTask(int id = 0, int mode = 0)
        {
            string currentUserId = User.Identity.GetUserId();
            Issue assignedtask = DBHandler.GetIssueById(id);
            if (assignedtask == null)
            {
                return HttpNotFound();
            }
            AssignedTasks taskAssigned = new AssignedTasks();
            taskAssigned.TaskTag = assignedtask.Tag.TagValue;
            taskAssigned.statusType = assignedtask.Status.StatusType;
            taskAssigned.StatusId = assignedtask.Status.StatusId;
            taskAssigned.IssueId = assignedtask.IssueId;
            taskAssigned.Client = assignedtask.Client;
            taskAssigned.ClientId = assignedtask.ClientId;
            taskAssigned.PrevDescription = DBHandler.removeStringFromDescription(assignedtask.Description);
            taskAssigned.ProductId = assignedtask.ProductId;
            taskAssigned.Priority = assignedtask.Priority;
            taskAssigned.PriorityId = assignedtask.PriorityId;
            taskAssigned.ApplicationUser = assignedtask.ApplicationUser;
            taskAssigned.userid = assignedtask.ApplicationUserId;
            taskAssigned.IssueTagId = assignedtask.TagId;
            taskAssigned.IssueCode = assignedtask.IssueCode;
            taskAssigned.AssignedTo = assignedtask.ApplicationUser == null ? "Non" : assignedtask.ApplicationUser.FullName;
            taskAssigned.timeCounter = "";
            taskAssigned.Description = "";

            taskAssigned.UploadedFileName = assignedtask.UploadedFileName;
            string[] filesUploaded = string.IsNullOrEmpty(assignedtask.UploadedFileName) ? null : assignedtask.UploadedFileName.Contains('$') ? assignedtask.UploadedFileName.Split('$') : null;
            List<FilesUploaded> objFilesUploaded = new List<FilesUploaded>();
            if (filesUploaded != null)
            {
                foreach (var item in filesUploaded)
                {
                    string fileUploaded = string.Empty;
                    string fileUserId = string.Empty;
                    string fileName = DataHelper.getFileName(item, out fileUserId, out fileUploaded);
                    objFilesUploaded.Add(new FilesUploaded { name = fileName });
                }
            }
            else if (!string.IsNullOrEmpty(assignedtask.UploadedFileName))
            {
                string fileUploaded = string.Empty;
                string fileUserId = string.Empty;
                string fileName = DataHelper.getFileName(assignedtask.UploadedFileName, out fileUserId, out fileUploaded);
                objFilesUploaded.Add(new FilesUploaded { name = fileName,dbFileName=assignedtask.UploadedFileName });
            }
            if (objFilesUploaded.Count() > 0)
            {
                ViewData["UploadedFiles"] = objFilesUploaded;
            }
            taskAssigned.CreatedBy = DataHelper.ToPascalConvention(assignedtask.CreatedBy);
            taskAssigned.UpdatedBy = DataHelper.ToPascalConvention(assignedtask.UpdatedBy);
            taskAssigned.CreatedOn = assignedtask.CreatedOn;
            taskAssigned.UpdatedOn = assignedtask.UpdatedOn;
            List<ApplicationUser> usersNotInAdminAndMangerRole = new List<ApplicationUser>();
            var roles = db.Roles.Where(m => m.Name.ToLower() != AppRoles.ADMINISTRATOR.ToLower() && m.Name.ToLower() != AppRoles.MANAGER.ToLower() && m.Name.ToLower().ToLower() != AppRoles.SYSTEMUSER.ToLower() && m.Name != AppRoles.SUPERUSER.ToLower()).ToList();
            foreach (var role in roles)
            {
                var users = allUsers.Where(m => m.Roles.Any(r => r.RoleId == role.Id) && m.Id != currentUserId).ToList();
                foreach (var u in users)
                {
                    usersNotInAdminAndMangerRole.Add(new ApplicationUser { Id = u.Id, FullName = u.FullName });
                }
            }
            var allusers = (from u in usersNotInAdminAndMangerRole select new { Id = u.Id, FullName = u.FullName });
            if (id > 0 && (taskAssigned.statusType.ToLower() == AppStatus.ASSIGNED || taskAssigned.statusType.ToLower() == AppStatus.REASSIGNEDBYTUSER || taskAssigned.statusType.ToLower() == AppStatus.REASSIGNEDBYMANAGER || taskAssigned.statusType.ToLower() == AppStatus.REASSIGNEDBYSUPERUSER))
            {
                ViewBag.userid = new SelectList(allusers.Where(u => u.Id != assignedtask.ApplicationUserId).ToList(), "Id", "FullName", assignedtask.ApplicationUserId);
            }
            else if (taskAssigned.statusType.ToLower() == AppStatus.CLOSED)
            {
                ViewBag.userid = new SelectList(allusers, "Id", "FullName", assignedtask.ApplicationUserId);
            }
            else
            {
                ViewBag.userid = new SelectList(allusers, "Id", "FullName");
            }
            if (User.IsInRole(AppRoles.MANAGER))
            {
                taskAssigned.selectedProduct = DBHandler.getUserProductsByUserId(currentUserId);
            }
            else { taskAssigned.selectedProduct = DBHandler.getUserProductsByUserId(taskAssigned.AssignedBy); }

            //List<Product> listProducts = new List<Product>();
            //if (!string.IsNullOrEmpty(taskAssigned.selectedProduct))
            //{
            //    string[] selectedProducts = taskAssigned.selectedProduct.Split(',');
            //    listProducts = (from item in db.Products where selectedProducts.Contains(item.ProductId.ToString()) select item).ToList();
            //}
            //    ViewBag.ProductId = new SelectList(listProducts, "ProductId", "ProductName", taskAssigned.ProductId);
            ViewBag.ProductId = new SelectList(db.Products, "ProductId", "ProductName", taskAssigned.ProductId);
            return View(taskAssigned);
        }
        public ActionResult AssignedTask(AssignedTasks taskAssigned)
        {
            //  assignedtask.ApplicationUser == null ? "Non" : assignedtask.ApplicationUser.FullName;
            if (ModelState.IsValid)
            {
                AssignedTask obj_assignedtask = new AssignedTask();
                Issue objTask = db.Issues.Find(taskAssigned.IssueId);
                objTask.ProductId = taskAssigned.ProductId;
                objTask.UploadedFileName = taskAssigned.UploadedFileName;
                List<Status> list_Status = db.Status.Select(s => s).ToList();
                string statusType = db.Status.Find(objTask.StatusId).StatusType.ToString().ToLower();
                var user = db.Users.Find(User.Identity.GetUserId());
                string hrLine = "_hrLine_";
                string divStartGreen = "_dsg_";
                string nextLIne = "_newline_";
                string boldStart = "_bs_";
                string boldEnd = "_be_";
                string divStart = "_ds_";
                string divEnd = "_de_";
                string dvStart = "_dstart_";
                string newkDescription = System.Net.WebUtility.HtmlDecode(String.IsNullOrEmpty(taskAssigned.Description) ? "" : taskAssigned.Description);
                string preDescription = objTask.Description;
                int statusId = 0;
                string mailSubject = "";
                var assignedUserName = db.Users.Find(taskAssigned.userid);
                string u_Name = assignedUserName.FullName;
                if (statusType == AppStatus.UNASSIGNED)
                {
                    objTask.Description = divStart + dvStart + hrLine + dvStart + "Task " + objTask.IssueCode + " is assigned by " + DataHelper.ToPascalConvention(user.FullName) + "." + divEnd + hrLine + divEnd + boldStart + "Commented By:" + boldEnd + " " + DataHelper.ToPascalConvention(db.Users.Where(u => u.Email == User.Identity.Name).First().FullName) + nextLIne + boldStart + "Date:" + boldEnd + " " + objTask.UpdatedOn.ToString() + nextLIne + boldStart + "Comments:" + boldEnd + nextLIne + newkDescription + divEnd + preDescription;
                    statusId = list_Status.First(s => s.StatusType.ToLower() == AppStatus.ASSIGNED).StatusId;
                    mailSubject = objTask.IssueCode + " has been assigned to " + u_Name + ".";
                }
                else
                {
                    mailSubject = objTask.IssueCode + " has been re-assigned to " + u_Name + ".";
                    if (User.IsInRole(AppRoles.TUSER))
                    {
                        statusId = list_Status.First(s => s.StatusType.ToLower() == AppStatus.REASSIGNEDBYTUSER).StatusId;
                    }
                    else if (User.IsInRole(AppRoles.MANAGER))
                    {
                        statusId = list_Status.First(s => s.StatusType.ToLower() == AppStatus.REASSIGNEDBYMANAGER).StatusId;
                    }
                    else if (User.IsInRole(AppRoles.SUPERUSER))
                    {
                        statusId = list_Status.First(s => s.StatusType.ToLower() == AppStatus.REASSIGNEDBYSUPERUSER).StatusId;
                    }
                    objTask.Description = divStart + divStartGreen + hrLine + dvStart + "Task " + objTask.IssueCode + " is re-assigned by " + DataHelper.ToPascalConvention(user.FullName) + "." + divEnd + hrLine + divEnd + boldStart + "Commented By:" + boldEnd + " " + DataHelper.ToPascalConvention(db.Users.Where(u => u.Email == User.Identity.Name).First().FullName) + nextLIne + boldStart + "Date:" + boldEnd + " " + objTask.UpdatedOn.ToString() + nextLIne + boldStart + "Comments:" + boldEnd + nextLIne + newkDescription + divEnd + preDescription;
                }
                var stopwatch = System.Diagnostics.Stopwatch.StartNew();
                taskAssigned.timeCounter = stopwatch.ToString();
                objTask.StatusId = statusId;
                objTask.ApplicationUserId = taskAssigned.userid;
                //  objTask.UploadedFileName = taskAssigned.UploadedFileName;
                objTask.CreatedBy = objTask.CreatedBy;
                objTask.UpdatedBy = User.Identity.GetUserId();
                objTask.CreatedOn = objTask.CreatedOn;
                objTask.UpdatedOn = DateTime.Now;
                db.Issues.Add(objTask);
                //  db.Entry(objTask).State = EntityState.Modified;
                db.SaveChanges();
                var all_issues = db.Issues.ToList();
                var allFilteredIssues = (from p in all_issues
                                         group p by objTask.IssueCode into g
                                         select g.OrderBy(p => p.UpdatedOn).Last()).ToList();
                int? statusID = allFilteredIssues[0].StatusId;
                int issueID = allFilteredIssues[0].IssueId;
                //obj_assignedtask.statusType = db.Status.Find(statusId).Status;
                obj_assignedtask.IssueId = issueID;
                obj_assignedtask.ApplicationUserId = taskAssigned.userid;
                obj_assignedtask.CreatedBy = objTask.CreatedBy;
                obj_assignedtask.UpdatedBy = User.Identity.GetUserId();
                obj_assignedtask.CreatedOn = objTask.CreatedOn;
                obj_assignedtask.UpdatedOn = DateTime.Now;
                db.AssignedTasks.Add(obj_assignedtask);
                db.SaveChanges();

                ////Email Sending------
                var pagelink = Url.Action("Details", "Issue", new { id = issueID }, protocol: Request.Url.Scheme);
                string anchorHtml = "<a href=\"" + pagelink + "\">Click here to change status</a>";
                var uri = new Uri(pagelink);
                string logoPath = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port + "/Images/pronetemaillogo.png";
                string imageHtml = "<img height='55' width='112' src='" + logoPath + "'/>";
                string body;
                string filepath = Server.MapPath(ConfigurationManager.AppSettings["TaskCreationEmailTemplate"].ToString());
                using (var sr = new StreamReader(filepath))
                {
                    body = sr.ReadToEnd();
                }
                string description = System.Net.WebUtility.HtmlDecode(newkDescription);
                // body = System.IO.File.ReadAllText(filepath);
                var assignedUser = db.Users.Find(taskAssigned.userid);
                string userName = assignedUser.FirstName + " " + assignedUser.LastName;
                string messageBody = string.Format(body.ToString(), mailSubject, objTask.IssueCode, objTask.Tag.TagValue, objTask.Client.Name, objTask.Priority.PriorityType, objTask.Product.ProductName, objTask.Status.StatusType, DataHelper.ToPascalConvention((user.FirstName + " " + user.LastName)), objTask.UpdatedOn, description, anchorHtml, DateTime.Now.Year, imageHtml, "", userName, "Assigned To", "'border: solid 1px black;'");//
                string sender = ConfigurationManager.AppSettings["SenderEmailAddress"].ToString();
                try
                {
                    MailManager.sendEmailToManagerRoles(sender, messageBody, mailSubject, "assignTask", obj_assignedtask.ApplicationUserId, objTask.UploadedFileName);
                    return RedirectToAction("Index", new { id = issueID, statusId = statusID });
                }
                catch (Exception)
                {
                    return RedirectToAction("Index", new { id = issueID, statusId = statusID });
                }
                ////Email Sending End-----
            }
            Issue issue = db.Issues.Find(taskAssigned.IssueId);
            taskAssigned.TaskTag = issue.Tag.TagValue;
            taskAssigned.statusType = issue.Status.StatusType;
            taskAssigned.IssueId = issue.IssueId;
            taskAssigned.userid = issue.ApplicationUserId;
            taskAssigned.Client = issue.Client;
            taskAssigned.ClientId = issue.ClientId;
            taskAssigned.AssignedTo = issue.ApplicationUser == null ? "Non" : issue.ApplicationUser.FullName;
            taskAssigned.PrevDescription = DBHandler.removeStringFromDescription(issue.Description);
            taskAssigned.ProductId = issue.ProductId;
            taskAssigned.Priority = issue.Priority;
            taskAssigned.PriorityId = issue.PriorityId;
            taskAssigned.ApplicationUser = issue.ApplicationUser;
            taskAssigned.userid = issue.ApplicationUserId;
            taskAssigned.IssueTagId = issue.TagId;
            taskAssigned.IssueCode = issue.IssueCode;
            taskAssigned.Description = "";
            taskAssigned.CreatedBy = issue.CreatedBy;
            taskAssigned.UpdatedBy = issue.UpdatedBy;
            taskAssigned.CreatedOn = issue.CreatedOn;
            taskAssigned.UpdatedOn = issue.UpdatedOn;
            ViewBag.ProductId = new SelectList(db.Products, "ProductId", "ProductName", taskAssigned.ProductId);
            List<ApplicationUser> usersNotInAdminAndMangerRole = new List<ApplicationUser>();
            var roles = db.Roles.Where(m => m.Name.ToLower() != AppRoles.ADMINISTRATOR.ToLower() && m.Name.ToLower() != AppRoles.MANAGER.ToLower() && m.Name.ToLower() != AppRoles.SUPERUSER.ToLower());
            foreach (var role in roles)
            {
                var users = allUsers.Where(m => m.Roles.Any(r => r.RoleId == role.Id)).ToList();
                foreach (var u in users)
                {
                    usersNotInAdminAndMangerRole.Add(new ApplicationUser { Id = u.Id, FullName = u.FullName });
                }
            }
            if (taskAssigned.userid != null)
            {
                ViewBag.userid = new SelectList(usersNotInAdminAndMangerRole, "Id", "FullName", taskAssigned.userid);
            }
            else
            {
                ViewBag.userid = new SelectList(usersNotInAdminAndMangerRole, "Id", "FullName");
            }
            return View(taskAssigned);
        }