public ActionResult Edit(EventModel EventModel, HttpPostedFileBase file)
        {
            UserPermissionAction("event", RoleAction.view.ToString());
            CheckPermission();
            TempData["ShowMessage"] = "";
            TempData["MessageBody"] = "";
            try
            {
                if (ModelState.IsValid)
                {
                    if (string.IsNullOrEmpty(EventModel.EventDate.ToString()))
                    {
                        TempData["ShowMessage"] = "error";
                        TempData["MessageBody"] = "Please select a valid Event Date.";
                        return(View("Create", EventModel));
                    }

                    if (string.IsNullOrEmpty(EventModel.EventName))
                    {
                        TempData["ShowMessage"] = "error";
                        TempData["MessageBody"] = "Please Fill Event Name.";
                        return(View("Create", EventModel));
                    }
                    if (string.IsNullOrEmpty(EventModel.EventDescription))
                    {
                        TempData["ShowMessage"] = "error";
                        TempData["MessageBody"] = "Please Fill  EventDescription.";
                        return(View("Create", EventModel));
                    }
                    Mapper.CreateMap <CommunicationApp.Models.EventModel, CommunicationApp.Entity.Event>();
                    CommunicationApp.Entity.Event Event = Mapper.Map <CommunicationApp.Models.EventModel, CommunicationApp.Entity.Event>(EventModel);
                    var EventImage = "";
                    if (file != null)
                    {
                        if (Event.EventImage != "")
                        {   //Delete Old Image
                            string pathDel = Server.MapPath("~/EventPhoto");

                            FileInfo objfile = new FileInfo(pathDel);
                            if (objfile.Exists) //check file exsit or not
                            {
                                objfile.Delete();
                            }
                            //End :Delete Old Image
                        }

                        //Save the photo in Folder
                        var    fileExt  = Path.GetExtension(file.FileName);
                        string fileName = Guid.NewGuid() + fileExt;
                        var    subPath  = Server.MapPath("~/EventPhoto");

                        //Check SubPath Exist or Not
                        if (!Directory.Exists(subPath))
                        {
                            Directory.CreateDirectory(subPath);
                        }
                        //End : Check SubPath Exist or Not

                        var path = Path.Combine(subPath, fileName);
                        file.SaveAs(path);

                        EventImage = CommonCls.GetURL() + "/EventPhoto/" + fileName;
                    }
                    Event.LastUpdatedon = DateTime.Now;
                    Event.EventImage    = EventImage;
                    _EventService.UpdateEvent(Event);
                    TempData["ShowMessage"] = "success";
                    TempData["MessageBody"] = "Event successfully updated.";
                    return(RedirectToAction("Index"));
                }
                return(View(EventModel));
            }
            catch (Exception ex)
            {
                ErrorLogging.LogError(ex);

                return(View(EventModel));
            }
        }
        public ActionResult Create(EventModel EventModel, HttpPostedFileBase file)
        {
            UserPermissionAction("event", RoleAction.view.ToString());
            CheckPermission();
            TempData["ShowMessage"] = "";
            TempData["MessageBody"] = "";
            try
            {
                if (ModelState.IsValid)
                {
                    if (string.IsNullOrEmpty(EventModel.EventDate.ToString()))
                    {
                        TempData["ShowMessage"] = "error";
                        TempData["MessageBody"] = "Please select a valid Event Date.";
                        return(View("Create", EventModel));
                    }

                    if (string.IsNullOrEmpty(EventModel.EventName))
                    {
                        TempData["ShowMessage"] = "error";
                        TempData["MessageBody"] = "Please Fill Event Name.";
                        return(View("Create", EventModel));
                    }

                    if (string.IsNullOrEmpty(EventModel.EventDescription))
                    {
                        TempData["ShowMessage"] = "error";
                        TempData["MessageBody"] = "Please Fill  EventDescription.";
                        return(View("Create", EventModel));
                    }
                    //CultureInfo culture = new CultureInfo("en-US");
                    //DateTime TodayDate = Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyyy"), culture);
                    //if (Convert.ToDateTime(EventModel.EventDate.ToString("MM/dd/yyyy"), culture) < TodayDate)
                    //{

                    //    TempData["ShowMessage"] = "error";
                    //    TempData["MessageBody"] = "You cannot add old date event.";
                    //    return View("Create", EventModel);
                    //}

                    Mapper.CreateMap <CommunicationApp.Models.EventModel, CommunicationApp.Entity.Event>();
                    CommunicationApp.Entity.Event Event = Mapper.Map <CommunicationApp.Models.EventModel, CommunicationApp.Entity.Event>(EventModel);
                    string EventImage = "";
                    if (file != null)
                    {
                        if (Event.EventImage != "")
                        {   //Delete Old Image
                            string pathDel = Server.MapPath("~/EventPhoto");

                            FileInfo objfile = new FileInfo(pathDel);
                            if (objfile.Exists) //check file exsit or not
                            {
                                objfile.Delete();
                            }
                            //End :Delete Old Image
                        }

                        //Save the photo in Folder
                        var    fileExt  = Path.GetExtension(file.FileName);
                        string fileName = Guid.NewGuid() + fileExt;
                        var    subPath  = Server.MapPath("~/EventPhoto");

                        //Check SubPath Exist or Not
                        if (!Directory.Exists(subPath))
                        {
                            Directory.CreateDirectory(subPath);
                        }
                        //End : Check SubPath Exist or Not

                        var path = Path.Combine(subPath, fileName);
                        file.SaveAs(path);

                        EventImage = CommonCls.GetURL() + "/EventPhoto/" + fileName;
                    }

                    Event.CustomerId = Convert.ToInt32(Session["CustomerId"]);//CutomerId
                    Event.IsActive   = true;
                    Event.CreatedOn  = DateTime.Now;
                    Event.EventImage = EventImage;
                    _EventService.InsertEvent(Event);
                    List <int> CustomerIds = new List <int>();
                    var        Customers   = new List <Customer>();
                    try
                    {
                        if (EventModel.All == true)
                        {
                            EventModel.SelectedCustomer = null;
                            Customers = _CustomerService.GetCustomers().ToList();
                            foreach (var Customer in Customers)
                            {
                                CustomerIds.Add(Convert.ToInt32(Customer.CustomerId));
                                EventCustomer EventCustomer = new Entity.EventCustomer();
                                EventCustomer.EventId    = Event.EventId;
                                EventCustomer.CustomerId = Customer.CustomerId;
                                _EventCustomerService.InsertEventCustomer(EventCustomer);
                            }
                        }
                        else if (EventModel.SelectedCustomer != null)
                        {
                            foreach (var Customer in EventModel.SelectedCustomer)
                            {
                                CustomerIds.Add(Convert.ToInt32(Customer));
                                EventCustomer EventCustomer = new Entity.EventCustomer();
                                EventCustomer.EventId    = Event.EventId;
                                EventCustomer.CustomerId = Event.CustomerId;
                                _EventCustomerService.InsertEventCustomer(EventCustomer);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ErrorLogging.LogError(ex);
                        throw;
                    }



                    string Flag    = "12";//status for Event;
                    var    Message = "New event saved.";
                    //send notification
                    try
                    {
                        var CustomerList = _CustomerService.GetCustomers().Where(c => CustomerIds.Contains(c.CustomerId) && c.CustomerId != EventModel.CustomerId && c.IsActive == true).ToList();
                        foreach (var Customer in CustomerList)
                        {
                            if (Customer != null)
                            {
                                if (Customer.ApplicationId != null && Customer.ApplicationId != "")
                                {
                                    bool NotificationStatus = true;

                                    string JsonMessage = "{\"Flag\":\"" + Flag + "\",\"Message\":\"" + Message + "\"}";
                                    try
                                    {
                                        //Save Notification
                                        Notification Notification = new Notification();
                                        Notification.NotificationSendBy = 1;
                                        Notification.NotificationSendTo = Convert.ToInt32(Customer.CustomerId);
                                        Notification.IsRead             = false;
                                        Notification.Flag           = Convert.ToInt32(Flag);
                                        Notification.RequestMessage = Message;
                                        _Notification.InsertNotification(Notification);
                                        if (Customer.DeviceType == EnumValue.GetEnumDescription(EnumValue.DeviceType.Android))
                                        {
                                            CommonCls.SendGCM_Notifications(Customer.ApplicationId, JsonMessage, true);
                                        }
                                        else
                                        {
                                            int count = _Notification.GetNotifications().Where(c => c.NotificationSendTo == Convert.ToInt32(Customer.CustomerId) && c.IsRead == false).ToList().Count();
                                            //Dictionary<string, object> Dictionary = new Dictionary<string, object>();
                                            //Dictionary.Add("Flag", Flag);
                                            //Dictionary.Add("Message", Message);
                                            //NotificationStatus = PushNotificatinAlert.SendPushNotification(Customer.ApplicationId, Message, Flag, JsonMessage, Dictionary, 1, Convert.ToBoolean(Customer.IsNotificationSoundOn));
                                            CommonCls.TestSendFCM_Notifications(Customer.ApplicationId, JsonMessage, Message, count, true);
                                            ////Save Notification
                                            //Notification Notification = new Notification();
                                            //Notification.NotificationSendBy = 1;
                                            //Notification.NotificationSendTo = Customer.CustomerId;
                                            //Notification.IsRead = false;
                                            //Notification.RequestMessage = Message;
                                            //_Notification.InsertNotification(Notification);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        CommonCls.ErrorLog(ex.ToString());
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ErrorLogging.LogError(ex);
                        throw;
                    }

                    TempData["ShowMessage"] = "success";
                    TempData["MessageBody"] = "Event successfully saved.";
                    return(RedirectToAction("Index"));
                }
                var errors           = ModelState.Where(x => x.Value.Errors.Count > 0).Select(x => new { x.Key, x.Value.Errors }).ToArray();
                var modelStateErrors = this.ModelState.Keys.SelectMany(key => this.ModelState[key].Errors);
                var CustomerList1    = _CustomerService.GetCustomers();
                EventModel.CustomersList = CustomerList1.Select(x => new SelectListItem {
                    Value = x.CustomerId.ToString(), Text = x.FirstName
                }).ToList();
                EventModel.CustomerId = 1;
                return(View(EventModel));
            }
            catch (Exception ex)
            {
                ErrorLogging.LogError(ex);

                return(View(EventModel));
            }
        }