public ActionResult DeleteConfirmed(int id)
        {
            CouncilMembers councilMembers = db.CouncilMembers.Find(id);

            db.CouncilMembers.Remove(councilMembers);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        // GET: CouncilMembers/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            CouncilMembers councilMembers = db.CouncilMembers.Find(id);

            if (councilMembers == null)
            {
                return(HttpNotFound());
            }
            return(View(councilMembers));
        }
        public ActionResult Edit([Bind(Include = "CouncilMemberID,CouncilMember,Phone,EmailAddress,LiasonDistrict,IsActive,CreatedOn")] CouncilMembers councilMembers)
        {
            if (ModelState.IsValid)
            {
                db.Entry(councilMembers).State = EntityState.Modified;

                councilMembers.IsActive   = true;
                councilMembers.ModifiedBy = 1;
                councilMembers.ModifiedOn = DateTime.Now;

                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(councilMembers));
        }
        public ActionResult Create([Bind(Include = "CouncilMemberID,CouncilMember,Phone,EmailAddress,LiasonDistrict,IsActive")] CouncilMembers councilMembers)
        {
            if (ModelState.IsValid)
            {
                db.CouncilMembers.Add(councilMembers);
                try
                {
                    councilMembers.IsActive   = true;
                    councilMembers.CreatedBy  = 1;
                    councilMembers.CreatedOn  = DateTime.Now;
                    councilMembers.ModifiedOn = DateTime.Now;
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                }
                return(RedirectToAction("Index"));
            }

            return(View(councilMembers));
        }
        private async void DoEnd()
        {
            await DialogHost.Show(new OkCancelMessageDialog()
            {
                DataContext = "Do you want to end the election?"
            },
                                  "RootDialog", delegate(object sender, DialogClosingEventArgs args)
            {
                if (Equals(args.Parameter, false))
                {
                    return;
                }
                if (Equals(args.Parameter, "Ok") && IsEndDialog)
                {
                    IsEndDialog = false;
                    return;
                }

                if (Equals(args.Parameter, "Ok") && !IsEndDialog)
                {
                    IsEndDialog = true;
                    args.Cancel();
                    args.Session.UpdateContent(new PleaseWaitView());
                    bool result = false;
                    Task.Run(() =>
                    {
                        try
                        {
                            int votes   = 0;
                            int cmvotes = 0;
                            //var cmMember = new CouncilMembers();
                            List <ElectionHistory> electHistory = new List <ElectionHistory>();
                            var positions    = _context.CouncilPositions.ToList();
                            var members      = _context.CouncilMembers.ToList();
                            var hasVotes     = _context.StudentVotes.FirstOrDefault();
                            bool resultVotes = hasVotes != null;
                            //if (!positions.Any() && !members.Any())
                            if (!resultVotes)
                            {
                                IsEndDialog = true;
                                args.Session.UpdateContent(new OkMessageDialog()
                                {
                                    DataContext = "Failed to End"
                                });
                                return;
                            }
                            //int highestVote = 0;
                            foreach (var position in positions)
                            {
                                var cmMember = new CouncilMembers();
                                foreach (var member in members)
                                {
                                    if (member.CouncilPosition == position)
                                    {
                                        votes   = CalculateVotes(member.Student);
                                        cmvotes = CalculateVotes(cmMember?.Student);
                                        if (votes > cmvotes)
                                        {
                                            //highestVote = votes;
                                            cmMember = member;
                                        }
                                    }
                                }

                                var eHistory = new ElectionHistory()
                                {
                                    Position  = cmMember.CouncilPosition.Position,
                                    Name      = cmMember.Student.FullName,
                                    DateYear  = DateTime.Now,
                                    VoteCount = CalculateVotes(cmMember?.Student)
                                };
                                electHistory.Add(eHistory);
                            }
                            _context.ElectionHistory.AddRange(electHistory);
                            _context.CouncilMembers.RemoveRange(_context.CouncilMembers);
                            _context.PartyLists.RemoveRange(_context.PartyLists);
                            var list = _context.StudentVotes.ToList();
                            _context.StudentVotes.RemoveRange(list);
                            _context.ElectionStatus.RemoveRange(_context.ElectionStatus);
                            _context.SaveChangesAsync();
                            result = true;
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                            result = false;
                        }
                    }).ContinueWith((t, _) =>
                    {
                        LoadData();
                        //args.Session.Close(false);
                        Messenger.Default.Send(new EndElectionMessage());
                        args.Session.UpdateContent(new OkMessageDialog()
                        {
                            DataContext = result ? "End Successfull, Check Election History for results." : "Failed to end"
                        });
                    }, null, TaskScheduler.FromCurrentSynchronizationContext());
                }
            });

            //int votes = 0;
            //int cmvotes = 0;
            ////var cmMember = new CouncilMembers();
            //List<ElectionHistory> electHistory = new List<ElectionHistory>();
            //var positions = _context.CouncilPositions.ToList();
            //var members = _context.CouncilMembers.ToList();
            //if (!positions.Any() && !members.Any())
            //{
            //    await DialogHost.Show(new OkMessageDialog() {DataContext = "Failed to End"}, "RootDialog");
            //    return;
            //}
            //foreach (var position in positions)
            //{
            //    var cmMember = new CouncilMembers();
            //    foreach (var member in members)
            //    {
            //        if (member.CouncilPosition == position)
            //        {
            //            votes = CalculateVotes(member.Student);
            //            cmvotes = CalculateVotes(cmMember?.Student);
            //            if (votes > cmvotes)
            //            {
            //                cmMember = member;
            //            }
            //        }
            //    }

            //    var eHistory = new ElectionHistory()
            //    {
            //        Position = cmMember.CouncilPosition.Position,
            //        Name = cmMember.Student.FullName,
            //        DateYear = DateTime.Now,
            //        VoteCount = cmvotes
            //    };
            //    electHistory.Add(eHistory);
            //}
            //_context.ElectionHistory.AddRange(electHistory);
            //_context.CouncilMembers.RemoveRange(_context.CouncilMembers);
            //_context.PartyLists.RemoveRange(_context.PartyLists);
            //_context.StudentVotes.RemoveRange(_context.StudentVotes);
            //_context.ElectionStatus.RemoveRange(_context.ElectionStatus);
            //_context.SaveChanges();
            //LoadData();
            ////RaisePropertyChanged(() => StartCommand);
            ////RaisePropertyChanged(() => CanStart);
        }
        private void AddClosingEventHandler(object sender, DialogClosingEventArgs args)
        {
            bool result = false;

            if (Equals(args.Parameter, false))
            {
                return;
            }

            if (args.Parameter is TextBox)
            {
                args.Cancel();
                args.Session.UpdateContent(new PleaseWaitView());
                TextBox txtName = (TextBox)args.Parameter;
                string  name    = txtName.Text.Trim();
                if (string.IsNullOrEmpty(name) || string.IsNullOrWhiteSpace(name))
                {
                    args.Cancel();
                    args.Session.UpdateContent(new OkMessageDialog()
                    {
                        DataContext = "Null entry"
                    });
                    return;
                }

                var duplicate = _context.PartyLists.FirstOrDefault(c => c.Name == name);
                if (duplicate != null)
                {
                    args.Session.UpdateContent(new OkMessageDialog()
                    {
                        DataContext = "Duplicate name"
                    });
                    return;
                }
                Task.Run(() =>
                {
                    var newPartyList = new PartyList()
                    {
                        Name = name
                    };
                    try
                    {
                        //_context.Entry(newPartyList).State = EntityState.Added;
                        _context.PartyLists.Add(newPartyList);

                        var cplist = _context.CouncilPositions;
                        var cmlist = new List <CouncilMembers>();
                        foreach (var cp in cplist)
                        {
                            var cm = new CouncilMembers
                            {
                                CouncilPosition = cp,
                                PartyList       = newPartyList
                            };
                            cmlist.Add(cm);
                        }
                        foreach (var entity in cmlist)
                        {
                            //_context.Entry(entity).State = EntityState.Added;
                            _context.CouncilMembers.Add(entity);
                        }
                        _context.SaveChanges();
                        result = true;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);

                        result = false;
                    }
                    Thread.Sleep(500);
                    return(newPartyList);
                }).ContinueWith(
                    (t, _) =>
                {
                    if (result)
                    {
                        PartyLists.Add(t.Result);
                        RaisePropertyChanged(() => PartyLists);
                        args.Session.Close(false);
                    }
                    else
                    {
                        args.Session.UpdateContent(new OkMessageDialog()
                        {
                            DataContext = "Failed to add"
                        });
                    }
                }, null, TaskScheduler.FromCurrentSynchronizationContext());
            }
        }