public async Task <ActionResult> Edit(CreateCarouselModel model, string Submittype)
        {
            if (model.CoverPictures.Count() == 0 && model.CoverPictureFiles.Count() == 0)
            {
                ModelState.AddModelError("CoverPictures", "Please upload an image");
            }
            else
            {
                //validate file
                foreach (var file in model.CoverPictureFiles)
                {
                    if (!FileMethod.IsValidType(file, filter_imgs))
                    {
                        ModelState.AddModelError("CoverPictures", Language.Carousel.ValidAttachment);
                        break;
                    }
                }
            }

            if (ModelState.IsValid)
            {
                var apimodel = new CreateCarouselModel
                {
                    Id             = model.Id,
                    Title          = model.Title,
                    Description    = model.Description,
                    Display        = model.Display,
                    DisplayDate    = model.DisplayDate,
                    TextLocation   = model.TextLocation,
                    FreeTextArea   = model.FreeTextArea,
                    LastModifiedBy = CurrentUser.UserId,
                    CoverPictures  = model.CoverPictures
                };

                //attachment 1: cover pics
                if (model.CoverPictureFiles.Count() > 0)
                {
                    var files = await FileMethod.UploadFile(model.CoverPictureFiles.ToList(), CurrentUser.UserId, "carousel");

                    if (files != null)
                    {
                        apimodel.CoverFilesId = files.Select(f => f.Id).ToList();
                    }
                }

                var response = await WepApiMethod.SendApiAsync <string>(HttpVerbs.Post, $"Carousels/Carousel/Edit", apimodel);

                if (response.isSuccess)
                {
                    await LogActivity(Modules.CarouselManagement, "Edit Carousel: " + response.Data, model);

                    if (Submittype == "Save")
                    {
                        TempData["SuccessMessage"] = "Carousel titled " + response.Data + " updated successfully and saved as draft.";

                        return(RedirectToAction("Index", "Carousel", new { area = "CarouselManagement" }));
                    }
                    else
                    {
                        TempData["SuccessMessage"] = "Carousel titled " + response.Data + " updated successfully.";
                        return(RedirectToAction("Details", "Carousel", new { area = "CarouselManagement", @id = model.Id }));
                    }
                }
                else
                {
                    TempData["ErrorMessage"] = "Failed to edit Publication.";

                    return(RedirectToAction("Edit", "Carousel", new { area = "CarouselManagement", @id = model.Id }));
                }
            }

            return(View(model));
        }
        public async Task <ActionResult> Create(CreateCarouselModel model, string Submittype)
        {
            if (model.CoverPictures.Count() == 0 && model.CoverPictureFiles.Count() == 0)
            {
                ModelState.AddModelError("CoverPictures", "Please upload an image");
            }
            else
            {
                //validate file
                foreach (var file in model.CoverPictureFiles)
                {
                    if (!FileMethod.IsValidType(file, filter_imgs))
                    {
                        ModelState.AddModelError("CoverPictures", Language.Carousel.ValidAttachment);
                        break;
                    }
                }
            }

            if (ModelState.IsValid)
            {
                var apimodel = new CreateCarouselModelNoFile
                {
                    Title         = model.Title,
                    Description   = model.Description,
                    Display       = model.Display,
                    DisplayDate   = model.DisplayDate,
                    TextLocation  = model.TextLocation,
                    FreeTextArea  = model.FreeTextArea,
                    CoverPictures = model.CoverPictures,
                    CreatedBy     = model.CreatedBy,
                    CreatedDate   = model.CreatedDate
                };

                if (model.CoverPictureFiles.Count() > 0)
                {
                    var files = await FileMethod.UploadFile(model.CoverPictureFiles.ToList(), CurrentUser.UserId, "carousel");

                    if (files != null)
                    {
                        apimodel.CoverFilesId = files.Select(f => f.Id).ToList();
                    }
                }

                var response = await WepApiMethod.SendApiAsync <string>(HttpVerbs.Post, $"Carousels/Carousel/Create", apimodel);

                if (response.isSuccess)
                {
                    string[] resparray = response.Data.Split('|');
                    string   newid     = resparray[0];
                    string   title     = resparray[1];

                    if ((model.CoverPictureFiles.Count() > 0))
                    {
                        await UploadImageFiles(int.Parse(newid), model.CoverPictureFiles.First());
                    }

                    await LogActivity(Modules.CarouselManagement, "Create New Carousel: " + title);

                    if (Submittype == "Save")
                    {
                        TempData["SuccessMessage"] = "New Carousel titled " + title + " created successfully and saved as draft.";

                        return(RedirectToAction("Index", "Carousel", new { area = "CarouselManagement" }));
                    }
                    else
                    {
                        return(RedirectToAction("Details", "Carousel", new { area = "CarouselManagement", @id = newid }));
                    }
                }
                else
                {
                    TempData["SuccessMessage"] = "Failed to create new Carousel.";

                    return(RedirectToAction("Index", "Carousel", new { area = "CarouselManagement" }));
                }
            }

            return(View(model));
        }
        public async Task <ActionResult> Edit(Models.EditKMCModel model)
        {
            if (model.IsPublic)
            {
                ModelState.Remove("RoleIds");
            }

            if (model.IsEditor)
            {
                ModelState.Remove("File");
                ModelState.Remove("Type");
            }
            else
            {
                ModelState.Remove("EditorCode");

                //validation of file type
                if (model.File != null)
                {
                    var isValid = true;

                    switch (model.Type)
                    {
                    case KMCType.Image:
                        isValid = FileMethod.IsValidType(model.File, filter_imgs);
                        break;

                    case KMCType.Video:
                        isValid = FileMethod.IsValidType(model.File, filter_videos);
                        break;

                    case KMCType.Audio:
                        isValid = FileMethod.IsValidType(model.File, filter_audios);
                        break;

                    case KMCType.Document:
                        isValid = FileMethod.IsValidType(model.File, filter_docs);
                        break;

                    case KMCType.Others:

                        break;

                    default:
                        break;
                    }

                    if (!isValid)
                    {
                        ModelState.AddModelError("File", Language.KMC.ValidIsValidTypeFile);
                    }
                }
                else
                {
                    if (model.FileId != null)
                    {
                        ModelState.Remove("File");
                    }
                }
            }

            if (ModelState.IsValid)
            {
                var modelapi = new EditKMCModel
                {
                    KMCCategoryId = model.CategoryId,
                    Title         = model.Title,
                    Description   = model.Description,
                    Type          = model.Type,
                    IsPublic      = model.IsPublic,
                    IsShow        = model.IsShow,
                    IsEditor      = model.IsEditor,
                    RoleIds       = model.RoleIds,
                    EditorCode    = model.EditorCode,
                    FileId        = model.IsEditor ? null : model.FileId,
                    ThumbnailUrl  = model.ThumbnailUrl
                };

                if (model.ThumbnailFile != null)
                {
                    var filename = FileMethod.SaveFile(model.ThumbnailFile, Server.MapPath("~/img/kmc-thumbnail"), model.ThumbnailUrl);
                    modelapi.ThumbnailUrl = filename;
                }

                if (model.File != null)
                {
                    var responseFile = await FileMethod.UploadFile(new List <HttpPostedFileBase> {
                        model.File
                    }, CurrentUser.UserId, "KMC/", model.File.ContentType);

                    if (responseFile != null)
                    {
                        modelapi.FileId   = responseFile.Select(f => f.Id).FirstOrDefault();
                        modelapi.FileType = model.File.ContentType;
                    }
                }

                var response = await WepApiMethod.SendApiAsync <bool>(HttpVerbs.Put, $"KMC/Manage?id={model.Id}", modelapi);

                if (response.isSuccess)
                {
                    if (!model.IsPublic && model.IsShow)//send notification
                    {
                        var userIds = new List <int>();

                        foreach (var roleId in model.RoleIds)
                        {
                            var responseUsers = await WepApiMethod.SendApiAsync <List <UserModel> >(HttpVerbs.Get, $"Administration/Role/GetAllUser?roleId={roleId}");

                            if (responseUsers.isSuccess)
                            {
                                userIds = userIds.Union(responseUsers.Data.Select(r => r.Id).ToList()).ToList();
                            }
                        }

                        if (userIds.Count > 0)
                        {
                            ParameterListToSend notificationParameter = new ParameterListToSend();
                            notificationParameter.Link = $"<a href = '" + BaseURL + "/KMC/Home/Browse/" + model.Id.ToString() + "' > here </a>";

                            CreateAutoReminder notification = new CreateAutoReminder
                            {
                                NotificationType      = NotificationType.KMCCreated,
                                NotificationCategory  = NotificationCategory.Learning,
                                ParameterListToSend   = notificationParameter,
                                StartNotificationDate = DateTime.Now,
                                ReceiverId            = userIds
                            };

                            var responseNotification = await WepApiMethod.SendApiAsync <ReminderResponse>(HttpVerbs.Post, $"Reminder/SLA/GenerateAutoNotificationReminder/", notification);
                        }
                    }

                    await LogActivity(Modules.KMC, "Edit KMC", model);

                    TempData["SuccessMessage"] = Language.KMC.AlertSuccessUpdate;

                    return(RedirectToAction("Details", "Manage", new { area = "KMC", @id = model.Id }));
                }
                else
                {
                    TempData["ErrorMessage"] = Language.KMC.AlertFailDelete;
                }
            }

            model.filter_imgs   = filter_imgs;
            model.filter_videos = filter_videos;
            model.filter_audios = filter_audios;
            model.filter_docs   = filter_docs;

            model.Roles = new SelectList(await GetRoles(), "Id", "Name", 0);

            return(View(model));
        }